# ERROR: insert or update on table "x" violates foreign key constraint "fk" — Key (user_id)=(5) is not present in table "users"

**Code:** `23503` · **Category:** PostgreSQL · **Seen on:** PostgreSQL

The row references a parent row that doesn’t exist.

## 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

1. Insert parents first
2. Validate the referenced id exists
3. Defer constraints within a transaction for bulk loads (DEFERRABLE)

## Detailed fix by platform

### PostgreSQL

1. `SELECT 1 FROM users WHERE id = 5;`

## How to diagnose

1. **Key** — DETAIL shows which key?
2. **Order** — Parent inserted first?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/postgresql/insert-violates-foreign-key-constraint)
