ERROR: update or delete on table "users" violates foreign key constraint on table "orders" — Key (id)=(5) is still referenced
You tried to delete a row that other rows still reference.
Seen on:
PostgreSQL
Meaning
Child rows exist and the foreign key has no ON DELETE CASCADE/SET NULL.
Common causes
- Deleting a parent with children
- FK defined without cascade behaviour
⚡ Quick fix
- Delete or reassign children first
- Recreate the FK with ON DELETE CASCADE / SET NULL
- Use soft deletes
Detailed fix by platform
PostgreSQL
ALTER TABLE orders DROP CONSTRAINT orders_user_id_fkey, ADD CONSTRAINT orders_user_id_fkey FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE;
How to diagnose
- Child — Which table references it?
- Recent change — Did it start after a deploy, upgrade, or configuration change? Compare with the last working version.
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
Was this page helpful?
Report a correction or suggest an improvement
Last updated 2 Oct 2026