Postgres - Cascade delete not working - postgresql

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

Related

How to use cascade update on PK in postgresql

ALTER EMPLOYEE
DROP CONSTRAINT MGR_SSN
Change all SSN ON UPDATE CASCADE
When an employee’s SSN is updated
- then propagate that change to all pertinent FKs
Link to DB: https://www.db-fiddle.com/f/wCwvN6pFq2cXvfuE57QTum/0#&togetherjs=8aityz4DAt
If you need to update a primary key, your DB design is not good.
That being said, it is possible to add a foreign key to a table with the constraint you have given. See below (I have added a ON DELETE SET NULL).
I repeat: I recommend you review your DB design.
ALTER TABLE Employee
ADD FOREIGN KEY (Mgr_SSN) REFERENCES Employee(SSN) ON DELETE SET NULL ON UPDATE CASCADE

Delete "on delete cascade" constraint

In a previous command, I foolishly wrote:
alter table UserInfo
add column gcal_id integer references GoogleCal on delete cascade
I've since realized that I don't want on delete cascade. How do I alter gcal-id in UserInfo to no longer have that constraint without losing the information saved in current entries?
Happily, it's fairly simple.
First \d+ UserInfo to see the constraint name, which will appear below the table's column definitions.
In your case it will probably be something like
Foreign-key constraints:
"userinfo_gcal_id_fkey" FOREIGN KEY (gcal_id) REFERENCES googlecal(id) ON DELETE CASCADE
Then, just drop and re-add the constraint in one command:
ALTER TABLE UserInfo
DROP CONSTRAINT userinfo_gcal_id_fkey,
ADD CONSTRAINT userinfo_gcal_id_fkey FOREIGN KEY (gcal_id) REFERENCES googlecal(id);
omitting the ON DELETE CASCADE part.

Drop cascade condition in PostgreSQL

I have a table with a foreign key reference and I had added a on_delete_cascade condition with that foreign key.
I don't need the rows to be deleted even if the foreign key object gets deleted.
How can I change the drop condition without have to drop the column?
Just drop the conatraint and then add it back without the ON DELETE CASCADE clause:
ALTER TABLE some_table DROP CONSTRAINT some_key,
ADD CONSTRAINT some_key FOREIGN KEY (id) REFERENCES tab(a_id);
Check what the real experts wrote by reading here:
https://www.postgresql.org/message-id/CABvLTWHdT0tTygV0-O_ZgLRRAGZAg0W4zvghfF2PshAzvkAaGg%40mail.gmail.com

What happens when two equal foreign keys with conflicting on-deletes are defined on the same table in PostgreSQL?

In order to delete some rows referenced by a foreign key constraint without cascading on delete, I created a temporary foreign key constraint, deleted the row, and then deleted the temporary constraint:
ALTER TABLE rel_user_right
ADD CONSTRAINT temp_fk_rel_user_right_user_right_02
FOREIGN KEY (right_id) REFERENCES user_right (id)
ON DELETE CASCADE;
DELETE FROM user_right WHERE "name" LIKE '%.statusLight.%';
ALTER TABLE rel_user_right
DROP CONSTRAINT temp_fk_rel_user_right_user_right_02;
where this table already had the following constraint defined on it:
ALTER TABLE rel_user_right
ADD CONSTRAINT fk_rel_user_right_user_right_02
FOREIGN KEY (right_id) REFERENCES user_right (id);
This worked fine for me, but seems to have failed on my colleague's computer. As you can see, the two FK constraints define conflicting ON DELETE behaviour. Is precedence defined in this situation, or is it non-deterministic?
Postgres allows to create two references differing only in ON DELETE clause.
I could find no information on the impact of such a case.
In my tests I was unable to cover the existing constraint with new one (i.e. DELETE was always restricted despite of the existence of the second cascading constraint).
However this behaviour is undocumented and one should not rely on it.
The normal way to proceed should be replacing the old constraint with new one:
ALTER TABLE rel_user_right
ADD CONSTRAINT fk_rel_user_right_user_right_temp
FOREIGN KEY (right_id) REFERENCES user_right (id)
ON DELETE CASCADE,
DROP CONSTRAINT fk_rel_user_right_user_right;
DELETE FROM user_right WHERE "name" LIKE '%.statusLight.%';
ALTER TABLE rel_user_right
ADD CONSTRAINT fk_rel_user_right_user_right
FOREIGN KEY (right_id) REFERENCES user_right (id),
DROP CONSTRAINT fk_rel_user_right_user_right_temp;
DISABLE CONSTRAINT would be useful here, but there is no such feature in Postgres (there have been attempts to implement it, but they did not end in success). You can use DISABLE TRIGGER for it, but the above solution is simpler and more natural.

PostgreSQL constraints - ON DELETE CASCADE not being restored

I have run into problems when restoring a PostgreSQL database schema in another server. More precisely, some of the tables don't seem to have the same foreign key constraints associated with them that they used to in the original database. For example, the ON DELETE CASCADE clause seems to have completely evaporated from all of the constraint definitions.
That's probably because the dumping procedure didn't backup the ON DELETE CASCADE clauses in your table definitions.
Firstly you should delete the foreign key constraints on your tables and then go on to altering them:
Something like the following:
ALTER TABLE ONLY *your_table* DROP CONSTRAINT your_constraint;
After that, recreate the constraints with something like:
ALTER TABLE ONLY your_table ADD CONSTRAINT your_constraint (...ON DELETE CASCADE, etc..);