Skip to content

Generate throws when combining polymorphism and self-relations #1764

@platon-ivanov

Description

@platon-ivanov

Description and expected behavior

Using the example from Prisma docs. https://www.prisma.io/docs/orm/prisma-schema/data-model/relations/self-relations and add adding polymorphism like this:

model User {
  id          Int     @id @default(autoincrement())
  name        String?
  successorId Int?    @unique
  successor   User?   @relation("BlogOwnerHistory", fields: [successorId], references: [id])
  predecessor User?   @relation("BlogOwnerHistory")

  userType String
  @@delegate(userType)
}

model Organization extends User {
}

model Person extends User {
}

Throws the error:

$ npx zenstack generate

⌛️ ZenStack CLI v2.4.1, running plugins
✔ Generating Prisma schema
Prisma schema loaded from db/logical-1724338739613.prisma
Error: Prisma schema validation - (get-dmmf wasm)
Error code: P1012
error: Error validating model "User": Wrongly named relation detected. The fields `delegate_aux_User_successor_Organizati_0` and `delegate_aux_User_predecessor_Organiza_0` in model `User` both use the same relation name. Please provide different relation names for them through `@relation(<name>).
  -->  db/logical-1724338739613.prisma:25
   |
24 |     userType String
25 |     delegate_aux_User_successor_Organizati_0 Organization? @relation("BlogOwnerHistory", fields: [delegate_aux_User_successorId_Organiza_0], references: [id], map: "delegate_aux_User_successor_Organizati_0_fk")
26 |     delegate_aux_User_successorId_Organiza_0 Int? @unique(map: "delegate_aux_User_successorId_Organiza_1")
   |

Validation Error Count: 1
[Context: getDmmf]

Prisma CLI Version : 5.14.0
✖ Generating PrismaClient enhancer
Prisma Enhancer: Failed to run "prisma generate"

This example was taken from the according Discord discussion. In my use, the same error is thrown with one-to-many and many-to-many relations.

Environment (please complete the following information):

  • ZenStack version: 2.6.2
  • Prisma version: 5.20.0
  • Database type: Postgresql

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions