有人知道如何正确地从两个不同的会话中清除对象。
在下面的代码段中,对model.id
的最后访问失败,并显示DetachedInstanceError
。
with session_scope() as session:
model = session.query(Model).filter(Model.id='some_id').first()
print(model.id)
# Change Model
session.commit()
session.expunge(model)
print(model.id)
with session_scope() as session:
session.add(model)
# Change Model
session.commit()
session.expunge(model)
print(model.id)
session_scope
的定义如https://docs.sqlalchemy.org/en/latest/orm/session_basics.html所示