Corstiaan
Corstiaan

Reputation: 1114

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?

enter image description 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;

Upvotes: 0

Views: 2445

Answers (2)

Sirsendu
Sirsendu

Reputation: 283

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

Upvotes: 1

Corstiaan
Corstiaan

Reputation: 1114

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...

Upvotes: 0

Related Questions