Hi @cgarrotti 👋
At the moment, Prisma does not have support for model inheritance. However, we do have a feature request for it.
- #12604
You can add your use case to the feature request and leave a 👍 to help our engineers prioritize it.
If this answers your question, it would be great if you could mark this Discussion as answered to indicate that it has been resolved.
Otherwise please let us know how else we can help you further or close the Discussion if it was resolved in some other way 🙏
prism support inheritance in model ?
model with pre-defined sub-model
database - How to handle inheritnace with Prisma? - Stack Overflow
node.js - Is there is any support for Single Table Inheritance in Prisma? - Stack Overflow
I'm thinking about making this switch 'cause the amount of complexity and inheritance in this project is such that Prisma not being able to deal with it would do so much damage to efficiency and produtivity.
However before making this decision I would like to get feedback from the community about how TypeORM works with nest, and if any of you have any solution for me to keep using Prisma.
In short: No there is such thing as abstract models or 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 normal prisma.schema.
An example from the ZenStack documentation:
abstract model Basic { id String @id createdAt DateTime @default(now()) updatedAt DateTime @updatedAt } model User extends Basic { name String }The generated prisma file only contains one User model:
model User { id String @id createdAt DateTime @default(now()) updatedAt DateTime @updatedAt name String @id }
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.
model Profile {
id Int @id @default(autoincrement())
name String
email String @unique
user User? @relation(fields: [userId], references: [id])
userId Int?
admin Admin? @relation(fields: [adminId], references: [id])
adminId Int?
}
model User {
id Int @id @default(autoincrement())
profile Profile @relation(fields: [profileId], references: [id])
profileId Int
// ... other specific fields ...
}
model Admin {
id Int @id @default(autoincrement())
profile Profile @relation(fields: [profileId], references: [id])
profileId Int
// ... other specific fields ...
}
In this example, the Profile model holds fields common to both User and Admin.