Support inheritance for models
See original GitHub issueThis needs to be supported and tested:
class Base extends Model {}
Base.init({ whatever: DataTypes.STRING }, { tableName: 'base_table' })
class Derived extends Base {}
Derived.init({ second: DataTypes.INTEGER }, { tableName: 'derived_table' })
Derived.options.__proto__ === Base.options
Derived.attributes.__proto___ === Base.attributes
Derived inherits all attributes and options from Base.
Model.init()
should do something like
this.options = Object.assign(Object.create(this.options), options)
Issue Analytics
- State:
- Created 7 years ago
- Reactions:12
- Comments:9 (8 by maintainers)
Top Results From Across the Web
Inheritance Object Model Concept - Service Architecture
Inheritance in the object model is a means of defining one class in terms of another. This is common usage for most of...
Read more >Modeling Inheritance - Overview - Documentation
Modeling Inheritance - Overview. This article is relevant to entity models that utilize the deprecated Visual Studio integration of Telerik Data Access.
Read more >How to model inheritance in Prisma | Basedash
Relational databases don't support inheritance per-se, but with some effort we can arrange things with Prisma to give a pretty good ...
Read more >Modeling Inheritance - Apache Cayenne
Inheritance is a familiar and a very useful concept to any Java developer. There are three common ways of mapping it to a...
Read more >Inheritance
Inheritance is an object-oriented programming concept used to model an "is-a" relationship between two classes. It allows one class (the derived class or ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
All that needs to be done is default
tableName
to the model name. It’s really not an issue. We can checkoptions.tableName
before you seting up the new object with inheritance or usehasOwnProperty()
.I think you are misunderstanding the “Model” term in Sequelize. Sequelize uses the Active Record pattern. The Model is the class, instances are concrete rows. This is not a mistake, it’s a design choice. After all, classes in JavaScript are just objects themselves, so there is no reason to separate them like in v3 where there was an Instance class and a Model class. It didn’t have any benefit. The Model class is abstract, it could just as well be named
Entity
orRow
, it’s just naming. A different name doesn’t make inheritance messy or decorators not work.The use case is huge. Any application that stores data of legal importance needs to keep a change history. Comparing the number of these users to the number of users needing “abstract” models for inheritance doesn’t make sense because they don’t contradict each other. With the way I suggest it, you can easily support both use cases. With your way it only supports the abstract model use case.
What’s the problem with
abstract: true
?I don’t understand this paragraph, could you explain?
init
is a static constructor. It initialises the static side of the class. Models are classes and in JavaScript, classes are objects. Instantiating the concrete Model (e.g.User
) is done bynew User()
and gives you aUser
instance/row.5 years later…