Prisma error P2001 (Record Not Found in Where Condition) explained: what it means, why it happens, and how to fix it — with copy-paste code examples.
Log the failing model_name, argument_name and argument_value from the error message. If that does not apply, verify the record exists with findUnique() before the write, or switch to update/delete semantics that tolerate absence — the full checklist is below.
Error code: P2001
Official name: Record Not Found in Where Condition
Service: Prisma
The record searched for in the where condition ({model_name}.{argument_name} = {argument_value}) does not exist
const existing = await prisma.user.findUnique({ where: { id } })
if (!existing) throw new NotFoundException(`User ${id}`)
return prisma.user.update({ where: { id }, data: { name } })
Explicit guard keeps the 404 behavior visible instead of leaking a Prisma error.
const { count } = await prisma.user.updateMany({
where: { id },
data: { name },
})
// count === 0 when nothing matched
updateMany never throws for missing rows - use the count to decide the response.
Catch P2001 in handlers and answer 404, mirroring REST semantics for missing resources.
catch (e) {
if (e.code === 'P2001') {
return res.status(404).json({ error: 'Resource not found' })
}
throw e
}
Prefer findUnique with a guard so the missing-record case is explicit in business logic.
const user = await prisma.user.findUnique({ where: { id } })
if (!user) {
return notFound()
}
await prisma.user.update({ where: { id }, data: { lastSeen: new Date() } })
Most often this happens when findUnique() with an id that was never created or was already deleted, or when update() or delete() on a record that disappeared between read and write.
Log the failing model_name, argument_name and argument_value from the error message.
This page documents fixes for: express, 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.