MySQL删除外键约束,同表FK,删除所有行不起作用?
我有一个表,storage_directory
。还有两列,directory_id
和 directory
。 directory
与 directory_id
具有 FK 关系,因为目录可以位于我表中的目录内。
虽然,当我尝试删除引用另一个 directory_id 的目录时,我收到 FK 约束失败,即使我还在同一语句中删除引用的目录也是如此。
Directory_id | 目录 |
---|---|
111 | (空) |
222 | 111 |
333 | 222 |
从 Directory_id IN ('222', '333') 的目录中删除
。
我不明白为什么会失败,因为删除后的结果将不包含外键约束。有没有办法在不消除约束的情况下解决这个问题?
I have a table, storage_directory
. And two columns, directory_id
and directory
. directory
has a FK relationship to directory_id
as a directory can be within a directory in my table.
Although, when I try to delete a directory that references another directory_id I receive a FK constraint fails, even when I am also deleting the referenced directory in the same statement.
directory_id | directory |
---|---|
111 | (null) |
222 | 111 |
333 | 222 |
DELETE FROM directories WHERE directory_id IN ('222', '333')
.
I do not understand why this fails, as the result after the delete would not contain a foreign key constraint. Is there a way around this without removing the constraint?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过在执行删除之前将列更新为 null 解决了此问题。不理想,但目前有效。直到我找到另一个解决方案(或者忘记它并将其投入生产)
Resolved this issue by updating the column to null before performing a delete. Not ideal, but it works for now. Until I find another solution (or forget about it and put it in production anyways)