我写了自己的金字塔ISession
interface的实现,它应该将Session存储在数据库中。一切工作真的很好,但不知何故pyramid_tm
抛出这个。一旦它被激活时,这样说:使用SQLAlchemy创建金字塔会话时发生DetachedInstanceError
DetachedInstanceError: Instance <Session at 0x38036d0> is not bound to a Session;
attribute refresh operation cannot proceed
(不要误会所在位置:<Session ...>
是模型中,“......一个会话”最可能指的SQLAlchemy的会话类名(我称之为DBSession
以避免混淆)。
我已经通过邮件列表看起来和SO,似乎只要有人有问题,他们是
- 产生一个新的线程或
- 手册ly电话
transaction.commit()
我没有这些事情。然而,这里的特色是,我的会话被金字塔传递了很多。首先我做DBSession.add(session)
然后return session
。我可以事后处理会话,刷新消息等。
但是,似乎一旦请求完成,我就会得到这个异常。这里是完整的回溯:
Traceback (most recent call last):
File "/home/javex/data/Arbeit/libraries/python/web_projects/pyramid/lib/python2.7/site-packages/waitress-0.8.1-py2.7.egg/waitress/channel.py", line 329, in service
task.service()
File "/home/javex/data/Arbeit/libraries/python/web_projects/pyramid/lib/python2.7/site-packages/waitress-0.8.1-py2.7.egg/waitress/task.py", line 173, in service
self.execute()
File "/home/javex/data/Arbeit/libraries/python/web_projects/pyramid/lib/python2.7/site-packages/waitress-0.8.1-py2.7.egg/waitress/task.py", line 380, in execute
app_iter = self.channel.server.application(env, start_response)
File "/home/javex/data/Arbeit/libraries/python/web_projects/pyramid/lib/python2.7/site-packages/pyramid/router.py", line 251, in __call__
response = self.invoke_subrequest(request, use_tweens=True)
File "/home/javex/data/Arbeit/libraries/python/web_projects/pyramid/lib/python2.7/site-packages/pyramid/router.py", line 231, in invoke_subrequest
request._process_response_callbacks(response)
File "/home/javex/data/Arbeit/libraries/python/web_projects/pyramid/lib/python2.7/site-packages/pyramid/request.py", line 243, in _process_response_callbacks
callback(self, response)
File "/home/javex/data/Arbeit/libraries/python/web_projects/pyramid/miniblog/miniblog/models.py", line 218, in _set_cookie
print("Setting cookie %s with value %s for session with id %s" % (self._cookie_name, self._cookie, self.id))
File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/attributes.py", line 168, in __get__
return self.impl.get(instance_state(instance),dict_)
File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/attributes.py", line 451, in get
value = callable_(passive)
File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/state.py", line 285, in __call__
self.manager.deferred_scalar_loader(self, toload)
File "build/bdist.linux-x86_64/egg/sqlalchemy/orm/mapper.py", line 1668, in _load_scalar_attributes
(state_str(state)))
DetachedInstanceError: Instance <Session at 0x7f4a1c04e710> is not bound to a Session; attribute refresh operation cannot proceed
对于这种情况,我停用了调试工具栏。一旦激活它,错误就会从那里抛出。看来这里的问题是在任何时候访问对象。
我意识到我可以尝试以某种方式分离它,但是这看起来不是正确的方式,因为如果不明确地将其添加到会话中,则无法修改该元素。
所以,当我没有产生新的线程,我没有明确地调用提交,我猜事务是在请求完全消失之前提交的,然后再次访问它。我如何处理这个问题?
非常感谢。我研究了这个问题,补间的事情似乎可以解决它。但是,我揉揉了一个更容易的解决方案(请参阅我的答案)。 – javex 2013-03-12 17:23:43