I'm creating a user entity that would be joined with the profile entity, which can be Teacher or Student. This is my user entity

@Entity('users')
export class User {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ unique: true })
  email: string;

  @Exclude()
  @Column()
  password: string;

  @Column()
  role: Role;
}

And Teacher entity (Student looks quite similar):

    @Entity('teachers')
export class Teacher {
  @PrimaryGeneratedColumn()
  id: number;

  @OneToOne(() => User, { eager: true, cascade: true, onDelete: 'CASCADE' })
  @JoinColumn()
  user: User;

  @Column({nullable: true})
  @JoinTable()
  discipline: string;
}

As you can see, the Teacher entity is joined with the User entity, but I want also to join User entity with the Teacher or Student entity in order to be able to cascade delete the User and its profile. I tried to do something like this for the User entity, but it doesn't seem to work.

@OneToOne(() => Teacher | Student, {cascade: true, onDelete: 'CASCADE'})
profile: Teacher | Student;

My question is how can I specify that the join column Entity can be one of these two?

0

There are 0 best solutions below