23503 parent 🐘 PostgreSQL

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

  1. Delete or reassign children first
  2. Recreate the FK with ON DELETE CASCADE / SET NULL
  3. Use soft deletes

Detailed fix by platform

PostgreSQL

  1. 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

  1. Child — Which table references it?
  2. 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.