Why does TypeORM require the inverse side of OneToMany to be provided, but doesn't require the inverse side of ManyToOne to be provided?

白昼怎懂夜的黑 提交于 2021-01-01 06:53:05

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!