【发布时间】:2020-06-20 06:01:44
【问题描述】:
我对关系有点困惑,因为我习惯于通过 id 保存关系,而我发现的文档和示例建议获取整个对象并使用它(这不是很奇怪吗???)
我在 github 上发现了这个问题,解决了这个问题 (https://github.com/typeorm/typeorm/issues/447),他们建议使用只有 id 属性的对象,但它是从 2017 年开始的。这是一个好方法吗?它仍然是唯一的方法吗? (我觉得它很蹩脚)
async create( @Body() product: Product) {
product.category = <any>{ id: product.category };
return { payload: await this.repository.persist(product) };
}
另一个建议将该列命名为 categoryId,它会按预期工作(使用 id 而不是对象),但为什么呢?名字和这个有什么关系??
@Entity()
class Product {
@Column({ type: "int", nullable: true })
categoryId: number;
@ManyToOne(type => Category)
@JoinColumn({ name: "categoryId" })
category: Category;
}
我很困惑,求助^_^
【问题讨论】: