23001ERRORTier 2 — Caution✅ HIGH confidencerestrict violation
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
- 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.
DELETE FROM departments WHERE id = 1;
-- employees table has rows referencing department_id = 1 with ON DELETE RESTRICTFix 1: Delete child rows first, then delete the parent
When cascading deletion is appropriate.
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.
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
🔗 Related errors
📄 Reference pages