On Aug 22, 2006, at 10:04 AM, Alan Franzoni wrote:

> Hello, I've found what I think is an unexpected behaviour in  
> sqlalchemy; it
> has to do with mapped objects and foreign keys handling. Check out  
> this
> snippet:
>

snip

>
> What I think is unexpected is the missing constant 'link' between the
> helper property (user), which accepts User objects, and the mapped  
> property
> (user_id), which accepts integers. I would imagine that, if I  
> changed the
> 'user' property, the 'user_id' would immediately reflect that  
> change, and
> viceversa - after all, why not? I can't think of any possible  
> drawback.

I agree that this behavior is a bit misleading. However, I have found  
that everything works fine if you just stick to using the property  
(i.e. user) rather than the foreign-key attribute (i.e. user_id).  
Think of the foreign key attribute as an internal (private)  
attribute, and do not write your programs to rely on it. If you need  
the id of the user associated with a given address do this:  
address.user.user_id and not this: address.user_id).

>
> This way, in order to get the correct values, I need to do a
> session.refresh(obj), which can be a bit messy (it's a bit of pain  
> to do it
> again and again).
>
> Also, the final code snippet is not clear:
>
> addr1.user = user2
> addr1.user_id = 10
>
> In this case, it seems that the 'user' property has priority (when  
> flushed,
> the addr1 object has a user_id of 20), even though the user_id gets  
> set
> later; is it always the case or might this change depending on what  
> code is
> written?
>
> IMHO the objects should either react immediately to a related property
> change, or they shouldn't be both directly accessible, e.g. I  
> should only
> be able to use either user_id or user on an Address object at a  
> time, not
> both.

In Python nothing is really private, which means you can see (and  
manipulate) address.user_id. But that doesn't mean you should use it  
just because you can. Just ignore the address.user_id attribute and  
everything will work as expected.

~ Daniel



-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Sqlalchemy-users mailing list
Sqlalchemy-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users

Reply via email to