Thanks, seemed to work for what I needed.

On Thursday, January 16, 2014 12:44:31 PM UTC-7, Michael Bayer wrote:
>
>
> On Jan 16, 2014, at 1:48 PM, Rich <[email protected] <javascript:>> 
> 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. 
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to