tags:

views:

402

answers:

1

I have the following table relationship in my database:

          Parent
          /    \
    Child1      Child2
        \        /
        GrandChild

I am trying to create the FK relationships so that the deletion of the Parent table cascades to both child and the grandchild table. For any one particular granchild, it will either be parented to one or the other child tables, but never both at the same time.

When I'm trying to add ON DELETE CASCADE to the FK relationships, everything is fine adding them to one "side" of the two children (Parent-Child1-GrandChild is fine for Cascade Delete). However, as soon as I add the Cascade Delete on the Child2 "side" of the relationship SQL tells me that the FK would cause multiple cascade paths. I was under the impression that multiple cascade paths only apply when more than one FK indicates the SAME table. Why would I be getting the multiple cascade paths error in this case?

PS The table relationships at this point would be very difficult to change so simply telling me to change my table structure is not going to be helpful, thanks.

+4  A: 

The message means that if you delete a Parent record, there are two paths that lead to all deletable GrandChild records.

Fix: Remove the ON DELETE CASCADE options in the FKs, and create INSTEAD OF DELETE triggers for the ChildX tables, deleting all grandchild records, and then the childX records themselves.

devio