question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unique constraint error inside a transaction throws unparsed error (but works fine when using Node API)

See original GitHub issue

Bug 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

  1. Have a model with a unique constraint, e.g. a Site with a globally unique URL slug.

  2. 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:closed
  • Created 2 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
millspcommented, Jul 31, 2021

Hey @kripod, I’m so happy to see you here! This is fixed in the upcoming https://github.com/prisma/prisma/pull/8384

1reaction
matthewmuellercommented, Jun 28, 2021

Just to clarify because it took me a couple reads.

  • This isn’t a bug with NAPI, it’s a problem with our existing protocol
  • We should fix this because NAPI is not yet the default
  • Even when NAPI is the default, we’ll still keep the older protocol around
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found