-
-
Notifications
You must be signed in to change notification settings - Fork 106
[Feature Request] Add New Attributes @createdBy and @updatedBy #1505
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
Comments
|
This would be a nice addition! Perhaps this more generic approach would be a better fit: The idea would be to let Zenstack disambiguate the relations by generating the opposite fields. model User {
id Int @id @default(autoincrement())
// createdPosts Post[] @relation("createdPosts") -> no need to specify it in Zmodel, ZenStack will generate it in the schema.prisma file.
// updatedPosts Post[] @relation("updatedPosts")
}
model Post {
id Int @id @default(autoincrement())
createdBy User @relation("createdPosts", fields: [createdById], references: [id])
createdById Int @default(auth().id)
updatedBy User @relation("updatedPosts", fields: [updatedById], references: [id])
updatedById Int @default(auth().id)
}
abstract model Base {
id Int @id @default(autoincrement())
createdBy User @relation("created{model}s", fields: [createdById], references: [id]) // <- here
createdById Int @default(auth().id)
updatedBy User @relation("updated{model}s", fields: [updatedById], references: [id]) // <- here
updatedById Int @default(auth().id)
}
model Post extends Base {
id Int @id @default(autoincrement())
} The remaining question is how to define the type of relation: new field attributes like I saw that this issue was addressed through polymorphism (#653 (comment), #613 (comment)), but I would prefer not to be required to have an additional table for this purpose (or did I miss something?):
|
Hi @Azzerty23, thanks for the input! I just wanted to to add a bit more background about the relationship between polymorphism and "implicit opposite relation fields". The observation was that the need to declare opposite relation fields is especially cumbersome when you have some kind of "hub model" which links to many other models. model User {
createdPosts Post[]
updatedPosts Post[]
createdVideos Video[]
updatedVideos Video[]
...
} This also happens to be a good care where you want to model a polymorphic hierarchy: model User {
createdAsset Asset[]
updatedAsset Asset[]
...
}
model Asset {
...
type String
owner User @relation(...)
ownerId String
}
model Post extends Asset {
...
}
model Video extends Asset {
...
} Modeling a polymorphic hierarchy solves three problems in one shot:
I understand that not everyone wants the complexity of polymorphism just to solve #3. However, I think Prisma also had good reasons to require an explicit opposite relation. Maybe one of the biggest reasons is to make one-to-one, one-to-many distinctions very obvious. Another downside specific to ZenStack is that there'll be no way to access those implicit opposite relation fields in access policy rules because they're not declared. |
I appreciate your feedback @ymc9, always with so much pedagogy. I wasn't convinced at first, mainly because I didn't want to change my habits (yes, even in database design ^^), but the opportunity of #2 (having a "generic" way to query all assets) can prove useful. Thanks for the detailed explanation. |
At first, I wanna thank u for creating and working on a great library! I got a request about new attributes to manage spaces/memberships better. Also, it was mentioned in discord.
Is your feature request related to a problem? Please describe.
Introduce new attributes
@updatedBy
and@createdBy
similar to Prisma's@updtedAt
to avoid for creating manual relationships between schemasDescribe the solution you'd like
Describe alternatives you've considered
Additional context
The conversion in discord:
Mr. Zero5Um
The comment from Mr. @ymc9
The text was updated successfully, but these errors were encountered: