Skip to content

Error with multiple upsert in $transaction #11038

Description

@yujonglee

Bug description

When I execute query, these two errors alternately appeared. One is ConnectorError and the other is Unique constraint error.

Second error is kind of strange, because I chose this approach(upsert with $transaction) to avoid race condition, but it seams like it is happened here. (I can verify that all names and userIds are unique.)

Maybe this is because some creation in $transaction is not broadcasted to others in $transaction, so creation can happen multiple time. (Now I remember I read something similar in docs)

Anyway, then what is the right way to do what I want? And what should I do for first error message?

Thank you.

Error: 
Invalid `prisma.tag.upsert()` invocation:


  Error occurred during query execution:
ConnectorError(ConnectorError { user_facing_error: None, kind: QueryError(Server(ServerError { code: 1213, message: "vttablet: rpc error: code = Aborted desc = (errno 1213) (sqlstate 40001) (CallerID: unsecure_grpc_client): Sql: \"insert into Tag(id, createdAt, updatedAt, `name`, userId) values (:v1, :v2, :v3, :v4, :v5)\", BindVars: {}", state: "40001" })) })
    at cb (/Users/yujonglee/dev/taggy.cloud/node_modules/@prisma/client/runtime/index.js:38692:17)
    at async Promise.all (index 0)
    at async createFile (/Users/yujonglee/dev/taggy.cloud/api/_build/index.js:1029:3)
    at async Promise.all (index 1)
    at async action3 (/Users/yujonglee/dev/taggy.cloud/api/_build/index.js:1202:3)
    at async Object.callRouteAction (/Users/yujonglee/dev/taggy.cloud/node_modules/@remix-run/server-runtime/data.js:36:14)
    at async handleDataRequest (/Users/yujonglee/dev/taggy.cloud/node_modules/@remix-run/server-runtime/server.js:110:18)
    at async requestHandler (/Users/yujonglee/dev/taggy.cloud/node_modules/@remix-run/server-runtime/server.js:45:20)
    at async /Users/yujonglee/dev/taggy.cloud/node_modules/@remix-run/express/server.js:45:22
Error: 
Invalid `prisma.tag.upsert()` invocation:


  Unique constraint failed on the (not available)
    at cb (/Users/yujonglee/dev/taggy.cloud/node_modules/@prisma/client/runtime/index.js:38688:17)
    at async Promise.all (index 0)
    at async createFile (/Users/yujonglee/dev/taggy.cloud/api/_build/index.js:1028:3)
    at async Promise.all (index 1)
    at async action3 (/Users/yujonglee/dev/taggy.cloud/api/_build/index.js:1201:3)
    at async Object.callRouteAction (/Users/yujonglee/dev/taggy.cloud/node_modules/@remix-run/server-runtime/data.js:36:14)
    at async handleDataRequest (/Users/yujonglee/dev/taggy.cloud/node_modules/@remix-run/server-runtime/server.js:110:18)
    at async requestHandler (/Users/yujonglee/dev/taggy.cloud/node_modules/@remix-run/server-runtime/server.js:45:20)
    at async /Users/yujonglee/dev/taggy.cloud/node_modules/@remix-run/express/server.js:45:22

How to reproduce

Just run query.

Expected behavior

For all strings in tagNames, create if not exist, in batch.
(tagNames is array of string)

Prisma information

  await prisma.$transaction(tagNames.map((name) => (
    prisma.tag.upsert({
      where: {
        userId_name: {
          userId,
          name,
        },
      },
      create: {
        userId,
        name,
      },
      update: {},
    })
  )));
model Tag {
  id        String   @id @default(uuid())
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  name String

  User   User   @relation(fields: [userId], references: [id], onDelete: Cascade)
  userId String

  @@unique(fields: [userId, name])
}

Environment & setup

  • OS: Mac OS
  • Database: PlanetScale
  • Node.js version: 16.6.1

Prisma Version

prisma                  : 3.7.0
@prisma/client          : 3.7.0
Current platform        : darwin
Query Engine (Node-API) : libquery-engine 8746e055198f517658c08a0c426c7eec87f5a85f (at node_modules/@prisma/engines/libquery_engine-darwin.dylib.node)
Migration Engine        : migration-engine-cli 8746e055198f517658c08a0c426c7eec87f5a85f (at node_modules/@prisma/engines/migration-engine-darwin)
Introspection Engine    : introspection-core 8746e055198f517658c08a0c426c7eec87f5a85f (at node_modules/@prisma/engines/introspection-engine-darwin)
Format Binary           : prisma-fmt 8746e055198f517658c08a0c426c7eec87f5a85f (at node_modules/@prisma/engines/prisma-fmt-darwin)
Default Engines Hash    : 8746e055198f517658c08a0c426c7eec87f5a85f
Studio                  : 0.445.0
Preview Features        : referentialIntegrity

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug/0-unknownBug is new, does not have information for reproduction or reproduction could not be confirmed.kind/bugA reported bug.topic: mysqltopic: upsertnested upsert

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions