Diagnostic Queries
Symptoms
An UPDATE or DELETE on a referenced (parent) row was blocked because dependent (child) rows still reference it under a RESTRICT/NO ACTION foreign key. PostgreSQL raises SQLSTATE 23503 (foreign_key_violation).
- Deleting/updating a parent row that has children.
- The FK uses RESTRICT or NO ACTION (no cascade).
- The DETAIL line names the referencing table and key.
What the server log shows
ERROR: update or delete on table "customers" violates foreign key constraint "orders_customer_fk" on table "orders"
DETAIL: Key (id)=(42) is still referenced from table "orders".
Why PostgreSQL raises this — what the manual says
Section 5.5.5 Foreign Keys:
“RESTRICT is a stricter setting than NO ACTION. It prevents deletion of a referenced row.”
A foreign key with RESTRICT/NO ACTION forbids removing or re-keying a parent row while child rows reference it. Such an operation would orphan the children, so PostgreSQL reports 23503.
Common causes
- Deleting a parent that still has dependent rows.
- Updating a referenced key value with children present.
- Expecting cascade behaviour where the FK uses RESTRICT/NO ACTION.
How to fix it
- Delete/repoint the child rows first, then the parent.
- Define the FK with
ON DELETE CASCADE/ON UPDATE CASCADEif that’s intended. - Use a soft-delete or reassign children before removing the parent.
Related & next steps
Reference: PostgreSQL 18 Section 5.4 “Constraints”.
Thanks — noted. This helps keep the database accurate.