On Mar 7, 2013, at 8:29 AM, jefro...@gmail.com wrote:

> Hello,
> 
> We are testing the new  __declare_last__ feature with 0.8. We face an issue 
> with the after_configured event in an application that use multiple engines.
> after_configured event is triggered multiple times (by 
> sqlalchemy.orm.configure_mappers), once for each engine.
> 
> The problem with this -as far as I understand it- is that the mapper registry 
> is shared and that the after_configured subscribers can be called multiple 
> times on the same mapper. We end up with exception like this:
> 
> ArgumentError: Error creating backref 'herd' on relationship 'Herd.animals': 
> property of that name exists on mapper 'Mapper|Animal|animal'
> 
> caused by the fact that __declare_last__ is called multiple times on the same 
> mappers ... relation being already defined.
> 
> Is there a way to prevent this in sqlalchemy ? Or should I prevent this using 
> state in my class ?


there's no connection between Engine objects and Mapper objects, so there's no 
implicit linkage between creating an engine and mappers being configured.   If 
there are multiple mapper configured events happening, that indicates your 
application is adding new mappers after the existing set of mappers are being 
used in some way, or you're calling configure_mappers() explicitly.    Sounds 
like you have some kind of multi-tenancy going on (multiple applications 
running in the same Python process), but if not it might be worth it to 
organize things such that configure_mappers() isn't called as much.

As for __declare_last__, it's a simple link to this event, which necessarily 
needs to be called each time newly configured mappers become available as it 
can't assume which mappings the event wishes to deal with.   So if there is 
state being established within __declare_last__, you'd want to check for that 
state already being present, and yes a class-level flag is often a simple way 
to deal with that.


-- 
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?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to