简体   繁体   English

Postgres-级联删除不起作用

[英]Postgres - Cascade delete not working

I have a table called "Reviews" and it references a record in a table "ReviewSetups". 我有一个名为“ Reviews”的表,它引用了“ ReviewSetups”表中的一条记录。 When I delete a ReviewSetup I was to also delete all child Reviews (so cascade delete). 当我删除一个ReviewSetup时,我还将同时删除所有子评论(因此请级联删除)。

I have setup the foreign key like below on the Reviews table but nothing gets deleted when I delete a parent ReviewSetup. 我已经在Reviews表上设置了如下所示的外键,但是在删除父ReviewSetup时没有任何内容被删除。

I have other entities in by db as well which I migrated with a FK in exactly the same way and those work fine. 我在db中也有其他实体,这些实体我以完全相同的方式与FK一起迁移,并且工作正常。

Does anyone have an idea what is going on here? 有人知道这里发生了什么吗?

在此处输入图片说明

EDIT 编辑

Here's the code: 这是代码:

-- Foreign Key: "FK_Reviews_ReviewSetup_Id_ReviewSetups_Id"

-- ALTER TABLE "Reviews" DROP CONSTRAINT "FK_Reviews_ReviewSetup_Id_ReviewSetups_Id";

ALTER TABLE "Reviews"
  ADD CONSTRAINT "FK_Reviews_ReviewSetup_Id_ReviewSetups_Id" FOREIGN KEY ("ReviewSetup_Id")
      REFERENCES "ReviewSetups" ("Id") MATCH SIMPLE
      ON UPDATE CASCADE ON DELETE CASCADE;

If you had to drop database again and again, it's better to disable constraints till you find the right culprit or re-design the schema. 如果必须一次又一次删除数据库,最好禁用约束,直到找到合适的罪魁祸首或重新设计架构为止。

Disable constraints and delete data, then re-enable again. 禁用约束并删除数据,然后再次重新启用。

Disable constraints : Alter table tablename NOCHECK CONSTRAINT constraintname 禁用约束: Alter table tablename NOCHECK CONSTRAINT constraintname

Enable again: Alter table tablename CHECK CONSTRAINT constraintname 再次启用: Alter table tablename CHECK CONSTRAINT constraintname

Ended up dropping the entire db and re-running the migration from scratch. 最终删除了整个数据库,并从头开始重新运行迁移。 Somehow that solved it. 以某种方式解决了它。 Somewhere, somehow the config was off a bit. 在某处,以某种方式关闭了配置。 Really curious what was the culprit though... 真的很好奇罪魁祸首是什么...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM