1
Esay 2013-05-17 09:06:50 +08:00
怀疑是不是wendy的主键也被复制给了clone_user,你插入的时候,主键重复了,所以会这样。
|
2
yetone 2013-05-17 10:35:34 +08:00 via Android
主键重复
|
3
lqs 2013-05-17 12:13:16 +08:00
sqlalchemy的session机制就是为了将多个同样的实例只在内存里留一份,然后被deepcopy了它就没法处理了
|
4
yueyoum 2013-05-17 19:43:47 +08:00
LZ是不是感到python很好学?
结果什么都不知道吧 __deepcopy__ |
5
banxi1988 OP @Esay
@yetone 你们这么一说我试了下,但还是会抱错: In [15]: ed = session.query(User).filter_by(name='ed').one() In [22]: import copy In [23]: clone_ed = copy.deepcopy(ed) In [24]: clone_ed Out[24]: <User('ed','Ed Jones','edspassword')> In [25]: clone_ed.id Out[25]: 4 In [26]: ed.id Out[26]: 4 In [27]: clone_ed.id = None In [28]: clone_ed.name = 'clone ed' In [29]: session.add(clone_ed) --------------------------------------------------------------------------- AssertionError Traceback (most recent call last) <ipython-input-29-c3ecb2f64abe> in <module>() ----> 1 session.add(clone_ed) /Users/yautou/work/python/youjia/ENV/lib/python2.7/site-packages/sqlalchemy/orm/session.pyc in add(self, instance, _warn) 1396 raise exc.UnmappedInstanceError(instance) 1397 -> 1398 self._save_or_update_state(state) 1399 1400 def add_all(self, instances): /Users/yautou/work/python/youjia/ENV/lib/python2.7/site-packages/sqlalchemy/orm/session.pyc in _save_or_update_state(self, state) 1408 1409 def _save_or_update_state(self, state): -> 1410 self._save_or_update_impl(state) 1411 1412 mapper = _state_mapper(state) /Users/yautou/work/python/youjia/ENV/lib/python2.7/site-packages/sqlalchemy/orm/session.pyc in _save_or_update_impl(self, state) 1664 self._save_impl(state) 1665 else: -> 1666 self._update_impl(state) 1667 1668 def _delete_impl(self, state): /Users/yautou/work/python/youjia/ENV/lib/python2.7/site-packages/sqlalchemy/orm/session.pyc in _update_impl(self, state, discard_existing) 1657 self.identity_map.replace(state) 1658 else: -> 1659 self.identity_map.add(state) 1660 self._attach(state) 1661 /Users/yautou/work/python/youjia/ENV/lib/python2.7/site-packages/sqlalchemy/orm/identity.pyc in add(self, state) 117 "A conflicting state is already " 118 "present in the identity map for key %r" --> 119 % (key, )) 120 else: 121 return AssertionError: A conflicting state is already present in the identity map for key (<class '__main__.User'>, (4,)) In [30]: clone_ed Out[30]: <User('clone ed','Ed Jones','edspassword')> In [31]: clone_ed.id In [32]: @yueyoum 我google过,但是我觉得还让我去实现个__deepcopy__ 我不如直接自己写一个clone() 方法。 @lqs 判断同样实例的是?id我上面测了,不是的。 |
6
yuelang85 2013-05-17 22:58:27 +08:00
楼主看清报错信息,说得很明白了,主键重复。
请不要用None做试验。。。。 |
7
lqs 2013-05-17 23:51:44 +08:00 2
@banxi1988 在 session.identity_map 保存了所有的对象。
lone_ed.id = None 这句的意思是『下次保存时把 id 从 4 改成 None 』,而不是『建立一个新的对象』。sqlalchemy会记住『这个对象的 id 以前是 4』。 |