Reputation: 16349
From TypeORM's documentation they describe Concrete table inheritance with the following example:
export abstract class Content {
@PrimaryGeneratedColumn()
id: number;
@Column()
title: string;
@Column()
description: string;
}
@Entity()
export class Photo extends Content {
@Column()
size: string;
}
@Entity()
export class Question extends Content {
@Column()
answersCount: number;
}
@Entity()
export class Post extends Content {
@Column()
viewCount: number;
}
My question is if you want to add a relation to the abstract class. SO ....
export abstract class Content {
@PrimaryGeneratedColumn()
id: number;
@Column()
title: string;
@Column()
description: string;
// WOULD LIKE TO ADD SOMETHING LIKE THIS ...
@ManyToOne(() => User, user => user.createdContent)
createdBy: User
}
@Entity()
export default class User {
// AND THIS ...
@OneToMany(() => Content, content => content.authoredBy)
authoredContent: Content[]
}
Is there a possible solution to make this possible. NOTE: You can achieve this with Single Table Inheritance
Upvotes: 4
Views: 1027
Reputation: 1645
This is most likely impossible by design, since in a SQL database, you can't have a foreign key that references multiple different tables at once (without some very non-standard shenanigans).
Single table inheritance is your best bet in my opinion, because all the variants are stored in a single table. You can also read through this thread for more reference: Foreign Key to multiple tables
Upvotes: 1