为什么TypeORM需要提供OneToMany的反面,而不需要提供ManyToOne的反面? [英] Why does TypeORM require the inverse side of OneToMany to be provided, but doesn't require the inverse side of ManyToOne to be provided?

查看:85
本文介绍了为什么TypeORM需要提供OneToMany的反面,而不需要提供ManyToOne的反面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下引用摘自 TypeORM 文档:

@OneToMany 没有@ManyToOne 就不能存在.如果要使用@OneToMany,则需要@ManyToOne.但是,逆不是必需的:如果您只关心 @ManyToOne 关系,则可以在相关实体上没有 @OneToMany 的情况下定义它.在哪里设置 @ManyToOne - 它的相关实体将具有关系 ID"和外键.

@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.

为什么需要提供 OneToMany 的反面而不是 ManyToOne?

Why do you need to provide the inverse side of OneToMany but not ManyToOne?

推荐答案

是因为 ManyToOne 将外键放在当前实体表中,所以需要 SQL 表定义生成.归根结底只是一个SQL列作为外键,TypeOrm需要一个实体定义才能正确生成SQL语句.

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 需要知道外键在哪里生成列表的 fetch SQL 查询,因此需要 ManyToOne 映射.如果 OneToMany 未定义,则不会获取任何内容,因此它也可以工作.

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.

这篇关于为什么TypeORM需要提供OneToMany的反面,而不需要提供ManyToOne的反面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆