In the course of parsing / changing someone else's code, errors occur when synchronizing dependent entities (Person - Child) during the synchronization script. Apparently, there is such a situation that session.add(person) tries to add to the database the orm-Child object that was previously deleted by the synchronizer:
session.add(person) File "/home/web/portal/env/local/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 1559, in add self._save_or_update_state(state) File "/home/web/portal/env/local/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 1578, in _save_or_update_state self._save_or_update_impl(st_) File "/home/web/portal/env/local/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 1828, in _save_or_update_impl self._update_impl(state) File "/home/web/portal/env/local/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 1814, in _update_impl state_str(state) InvalidRequestError: Instance '<Child at 0xec10afec>' has been deleted. Use the make_transient() function to send this object back to the transient state. What does marking the object
make_transient()meanmake_transient()How and why should it be used in such a context?In the course of the code, another strategy of deleting objects with one request is also used: via
.delete(). The question is what gives such requests and when thesynchronize_sessionflag is used?
session.query(Child).filter(Child.id==child.id).delete(synchronize_session=False)