cascade="all, delete-orphan" does not work for many to many relationships

Issue #566 resolved
Former user created an issue

output from test program:

betelgeuse@pena ~/python $ python create.py
sqlite://
1 [object at 0xb7c1d9ac>](<__main__.ClassFile) 2 [object at 0xb7c1d9ac>](<__main__.ClassFile)
2 [object at 0xb7c1d9ac>](<__main__.ClassFile)
Traceback (most recent call last):
  File "create.py", line 68, in <module>
    assert len(files) == 1
AssertionError

Here we see that the ClassFile is attached to slot 2 but it's gone from the DB because the assertion fails.

Comments (2)

  1. Mike Bayer repo owner
    • changed status to wontfix
    • changed component to orm

    its not the "delete-orphan" cascade that is deleting the secondary object; its the "delete" cascade.

    additionally, the "delete-orphan" cascade implies "delete", however i have separated that out in changeset:2602. but "delete-orphan" still only fires off in response to a collection/attribute removal, not a deletion of the parent (this is why "delete" and "delete-orphan" cascade are meant to be joined together). so with the "delete" cascade removed from your example using the trunk, the child ClassFile object never gets marked for deletion no matter what you do with the parent slot objects (unless you remove the ClassFile from the collections explicitly and/or mark it for deletion directly).

    generally the cascade rules are meant to be convenience functions only and if you try to trip them up with scenarios like this, you'll succeed. i dont think its appropriate to have "delete" cascade set on a many-to-many relationship.

    in Hibernate, which is what our cascades are modeled after, "delete-orphan" is only valid for a one-to-many relationship (plus they recommend cascades not be used at all on many-to-many or many-to-one). So they would agree with my intuition here, so I am considering adding an assertion for this condition. Based on that I think i have to mark this particular issue as "wontfix", even though i think i am inspired to make some changes in response to this issue.

    looking through the workings of cascades just now, particularly deletes, i have identified a lot of places where i think redundant operations are occuring and I probably will be cleaning up a lot of this soon.

  2. Log in to comment