Prisma error P2011 (Null Constraint Violation) explained: what it means, why it happens, and how to fix it — with copy-paste code examples.
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: P2011
Official name: Null Constraint Violation
Service: Prisma
Null constraint violation on the {constraint}
catch (e) {
if (e.code === 'P2011') {
console.error('constraint:', e.meta.constraint)
}
}
meta.constraint carries the column or relation name that refused null.
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.
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}`,
})
}
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
},
})
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.
Read error.meta.constraint to identify the column that rejected the null.
This page documents fixes for: nestjs, prisma-client.
Recommendations are editorial — DB Error Reference takes no payment or affiliate fees for tool listings.
This page is based on the official Prisma documentation linked below and adds practical troubleshooting guidance on top.