On Jan 16, 2014, at 1:48 PM, Rich <richardmu...@gmail.com> wrote:

> I've been using delete cascading on a particular relationship for some time 
> and has worked well.  My requirements have now changed so that I need to 
> cascade a delete to only certain items in the relationship based on complex 
> criteria.  I'm not sure of a good way to handle this. There is a 
> before_delete event on instances, but (unless I'm mistaken) I can't delete 
> the other items because that would alter the session.
> 
> Does anybody have good suggestions on how to handle this scenario?
> 
> If it helps, I'm trying to implement functionality very similar to 
> delete-orphan, with a custom definition of what orphan means.

you’d implement a before_flush() listener, where you iterate through items in 
“session.deleted”, find those which match your criteria and then operate on the 
collections accordingly.  Within before_flush() you can call additional 
session.delete() calls for any other objects.

if the listener is specific to “orphan”, you can also find “orphans” by 
iterating through the session, identifying those which match the classes you 
are looking for (e.g. with isinstance()) and then checking if they refer to the 
parent (e.g. with a backref). If you don’t have backrefs then you may need to 
use the _is_orphan() method which isn’t public API.


Attachment: signature.asc
Description: Message signed with OpenPGP using GPGMail

Reply via email to