多对多关系自定义表字段

时间:2020-05-11 21:26:45

标签: mysql node.js typescript typeorm

我正在使用一个已经存在的数据库上的typeorm。 我有三张桌子

+-------------+--------------+----------------------------+
|                        ROLE                             |
+-------------+--------------+----------------------------+
| r_id        | int(11)      | PRIMARY KEY AUTO_INCREMENT |
| r_name      | varchar(50)  |                            |
| r_status    | tinyint      |                            |
+-------------+--------------+----------------------------+
+-------------+--------------+----------------------------+
|                        PERMISSION                       |
+-------------+--------------+----------------------------+
| p_id        | int(11)      | PRIMARY KEY AUTO_INCREMENT |
| p_name      | varchar(50) |                            |
+-------------+--------------+----------------------------+
+-------------+--------------+----------------------------+
|                       PERMISSION_ROLE                   |
+-------------+--------------+----------------------------+
| p_id        | int(11)      | PRIMARY KEY FOREIGN KEY    |
| r_id        | int(11)      | PRIMARY KEY FOREIGN KEY    |
+-------------+--------------+----------------------------+

我在typeorm中写了这些实体 -角色

@Entity({ name: "ROLE" })
export class Role {
  @PrimaryGeneratedColumn()
  public r_id: Number;
  @Column()
  public r_nome_ruolo: string;
  @Column()
  public r_stato: Boolean;

  @OneToMany(() => PermissionRole, (permission_role) => permission_role.permission)
  @JoinColumn({ name: "r_id" })
  permission_role: PermissionRole[];
}
  • 权限

@Entity({ name: "PERMISSION" })
export class Permission {
  @PrimaryGeneratedColumn()
  public p_id: Number;
  @Column()
  public p_nome_permesso: string;
  @OneToMany(() => PermissionRole, (permission_role) => permission_role.permission)
  @JoinColumn({ name: "p_id" })
  permission_role: PermissionRole[];
}
  • PERMISSION_ROLE
@Entity({ name: "PERMISSION_ROLE" })
export class PermissionRole {
  @ManyToOne(() => Permission, (permission: Permission) => permission.p_id, { primary: true })
  @JoinColumn({ name: "p_id" })
  permission: Permission;
  @ManyToOne(() => Role, (role: Role) => role.r_id, { primary: true })
  @JoinColumn({ name: "r_id" })
  role: Role;
}

问题是我无法从关系中检索数据。 如果我尝试这样的事情:

permissionRepository.find({ relations: ["permission_role"] })

roleRepository.find({ relations: ["permission_role"] })

我在结果中收到Permission_role,但数组为空。

预先感谢大家会有所帮助

1 个答案:

答案 0 :(得分:0)

已解决 我这样修改了实体角色和权限:

@Entity({  name: "ROLE" })
export class Role {
  @PrimaryGeneratedColumn()
  public r_id: Number;
  @Column()
  public r_nome_ruolo: string;
  @Column()
  public r_stato: Boolean;


  @ManyToMany(() => Permission, (permission) => permission.roles)
  @JoinTable({
    name: "PERMISSION_ROLE",
    joinColumn: {
      name: "r_id",
      referencedColumnName: "r_id",
    },
    inverseJoinColumn: {
      name: "p_id",
      referencedColumnName: "p_id",
    },
  })
  permissions: Permission[];
}
@Entity({ name: "PERRMISSION" })
export class Permission {
  @PrimaryGeneratedColumn()
  public p_id: Number;
  @Column()
  public p_nome_permesso: string;

  @ManyToMany(() => Role, (role) => role.permissions)
  public roles: Role[];
}