我遇到了一个非常奇怪的错误,很难再现:AttributeError: 'list' object has no attribute '_sa_adapter'
。它似乎来自一对多的关系,但一切看起来都很好,我不能简单地重现这个错误。如果我单独运行测试套件,则没有错误,只有在整个测试运行时才会发生。也许它来自没有正确清理的东西......
这是完整的堆栈:
Traceback (most recent call last):
File "/Users/ca/[REDACTED].py", line 79, in setUp
self.client.save()
File "/Users/ca/[REDACTED].py", line 370, in save
Session.add(self)
File "/Users/ca/.pythonbrew/venvs/Python-2.7.3/api/lib/python2.7/site-packages/SQLAlchemy-0.7.5-py2.7-macosx-10.7-x86_64.egg/sqlalchemy/orm/scoping.py", line 113, in do
return getattr(self.registry(), name)(*args, **kwargs)
File "/Users/ca/.pythonbrew/venvs/Python-2.7.3/api/lib/python2.7/site-packages/SQLAlchemy-0.7.5-py2.7-macosx-10.7-x86_64.egg/sqlalchemy/orm/session.py", line 1223, in add
self._save_or_update_state(state)
File "/Users/ca/.pythonbrew/venvs/Python-2.7.3/api/lib/python2.7/site-packages/SQLAlchemy-0.7.5-py2.7-macosx-10.7-x86_64.egg/sqlalchemy/orm/session.py", line 1238, in _save_or_update_state
halt_on=self._contains_state):
File "/Users/ca/.pythonbrew/venvs/Python-2.7.3/api/lib/python2.7/site-packages/SQLAlchemy-0.7.5-py2.7-macosx-10.7-x86_64.egg/sqlalchemy/orm/mapper.py", line 1814, in cascade_iterator
parent_dict, visited_states, halt_on))
File "/Users/ca/.pythonbrew/venvs/Python-2.7.3/api/lib/python2.7/site-packages/SQLAlchemy-0.7.5-py2.7-macosx-10.7-x86_64.egg/sqlalchemy/orm/properties.py", line 817, in cascade_iterator
get_all_pending(state, dict_)
File "/Users/ca/.pythonbrew/venvs/Python-2.7.3/api/lib/python2.7/site-packages/SQLAlchemy-0.7.5-py2.7-macosx-10.7-x86_64.egg/sqlalchemy/orm/attributes.py", line 775, in get_all_pending
current = getattr(current, '_sa_adapter')
AttributeError: 'list' object has no attribute '_sa_adapter'
你知道它可能来自哪里吗?
谢谢,
CHX
答案 0 :(得分:3)
这意味着您正在不恰当地操纵检测对象 - 例如说myobject.__dict__[somekey] = [1, 2, 3]
。映射对象不支持直接__dict__
操作。
答案 1 :(得分:2)
旧线程,但对于稍后查看的任何人,您希望确保您的值通过 setattr ,以便SA的映射有机会拦截。所以你可以这样做:
obj.attr = myval
或
setattr(obj, 'attr', myval)
答案 2 :(得分:0)
我的情况是另一个会话仍然在其他线程(调度程序一个)中打开。所以,我有一个线程在激活作业后关闭会话,http服务器线程像魅力一样工作。