sqlalchemy.orm.exc.DetachedInstanceError: Instance is not bound to a Session; at

場景:

sqlalchemy進行查詢時,報以下錯誤

sqlalchemy.orm.exc.DetachedInstanceError:
Instance <User at 0x45844f0> is not bound to a Session; 
attribute refresh operation cannot proceed (Background on this error at: http://sqlalche.me/e/bhk3)

實例<User at 0x45844f0>未綁定到會話;屬性刷新操作無法繼續(此錯誤的背景:http://sqlalche.me/e/bhk3)

原程序如下
users = session.query(User).filter(or_(User.account == 'zhangsan', User.account == 'lisi')).all()
session.commit()
session.close()

解決方案(一)

Session=sessionmaker(bind=eng) 
改爲 
Session=sessionmaker(bind=eng,expire_on_commit=False)  

解決方案(二)

刪除 session.commit()

具體原因待考證

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章