简体   繁体   中英

Postgres - Cascade delete not working

I have a table called "Reviews" and it references a record in a table "ReviewSetups". When I delete a ReviewSetup I was to also delete all child Reviews (so cascade delete).

I have setup the foreign key like below on the Reviews table but nothing gets deleted when I delete a parent 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.

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

Enable again: 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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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