3 messages in com.googlegroups.sqlalchemy[sqlalchemy] Re: detect if objects ha...
FromSent OnAttachments
Kumar McMillan24 Sep 2007 07:45 
Michael Bayer24 Sep 2007 08:03 
Kumar McMillan25 Sep 2007 07:45 
Subject:[sqlalchemy] Re: detect if objects have been deleted from a session?
From:Michael Bayer (mike@zzzcomputing.com)
Date:09/24/2007 08:03:27 AM
List:com.googlegroups.sqlalchemy

On Sep 24, 2007, at 10:46 AM, Kumar McMillan wrote:

The implementation below seems to detect deletions of the same object but *not* the above scenario where the objects deleted were children :

def object_was_deleted(session, obj): from sqlalchemy.orm.mapper import object_mapper for c in [obj] + list(object_mapper(obj).cascade_iterator( 'delete', obj)): if c in session.uow.deleted: return True elif not session.uow._is_valid(c): return True return False

Is there a way to detect deleted children? Thanks for reading and let me know if you need a more concrete example

the session.deleted collection indicates all objects which were deleted due to a session.delete() operation. Since it seems like you are calling session.delete() for each object individually, they will all appear in this collection before a flush. However, if you are relying upon cascading (i.e. cascade="delete" or cascade="delete, delete-orphan"), those cascades are calculated at flush time, so theres no information available about those beforehand.

but also i dont understand the problem you're having. the ConcurrentModificationError should only happen if you are issuing a literal DELETE statement to the database which conflicts with the session trying to delete the instance, or if you've already issued the delete and flushed in a session concurrent to the current one. If you are using just one session and using only session.delete() to issue deletes, no error should occur, no matter how many times you call session.delete() on any of the objects (and also this has no relation to selects).