On Dec 26, 2011, at 7:34 AM, jerryji wrote:

> I noticed that the target objects passed in SA event are stripped off
> relationship columns.
> 
> E.g., relationship column Post.user --
> 
> """
> class User(Base):
>    __tablename__ = 'users'
>    user_id = Column(Unicode(32), primary_key=True)
>    user_name = Column(Unicode(32))
>    ...
> 
> class Post(Base):
>    __tablename__ = 'posts'
>    user_id = Column(Unicode(32), ForeignKey('users.user_id'))
>    user = relationship(User, primaryjoin=user_id==User.user_id)
> """
> 
> And event snippet --
> 
> """
> def update_cache_listener(mapper, connection, target):
>    pass
> 
> event.listen(model.Post, 'after_insert', update_cache_listener)
> event.listen(model.Post, 'after_update', update_cache_listener)
> """
> 
> debug --
> 
> """
> (Pdb) l
> 9          def update_cache_listener(mapper, connection, target):
> 10  ->       pass
> (Pdb) target
> <myapp.models.Post object at 0xbca892c>
> (Pdb) target.user_id
> '123'
> (Pdb) target.user
> (Pdb)
> """
> 
> Is this expected behavior? How to get back the relationship columns in
> event listener function?

If it's the case that "target.user = someuser" wasn't called, and "123" is only 
a pending value on user_id, in this case it remains as None.   When inside of 
the flush, different rules are used to determine that "123" should be used to 
locate "target.user", and not the previous value of user_id, which is "None".

Ticket #2350 will narrow the scope at which this alternate mode occurs so that 
it does not impact user-defined extensions.    As this change is backwards 
incompatible with extensions that may expect the current behavior, it's 
scheduled for 0.8.

http://www.sqlalchemy.org/trac/ticket/2350

In general, it's discouraged to make strong assumptions of relationships being 
immediately available based on foreign key attribute settings, as the ORM is 
designed to work in the other direction, where user code deals only with the 
relationship, and foreign key assignments occur behind the scenes.  Some 
background on this is at 
http://www.sqlalchemy.org/trac/wiki/FAQ#Isetthefoo_idattributeonmyinstanceto7butthefooattributeisstillNone-shouldntithaveloadedFoowithid7
 .















> 
> Thanks!
> 
> Jerry
> 
> -- 
> 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 
> sqlalchemy+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/sqlalchemy?hl=en.
> 

-- 
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 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to