I am quite baffled by the "deferred loading" behavior on a class
member in the following code (see below). Looks like if I create an
object (t1) with some field (c2) having None as value, then after I
save, commit, and closed the object in a SQLAlchemy session, I cannot
update the c2 field. It will give me an InvalidRequestError error.

However, if I loaded the same object (into t2) from DB (even though
t2.c2 field and attribute still having None as value), I can modify
t2.c2 field even after I commit, and close the session that associated
with t2. So, is this very confusing, or did I miss some of the reasons
in this behavior?

Ben

sqlalchemy.exceptions.InvalidRequestError: Parent instance <class
'__main__.Test'> is not bound to a Session; deferred load operation of
attribute 'c2' cannot proceed


class Test(Cachable):
    def __init__(self, c1, c2=None):
        self.c1 = c1
        self.c2 = c2

engine = create_engine(url, echo=True)
meta = MetaData()
meta.bind = engine
SessionMaker = sessionmaker(bind=engine, autoflush=True,
transactional=True)
table = Table('t_Test', meta, autoload=True)
mapper(Test, table, order_by=None)

t1 = Test(1)

session = SessionMaker()
session.save(t1)
session.commit()
session.close()

#t1.c2 = 10 # this will cause "InvalidRequestError"

session = SessionMaker()
t2 = session.query(Test).filter_by(c1 = 1).one()
session.commit()
session.close()

t2.c2 = 10 # However, this will NOT cause "InvalidRequestError"


--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to