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

Unique constraint error when connecting an item in a 1-1 relation that is already connected #16743

Closed
emmatown opened this issue Dec 12, 2022 · 1 comment · Fixed by prisma/prisma-engines#3514
Assignees
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/regression A reported bug in functionality that used to work before. team/client Issue for team Client. topic: constraint
Milestone

Comments

@emmatown
Copy link

emmatown commented Dec 12, 2022

Bug description

When connecting an item that is already connected in a 1-1 relation, the item is not disconnected from the previous item when connecting to a new item so a unique constraint error occurs. In versions prior to 4.7.0, the item was disconnected as expected.

How to reproduce

  1. Create a schema.prisma with the schema below.
  2. Run prisma db push
  3. Create an index.js with the code below.
  4. Run node index.js
  5. See the following output and error
prisma:query BEGIN
prisma:query INSERT INTO "public"."B" DEFAULT VALUES RETURNING "public"."B"."id"
prisma:query INSERT INTO "public"."A" ("bId") VALUES ($1) RETURNING "public"."A"."id"
prisma:query SELECT "public"."A"."id", "public"."A"."bId" FROM "public"."A" WHERE "public"."A"."id" = $1 LIMIT $2 OFFSET $3
prisma:query COMMIT
prisma:query BEGIN
prisma:query SELECT "public"."B"."id" FROM "public"."B" WHERE ("public"."B"."id" = $1 AND 1=1) OFFSET $2
prisma:query INSERT INTO "public"."A" ("bId") VALUES ($1) RETURNING "public"."A"."id"
prisma:query ROLLBACK
node_modules/.pnpm/@prisma+client@4.7.1_prisma@4.7.1/node_modules/@prisma/client/runtime/index.js:34869
      throw new PrismaClientKnownRequestError(message, {
            ^

PrismaClientKnownRequestError: 
Invalid `client.a.create()` invocation in
index.js:7:18

  4 
  5 (async () => {
  6   const item = await client.a.create({ data: { b: { create: {} } } });
→ 7   await client.a.create(
Unique constraint failed on the fields: (`bId`)
    at RequestHandler.handleRequestError (node_modules/.pnpm/@prisma+client@4.7.1_prisma@4.7.1/node_modules/@prisma/client/runtime/index.js:34869:13)
    at RequestHandler.handleAndLogRequestError (node_modules/.pnpm/@prisma+client@4.7.1_prisma@4.7.1/node_modules/@prisma/client/runtime/index.js:34841:12)
    at RequestHandler.request (node_modules/.pnpm/@prisma+client@4.7.1_prisma@4.7.1/node_modules/@prisma/client/runtime/index.js:34836:12)
    at async PrismaClient._request (node_modules/.pnpm/@prisma+client@4.7.1_prisma@4.7.1/node_modules/@prisma/client/runtime/index.js:35926:16)
    at async index.js:7:3 {
  code: 'P2002',
  clientVersion: '4.7.1',
  meta: { target: [ 'bId' ] },
  batchRequestIdx: undefined
}

Node.js v18.12.1

Expected behavior

The first item is disconnected before connecting to the new item like on versions prior to 4.7.0.
The output of the same code on Prisma 4.6.1:

prisma:query BEGIN
prisma:query INSERT INTO "public"."B" DEFAULT VALUES RETURNING "public"."B"."id"
prisma:query INSERT INTO "public"."A" ("bId") VALUES ($1) RETURNING "public"."A"."id"
prisma:query SELECT "public"."A"."id", "public"."A"."bId" FROM "public"."A" WHERE "public"."A"."id" = $1 LIMIT $2 OFFSET $3
prisma:query COMMIT
prisma:query BEGIN
prisma:query SELECT "public"."B"."id" FROM "public"."B" WHERE ("public"."B"."id" = $1 AND 1=1) OFFSET $2
prisma:query SELECT "public"."A"."id", "public"."A"."bId" FROM "public"."A" WHERE (1=1 AND "public"."A"."bId" IN ($1)) OFFSET $2
prisma:query UPDATE "public"."A" SET "bId" = $1 WHERE ("public"."A"."id" IN ($2) AND 1=1)
prisma:query INSERT INTO "public"."A" ("bId") VALUES ($1) RETURNING "public"."A"."id"
prisma:query SELECT "public"."A"."id", "public"."A"."bId" FROM "public"."A" WHERE "public"."A"."id" = $1 LIMIT $2 OFFSET $3
prisma:query COMMIT

Prisma information

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

model A {
  id  Int  @id @default(autoincrement())
  b   B?   @relation(fields: [bId], references: [id])
  bId Int? @unique
}

model B {
  id Int @id @default(autoincrement())
  a  A?
}
const { PrismaClient } = require("@prisma/client");

const client = new PrismaClient({ log: ["query"] });

(async () => {
  const item = await client.a.create({ data: { b: { create: {} } } });
  await client.a.create({ data: { b: { connect: { id: item.bId } } } });
})();

Environment & setup

  • OS: macOS 13.0.1
  • Database: PostgreSQL 14.6
  • Node.js version: 18.12.1

Prisma Version

prisma                  : 4.7.1
@prisma/client          : 4.7.1
Current platform        : darwin-arm64
Query Engine (Node-API) : libquery-engine 272861e07ab64f234d3ffc4094e32bd61775599c (at node_modules/.pnpm/@prisma+engines@4.7.1/node_modules/@prisma/engines/libquery_engine-darwin-arm64.dylib.node)
Migration Engine        : migration-engine-cli 272861e07ab64f234d3ffc4094e32bd61775599c (at node_modules/.pnpm/@prisma+engines@4.7.1/node_modules/@prisma/engines/migration-engine-darwin-arm64)
Introspection Engine    : introspection-core 272861e07ab64f234d3ffc4094e32bd61775599c (at node_modules/.pnpm/@prisma+engines@4.7.1/node_modules/@prisma/engines/introspection-engine-darwin-arm64)
Format Binary           : prisma-fmt 272861e07ab64f234d3ffc4094e32bd61775599c (at node_modules/.pnpm/@prisma+engines@4.7.1/node_modules/@prisma/engines/prisma-fmt-darwin-arm64)
Format Wasm             : @prisma/prisma-fmt-wasm 4.7.1-1.272861e07ab64f234d3ffc4094e32bd61775599c
Default Engines Hash    : 272861e07ab64f234d3ffc4094e32bd61775599c
Studio                  : 0.477.0
@emmatown emmatown added the kind/bug A reported bug. label Dec 12, 2022
@Druue Druue added bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. team/client Issue for team Client. kind/regression A reported bug in functionality that used to work before. topic: constraint labels Dec 12, 2022
@janpio janpio removed the kind/bug A reported bug. label Dec 12, 2022
@Weakky Weakky 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 Dec 15, 2022
@Weakky
Copy link
Member

Weakky commented Dec 15, 2022

Regression confirmed. Thanks for letting us know 🙏🏻

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/regression A reported bug in functionality that used to work before. team/client Issue for team Client. topic: constraint
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants