Unique constraint error inside a transaction throws unparsed error (but works fine when using Node API)
See original GitHub issueBug description
While trying to auto-generate slugs for user-made pages at Copyfolio, we noticed that unique constraint violation errors aren’t caught properly inside a prisma.$transaction
.
How to reproduce
-
Have a model with a unique constraint, e.g. a
Site
with a globally unique URLslug
. -
Run some code which is meant to detect unique
slug
constraint violation errors – and then auto-generate a new slug to try with, but that isn’t part of the code below:
/* Fill our database with some initial data */
await prisma.site.create({ id: "1", slug: "taken-by-1" });
await prisma.site.create({ id: "2", slug: "another" });
try {
await prisma.$transaction([
prisma.site.updateMany({
where: { id: "2" },
data: { slug: "taken-by-1" },
}),
/* Any other site update is required for the test to fail */
prisma.site.update({
where: { id: "2" },
data: { id: "2" },
}),
]);
} catch (error) {
if (
error instanceof PrismaClientKnownRequestError &&
error.code === "P2002"
) {
console.debug("Unique constraint violation detection works");
} else {
console.error("Unknown error, as violation detection fails");
throw error;
}
}
It should throw an unknown error, which isn’t the expected behavior. However, then using previewFeatures = ["nApi"]
, the code above works as expected.
Expected behavior
No response
Prisma information
model Site {
id String @id @default(cuid())
slug String @unique
}
Environment & setup
- OS: mac OS v11.4
- Database: PostgreSQL 12/13
- Node.js version: v14.16.1
Prisma Version
prisma : 2.23.0
@prisma/client : 2.23.0
Current platform : darwin
Query Engine : query-engine adf5e8cba3daf12d456d911d72b6e9418681b28b (at node_modules/@prisma/engines/query-engine-darwin)
Migration Engine : migration-engine-cli adf5e8cba3daf12d456d911d72b6e9418681b28b (at node_modules/@prisma/engines/migration-engine-darwin)
Introspection Engine : introspection-core adf5e8cba3daf12d456d911d72b6e9418681b28b (at node_modules/@prisma/engines/introspection-engine-darwin)
Format Binary : prisma-fmt adf5e8cba3daf12d456d911d72b6e9418681b28b (at node_modules/@prisma/engines/prisma-fmt-darwin)
Default Engines Hash : adf5e8cba3daf12d456d911d72b6e9418681b28b
Studio : 0.393.0
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (6 by maintainers)
Top Results From Across the Web
Check for UNIQUE constraint in Node-Postgres - Stack Overflow
Error code of unique_violation is 23505. Additionally error object has constraint field that reports name of the constraint being violated.
Read more >Error and Transaction Handling in SQL Server Part Three
Strategies and Principles. In Part Two we looked at how SQL Server acts in case of an error, and we found that there...
Read more >How to handle unique constraint violations
Today, we'll discuss how to best handle unique constraint violations. ... in an exception that would lead to a 500 (internal server) error....
Read more >Handling exceptions and errors (Reference) - Prisma
This page covers how to handle exceptions and errors. ... This will throw an error because the email field has the @unique attribute...
Read more >API with NestJS #85. Defining constraints with raw SQL
The unique constraint ensures that all values in a particular column are unique across the table. A good example is the users table...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hey @kripod, I’m so happy to see you here! This is fixed in the upcoming https://github.com/prisma/prisma/pull/8384
Just to clarify because it took me a couple reads.