避免在Pyramid Paster Pshell中事务提交后会话过期

0 投票
2 回答
927 浏览
提问于 2025-04-16 21:00

看起来在使用 Pyramid 的 paster pshell 时,transaction.commit() 是我唯一能用来提交更改的方法。我明白这样做在提供网页服务时是好的,但在命令行中,之后再调用 SQLAlchemy 的 MyModel.my_attribute 时,我会遇到:

DetachedInstanceError: Parent instance <MyModel at 0x9394d0c> is not bound to
a Session; lazy load operation of attribute 'my_attribute' cannot proceed

我该怎么避免这个问题呢?

2 个回答

0

我认为这是因为SQLA会话中的expire_on_commit选项。也就是说,一旦你提交了事务,你之前用的那些对象就必须被刷新或者合并到新的会话中。关键是,这个问题其实和transaction模块本身没有直接关系。

1

在调用 ZopeTransactionExtension() 时,记得加上 keep_session=True 这个参数。

来源: https://pypi.python.org/pypi/zope.sqlalchemy#long-lasting-session-scopes

撰写回答