主键一对多的Typeorm

时间:2020-06-18 19:33:21

标签: typeorm

我有两个实体:

@Entity('cast')
export class Cat {
  @PrimaryGeneratedColumn()
  cat_id: number;

  @JoinColumn({ name: 'owner_id' })
  @ManyToOne(() => Owner, (owner) => owner.owner_id)
  owner_id: Owner;
}

@Entity('owners')
export class Owner {
  @PrimaryColumn('uuid')
  owner_id: string;

  @Column()
  description: string;

  @Column()
  datetime: Date;
}

如何正确添加 OneToMany 装饰器,这样我就可以获得这样的结果 (具有 owner_id = owner.owner_id Cat 实体的列表):

const response = await ownerRepo.find(...);

response // 
[{ 
  owner_id: 'aaa',
  description: 'hello',
  datetime: 'iso string',
  cats: [
    {
      cat_id: 1,
      owner_id: 'aaa',
    },
    {
      cat_id: 2,
      owner_id: 'aaa',
    },
  ]
}]

我需要一组具有匹配owner_id的猫。

0 个答案:

没有答案