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

Zenstack having problems with upserting relations with compound keys #1271

Closed
CollinKempkes opened this issue Apr 19, 2024 · 4 comments · Fixed by #1273
Closed

Zenstack having problems with upserting relations with compound keys #1271

CollinKempkes opened this issue Apr 19, 2024 · 4 comments · Fixed by #1273
Milestone

Comments

@CollinKempkes
Copy link

Description and expected behavior
The problem is that zenstack is having some kind of problem of upserting relations when the where part of the upsert is using a compound/ unique key. With an unenhanced client it works as expected. Also when using the normal PrimaryKey it also work as expected, just in this scenario using unique keys is broken.

To recreate this behaviour use this code:

import "../../../prisma/zmodel/auth/user.zmodel"

model Test {
  id String @id @default(dbgenerated("uuid_generate_v4()")) @db.Uuid
  linkingTable LinkingTable[]
  key String @default('test')
  locale String @default('EN')

  @@schema("public")
  @@map("test")

  @@unique([key, locale])
  @@allow("all", true)
}

model LinkingTable {
  test_id String @db.Uuid
  test Test @relation(fields: [test_id], references: [id])

  another_test_id String @db.Uuid
  another_test AnotherTest @relation(fields: [another_test_id], references: [id])

  @@id([test_id, another_test_id])
  @@map("__linking_table")
  @@schema("public")


  @@allow("all", true)
}

model AnotherTest {
  id String @id @default(dbgenerated("uuid_generate_v4()")) @db.Uuid
  status String
  linkingTable LinkingTable[]

  @@schema("public")
  @@map("another_test")

  @@allow("all", true)
}
import { PrismaClient } from '@equitybytes/nest-prisma';
import { enhance } from '@zenstackhq/runtime';

describe('test', () => {
  const enhancedPrisma = enhance(new PrismaClient());
  it('should be true', async () => {
    const enhancedPrisma = enhance(new PrismaClient());

    const test = await enhancedPrisma.test.create({
      data: {
        key: 'hello',
      },
    });
    const anotherTest = await enhancedPrisma.anotherTest.create({
      data: {
        status: 'available',
      },
    });

    await enhancedPrisma.test.upsert({
      where: {
        key_locale: {
          key: test.key,
          locale: test.locale,
        },
      },
      create: {
        linkingTable: {
          create: {
            another_test_id: anotherTest.id,
          },
        },
      },
      update: {
        linkingTable: {
          create: {
            another_test_id: anotherTest.id,
          },
        },
      },
    });
  });
});

The responding error message is:

Error calling enhanced Prisma method `upsert`: 
Invalid `prisma.test.findUniqueOrThrow()` invocation:

    {
      select: {
        id: true
      },
    + where: {
    +   id: String,
    +   key_locale: TestKeyLocaleCompoundUniqueInput,
    +   AND: TestWhereInput | TestWhereInput[],
    +   OR: TestWhereInput[],
    +   NOT: TestWhereInput | TestWhereInput[],
    +   key: StringFilter | String,
    +   locale: StringFilter | String,
    +   linkingTable: LinkingTableListRelationFilter
    + }
    }

    Argument `where` is missing.

Debug log also says:

[Nest] 53375  - 04/19/2024, 4:18:36 PM     LOG [Prisma] Duration: 1ms
  console.log
    prisma:info [policy] `findUniqueOrThrow` LinkingTable: looking up upstream entity of Test, { where: undefined, select: { id: true } }

      at Object.Ku (../../node_modules/@prisma/client/runtime/library.js:22:432)

[Nest] 53375  - 04/19/2024, 4:18:36 PM   DEBUG [Prisma] Message: [policy] `findUniqueOrThrow` LinkingTable: looking up upstream entity of Test, { where: undefined, select: { id: true } }
[Nest] 53375  - 04/19/2024, 4:18:36 PM   DEBUG [Prisma] Target: zenstack
[Nest] 53375  - 04/19/2024, 4:18:36 PM   DEBUG [Prisma] Timestamp: Fri Apr 19 2024 16:18:36 GMT+0200 (Central European Summer Time)

Environment:

  • ZenStack version: 1.12.3
  • Prisma version: 5.12.1
  • Database type: Postgresql
@ymc9
Copy link
Member

ymc9 commented Apr 20, 2024

Thanks @CollinKempkes . I'm able to reproduce it. Will make a fix soon.

@ymc9
Copy link
Member

ymc9 commented Apr 21, 2024

Hi @CollinKempkes , could you check if the new 1.12.4 release resolves the issue for you?

@ymc9 ymc9 closed this as completed Apr 30, 2024
@mattgstevens
Copy link

Hi @ymc9 , following up as Collin is afk for the next weeks.

This did fix the issue, thank you for the release 🌈

@ymc9
Copy link
Member

ymc9 commented Apr 30, 2024

Thank you for the confirmation!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants