Prisma · query error · P2003

P2003: Foreign key constraint violated

what it looks like
PrismaClientKnownRequestError:
Invalid `db.post.create()` invocation:

Foreign key constraint violated on the foreign key

What it means

A row points at a related row that doesn't exist (on create or update), or you deleted a row that other rows still point at.

Common causes

  • Creating a record with a userId from a stale session or a hard-coded id
  • Deleting a user that still has posts, subscriptions or accounts
  • Seed scripts that insert children before parents

How to fix it

  1. Use connect with an id you just read from the database (or the signed-in session)
  2. Add onDelete: Cascade to the relation if children should go with the parent, then push or migrate
  3. Insert parents before children in seeds, or wrap both in db.$transaction

works with Prisma ORM 6.19 · Next.js 16last verified

Prompt for Cursor or Claude Code

Prisma throws P2003 foreign key constraint violated. Find which relation it is, check whether the parent record exists, and either fix the id being used or add onDelete: Cascade (or SetNull) to the relation if that's the intended behavior. Tell me before changing the schema.

Not quite your error? Paste yours into the free VibeBoot Doctor or browse the Prisma error fixer.

next: skip the next one

Skip the setup. The VibeBoot SaaS Starter ships a ready prisma/schema.prisma and one Prisma client in src/lib/db.ts (SQLite by default, Postgres by changing the provider), plus auth and Stripe on Next.js 16.4.3. $49, one-time.

14-day money-back guarantee · lifetime updates

$ fixed it? get the next one first

New fixes for what AI agents break, by email.

When we add a fix or a Cursor rule for a new Next.js, Auth.js, Prisma or Stripe break, it goes out in the VibeBoot newsletter. Free, occasional, unsubscribe in one click.

Subscribe free