[sqlalchemy] Cascade in many-to-many relationships: when orphans are not orphans

2011-08-25 Thread Benjamin Sims
Thanks for the help with my query the other day - as ever response was swift
and bang on.

I'm now trying to set up another m:n relationship in ORM correctly.
Pseudocode:

parentA.children.append(child1)
parentA.children.append(child2)

parentB.children.append(child2)

session.delete(parentA)

At this stage, I would like child1 to be deleted and child2 to survive.

However, if I use (cascade = all), then both children will be deleted when
ParentA is. I hoped that delete-orphan would be applicable in this
situation, but that requires that single-parent be True, which I understand
it cannot for a true many-to-many.

So I guess what I am asking is - is it possible for child objects which have
still have remaining parents to survive, while deleting those with no
parents left?

Thanks,
Ben

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



Re: [sqlalchemy] Cascade in many-to-many relationships: when orphans are not orphans

2011-08-25 Thread Michael Bayer

On Aug 25, 2011, at 7:06 PM, Benjamin Sims wrote:

 Thanks for the help with my query the other day - as ever response was swift 
 and bang on.
 
 I'm now trying to set up another m:n relationship in ORM correctly. 
 Pseudocode:
 
 parentA.children.append(child1)
 parentA.children.append(child2)
 
 parentB.children.append(child2)
 
 session.delete(parentA)
 
 At this stage, I would like child1 to be deleted and child2 to survive. 
 
 However, if I use (cascade = all), then both children will be deleted when 
 ParentA is. I hoped that delete-orphan would be applicable in this situation, 
 but that requires that single-parent be True, which I understand it cannot 
 for a true many-to-many.
 
 So I guess what I am asking is - is it possible for child objects which have 
 still have remaining parents to survive, while deleting those with no parents 
 left?

That's not something supported by delete, delete-orphan cascade and its why 
the single_parent=True flag is required - so that users aren't misled into 
thinking it can work that way.

You'd need to roll this using attribute events most likely.   The potential 
expense is that you may have to emit SQL in order to load the full collection 
of parents for each child in order to detect the orphan condition (which is 
why SQLA doesn't support this automatically, it would be extremely inefficient 
implemented generically).


-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.