简体   繁体   中英

How to make One-to-Many relationship between two tables, but to store data in third table? TypeORM

I have an app, with two types of users, Teachers, and Students, both stored in one table. And the students can be part of a group, so they will be in many-to-one relation with Groups. These are my user and group entities:

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

  @ApiProperty()
  @Column()
  username: string;

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

  @ApiProperty()
  @Column()
  role: Role;

  @ManyToOne(() => Group, (group) => group.students, { nullable: true })
  group: Group;
}


@Entity('groups')
export class Group {
  @ApiProperty()
  @PrimaryGeneratedColumn()
  id: number;

  @ApiProperty()
  @Column()
  groupName: string;

  @ApiProperty()
  @Column()
  year: string;
  
  @ApiProperty({ type: () => [User] })
  @OneToMany(() => User, (user) => user.group)
  @JoinTable()
  students: User[];
}

The problem is that every user now has this group column, but teachers do not need it. Is it possible somehow to set the third table to store userId and groupId (as it is in the case of Many-To-Many relation), to keep the users' table clean of unnecessary columns? Thank you!

Why dont you use Many-To-Many relation?

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

  @ApiProperty()
  @Column()
  username: string;

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

  @ApiProperty()
  @Column()
  role: Role;
}

@Entity('groups')
export class Group {
  @ApiProperty()
  @PrimaryGeneratedColumn()
  id: number;

  @ApiProperty()
  @Column()
  groupName: string;

  @ApiProperty()
  @Column()
  year: string;
  
  @ApiProperty({ type: () => [User] })
  @ManyToMany(() => User)
  @JoinTable()
  students: User[];
}

This will produce following tables:

+-------------+--------------+----------------------------+
|                        users                            |
+-------------+--------------+----------------------------+
| id          | int(11)      | PRIMARY KEY AUTO_INCREMENT |
| username    | varchar(255) |                            |
| password    | varchar(25)  |                            |
| role        | enum         |                            |
+-------------+--------------+----------------------------+

+-------------+--------------+----------------------------+
|                        groups                           |
+-------------+--------------+----------------------------+
| id          | int(11)      | PRIMARY KEY AUTO_INCREMENT |
| groupName   | varchar(255) |                            |
| year        | varchar(4)   |                            |
+-------------+--------------+----------------------------+

+-------------+--------------+----------------------------+
|                  group_students_user                    |
+-------------+--------------+----------------------------+
| userId      | int(11)      | PRIMARY KEY FOREIGN KEY    |
| groupId     | int(11)      | PRIMARY KEY FOREIGN KEY    |
+-------------+--------------+----------------------------+

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM