@
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.idOut[25]: 4
In [26]:
ed.idOut[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.idIn [32]:
@
yueyoum 我google过,但是我觉得还让我去实现个__deepcopy__ 我不如直接自己写一个clone()
方法。
@
lqs 判断同样实例的是?id我上面测了,不是的。