le 27.06.2007 15:36 Michael Bayer a écrit:
> 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).
>   
Thanks Mickael,

I first tried to add the backref on only one side but the relationship 
was not bi-directional ; user.addresses.append(a) updated address.user 
but not the other way, that's why I added it both sides.

Then, with backrefs defined on both "add_property", I had a look at the 
structures created at the mapper level (properties, reverse_property, 
...) and they seemed coherent but I was not sure it was enough.
The only difference I saw is that you could find the backref "field" in 
the properties list of both sides but the PropertyLoader objects were 
coherent (User.mapper.properties['addresses'].reverse_property == 
Address.mapper.properties['user'] and 
Address.mapper.properties['user'].reverse_property == 
User.mapper.properties['addresses']) and is_backref was positionned on 
one side and not the other as you say it is needed below...
> 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).
>
>   
OK, I'll do it that way if it is the supported way. Thanks for the hint.
Does the "is_backref" needs to be on a specific side of the relation ??

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to [email protected]
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