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

[Feature Request] Polymorphic Models: Query base model with concrete model's props #1740

Open
drichardcarl opened this issue Sep 25, 2024 · 0 comments

Comments

@drichardcarl
Copy link

drichardcarl commented Sep 25, 2024

Is your feature request related to a problem? Please describe.

Using this as an example (found on official docs):

model User {
  id Int @id @default(autoincrement())
  contents Content[]
}

model Content {
  id Int @id @default(autoincrement())
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
  published Boolean @default(false)
  owner User @relation(fields: [ownerId], references: [id])
  ownerId Int
  contentType String

  @@delegate(contentType)
}

model Post extends Content {
  title String
}

model Video extends Content {
  name String
  duration Int
}

How can I query Content based on Post's title or Video's name?
Documentation doesn't have an example for this and the generated types also prevent me from doing so.

Describe the solution you'd like

I think I can achieve this if I could have access to the relation. I know that under the hood, zenstack generates this prisma schema

model Content {
  ...
  delegate_aux_post Post?
  delegate_aux_video Video?
}

model Post extends Content {
  ...
  delegate_aux_content Content @relation(fields: [id], references: [id], onDelete: Cascade, onUpdate: Cascade)
}

model Video extends Content {
  ...
  delegate_aux_content Content @relation(fields: [id], references: [id], onDelete: Cascade, onUpdate: Cascade)
}

and does all mapping and processing under.
That's why I think, if the relation is available to query on Content I can easily achieve what I desire.

With this, if possible to have access to relation, I can just do

prisma.content.findMany({
  include: {
    delegate_aux_post:{ where: {...} }
    delegate_aux_video:{ where: {...} }
  }
})

to achieve what I want.

I can see that I can access delegate_aux_post and delegate_aux_video fields of Content using prisma client but not when using the enhanced client.

Describe alternatives you've considered

It would be great if Zenstack could support this feature someday. But for now, I guess I'll just manually manage my polymorphic model, since I badly needed this feature.

Additional context

Created from Discord thread: https://discord.com/channels/1035538056146595961/1267761052230221847

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

1 participant