我在 SQLAlchemy 中使用声明性基础模型,并且在提交时遇到以下问题:
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 693, in commit
self.transaction.commit()
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 332, in commit
self._prepare_impl()
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 311, in _prepare_impl
self.session.flush()
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 1788, in flush
self._flush(objects)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 1870, in _flush
flush_context.execute()
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/unitofwork.py", line 351, in execute
postsort_actions = self._generate_actions()
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/unitofwork.py", line 325, in _generate_actions
for rec in cycles
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/unitofwork.py", line 325, in <genexpr>
for rec in cycles
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/unitofwork.py", line 542, in per_state_flush_actions
dep.per_state_flush_actions(uow, states_for_prop, False)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/dependency.py", line 162, in per_state_flush_actions
state, state.dict)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/attributes.py", line 699, in get_all_pending
if original not in (NEVER_SET, PASSIVE_NO_RESULT, None) and \
File ".../workspace/file.py", line 250, in __eq__
return self.foo == other.foo
AttributeError: 'symbol' object has no attribute 'foo'
当 foo 是我的模型的一个属性时,它是一个非 SQLAlchemy 类。原因是 python 中的“in”运算符依赖于__eq__
检查列表成员资格。
问题是如何避免更改__eq__
实现以适应 SQLAlchemy 的怪癖?
我显然可以解决other
某种util.symbol
(特别是NEVER_SET
)的场景,但我认为是不完整的。
更新
这是我的__eq__
方法:
def __eq__(self, other):
return self.foo == other.foo
SQLAlchemy 符号根本没有foo
属性......