Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No PrismaClientKnownRequestError when unique constraint fails in transaction #7786

Closed
Tracked by #7956
remioo opened this issue Jun 21, 2021 · 1 comment · Fixed by #8384
Closed
Tracked by #7956

No PrismaClientKnownRequestError when unique constraint fails in transaction #7786

remioo opened this issue Jun 21, 2021 · 1 comment · Fixed by #8384
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/client Issue for team Client. topic: error topic: transaction
Milestone

Comments

@remioo
Copy link

remioo commented Jun 21, 2021

Bug description

If you attempt to insert a row while in a transaction and a unique constraint fails, the error sent is:

ConnectorError(ConnectorError { user_facing_error: Some(KnownError { message: "Unique constraint failed on the fields: (`user_id`)", meta: Object({"target": Array([String("user_id")])}), error_code: "P2002" }), kind: UniqueConstrai
ntViolation { constraint: Fields(["user_id"]) } })

If not in a transaction, error is a PrismaClientKnownRequestError with code P2002

How to reproduce

Without transaction

 try {
    const user = await dboClient.user.create({data: {account_id: '1fe4ed1d-afe7-482c-be6b-430179776c4a', user_id: '9a535076-59f2-4145-9232-7302d82313d0'}});
    console.log(user);
  } catch (e) {
    console.log(e.toString() + '\n' + JSON.stringify(e));
  }

Error:

Invalid `prisma.user.create()` invocation:
  Unique constraint failed on the fields: (`user_id`)
{"code":"P2002","clientVersion":"2.23.0","meta":{"target":["user_id"]}}

With transaction

  try {
    const [,user] = await dboClient.$transaction([
      dboClient.user.findUnique({where: {user_id: '9a535076-59f2-4145-9232-7302d82313d0'}}),
      dboClient.user.create({data: {account_id: '1fe4ed1d-afe7-482c-be6b-430179776c4a', user_id: '9a535076-59f2-4145-9232-7302d82313d0'}})
    ]);
    console.log(user);
  } catch (e) {
    console.log(e.toString() + '\n' + JSON.stringify(e));
  }

Error:

Error: Error occurred during query execution:
ConnectorError(ConnectorError { user_facing_error: Some(KnownError { message: "Unique constraint failed on the fields: (`user_id`)", meta: Object({"target": Array([String("user_id")])}), error_code: "P2002" }), kind: UniqueConstrai
ntViolation { constraint: Fields(["user_id"]) } })

Expected behavior

Even in the transaction, Error should be a PrismaClientKnownRequestError with a code P2002

Prisma information

model User {
  user_id    String       @id @db.Uuid @default(dbgenerated("gen_random_uuid()"))
}

Environment & setup

  • OS: Windows
  • Database: PostgreSQL
  • Node.js version: v14.16.0

Prisma Version

prisma               : 2.25.0
@prisma/client       : Not found
Current platform     : windows
Query Engine         : query-engine c838e79f39885bc8e1611849b1eb28b5bb5bc922 (at node_modules\@prisma\engines\query-engine-windows.exe)
Migration Engine     : migration-engine-cli c838e79f39885bc8e1611849b1eb28b5bb5bc922 (at node_modules\@prisma\engines\migration-engine-windows.exe)
Introspection Engine : introspection-core c838e79f39885bc8e1611849b1eb28b5bb5bc922 (at node_modules\@prisma\engines\introspection-engine-windows.exe)
Format Binary        : prisma-fmt c838e79f39885bc8e1611849b1eb28b5bb5bc922 (at node_modules\prisma\node_modules\@prisma\engines\prisma-fmt-windows.exe)
Default Engines Hash : c838e79f39885bc8e1611849b1eb28b5bb5bc922
Studio               : 0.402.0

@remioo remioo added the kind/bug A reported bug. label Jun 21, 2021
@janpio janpio added bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. topic: transaction topic: error team/client Issue for team Client. process/candidate labels Jun 21, 2021
@pantharshit00 pantharshit00 added bug/2-confirmed Bug has been reproduced and confirmed. and removed bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. labels Jul 29, 2021
@pantharshit00
Copy link
Contributor

I can reproduce with 2.29.0-dev.5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/client Issue for team Client. topic: error topic: transaction
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants