ERROR: insert or update on table "x" violates foreign key constraint "fk" — Key (user_id)=(5) is not present in table "users"
The row references a parent row that doesn’t exist.
Seen on:
PostgreSQL
Meaning
The DETAIL line names the missing key. Inserting children before parents, deleted parents, or wrong ids from the client.
Common causes
- Parent row doesn’t exist (wrong id)
- Insert order wrong in seeds/imports
- Parent deleted concurrently
⚡ Quick fix
- Insert parents first
- Validate the referenced id exists
- Defer constraints within a transaction for bulk loads (DEFERRABLE)
Detailed fix by platform
PostgreSQL
SELECT 1 FROM users WHERE id = 5;
How to diagnose
- Key — DETAIL shows which key?
- Order — Parent inserted first?
🧠 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