问题
The following quote is taken from the TypeORM docs:
@OneToMany cannot exist without @ManyToOne. If you want to use @OneToMany, @ManyToOne is required. However, the inverse is not required: If you only care about the @ManyToOne relationship, you can define it without having @OneToMany on the related entity. Where you set @ManyToOne - its related entity will have "relation id" and foreign key.
Why do you need to provide the inverse side of OneToMany but not ManyToOne?
回答1:
It is because ManyToOne puts the foreign key in the current entity table, it is necessary for SQL table definition generation. After all it is just a SQL column that is a foreign key in the end, and TypeOrm requires an entity definition that allows it to correctly generate SQL statements.
TypeOrm needs to know where the foreign key is to generate the fetch SQL query of the list, thus requiring the ManyToOne mapping. If OneToMany is undefined then nothing is fetched, so it also works.
来源:https://stackoverflow.com/questions/58944885/why-does-typeorm-require-the-inverse-side-of-onetomany-to-be-provided-but-doesn