Repository navigation
Closed
1 / 11 of 1 issue completedLabels
area: modelsIssues related to Serverpod Models.enhancementNew feature or requestrfcRequest for comments
Description
A common feature request for Serverpod is to add support for inheritance in model files. It's easy to see how this can be a handy feature. The current approach is to support similar functionality through composition. But there are obvious pros of using inheritance instead.
- By using inheritance over composition, it would be possible to guarantee that fields are present instead of having nullable fields to compose different types of similar objects.
- We could support sealed classes. This would be great to ensure that developers handle all possible cases for a message with different compositions.
However, adding classes is a challenging task.
- To seal the classes, we will need to either place them in the same file or use the
part ofdirective. - Deserialization of inherited classes is tricky because we do not necessarily know the type of the class we are deserializing.
- How do we handle database tables?
Implementation
Several aspects need to be considered to implement inheritance in model files.
- Add an (optional)
extendsandsealedkeyword to the model files. The keyword. A base class can setsealedtotrue, in which case the class also becomes abstract. Any model in the package that extends the sealed class will be part of the seal. - All generated files need to be connected through
part ofto support the seal feature. - When serializing classes that use the
extendskeyword, the serializer will also save the class name (in the same way this happens in streams or when caching objects). - In the first version, we can make tables optional for extended models, but it should be possible to add a
tablename to the extended model and just add all columns in the object-relation mapping.
Examples
Setting up a sealed model:
class: MyModel
sealed: true
fields:
name: StringExtending a model:
class: MyExtendedModel
extends: MyModel
fields:
anotherName: String(Maybe) not in the first version, add a table to an extended model:
class: MyExtendedModelWithTable
extends: MyModel
table: my_extended_model_with_table
fields:
yetAnotherName: StringReacted by GJJ2019, Doug Todd, Nico, iheb meftah, galad, Fredrik, hientrung, Damian Bast, Daniel Montibeller, Rúben Ferreira and 8 more
Reacted by iheb meftah, Bin Emmanuel and Ben Auer
Sub-issues
Collapse Sub-issuesSub-issues
Loading 1 sub-issues
Metadata
Metadata
Assignees
Labels
area: modelsIssues related to Serverpod Models.enhancementNew feature or requestrfcRequest for comments
Type
Projects
- StatusShow more project fieldsDone 🚀

Activity
lukehutch commentedon Feb 22, 2024
+1 on this -- I can envision cases where a subclassed model should be able to be used to write to a parent model's table (where any fields added in the subclass are dropped, i.e. they become client/server-only fields, but are never persisted to the database), but also cases where a subclassed model should be able to be associated with its own table (whether or not it adds its own fields).
dkbast commentedon Mar 11, 2024
Having sealed classes would be super handy - currently migrating a small project to serverpod to see how it fits into my tech stack and since I was using sealed classes I landed here. For now I will go ahead with reusing my freezed class as a custom model which may be "good enough" for now :)
UPDATE: it appears to be impossible to mix freezed classes and those generated by Serverpod due to different fromJson signatures and json_serializable not being ok with that. (Can't even nest freezed classes when specifying the serverpod serializer) This a bit of a bummer and makes complex custom models way too hard to use. I decided to simply my data structure to make it conform with the generator, but having a different option would be great :)
Just saw this RFC again, and from what I remember "Hibernate" from the Java world did a pretty good job at this - https://www.baeldung.com/hibernate-inheritance for a good overview
BenAuerDev commentedon Aug 13, 2024
I'll pick this one up :)
lukehutch commentedon Aug 13, 2024
@BenAuerDev see also #1618... This issue is a dup of that. Note especially the last comment, tangentially related to inheritance (since a subclass model object should be able to be written into a superclass model's table): it would be nice if there was support for writing the same model into multiple tables, so that backups can be maintained (eg for law enforcement data retention purposes).
BenAuerDev commentedon Aug 14, 2024
Thanks @lukehutch I'll look into it 🚀 🚀
SandPod commentedon Aug 29, 2024
Related issue closed as a duplicate: #793
gazialankus commentedon Nov 29, 2024
Thank you for implementing inheritance.
Is there any way to extend classes from other modules?
It identifies UserInfo correctly here, writing UserInfoo gives another error.
Is there a way to extend UserInfo?
cc @BenAuerDev
BenAuerDev commentedon Nov 29, 2024
Hi @gazialankus!
Thanks for trying out the inheritance feature 🚀
Unfortunately, this is not possible. To support subclassing, parent classes are structured differently from standard classes. To avoid modifications to classes critical to the framework's functionality, we’ve put this restriction in place.
connordodge commentedon Jan 22, 2025
I'm working on a project right now where I'm exploring serverpod, and I want to use inheritance, mixins, and/or interfaces for my serverpod classes. Trying to figure out how to do that led me to this issue, and I'm glad to see it getting worked on. It looks like some progress was made, but I'm seeing that this was moved from Next to Later in the roadmap just a few hours ago. Is there a release coming up and this just isn't ready, or has this been put on hold for now?
EDIT - Nevermind, I see that there is no assignee to work on it. Thanks!
SandPod commentedon Jan 22, 2025
@connordodge - Glad to hear you are exploring Serverpod!
I did some cleanup in our roadmap to better clarify what is being worked on right now and what is next up. There is still quite a lot of work left before the inheritance is stable but we intend to finish that work during this spring.
A separate initiative will be interface support: #2842
connordodge commentedon Jan 22, 2025
Glad to hear it! Thank you.
SandPod commentedon Jun 24, 2025
Update: There are limitations in our protocol layer that prevent us from finalizing inheritance. The experimental feature can be used as is, but information is lost when serializing classes that utalize inheritance.
This is outlined in the documentation and we need a good way to solve this issue before inheritance can be stabilized: https://docs.serverpod.dev/concepts/experimental#inheritance
gazialankus commentedon Aug 28, 2025
The current state for database models and inheritance:
Just wanted to share this as a tip for others that may be considering inheritance and and database support for it. Initially I didn't understand #2799 well enough but after doing a trial and error it all made sense.
TheLastGimbus commentedon Sep 13, 2025
Hi 👋 I wanted to share what I'm doing to give some perspective:
I'm making a kayaking gear rental app. All gear has it's producer and name, and I want to put their all id's as foreign key in rentals table. But:
So, classic inheritance, right?
So I followed this very elegant pattern: https://stackoverflow.com/a/3579462 and just made separate table for extra type-specific data
And now, every function/widgets that deals with them looks like
Or, i could even skip the first
gearparameter, (since subtypes include it from join query) and blindly typeextra.gear.nameWell, i didn't sign up for high-oop statically typed language to use
dynamiceverywhere 😒😒So, I would ❤️love❤️ if I could have a dummy
GearExtrasealed class that would include all of them, and then could doBUT this is currently impossible if child-classes are also database tables 😔😔 - i don't know why - probably because that could imply that they should also have some common behavior with the db - like
GearExtra.db.findAll()- but that's not really needed (at least for me 😛) - i just need my Dart to behaveOR, in some distant universe, if we could have all of this oop-inheritance working with db full scale - that is, i could just make my sealed
Gearclass, then extend it in all gear subtypes, and serverpod's generator would do all of extra-table stuff automatically - I know this is a bit over-the-moon, I'm very happy with myGearExtrafor now 🙏🙏SandPod commentedon Sep 19, 2025
@TheLastGimbus Is
GearExtrastill restricted if it is not a table class but simply a sealed class without a table? I believe we should allow this if I remember the implementation correctly.TheLastGimbus commentedon Sep 24, 2025
@SandPod oh yesss, little update - i did manage to make
GearExtraa sealed class and other make other subtypesextendsit - but, it turns out that the top-levelGearExtradoesn't even generatefromJson- as said by @obiwanzenobi in #3173 (comment)So it's semi-useless, because i cannot use it in endpoints - i think that only part that's common inside
GearExtrais sent (no subtype data) - and since in my case there is nothing, i still need toand then on the client
marcelomendoncasoares commentedon Nov 17, 2025
With the merge of #4227, polymorphism is now officially supported in endpoints and will be available on Serverpod 3.0! Since this is the last missing piece holding the feature as experimental, we will also consider this RFC closed and deal with the evolution of the feature as natural enhancement.
@TheLastGimbus The lack of
fromJsonon sealed classes should no longer be an issue, since now we deserialize all classes using theProtocol().deserializemethod - that will forward the deserialization to the child class, ignoring the presence or absence offromJsonon the base class. If you can try your use case with the latest main, it would be great to have your feedback on the feature!