Skip to content

No PrismaClientKnownRequestError when unique constraint fails in transaction #7786

Closed

Description

@remioo

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

Activity

  1. added
    kind/bug
    on Jun 21, 2021
  2. added
    bug/1-unconfirmed
    on Jun 21, 2021
  3. added
    bug/2-confirmed
    and removed
    bug/1-unconfirmed
    on Jul 29, 2021
  4. pantharshit00 commented on Jul 29, 2021

    @pantharshit00
    Contributor

    I can reproduce with 2.29.0-dev.5

  5. added this to the 2.29.0 milestone on Aug 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug/2-confirmedkind/bugtopic: errortopic: transaction

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions