如何清除物体两次

时间:2018-10-09 13:17:43

标签: sqlalchemy

有人知道如何正确地从两个不同的会话中清除对象。

在下面的代码段中,对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所示

0 个答案:

没有答案