6 messages in com.googlegroups.sqlalchemy[sqlalchemy] Re: Executing an action ...
FromSent OnAttachments
Matthias31 Jul 2008 07:35 
Michael Bayer31 Jul 2008 08:40 
Heston James - Cold Beans31 Jul 2008 08:50 
Matthias03 Aug 2008 21:06 
Michael Bayer04 Aug 2008 07:10 
Matthias05 Aug 2008 03:59 
Subject:[sqlalchemy] Re: Executing an action on delete of an instance
From:Michael Bayer (mike@zzzcomputing.com)
Date:07/31/2008 08:40:52 AM
List:com.googlegroups.sqlalchemy

On Jul 31, 2008, at 10:35 AM, Matthias wrote:

Hi, assume the classes A, B and C. Class A and B have a oneToMany relationship to class C.

C contains a uri which points to a file on the system. I am using "ondelete='CASCADE'", so the C's belonging to A's and B's are deleted when the A or B will be deleted. So no matter if i delte a C directly or if i delete A or B, i need a "hook" which makes it possible to delete the belonging files (related to the C's). Is this posible?

If it does matter to you, i use SQLAlchemy 0.42 with TurboGears 1.0.4.4.

SQLA wont pick up automatically on an "ondelete='CASCADE'" since that operation occurs within the database. However, if you want an activity to occur when a "C" is deleted using the SQLA ORM, the MapperExtension.after_delete() method is a good place for that.

The ORM will by default circumvent "ondelete='CASCADE'" using relation() for objects that are present in the session, such as if you had an A, deleted it, and the relation featured "delete" cascade onto a collection of C's, it would load all the C's in and explicitly delete them in which case your hook would take place. The flag "passive_deletes" turns off this behavior which can be wasteful for large collections that would otherwise be handled by the database's own cascade.