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.
Exact message you may see
violates foreign key constraintis not present in table
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 not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
Most viewed in PostgreSQL
- could not connect PostgreSQL: could not connect to server / connection refused — Is the server running on host and accepting TCP/IP connections?
- pg_restore text dump pg_restore: error: input file appears to be a text format dump. Please use psql.
- 25P02 ERROR: current transaction is aborted, commands ignored until end of transaction block
Other ways to find it
Where this error sits
Origin: Data. Errors that are often confused with or caused by this one (click to open):
🧠 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.
💬 What worked for others
No tips yet — did something else fix this for you?
Share what worked for you
Tips are checked by an editor before they appear. Links are removed.
Did this fix work for you?
Report a correction or suggest an improvement
Last updated 14 Oct 2026