-
Notifications
You must be signed in to change notification settings - Fork 285
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
Is it possible to implement class-table inheritance? #46
Comments
Hey @slavafomin, unfortunately this is not possible. But sounds very interesting and challenging. And I think this could be implemented in sequelize-typscript. Thank you too :) |
Thank you for getting back to me. The idea is pretty straightforward, here's the simplified example: -- Base Table
CREATE TABLE persons (
id serial PRIMARY KEY,
name text
);
-- Derived Table
CREATE TABLE students (
id integer PRIMARY KEY REFERENCES persons,
faculty text
);
-- Derived Table
CREATE TABLE professors (
id integer PRIMARY KEY REFERENCES persons,
degree text
); @TableInheritance({
type: 'class-table', // type of inheritance
discriminatorColumnName: 'type'
})
class Person {
name: string;
}
@DerivedTable()
class Student extends Person {
faculty: string;
}
@DerivedTable()
class Professor extends Person {
degree: string;
} // Returns both students and professors with given name:
Person.find({ name: 'John' }).then(persons => {
for (const person of persons) {
if (person instanceOf Student) {
// We've got student called John
}
if (person instanceOf Professor) {
// We've got professor called John
}
}
}); |
Hey @slavafomin, thanks for your suggestions. I investigated this a little bit deeper and came to the conclusion that an implementation for single table inheritance in sequelize-typscript wouldn’t be an issue. Multi table inheritance aka class table inheritance on the other side should be implemented in sequelize, not in sequelize-typescript. I did not find anything in the first place regarding multi table inheritance in sequelize. But here is a discussion about it: sequelize/sequelize#1243 One problem with multi table inheritance - which I can currently think of - is, that bulk creations would be very difficult. Since there are two tables for one entity (e.g. Student in your example), you won’t get the primary keys of the first table (Person in your example) of each entry in a bulk creation, which are necessary for the second table(Student). Or ones need to insert all base data separately, which would be not very performant. Regarding your examples: |
No, actually this makes sense. The example was primarily taken from TypeORM implementation, which we currently use in our project. |
Does TypeORM support bulk creations of models using multi table inheritance? |
The problem is that TypeORM has a lot of issues at the moment and inheritance is one of it's biggest problems for us, it's not reliable and considered experimental. That's why I've asked this question in the first place ) |
Any updates on this ? |
@kokominaj Not yet, sry. But any PRs are welcome :) |
Hello, We can do something like that? You have multiple interfaces with their own properties, but in the database they are stored in the same table.
|
Hello!
Thank you for your hard work, this module looks very promising.
However, do you mind if I ask? Maybe you are familiar with this problem. Is it possible to implement class-table inheritance with Sequelize and this module?
Thank you.
The text was updated successfully, but these errors were encountered: