On Feb 14, 2013, at 5:18 PM, ru...@yahoo.com wrote: > > What is the best way to get the .Person attribute updated to match > a new value assigned to .person? > > > items = session.query(Task).order_by(Task.id).all() > print ('before change:\n', str(items[0].t2p_[0]), '\n Person =', > str(items[0].t2p_[0].Person)) > items[0].t2p_[0].person=21 > print ('after change:\n', str(items[0].t2p_[0]), '\n Person =', > str(items[0].t2p_[0].Person)) > pass >
The ORM relationship construct is designed to work from the perspective of the object relationships being manipulated by the user, which then result in foreign key attributes being updated automatically. It does not automate the reverse direction, so while you are free to manipulate the value of integer foreign key attributes, the corresponding relationship attribute is only updated by loading the value from the database. So if you were to expire that attribute via Session.expire(Task, ['Person']) and then re-access it, new SQL would be emitted to reload this value, and assuming autoflush is enabled your change to the FK attribute will be flushed out and the SELECT will retrieve the new state. This expiration also happens automatically by default after a transaction is ended via commit(). The best practices with SQLAlchemy though favor working with the object graph, that is task.Person = my_person, rather than dealing with FK attributes directly, since otherwise you're working against the automation features provided by relationship(). -- 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.