Repository navigation
update()/delete() return null instead of throwing P2025 in Prisma 8. What's the recommended migration pattern? #31363
|
Hi Prisma team 👋 In Prisma 7, try {
await prisma.user.update({ where: { id }, data })
} catch (e) {
if (e.code === 'P2025') return res.status(404).send()
throw e
}In Prisma 8 (I checked My questions:
Context: why I'm asking I'm building prisma-v8-codemod, an open-source AST codemod that rewrites Prisma 7 queries to the Prisma 8 ORM API: // Prisma 7
prisma.user.findMany({ where: { active: true }, take: 10 })
// Prisma 8
db.orm.public.User.where({ active: true }).limit(10).all()Where it is today (v0.3.4):
It's early and still in active development, so any feedback is welcome: mappings that look wrong, queries it should handle, or anything that's changing in upcoming RCs. If it's useful to the team, I'd be glad to keep it in sync with each RC or contribute any of it upstream. Thanks! |
Replies: 6 comments 2 replies
|
A few important findings from the 1. Heads-up:
|
|
I checked the published types for 1. Behavior. In
There is no 2. A pattern that keeps the old "404 on miss" behavior. Since both return class NotFoundError extends Error {}
async function orThrow<T>(result: Promise<T | null>, message = "Record not found"): Promise<T> {
const row = await result;
if (row === null) throw new NotFoundError(message);
return row;
}
// Prisma 8, rc.28 naming (use updateFirst on newer builds)
const user = await orThrow(
db.orm.public.User.where({ id }).update(data),
);try {
await orThrow(db.orm.public.User.where({ id }).update(data));
} catch (e) {
if (e instanceof NotFoundError) return res.status(404).send();
throw e;
}If you only need to know whether anything matched, 3. Docs. I couldn't find this called out in the Prisma 7 to 8 migration notes, so a short note there (and a mention that the method names are still moving between the RCs) sounds worthwhile. A feature request for |
|
Yeah, this one is real, and it will bite you quietly on upgrade. In Prisma 7, update and delete throw P2025 when nothing matches, so a lot of apps lean on that catch to return a 404. In Prisma 8 the fluent API just returns null instead, so that catch never fires and the 404 silently stops happening. Your codemod is right to flag every rewritten update and delete, that is exactly where this breaks. For now the way to keep the old behavior is a small wrapper. Call update or delete, check the result, and throw when it is null: const row = await db.orm.public.User.where({ id }).update({ name });
if (!row) throw new Error('Record to update not found');Then map that to a 404 in your handler. If you want to keep your existing P2025 catch blocks working as is, throw a PrismaClientKnownRequestError with code P2025 instead of a plain error. As far as I can tell there is no built-in updateOrThrow or deleteOrThrow in the RC, so the wrapper is the way for now. Worth asking the team if they plan to add one, since firstOrThrow already exists. And yes, this should be in the migration docs. The Coming from Prisma ORM 7 table is the right place, because a lot of apps rely on P2025 for 404s and this change is easy to miss. A PR adding that note would help. |
|
For the codemod, I’d keep the null check on the result of the write itself. A separate existence check introduces a race if the record disappears between the read and the write. Also, injecting a synthetic A useful migration fixture would exercise three outcomes for the rewritten call: a returned row preserves the success response, a resolved |
|
@nemo-crypto @tydavid5 @devrafcks @aetheramizu v0.3.5 (from Mohamad's notes)
v0.3.6 (error handling)
Next, I'll open a feature request for If anyone has a Prisma 7 codebase handy, a dry run would really help ( Thanks again! |
|
Thanks a lot for the mention, @jeeva-m-21! Really glad the |
A few important findings from the
prisma/ormcodebase — including a breaking rename that just landed onmainright after8.0.0-rc.28(very relevant forprisma-v8-codemod!):1. Heads-up:
.update()and.delete()were just renamed onmainIn PR #31350 (commit
d546693, merged afterrc.28), single-row ORM mutations were renamed with no deprecated aliases:collection.update(...)→collection.updateFirst(...)collection.delete()→collection.deleteFirst()(The lower-level
db.sql.*.update()/.delete(),updateAll,updateAndCount,deleteAll,deleteAndCount, andupsertkeep their names.)2. Current behavior & recommended pattern for "not found"
P2025/ Prisma 7 error codes: ADR 305 (Prisma 8 …