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 89 forks source link

@@unique on polymorphic models #1758

Open SinPP opened 5 days ago

SinPP commented 5 days ago

Description and expected behavior I am trying to use polymorphic models. I have a base model and a model which extends the base model. I want to create a unique constraint on the extended model which includes a combination of the base model and the extend model. It fails with an error telling me that the field is not found on the extended model.

Screenshots image

Environment (please complete the following information):

Additional context Error: Error validating model "Store": The unique index definition refers to the unknown fields: organizationId.

model Content {
    contentType String

    organization   Organization @relation("OrganizationContents", fields: [organizationId], references: [id])
    organizationId String       @db.Uuid

    @@delegate(contentType)
}
model Store extends Content {
    name      String

    @@unique([organizationId, name])
}
ymc9 commented 2 days ago

Hi @SinPP , thanks for filing this.

Unfortunately using fields from base model in unique constraint is not possible, because at Prisma level, the base and concrete models are distinct tables connected with a relation.

We can improve the checker to report an error for such usage.