Prisma Error P2011: Null Constraint Violation

Prisma error P2011 (Null Constraint Violation) explained: what it means, why it happens, and how to fix it — with copy-paste code examples.

Error code P2011 Prisma Last verified 2026-08-19

Quick Answer

Read error.meta.constraint to identify the column that rejected the null. If that does not apply, check the model: fields missing ? are required and must be supplied (or given a @default) — the full checklist is below.

Error Code

Error code: P2011
Official name: Null Constraint Violation
Service: Prisma

What does this error mean?

Null constraint violation on the {constraint}

Common Causes

How to Fix

  1. Read error.meta.constraint to identify the column that rejected the null
  2. Check the model: fields missing ? are required and must be supplied (or given a @default)
  3. Look for @default(@db.Text) or @updatedAt fields the client forgets to provide - Prisma usually fills these, but raw SQL may not
  4. For relations, mark them optional (user Profile?) or create the connected record in the same nested write
  5. If the schema says optional but the DB says NOT NULL, run prisma migrate dev to align them

Code Examples

Which field is null? typescript
catch (e) {
  if (e.code === 'P2011') {
    console.error('constraint:', e.meta.constraint)
  }
}

meta.constraint carries the column or relation name that refused null.

Provide required fields explicitly typescript
await prisma.user.create({
  data: {
    email: 'ada@example.com',
    name: 'Ada', // required in schema - never null
  },
})

Every non-optional field must be present with a value or a schema-level @default.

Framework-Specific Fixes

nestjs

Map P2011 to HTTP 400 and echo the constraint name in the payload for immediate feedback.

if (e.code === 'P2011') {
  return response.status(400).json({
    message: `Missing required value: ${e.meta.constraint}`,
  })
}
prisma-client

Use nested creates to satisfy required relations in one call instead of a second failing update.

await prisma.user.create({
  data: {
    email,
    profile: { create: { bio } }, // satisfies required relation
  },
})

You Might Also Like

Frequently Asked Questions

Why am I seeing Prisma error P2011?

Most often this happens when create()/update() leaves out a field marked as required (non-optional) in the schema, or when passing null explicitly for a non-nullable field.

How do I fix Prisma error P2011?

Read error.meta.constraint to identify the column that rejected the null.

Which frameworks have documented fixes for error P2011?

This page documents fixes for: nestjs, prisma-client.

Official Sources

This page is based on the official Prisma documentation linked below and adds practical troubleshooting guidance on top.