PG
PRO
23001ERRORTier 2 — Caution✅ HIGH confidence

restrict violation

Category: Integrity Constraint ViolationVersions: All Postgres versions

What this means

SQLSTATE 23001 is raised when a DELETE or UPDATE is blocked by a RESTRICT foreign key constraint — a child row still references the row being deleted or updated in the parent table.

Why it happens

  1. 1Deleting or updating a parent row that has child rows referencing it under a RESTRICT foreign key

How to reproduce

Deleting a parent row with existing child references.

trigger — this will ERROR
DELETE FROM departments WHERE id = 1;
-- employees table has rows referencing department_id = 1 with ON DELETE RESTRICT
ERROR: update or delete on table "departments" violates foreign key constraint "employees_department_id_fkey" on table "employees"

Fix 1: Delete child rows first, then delete the parent

When cascading deletion is appropriate.

fix
DELETE FROM employees WHERE department_id = 1;
DELETE FROM departments WHERE id = 1;

Why this works

Removing dependent rows before the parent satisfies the RESTRICT constraint.

Fix 2: Change the foreign key to ON DELETE CASCADE

When child rows should be automatically deleted with the parent.

fix
ALTER TABLE employees
  DROP CONSTRAINT employees_department_id_fkey,
  ADD CONSTRAINT employees_department_id_fkey
    FOREIGN KEY (department_id) REFERENCES departments(id) ON DELETE CASCADE;

Why this works

CASCADE automatically deletes child rows when the parent is deleted, eliminating the RESTRICT error.

What not to do

Drop the foreign key constraint to allow the delete

Why it's wrong: Removing the constraint leaves orphaned child rows with invalid references, corrupting referential integrity.

Sources

📚 Official docs: https://www.postgresql.org/docs/current/errcodes-appendix.html

🔧 Source ref: Class 23 — Integrity Constraint Violation

Confidence assessment

✅ HIGH confidence

Standard SQLSTATE for RESTRICT foreign key violations. Stable across all versions.

See also

📄 Reference pages

Foreign Key ConstraintsON DELETE CASCADERESTRICT
⚙️ This error reference was generated with AI assistance and reviewed for accuracy. Examples are provided to illustrate common scenarios and may not cover every case. Always test fixes in a development environment before applying to production. Spotted an error? Suggest a correction →