zenstackhq / zenstack

Fullstack TypeScript toolkit that enhances Prisma ORM with flexible Authorization layer for RBAC/ABAC/PBAC/ReBAC, offering auto-generated type-safe APIs and frontend hooks.
https://zenstack.dev
MIT License
2.07k stars 88 forks source link

BUG: Two enums with the same value causes error #498

Closed jasonmacdonald closed 1 year ago

jasonmacdonald commented 1 year ago

Description and expected behavior I'm unsure if this is just a visual-studio plugin bug or if it goes deeper (EDIT: I can confirm this blocks generation), but It seems you cannot use two enums in your schema, which both use the same name as one of its values. Whenever you try to reference the value of the second ENUM the error appears Value is not assignable to parameter zmodel.

Note that I get no such error in Prisma itself.

Simplified Reproduction

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

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

enum FirstEnum {
    E1 // used in both ENUMs
    E2
}

enum SecondEnum  {
    E1 // used in both ENUMs
    E3
    E4
}

model M {
    id Int @id
    first  SecondEnum @default(E1) // <-- error since two enums use the value and this is trying to reference the second one
    second FirstEnum @default(E1)
}

Environment (please complete the following information):

ymc9 commented 1 year ago

Hi @jasonmacdonald , thanks for reporting this, and I think I know why it's broken. The ZModel compiler hoists all enum fields to the global scope - apparently not the right thing to do ... but it shouldn't be hard to fix.

For now I think you can circumvent this issue using the @prisma.passthrough attribute to force passing the @default part to Prisma, like:

model User {
  id Int @id @default(autoincrement())
  email String @unique
  name String?
  first SecondEnum @prisma.passthrough('@default(E1)')  // <- here
  second FirstEnum @default(E1)
  posts Post[]
}
ymc9 commented 1 year ago

Fixed by #513