[Feature Request] Polymorphic Models: Query base model with concrete model's props
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