mysql - In what order are ON DELETE CASCADE constraints processed? -
here example of i've got going on:
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;
notice there no on delete cascade uncle-child relationship; i.e. deleting child not delete uncle(s) , vice-versa.
when have parent , uncle same child, , delete parent, seems innodb should able "figure out" , let cascade ripple through whole family (i.e. deleting parent deletes uncle , child well). however, instead, following:
error 1451 (23000): cannot delete or update parent row: foreign key constraint fails (`cascade_test/uncle`, constraint `fk_child` foreign key (`childid`) references `child` (`id`))
innodb trying cascade-delete child before uncle(s) refer it.
am missing something? supposed fail reason don't understand? or there trick making work (or bug in mysql)?
the parent deletion triggering child deletion stated , don't know why goes child table before uncle table. imagine have @ dbms code know sure, im sure there algorithm picks tables cascade first.
the system not 'figure out' stuff way imply here , following constraint rules. problem schema created in encounters constraint not let pass further.
i see saying.. if hit uncle table first delete record , delete child (and not hit uncle cascade child deletion). so, don't think schema set rely on kind of behavior in reality. think way know sure going on through code or 1 of mysql/postgresql programmers in here how processes fk constraints.
Comments
Post a Comment