Skip to content

[Bug] Constraint names are not unique when polymorphic model fields are used as part of a unique constraint #2001

Description

@Gabrola

On postgres, logical prisma schema generation fails when a base field is used as part of a composite unique constraint.

model User {
  id       Int       @id @default(autoincrement())

  contentUserLikes ContentUserLikes[]

  @@allow('all', true)
}

model Content {
  id          Int      @id @default(autoincrement())
  contentType String

  contentUserLikes ContentUserLikes[]

  @@delegate(contentType)

  @@allow('all', true)
}

model Post extends Content {
  title String
}

model Video extends Content {
  name     String
  duration Int
}

model ContentUserLikes {
    id          Int      @id @default(autoincrement())
    userId Int
    user   User @relation(fields: [userId], references: [id], onDelete: Cascade, onUpdate: Restrict)

    contentId Int
    content Content @relation(fields: [contentId], references: [id], onDelete: Cascade, onUpdate: Restrict)

    @@unique([userId, contentId])
}

This fails with the following error

- Generating PrismaClient enhancer
Error: Prisma schema validation - (get-dmmf wasm)
Error code: P1012
error: Error parsing attribute "@@unique": The given constraint name `ContentUserLikes_userId_delegate_aux_ContentUserLikes_conte_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-1740247636262.prisma:59
   | 
58 |     @@unique([userId, contentId])
59 |     @@unique([userId, delegate_aux_ContentUserLikes_contentI_0])
   | 
error: Error parsing attribute "@@unique": The given constraint name `ContentUserLikes_userId_delegate_aux_ContentUserLikes_conte_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-1740247636262.prisma:60
   | 
59 |     @@unique([userId, delegate_aux_ContentUserLikes_contentI_0])
60 |     @@unique([userId, delegate_aux_ContentUserLikes_contentI_1])
   | 

Validation Error Count: 2
[Context: getDmmf]

Prisma CLI Version : 6.3.1
✖ Generating PrismaClient enhancer
Prisma Enhancer: Failed to run "prisma generate" on logical schema: /tmp/tmp-3519865-AAekSeu95bZ9/logical-1740247636262.prisma

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions