Re: [sqlalchemy] Automap and naming of relationship attributes

2014-02-06 Thread Adrian Robert
Well, using the mapper event would be nicer, but in any case I was already 
iterating over Base.classes and adding them to my own module's namespace like 
so:

globals()[cls.__name__] = cls

It works for the rest of my application being able to see the classes by 
importing the module, but apparently not for this.  I'm not really expert at 
Python class and namespace innards, but from the error message as well as the 
default str() output it seems the automap-generated classes considers 
themselves to be in the sqlalchemy.ext.automap module but are not registered in 
that namespace.

Is there a way to tell the classes to use a different namespace from an 
instrument_class handler?  (And incidentally I'm already using my own base 
class through automap_base(declarative_base(cls=...)) but that doesn't make any 
difference.)




On 2014.2.6, at 15:59, Michael Bayer mike...@zzzcomputing.com wrote:

 Python pickle can't pickle class instances where the class isn't locatable as 
 module-level imports.  As automap necessarily creates classes on the fly, 
 these classes aren't part of any module.  to have them part of a module you'd 
 want to use an event to place them in the namespace of one of your own 
 modules, or you can implement a custom `__reduce__()` method on them (see the 
 Python docs for __reduce__()).
 
 a good event to use here might be instrument_class:
 
 http://docs.sqlalchemy.org/en/rel_0_9/orm/events.html#sqlalchemy.orm.events.MapperEvents.instrument_class
 
 
 
 On Feb 6, 2014, at 4:32 AM, Adrian Robert adrian.b.rob...@gmail.com wrote:
 
 One other point, I was trying out the dogpile cache example and ran into 
 (after I stuck a .encode('utf-8') into the key mangler since I'm using 
 Python-3 and pylibmc):
 
 _pickle.PicklingError: Can't pickle class 'sqlalchemy.ext.automap.Person': 
 attribute lookup sqlalchemy.ext.automap.Person failed
 
 This was fixed by a hack
 
   sqlalchemy.ext.automap.__dict__[cls.__name__] = cls
 
 run over all the automap-created classes.  It might be I'm only having to do 
 this because I'm doing something wrong elsewhere, but I just thought I'd 
 mention it in case it comes up for someone.
 
 
 
 On 2014.2.2, at 14:22, Adrian Robert adrian.b.rob...@gmail.com wrote:
 
 Thanks, that works beautifully.
 
 I had noticed name_for_scalar_relationship parameter but I guess wasn't 
 confident enough that I understood what was going on to try it.  :-[
 
 
 -- 
 You received this message because you are subscribed to a topic in the 
 Google Groups sqlalchemy group.
 To unsubscribe from this topic, visit 
 https://groups.google.com/d/topic/sqlalchemy/p6YkPuCs_Ks/unsubscribe.
 To unsubscribe from this group and all its topics, send an email to 
 sqlalchemy+unsubscr...@googlegroups.com.
 To post to this group, send email to sqlalchemy@googlegroups.com.
 Visit this group at http://groups.google.com/group/sqlalchemy.
 For more options, visit https://groups.google.com/groups/opt_out.
 
 -- 
 You received this message because you are subscribed to the Google Groups 
 sqlalchemy group.
 To unsubscribe from this group and stop receiving emails from it, send an 
 email to sqlalchemy+unsubscr...@googlegroups.com.
 To post to this group, send email to sqlalchemy@googlegroups.com.
 Visit this group at http://groups.google.com/group/sqlalchemy.
 For more options, visit https://groups.google.com/groups/opt_out.
 

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.


Re: [sqlalchemy] Automap and naming of relationship attributes

2014-02-06 Thread Michael Bayer

On Feb 6, 2014, at 1:56 PM, Adrian Robert adrian.b.rob...@gmail.com wrote:

 Well, using the mapper event would be nicer, but in any case I was already 
 iterating over Base.classes and adding them to my own module's namespace like 
 so:
 
globals()[cls.__name__] = cls
 
 It works for the rest of my application being able to see the classes by 
 importing the module, but apparently not for this.  I'm not really expert at 
 Python class and namespace innards, but from the error message as well as the 
 default str() output it seems the automap-generated classes considers 
 themselves to be in the sqlalchemy.ext.automap module but are not registered 
 in that namespace.
 
 Is there a way to tell the classes to use a different namespace from an 
 instrument_class handler?  (And incidentally I'm already using my own base 
 class through automap_base(declarative_base(cls=...)) but that doesn't make 
 any difference.)

I’m seeing that and working with a rudimental example I’m not seeing a solution 
to it.You can as automap suggests create the classes explicitly.



signature.asc
Description: Message signed with OpenPGP using GPGMail


Re: [sqlalchemy] Automap and naming of relationship attributes

2014-02-02 Thread Adrian Robert
Thanks, that works beautifully.

I had noticed name_for_scalar_relationship parameter but I guess wasn't 
confident enough that I understood what was going on to try it.  :-[

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.


Re: [sqlalchemy] Automap and naming of relationship attributes

2014-02-01 Thread Michael Bayer

On Feb 1, 2014, at 11:50 AM, Adrian Robert adrian.b.rob...@gmail.com wrote:

 Hi,
 
 I'm new to sqlalchemy though I've used other ORMs (e.g. Hibernate) before, 
 and I'm trying to use the new automap feature.
 
 However it seems to be using the foreign table name rather than a suffixed 
 version of the column name when naming relationships.  Is there a reason for 
 doing it this way?

this is only a default.   All the naming schemes are configurable as documented 
at 
http://docs.sqlalchemy.org/en/rel_0_9/orm/extensions/automap.html#overriding-naming-schemes
 .  In this case you’d be doing the name_for_scalar_relationship callable: 
http://docs.sqlalchemy.org/en/rel_0_9/orm/extensions/automap.html#sqlalchemy.ext.automap.name_for_scalar_relationship

 If the naming needs to be done this way for general consistency with how 
 things are done elsewhere in sqlalchemy,

SQLAlchemy avoids automatic naming schemes like the plague, and there are very 
few places they are present.  This extension is clearly one of them as it is 
necessary, but it’s entirely open ended.   


signature.asc
Description: Message signed with OpenPGP using GPGMail