数据库
首页 > 数据库> > 在DELETE CASCADE中,自引用MySQL表的深度超过15级失败

在DELETE CASCADE中,自引用MySQL表的深度超过15级失败

作者:互联网

我有一个自我引用外键的MySQL表. ON DELETE CASCADE在它中工作得非常好,但是我注意到一个奇怪的行为,它对于父实体最多只能工作14个级别.一旦我添加一个15级孩子并尝试删除父级,它就会开始抛出错误

“Cannot delete or update a parent row: a foreign key constraint fails”

这是层次结构的图像.

尝试删除Parent将抛出错误.

>在删除Child15时,可以删除Parent
>不删除Child15,如果我尝试删除Child1,则会成功删除它.

重现行为的示例模式是here.在构建架构之前粘贴此行查询,其中id = 1而不是id = 2

DELETE FROM table1 WHERE id=1;

我知道删除它的可能的解决方法

> SET FOREIGN_KEY_CHECKS = 0
>删除时向下移动

但是我想知道,对于ON CASCADE DELETE,这是一些已知的MySQL限制吗?

我使用的是MySQL服务器版本5.6

解决方法:

这是记录在案的行为:

If ON UPDATE CASCADE or ON UPDATE SET NULL recurses to update the same
table it has previously updated during the cascade, it acts like
RESTRICT. This means that you cannot use self-referential ON UPDATE
CASCADE or ON UPDATE SET NULL operations. This is to prevent infinite
loops resulting from cascaded updates. A self-referential ON DELETE
SET NULL, on the other hand, is possible, as is a self-referential ON
DELETE CASCADE. Cascading operations may not be nested more than 15
levels deep.

资料来源:InnoDB and FOREIGN KEY Constraints, Referential Actions

标签:mysql,foreign-keys,self-reference,cascading-deletes
来源: https://codeday.me/bug/20190528/1172765.html