Skip to content

Violating constraint when updating relation with composite key #8858

Open
prisma/prisma-engines
#3418

Description

@some-user123

Bug description

I'm having a model that references itself with a composite key including a clientId. One field of the reference is optional, the other is not (cf. #6042).

When updating the reference, it seems, prisma is trying to set both fields to null, which violates the constraint:

PrismaClientKnownRequestError3 [PrismaClientKnownRequestError]:
Invalid `prisma.object.update()` invocation:


  Null constraint violation on the fields: (`clientId`)

How to reproduce

  1. Use prisma.schema below
  2. Create first object: o1 = await prisma.object.create({ data: {clientId: 1}})
  3. Create second object referencing the first one: o2 = await prisma.object.create({ data: { clientId: 1, otherUuid: o1.uuid } })
  4. Add reference to first object as well: prisma.object.update({ data: { otherUuid: o2.uuid }, where: { clientId_uuid: { clientId: 1, uuid: o1.uuid } }, })
  5. Try to update the first object:
prisma.object.update({
    data: {
      otherObject: {
        connect: { clientId_uuid: { clientId: 1, uuid: o2.uuid } },
      },
    },
    where: { clientId_uuid: { clientId: 1, uuid: o1.uuid } },
  })

Expected behavior

The update should update the field otherUuid (in this case unchanged), bot not try to set clientId to null.

Prisma information

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

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

model object {
  clientId  Int     @db.Integer
  uuid      String  @default(uuid()) @db.Uuid
  otherUuid String? @db.Uuid

  otherObject        object? @relation(fields: [clientId, otherUuid], references: [clientId, uuid])
  otherObjectReverse object? @relation("objectToobject")

  @@id([clientId, uuid])
}

Environment & setup

  • OS: Windows
  • Database: PostgreSQL
  • Node.js version: 16.6.2

Prisma Version

Environment variables loaded from .env
prisma               : 2.29.1
@prisma/client       : 2.29.1
Current platform     : windows
Query Engine         : query-engine 1be4cd60b89afa04b192acb1ef47758a39810f3a (at node_modules\@prisma\engines\query-engine-windows.exe)
Migration Engine     : migration-engine-cli 1be4cd60b89afa04b192acb1ef47758a39810f3a (at node_modules\@prisma\engines\migration-engine-windows.exe)
Introspection Engine : introspection-core 1be4cd60b89afa04b192acb1ef47758a39810f3a (at node_modules\@prisma\engines\introspection-engine-windows.exe)
Format Binary        : prisma-fmt 1be4cd60b89afa04b192acb1ef47758a39810f3a (at node_modules\@prisma\engines\prisma-fmt-windows.exe)
Default Engines Hash : 1be4cd60b89afa04b192acb1ef47758a39810f3a
Studio               : 0.419.0

Activity

  1. some-user123 commented on Sep 8, 2021

    @some-user123
    Author

    Can be reproduced with 3.0.1 as well.

  2. pantharshit00 commented on Sep 9, 2021

    @pantharshit00
    Contributor

    Thanks for the details, I can reproduce this issue.

    Posted a minimal reproduce here for the team which they can run easily: https://github.com/harshit-test-org/prisma-issue-8858

  3. removed their assignment
    on Sep 9, 2021
  4. some-user123 commented on Nov 2, 2021

    @some-user123
    Author

    Is there an ETA for a fix? Is there anything I could do to support analysis/debugging?

  5. added
    tech/engines
    on Nov 29, 2021
  6. self-assigned this
    on Jan 20, 2022
  7. self-assigned this
    on Sep 5, 2022
  8. pimeys commented on Sep 5, 2022

    @pimeys

    This is still happening with 4.3.2. Needed to do some modification so it works with the latest version.

    schema.prisma:

    generator client {
      provider = "prisma-client-js"
    }
    
    datasource db {
      provider = "postgresql"
      url      = env("DATABASE_URL")
    }
    
    model object {
      clientId  Int     @db.Integer
      uuid      String  @default(uuid()) @db.Uuid
      otherUuid String? @db.Uuid
    
      otherObject        object? @relation("objectToobject", fields: [clientId, otherUuid], references: [clientId, uuid])
      otherObjectReverse object? @relation("objectToobject")
    
      @@id([clientId, uuid])
      @@unique([clientId, otherUuid])
    }

    script.ts:

    import { PrismaClient } from '@prisma/client'
    
    const prisma = new PrismaClient({ log: ['query', 'info', 'warn', 'error'] })
    const date = new Date(new Date().setMilliseconds(0))
    
    async function main() {
      await prisma.object.deleteMany()
    
      const o1 = await prisma.object.create({
        data: {
          clientId: 1,
          uuid: "b277f093-aace-4e7f-9d1c-eb656550be97",
        },
      })
    
      const o2 = await prisma.object.create({
        data: {
          clientId: 1,
          uuid: "56db69ee-ce71-415b-9cd0-adca467fd1de",
          otherUuid: "b277f093-aace-4e7f-9d1c-eb656550be97",
        },
      })
    
      await prisma.object.update({
        where: {
          clientId_uuid: {
            clientId: 1,
            uuid: "b277f093-aace-4e7f-9d1c-eb656550be97",
          },
        },
        data: {
          otherUuid: "56db69ee-ce71-415b-9cd0-adca467fd1de",
        },
      })
    
      await prisma.object.update({
        where: {
          clientId_uuid: {
            clientId: 1,
            uuid: "b277f093-aace-4e7f-9d1c-eb656550be97",
          },
        },
        data: {
          otherObject: {
            connect: {
              clientId_uuid: {
                clientId: 1,
                uuid: "56db69ee-ce71-415b-9cd0-adca467fd1de",
              },
            },
          },
        },
      })
    }
    
    main()
      .then(async () => {
        await prisma.$disconnect()
      })
      .catch(async (e) => {
        console.error(e)
        await prisma.$disconnect()
        process.exit(1)
      })

    The debug log:

      prisma:tryLoadEnv  Environment variables loaded from /prisma-8858/.env +0ms
      prisma:tryLoadEnv  Environment variables loaded from /prisma-8858/.env +2ms
      prisma:client  dirname /prisma-8858/node_modules/.prisma/client +0ms
      prisma:client  relativePath ../../../prisma +0ms
      prisma:client  cwd /prisma-8858/prisma +0ms
      prisma:client  clientVersion 4.3.1 +1ms
      prisma:client  clientEngineType library +0ms
      prisma:client:libraryEngine  internalSetup +0ms
      prisma:client:libraryEngine:loader  Searching for Query Engine Library in /prisma-8858/node_modules/.prisma/client +0ms
      prisma:client:libraryEngine:loader  loadEngine using /prisma-8858/node_modules/.prisma/client/libquery_engine-debian-openssl-3.0.x.so.node +0ms
      prisma:client  Prisma Client call: +40ms
      prisma:client  prisma.object.deleteMany(undefined) +1ms
      prisma:client  Generated request: +0ms
      prisma:client  mutation {
      deleteManyobject {
        count
      }
    }
     +0ms
      prisma:client:libraryEngine  sending request, this.libraryStarted: false +41ms
      prisma:client:libraryEngine  library starting +0ms
    prisma:info Starting a postgresql pool with 33 connections.
      prisma:client:libraryEngine  library started +18ms
    prisma:query BEGIN
    prisma:query SELECT "public"."object"."clientId", "public"."object"."uuid" FROM "public"."object" WHERE 1=1 OFFSET $1 /* traceparent=00-00-00-00 */
    prisma:query SELECT "public"."object"."clientId", "public"."object"."uuid" FROM "public"."object" WHERE 1=1 /* traceparent=00-00-00-00 */
    prisma:query DELETE FROM "public"."object" WHERE ("public"."object"."clientId","public"."object"."uuid") IN (($1,$2),($3,$4)) /* traceparent=00-00-00-00 */
    prisma:query COMMIT
      prisma:client  Prisma Client call: +25ms
      prisma:client  prisma.object.create({
      data: {
        clientId: 1,
        uuid: 'b277f093-aace-4e7f-9d1c-eb656550be97'
      }
    }) +0ms
      prisma:client  Generated request: +0ms
      prisma:client  mutation {
      createOneobject(data: {
        clientId: 1
        uuid: "b277f093-aace-4e7f-9d1c-eb656550be97"
      }) {
        clientId
        uuid
        otherUuid
      }
    }
     +0ms
      prisma:client:libraryEngine  sending request, this.libraryStarted: true +7ms
    prisma:query BEGIN
    prisma:query INSERT INTO "public"."object" ("clientId","uuid") VALUES ($1,$2) RETURNING "public"."object"."clientId", "public"."object"."uuid" /* traceparent=00-00-00-00 */
    prisma:query SELECT "public"."object"."clientId", "public"."object"."uuid", "public"."object"."otherUuid" FROM "public"."object" WHERE ("public"."object"."clientId" = $1 AND "public"."object"."uuid" = $2) LIMIT $3 OFFSET $4 /* traceparent=00-00-00-00 */
    prisma:query COMMIT
      prisma:client  Prisma Client call: +4ms
      prisma:client  prisma.object.create({
      data: {
        clientId: 1,
        uuid: '56db69ee-ce71-415b-9cd0-adca467fd1de',
        otherUuid: 'b277f093-aace-4e7f-9d1c-eb656550be97'
      }
    }) +0ms
      prisma:client  Generated request: +0ms
      prisma:client  mutation {
      createOneobject(data: {
        clientId: 1
        uuid: "56db69ee-ce71-415b-9cd0-adca467fd1de"
        otherUuid: "b277f093-aace-4e7f-9d1c-eb656550be97"
      }) {
        clientId
        uuid
        otherUuid
      }
    }
     +0ms
      prisma:client:libraryEngine  sending request, this.libraryStarted: true +4ms
    prisma:query BEGIN
    prisma:query INSERT INTO "public"."object" ("clientId","uuid","otherUuid") VALUES ($1,$2,$3) RETURNING "public"."object"."clientId", "public"."object"."uuid" /* traceparent=00-00-00-00 */
    prisma:query SELECT "public"."object"."clientId", "public"."object"."uuid", "public"."object"."otherUuid" FROM "public"."object" WHERE ("public"."object"."clientId" = $1 AND "public"."object"."uuid" = $2) LIMIT $3 OFFSET $4 /* traceparent=00-00-00-00 */
    prisma:query COMMIT
      prisma:client  Prisma Client call: +4ms
      prisma:client  prisma.object.update({
      where: {
        clientId_uuid: {
          clientId: 1,
          uuid: 'b277f093-aace-4e7f-9d1c-eb656550be97'
        }
      },
      data: {
        otherUuid: '56db69ee-ce71-415b-9cd0-adca467fd1de'
      }
    }) +0ms
      prisma:client  Generated request: +0ms
      prisma:client  mutation {
      updateOneobject(
        where: {
          clientId_uuid: {
            clientId: 1
            uuid: "b277f093-aace-4e7f-9d1c-eb656550be97"
          }
        }
        data: {
          otherUuid: "56db69ee-ce71-415b-9cd0-adca467fd1de"
        }
      ) {
        clientId
        uuid
        otherUuid
      }
    }
     +0ms
      prisma:client:libraryEngine  sending request, this.libraryStarted: true +4ms
    prisma:query BEGIN
    prisma:query SELECT "public"."object"."clientId", "public"."object"."uuid" FROM "public"."object" WHERE ("public"."object"."clientId" = $1 AND "public"."object"."uuid" = $2) /* traceparent=00-00-00-00 */
    prisma:query UPDATE "public"."object" SET "otherUuid" = $1 WHERE ("public"."object"."clientId","public"."object"."uuid") IN (($2,$3)) /* traceparent=00-00-00-00 */
    prisma:query SELECT "public"."object"."clientId", "public"."object"."uuid", "public"."object"."otherUuid" FROM "public"."object" WHERE ("public"."object"."clientId" = $1 AND "public"."object"."uuid" = $2) LIMIT $3 OFFSET $4 /* traceparent=00-00-00-00 */
    prisma:query COMMIT
      prisma:client  Prisma Client call: +4ms
      prisma:client  prisma.object.update({
      where: {
        clientId_uuid: {
          clientId: 1,
          uuid: 'b277f093-aace-4e7f-9d1c-eb656550be97'
        }
      },
      data: {
        otherObject: {
          connect: {
            clientId_uuid: {
              clientId: 1,
              uuid: '56db69ee-ce71-415b-9cd0-adca467fd1de'
            }
          }
        }
      }
    }) +1ms
      prisma:client  Generated request: +0ms
      prisma:client  mutation {
      updateOneobject(
        where: {
          clientId_uuid: {
            clientId: 1
            uuid: "b277f093-aace-4e7f-9d1c-eb656550be97"
          }
        }
        data: {
          otherObject: {
            connect: {
              clientId_uuid: {
                clientId: 1
                uuid: "56db69ee-ce71-415b-9cd0-adca467fd1de"
              }
            }
          }
        }
      ) {
        clientId
        uuid
        otherUuid
      }
    }
     +0ms
      prisma:client:libraryEngine  sending request, this.libraryStarted: true +5ms
    prisma:query BEGIN
    prisma:query SELECT "public"."object"."clientId", "public"."object"."uuid" FROM "public"."object" WHERE ("public"."object"."clientId" = $1 AND "public"."object"."uuid" = $2) OFFSET $3 /* traceparent=00-00-00-00 */
    prisma:query SELECT "public"."object"."clientId", "public"."object"."uuid", "public"."object"."otherUuid" FROM "public"."object" WHERE (1=1 AND ("public"."object"."clientId","public"."object"."otherUuid") IN (($1,$2))) OFFSET $3 /* traceparent=00-00-00-00 */
    prisma:query UPDATE "public"."object" SET "clientId" = $1, "otherUuid" = $2 WHERE ("public"."object"."clientId","public"."object"."uuid") IN (($3,$4)) /* traceparent=00-00-00-00 */
    prisma:query ROLLBACK
      prisma:client:request_handler  PrismaClientKnownRequestError: Null constraint violation on the fields: (`clientId`)
        at prismaGraphQLToJSError (/prisma-8858/node_modules/@prisma/client/runtime/index.js:20426:12)
        at LibraryEngine.buildQueryError (/prisma-8858/node_modules/@prisma/client/runtime/index.js:26363:12)
        at LibraryEngine.request (/prisma-8858/node_modules/@prisma/client/runtime/index.js:26297:22)
        at async RequestHandler.request (/prisma-8858/node_modules/@prisma/client/runtime/index.js:29873:24)
        at async PrismaClient._request (/prisma-8858/node_modules/@prisma/client/runtime/index.js:30864:16) {
      code: 'P2011',
      clientVersion: '4.3.1',
      meta: { constraint: [ 'clientId' ] }
    } +0ms
    PrismaClientKnownRequestError: 
    Invalid `prisma.object.update()` invocation in
    /prisma-8858/script.ts:36:23
    
      33   },
      34 })
      35 
    → 36 await prisma.object.update(
    Null constraint violation on the fields: (`clientId`)
        at RequestHandler.handleRequestError (/prisma-8858/node_modules/@prisma/client/runtime/index.js:29909:13)
        at RequestHandler.request (/prisma-8858/node_modules/@prisma/client/runtime/index.js:29892:12)
        at async PrismaClient._request (/prisma-8858/node_modules/@prisma/client/runtime/index.js:30864:16) {
      code: 'P2011',
      clientVersion: '4.3.1',
      meta: { constraint: [ 'clientId' ] }
    }
      prisma:client:libraryEngine  library stopping +16ms
      prisma:client:libraryEngine  library stopped +0ms
      prisma:client:libraryEngine:exitHooks  exit event received: exit +0ms
    
  9. removed their assignment
    on Sep 5, 2022
  10. self-assigned this
    on Nov 18, 2022
  11. added this to the 4.7.0 milestone on Nov 22, 2022
  12. janpio commented on Dec 1, 2022

    @janpio
    Contributor

    (PR that closed this was reverted)

  13. mcbethio commented on Feb 4, 2023

    @mcbethio

    @Weakky is the milestone correct for this issue? I think it should be 4.10.0 right?

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/bugtech/enginestopic: composite ids

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions