MySQL删除外键约束,同表FK,删除所有行不起作用?

发布于 2025-01-12 11:28:13 字数 827 浏览 2 评论 0原文

我有一个表,storage_directory。还有两列,directory_iddirectorydirectorydirectory_id 具有 FK 关系,因为目录可以位于我表中的目录内。

虽然,当我尝试删除引用另一个 directory_id 的目录时,我收到 FK 约束失败,即使我还在同一语句中删除引用的目录也是如此。

Directory_id目录
111(空)
222111
333222

从 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_iddirectory
111(null)
222111
333222

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不再让梦枯萎 2025-01-19 11:28:13

通过在执行删除之前将列更新为 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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文