Typeorm多对多关系/一侧带有复合键

时间:2020-08-20 21:51:52

标签: typeorm

“我的产品”表的主键由序列号+产品ID组成,

我想知道在我的人际关系中我是否只能使用这些主键中的一个键

是否可以在typeorm中定义它?

换句话说,在多对多关系中仅使用复合键的主键

员工:

@Entity({ name: 'employees' })
@Index(['matricula'], { unique: true })
export default class Employee {
  constructor(firstName: string, lastName: string) {
    this.firstName = firstName;
    this.lastName = lastName;
  }
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column({ name: 'matricula', nullable: false })
  matricula: string;

  @Column({ name: 'first_name', nullable: false })
  firstName: string;

  @Column({ name: 'last_name', nullable: false })
  lastName: string;

  @Column()
  departament_id: string;

  @ManyToMany(type => Products)
  @JoinTable({ name: 'employees_has_products' })
  products: Products[];

  //departament relations
  @OneToOne(
    () => Departament,
    (departament: Departament) => departament.manager,
  )
  @ManyToOne(
    () => Departament,
    (departament: Departament) => departament.employees,
  )
  @JoinColumn({ name: 'departament_id' })
  departament: Departament;

  //user relations
  @OneToOne(() => User, (user: User) => user.employee, {
    cascade: ['insert'],
  })
  user: User;

  //equipament relations
  @OneToMany(type => Equipament, equipament => equipament.employees)
  equipaments: Equipament[];

  @CreateDateColumn({ name: 'created_at' })
  createdAt: Date;

  @CreateDateColumn({ name: 'updated_at' })
  UpdatedAt: Date;
}

产品

@Entity({ name: 'products' })
export default class Products {
  @PrimaryGeneratedColumn()
  serial_number: string;

  @ManyToOne(type => Product, product => product.products, { primary: true })
  @JoinColumn({ name: 'product_id' })
  product: Product;

  @CreateDateColumn({ name: 'created_at' })
  createdAt: Date;

  @CreateDateColumn({ name: 'updated_at' })
  UpdatedAt: Date;
}

0 个答案:

没有答案