Skip to content

@@unique fails when used on polymorphic models with long names #1992

@diesal11

Description

@diesal11

Description and expected behavior
Given the following schema:

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

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

enum MyAppUserType {
    Local
    Google
    Microsoft
}

model MyAppCompany {
    id          String            @id @default(cuid())
    name        String
    users       MyAppUser[]

    userFolders MyAppUserFolder[]
}

model MyAppUser {
    id          String            @id @default(cuid())
    companyId   String
    type        MyAppUserType

    @@delegate(type)

    company     MyAppCompany      @relation(fields: [companyId], references: [id])
    userFolders MyAppUserFolder[]
}

model MyAppUserLocal extends MyAppUser {
    email    String
    password String
}

model MyAppUserGoogle extends MyAppUser {
    googleId String
}

model MyAppUserMicrosoft extends MyAppUser {
    microsoftId String
}

model MyAppUserFolder {
    id        String       @id @default(cuid())
    companyId String
    userId    String
    path      String
    name      String

    @@unique([companyId, userId, name])

    company   MyAppCompany @relation(fields: [companyId], references: [id])
    user      MyAppUser    @relation(fields: [userId], references: [id])
}

zenstack generate will generate an invalid logical schema and fail with the following error:

⌛️ ZenStack CLI v2.11.4, running plugins
✔ Generating Prisma schema
Prisma schema loaded from logical-1740022083351.prisma
Error: Prisma schema validation - (get-dmmf wasm)
Error code: P1012
error: Error parsing attribute "@@unique": The given constraint name `MyAppUserFolder_companyId_delegate_aux_MyAppUserFolder_user_key` has to be unique in the following namespace: global for primary key, indexes and unique constraints. Please provide a different name using the `map` argument.
  -->  logical-1740022083351.prisma:85
   | 
84 |     @@unique([companyId, userId, name])
85 |     @@unique([companyId, delegate_aux_MyAppUserFolder_userId_My_0, name])
   | 
error: Error parsing attribute "@@unique": The given constraint name `MyAppUserFolder_companyId_delegate_aux_MyAppUserFolder_user_key` has to be unique in the following namespace: global for primary key, indexes and unique constraints. Please provide a different name using the `map` argument.
  -->  logical-1740022083351.prisma:86
   | 
85 |     @@unique([companyId, delegate_aux_MyAppUserFolder_userId_My_0, name])
86 |     @@unique([companyId, delegate_aux_MyAppUserFolder_userId_My_1, name])
   | 
error: Error parsing attribute "@@unique": The given constraint name `MyAppUserFolder_companyId_delegate_aux_MyAppUserFolder_user_key` has to be unique in the following namespace: global for primary key, indexes and unique constraints. Please provide a different name using the `map` argument.
  -->  logical-1740022083351.prisma:87
   | 
86 |     @@unique([companyId, delegate_aux_MyAppUserFolder_userId_My_1, name])
87 |     @@unique([companyId, delegate_aux_MyAppUserFolder_userId_My_2, name])
   | 

Validation Error Count: 3
[Context: getDmmf]

Prisma CLI Version : 6.2.1
✖ Generating PrismaClient enhancer

Screenshots
If applicable, add screenshots to help explain your problem.

Environment (please complete the following information):

  • ZenStack version: 2.11.6
  • Prisma version: 6.3.1
  • Database type: Postgresql

Additional context

I may be wrong, but at first glance it appears the issue is in packages/schema/src/plugins/prisma/schema-generator.ts, the replicateForeignKeyModelLevelUnique method never calls PrismaSchemaGenerator.truncate to generate a valid constraint name for prisma.

https://github.com/zenstackhq/zenstack/blob/584d8afc1bdd90cad831237c889e3091499c9e67/packages/schema/src/plugins/prisma/schema-generator.ts#L517C1-L558C6

If I shorten the model names by removing the MyApp prefix from each the error does not occur, so I don't believe I'm doing something unsupported here.

I'm happy to investigate this issue further & develop a fix, but would appreciate any advice the team has.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions