Note behavior: Script: ============================================= from sqlalchemy import * from sqlalchemy.orm import *
engine = create_engine('postgres://salespylot:salespy...@localhost: 5444/salespylot',echo=True) metadata = MetaData() Session = sessionmaker(bind=engine) DBSession = Session() order_table = Table("orders", metadata, Column("orderid", String, primary_key=True), Column("customerid", String(255), ForeignKey('customers.customerid')) ) class Order(object): pass mapper(Order, order_table) customers_table = Table("customers", metadata, Column("customerid", String(255), Sequence('customers_id_seq'), primary_key=True), Column("phonenumber", String(255)), Column("firstname", String(255)), Column("lastname", String(255)) ) class Customer(object): def __repr__(self): return '<Customer: name=[%s %s] customerid=[%s]>' % (self.firstname, self.lastname, self.customerid) mapper(Customer, customers_table, properties={'orders': relation(Order, cascade='refresh-expire,expunge', single_parent=True, backref=backref('customer', cascade='save-update,merge,refresh- expire,expunge', lazy=True))}) ord=DBSession.query(Order).get(u'SALE65424') ord.customerid #this returns the current value, '7' ord.customerid = u'8' #update this value ord.customer # lazy load =========================================================== Output, starting at DBSession.query: --------------------------------------------------------------- >>> ord=DBSession.query(Order).get(u'SALE65424') 2010-05-05 10:02:07,994 INFO sqlalchemy.engine.base.Engine.0x...2f10 select version() 2010-05-05 10:02:07,997 INFO sqlalchemy.engine.base.Engine.0x...2f10 {} 2010-05-05 10:02:08,001 INFO sqlalchemy.engine.base.Engine.0x...2f10 select current_schema() 2010-05-05 10:02:08,001 INFO sqlalchemy.engine.base.Engine.0x...2f10 {} 2010-05-05 10:02:08,003 INFO sqlalchemy.engine.base.Engine.0x...2f10 BEGIN 2010-05-05 10:02:08,004 INFO sqlalchemy.engine.base.Engine.0x...2f10 SELECT orders.orderid AS orders_orderid, orders.customerid AS orders_customerid FROM orders WHERE orders.orderid = %(param_1)s 2010-05-05 10:02:08,004 INFO sqlalchemy.engine.base.Engine.0x...2f10 {'param_1': u'SALE65424'} >>> >>> ord.customerid u'7' >>> >>> ord.customerid = u'8' >>> >>> ord.customer 2010-05-05 10:02:08,012 INFO sqlalchemy.engine.base.Engine.0x...2f10 UPDATE orders SET customerid=%(customerid)s WHERE orders.orderid = % (orders_orderid)s 2010-05-05 10:02:08,012 INFO sqlalchemy.engine.base.Engine.0x...2f10 {'orders_orderid': u'SALE65424', 'customerid': u'8'} 2010-05-05 10:02:08,014 INFO sqlalchemy.engine.base.Engine.0x...2f10 SELECT customers.customerid AS customers_customerid, customers.phonenumber AS customers_phonenumber, customers.firstname AS customers_firstname, customers.lastname AS customers_lastname FROM customers WHERE customers.customerid = %(param_1)s 2010-05-05 10:02:08,015 INFO sqlalchemy.engine.base.Engine.0x...2f10 {'param_1': u'7'} <Customer: name=[Veronica Smith] customerid=[7]> >>> --------------------------------------------------------------- Even after the join criterion was changed (and even flushed to the database), the lazy load fetched the 'wrong' customer, using the old value of the customerid. Is the behavior by design? The possible rationale is "this makes relationship loading consistent regardless of autoflush being enabled and also regardless of eager vs. lazy loading". But, at the end of the day, the example above just seems wrong. A longer term enhancement to sqla might be to automatically expire any relation whose join columns include a column that gets updated in a session? Is that a manageable effort? Kent -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalch...@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.