PostgreSQL constraints - ON DELETE CASCADE not being restored - postgresql

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

Related

Query which will delete other table reference as well as from main table

I have a requirement of deleting records from the Postgres database tables.
We have a Customer table which is the main table, this table contains a primary key which is used in so many other tables as a FOREIGN KEY, I want to delete one of the customers as well as its reference used in other tables. Is there any way to delete the customer from main table as well as from other tables which contains foreign key.
Thanks in Advance.
In the other tables, you want a cascading delete foreign key reference. You can create one in the database using:
alter table othertable add constraint fk_othertable_customerid
foreign key (customerid) references customers(customerid)
on delete cascade;
Note: This assumes that customerid is the name of the column in both tables and that it is already defined in the other tables.
A cascading foreign key constraint does exactly what you specify. When a row is deleted in the reference table, then all related rows are deleted.
If you already have foreign key constraints on customerid, then drop the existing constraint and add the cascading version.

Deleting from one table also deletes from other table

Postgres on Linux
When I do the following command.
DELETE FROM some_table;
It also deletes data from another table. How do I found out how?
There is probably a foreign key constraint that points to your table and is defined with ON DELETE CASCADE.
Alternatively, there may be a trigger on the table that deletes the rows.
In psql, use \d some_table to see all such foreign keys and triggers.

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

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

Deletion with on restrict constraint postgres

Is it possible to delete the row "on cascade" in postgres if there is a restrict constraint?
And is it possible to change all restrict constraints to cascade constraints automatically?
Neither is possible. Your options would be to:
DROP CONSTRAINT, DELETE and ADD CONSTRAINT
or
DROP CONSTRAINT, ADD CONSTRAINT ... ON DELETE CASCADE and DELETE
From PostgreSQL Documentation you can see what alterations can be done on table constraints:
I. SQL Commands - ALTER TABLE