<p>Here is an example of what I've got going on:</p>

<pre><code>CREATE TABLE Parent (id BIGINT NOT NULL,
  PRIMARY KEY (id)) ENGINE=InnoDB;

CREATE TABLE Child (id BIGINT NOT NULL,
  parentid BIGINT NOT NULL,
  PRIMARY KEY (id),
  KEY (parentid),
  CONSTRAINT fk_parent FOREIGN KEY (parentid) REFERENCES Parent (id) ON DELETE CASCADE) ENGINE=InnoDB;

CREATE TABLE Uncle (id BIGINT NOT NULL,
  parentid BIGINT NOT NULL,
  childid BIGINT NOT NULL,
  PRIMARY KEY (id),
  KEY (parentid),
  KEY (childid),
  CONSTRAINT fk_parent_u FOREIGN KEY (parentid) REFERENCES Parent (id) ON DELETE CASCADE,
  CONSTRAINT fk_child FOREIGN KEY (childid) REFERENCES Child (id)) ENGINE=InnoDB;
</code></pre>

<p>Notice there is no ON DELETE CASCADE for the Uncle-Child relationship; i.e. deleting a Child does not delete its Uncle(s) and vice-versa.</p>

<p>When I have a Parent and an Uncle with the same Child, and I delete the Parent, it <em>seems</em> like InnoDB should be able to just "figure it out" and let the cascade ripple through the whole family (i.e. deleting the Parent deletes the Uncle and the Child as well).  However, instead, I get the following:</p>

<pre><code>  ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`cascade_test/uncle`, CONSTRAINT `fk_child` FOREIGN KEY (`childid`) REFERENCES `child` (`id`))
</code></pre>

<p>InnoDB is trying to cascade-delete the Child before the Uncle(s) that refer to it.</p>

<p>Am I missing something?  Is this <em>supposed</em> to fail for some reason I don't understand?  Or is there some trick to making it work (or is it a bug in MySQL)?</p>

[{'answer_id': 60185, 'author': 'Apocalisp', 'author_id': 3434, 'author_profile': 'https://Stackoverflow.com/users/3434', 'pm_score': 3, 'selected': False, 'text': "<p>In the simpler case, what happens if a record is deleted from Child and it has a referencing Uncle? That's unspecified, so the constraints fail for that anyway.</p>\n\n<p>If deleting a Child does not delete its Uncles, then what happens instead? Uncle.childid cannot be null.</p>\n\n<p>What you want is one of these three things:</p>\n\n<ol>\n<li>Uncle.childid can be null, and you want ON DELETE SET NULL for childid.</li>\n<li>Uncle.childid cannot be null, and you want ON DELETE CASCADE for childid.</li>\n<li>Childid does not belong on Uncle, and you want a ChildsUncle relation with ON DELETE CASCADE foreign key constraints to both Child and Uncle. Uncleid would be a candidate key for that relation (i.e. it should be unique).</li>\n</ol>\n"}, {'answer_id': 60229, 'author': 'Tony BenBrahim', 'author_id': 80075, 'author_profile': 'https://Stackoverflow.com/users/80075', 'pm_score': -1, 'selected': False, 'text': "<p>the design is all wrong. You should have single table, with parent child relationship (literrally).\nThen you can figure out uncles (and aunts) with a query</p>\n\n<p><code>\nselect id from persons where -find all children of the grandparents<br>\nparent id in (<br>\n    select parentid from persons   --find the grandparents<br>\n    where id in (<br>\n        select parentid from persons     --find the parents<br>\n        where id=THECHILD)\n    )<br>\nminus --and take out the child's parents<br>\nselect parentid from persons<br>\nwhere id=THECHILD<br>\n</code>  </p>\n"}, {'answer_id': 78024, 'author': 'Arthur Thomas', 'author_id': 14009, 'author_profile': 'https://Stackoverflow.com/users/14009', 'pm_score': 3, 'selected': True, 'text': "<p>The parent deletion is triggering the child deletion as you stated and I don't know why it goes to the child table before the uncle table.  I imagine you would have to look at the dbms code to know for sure, but im sure there is an algorithm that picks which tables to cascade to first.</p>\n\n<p>The system does not really 'figure out' stuff the way you imply here and it is just following its constraint rules.  The problem is the schema you created in that it encounters a constraint that will not let it pass further.  </p>\n\n<p>I see what you are saying.. if it hit the uncle table first it would delete the record and then delete the child (and not hit the uncle cascade from the child deletion).  But even so, I don't think a schema would be set up to rely on that kind of behavior in reality.  I think the only way to know for sure what is going on is to look through the code or get one of the mysql/postgresql programmers in here to say how it processes fk constraints.</p>\n"}, {'answer_id': 16425452, 'author': 'sactiw', 'author_id': 416369, 'author_profile': 'https://Stackoverflow.com/users/416369', 'pm_score': 0, 'selected': False, 'text': '<p>@Matt Solnit first of all this is really a good question and as far as I know when a record from Parent is to be deleted then innodb first tries to identify which other tables holds references to it so that it can delete the record from them as well. In your case it is Child table and Uncle table, now it seems that in this case it decides to first delete record from Child table and thus it repeats the same process for Child and eventually fails as Uncle holds reference to Child table but neither "ON DELETE CASCADE" nor "ON DELETE SET NULL" is specified for fk_child FK in Uncle table. However, it does seems that <em>if</em> innodb first tries to delete record from the Uncle table then deletion should have happened smoothly. Well after giving a second thought I think that since innodb follows ACID model thus it chooses Child over Uncle to start the deletion process becuase if it starts with Uncle even then the deletion in Child might still have failed e.g. assume a Friend table that has fk_child key (similar to Uncle) without ON DELETE CASCADE, now this would still have caused the whole transaction to fail and therefore this looks right behaviour to me. In others words innodb starts with table that may cause a possible failure in the transaction but that is my theory in reality it might be a completely different story. :)</p>\n'}]
['https://Stackoverflow.com/questions/60168', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/6198/']