kundante
kundante

Reputation: 2160

How to delete row from postgresql table without cascade

Assume my application two different tables, one of which references the other. And also an on delete cascade is defined?

I do not want to delete on delete cascade constraint but, sometimes from my local I want to delete that ignores cascade statement such as:

delete_without_cascade from table1 where id = id1

Is there a way to do that

Upvotes: 4

Views: 5480

Answers (1)

Igor
Igor

Reputation: 62318

Cascade on delete will delete children records when the parent is deleted. If you do not want to do that then you have to do something with the values in the column bound by the FK constraint because the db schema should not allow a record to have an orphaned value. If the column allows for null values then execute an update before to set the the FK column value to null before running the delete.

If the cascade was not present you would normally get an error when executing the delete statement because there would be orphaned records in other tables.

In case you are not understanding how Cascade on delete works, it does not work in reverse, ie. deleting a record that references a parent table will not cause the parent record to be deleted.

Upvotes: 0

Related Questions