On Jun 27, 2007, at 6:00 AM, remi jolin wrote:

>
> Hello,
>
> Suppose we have the Address and User mappers as they are defined in  
> SA's
> documentation
>
> I was wondering if the 2 syntax bellow were equivalent :
>
> 1/
> User.mapper.add_property('addresses', relation(Address,
> backref=BackRef('user', **user_args)), **addresses_args)
>
> 2/
> Address.mapper.add_property('user', backref='addresses', **user_args)
> User.mapper.add_property('addresses', backref='user',  
> **addresses_args)
>

no, they are not.  the example in 2. is incorrect.  you only need one  
property with a backref to set up the bi-directional relationship.   
setting both properties in both directions will have the effect of  
only some of the properties taking effect..and in an undefined way  
(i.e. it might break).

the two equivalent conditions you have in mind are:

User.mapper.add_property('addresses',
relation(Address,  backref=backref('user', **user_args)),  
**addresses_args)

and

Address.mapper.add_property('user',  
attributeext=attributes.GenericBackrefExtension('addresses'),  
is_backref=True, **user_args)
User.mapper.add_property('addresses',  
attributeext=attributes.GenericBackrefExtension('user'),  
**addresses_args)

where GenericBackrefExtension handles bi-directional attribute  
population,  i.e. someaddress.user = User() firing off  
someaddress.user.addresses.append(someaddress), and  the "is_backref"  
flag is needed to be on one side of the bi-directional relationship  
in some cases during a flush() (currently, only in post_update  
relations).






--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to