I've been kinda confused by the relationships as I'm used to save relationship by id, while docs and examples I found suggest to get the entire object and use that instead (Isn't this strange???)
I found this on github addressing this issue ( https://github.com/typeorm/typeorm/issues/447 ) , where they suggest to use an object with just the id property, but it's from 2017. Is that a good way to do it ? And is it still the only way to do it ? (I find it pretty lame tbh)
async create( @Body() product: Product) {
product.category = <any>{ id: product.category };
return { payload: await this.repository.persist(product) };
}
Another one suggested to name the column as categoryId and it would work as expected (with id instead of object) but WHY? What does the name have to do with that ??
@Entity()
class Product {
@Column({ type: "int", nullable: true })
categoryId: number;
@ManyToOne(type => Category)
@JoinColumn({ name: "categoryId" })
category: Category;
}
I'm just confused, help ^_^