Change session.expire_all() to session.expunge_all() in delete_cascaded()
rsyring opened this issue · comments
Expire all leaves the entities that are in the session in the session. That likely increases memory usage and doesn't seem to have an advantage since delete_cascaded() is often called during test setup when we basically want everything reset. .expunge_all()
seems like the better choice, I just didn't know about that method when I wrote .delete_cascaded()
originally.
Might want to think about this a bit more. We have a lot of tests that assume objects are still in the session, even if delete_cascaded
was called subsequently. E.g. setup_class
creates objects for use in many tests, but setup
calls delete_cascaded
on an unrelated entity. Those objects are no longer associated with the db session, so making this change will require quite a bit of cleanup.
Agreed the proposed solution, changing the default, probably isn't the way to deal with this. In another project, I created a "delete everything" function which used expunge_all()
internally and it caused some problems like you described.
At the same time, it probably is worth thinking about the session a bit more, realizing it's also an object cache, and figuring out when it would be appropriate to clear that or just start a new session. Then again, this could be premature optimization since it's never really caused a problem that I know of. I'm just thinking about these kinds of things more since I'm using the "no automatic queries" paradigm more, and really liking it, and sometimes the objects in the session are something you have to think more about using that approach.