Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zenstack does not let me define multiple fields that are referencing the same model #653

Closed
cankush625 opened this issue Aug 27, 2023 · 2 comments

Comments

@cankush625
Copy link

Description and expected behavior
I have a blog model that extends the AuditModel. Each blog has an author and the audit model has two fields which are created_by and updated_by. All these three fields reference to the User table, which is obvious that all three actors are users. When I try to generate a .prisma file using npx zenstack generate command, it gives a validation error that these fields refer to the same relation to model "User".

Model's in .zmodel file:

abstract model AuditModel {
    createdBy User @relation(fields: [createdById], references: [id])
    updatedBy User @relation(fields: [updatedById], references: [id])
    createdById Int
    updatedById Int
}

model Blog extends AuditModel {
  author User @relation(fields: [authorId], references: [id])
  authorId Int
  title String
  content String

  @@unique([authorId, title])
  @@index([id, authorId])
}

Expected result:
Multiple fields should be allowed to refer to the same model

Screenshots
image

Environment:

  • ZenStack version: 1.0.0-beta.20
  • Prisma version: 5.1.1
  • Database type: Postgresql
@jiashengguo
Copy link
Member

As Prisma requested, to disambiguate relations for the same model, you need to add the name argument in the @relation attribute to disambiguate them:
https://www.prisma.io/docs/concepts/components/prisma-schema/relations#disambiguating-relations

unfortunately, for now, it's not possible to do so in the abstract model because it can't reference the inherited fields.

We are thinking about two possible ways to resolve it:

  1. No need to write the reserved relation in ZModel, ZenStack will generate it automatically with some name conversion rules.
  2. Support polymorphism [Feature Request] Support for Polymorphic Associations #430 - the ability to model relations of multiple entity types like below:
model User {
    created Base[]
    updated Base[]
    deleted Base[]
    ...
}

@jiashengguo
Copy link
Member

fixed by #430

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants