I have few prisma models and each of these models have few common fields, for eg. audit fields like created_at, updated_at.
model User {
id Int @id @default(autoincrement())
created_at DateTime @default(now())
updated_at DateTime @updatedAt
email String @unique
username String @unique
password String?
firstName String?
lastName String?
isActive Boolean
deletedAt DateTime?
blogs Blog[]
profile Profile?
}
model Profile {
id Int @id @default(autoincrement())
created_at DateTime @default(now())
updated_at DateTime @updatedAt
bio String?
user User @relation(fields: [userId], references: [id])
userId Int @unique
}
model Blog {
id Int @id @default(autoincrement())
created_at DateTime @default(now())
updated_at DateTime @updatedAt
author User @relation(fields: [authorId], references: [id])
authorId Int
title String
content String
@@unique([authorId, title])
@@index([id, authorId])
}
I would like to define these all fields in one abstract base model and then extend all other models using that base model so that the repetitive code can be avoided and the risk of missing to add those fields in newer models will be lowered.
I tried to find the solution in prisma doc but I’m not able to find anything there. I can see some solutions online that are doing some hacks or using third party tools to achieve similar results but I would prefer something out of the box, if available.
For eg, In Django, we define base model and inherit other models from same. If anyone know how to achieve similar thing with prisma or if you are using some other way to manage massive number of models in production then that would be of great help.
Thanks.
2
Answers
Prisma ORM does not have the concept of "abstract models" in the same way Django ORM does. In Django, an abstract model serves as a base class for other models but does not produce a database table of its own. Instead, its fields are added to the child models that inherit from it.
In Prisma, you can’t define a model that is only used for inheritance purposes. However, there are certain approaches you can take to emulate abstract-like behavior:
Using Generators or Scripts:
You could set up a generator or a script to produce the final schema.prisma file by combining shared fields and model-specific fields. This is a more involved setup but can help maintain DRYness if you have a lot of shared fields.
Model Relations instead of Inheritance:
Consider if a relation between models might serve better than inheritance. For instance, if there’s a shared set of attributes across multiple models, those attributes could be refactored into their own model, and a relation could be established.
In this example, the Profile model holds fields common to both User and Admin.
In short: No there is such thing as abstract models of inheritance in general implemented in Prisma as of now.
However there is an open Github issue proposal which describes how interfaces could be used for that kind of abstraction.
https://github.com/prisma/prisma/issues/2506. Unfortunately there hasn’t been made any real progress on the issue since 2020.
Thus people started to implement it by themselves.
A custom library called ZenStack built on top of Prisma offers
abstract models
. You can define.zmodel
model files which are compiled to the normalprisma.schema
.An example from the ZenStack documentation: