Description
Background
This is probably one of the Prisma feature requests that got the most likes 😄. The name "polymorphic association" may sound a bit "obscure". Other terms can be union support, generic foreign key, abstract relation, etc.
It's an extremely useful feature for many applications, and from ZenStack (access control) point of view, it's even more useful, as it'll allow you to more easily consolidate access policies at an abstract level while still being able to make refinements at concrete levels. Here's an example for modeling a CMS (note ZenStack already has the abstract
syntax today):
model User {
id Int
contents Content[]
}
abstract model Content {
id Int
owner User @relation(fields: [ownerId], references: [id])
ownerId Int
deleted Boolean @default(false)
@@deny('read', deleted)
@@allow('all', auth().role == 'ADMIN')
}
model Post extends Content {
published Boolean
@@deny('delete', published)
}
model Video extends Content {
...
}
Proposed Solution
To be honest, we haven't thought deeply about the design and implementation, but I think the general approach would be to provide a concise syntax to model it in ZModel and transpile such relations into Prisma Schema using a [relationId, relationType]
tuple. The downside is we lose native fk constraint support for integrity and need to compensate for it using transactions.
It's possible that addressing it from ZenStack side is easier than adding such support in Prisma. But there are still many details to consider and feedback to collect, and it'll be quite some work.
Related: #613