Prisma error P2025 (Required Record Not Found) explained: what it means, why it happens, and how to fix it — with copy-paste code examples.
Inspect error.meta.cause and error.meta.model to locate the operation and the missing record. If that does not apply, verify the id was really created by the database (not undefined, NaN, or a fake value) before connecting — the full checklist is below.
Error code: P2025
Official name: Required Record Not Found
Service: Prisma
An operation failed because it depends on one or more records that were required but not found. {cause}
try {
await prisma.profile.delete({ where: { userId } })
} catch (e) {
if (e.code === 'P2025') {
// meta.cause explains which required record was missing
console.log(e.meta.cause)
}
}
P2025 wraps a short description of the missing dependency in meta.cause.
await prisma.user.deleteMany({ where: { id } })
// deleteMany never throws when the row is absent
deleteMany() is naturally idempotent - no record means no-op instead of P2025.
Map P2025 to HTTP 404 Not Found in the global filter and include the model name for debuggable API errors.
if (e.code === 'P2025') {
return response.status(404).json({
message: `${e.meta.model ?? 'Record'} not found`,
})
}
Catch P2025 in route handlers and answer 404 instead of 500 so clients can react to missing resources.
router.delete('/users/:id', async (req, res) => {
try {
await prisma.user.delete({ where: { id: req.params.id } })
res.status(204).end()
} catch (e) {
if (e.code === 'P2025') return res.status(404).json({ error: 'User not found' })
throw e
}
})
Use findFirst() before nested writes so the absence of the parent record is handled explicitly.
const parent = await prisma.post.findFirst({ where: { id: postId } })
if (!parent) {
return { status: 404, body: 'Post not found' }
}
await prisma.comment.create({
data: { text, postId: parent.id },
})
Most often this happens when deleting or updating a record by an id that no longer exists, or when nested write referencing a related record with connect: { id } when that id is missing.
Inspect error.meta.cause and error.meta.model to locate the operation and the missing record.
This page documents fixes for: nestjs, 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.