简体   繁体   English

Typeorm – 如何使用 TS `Relation` 类型来处理一对多关系?

[英]Typeorm – How do you use the TS `Relation` type for one-to-many relations?

On the Typeorm FAQs page , the docs give instructions to wrap entities in a Relation type to prevent circular dependencies, but the example they give is with a one-to-one relationship.Typeorm FAQs 页面上,文档给出了将实体包装在Relation类型中以防止循环依赖的说明,但他们给出的示例是一对一关系。 This leaves the solution for a many-to-one or one-to-many rather ambiguous.这使得多对一或一对多的解决方案相当模糊。 Which of the following would be the correct implementation?以下哪项是正确的实施?

@Entity()
export class User {
    @OneToMany(() => Profile, (profile) => profile.users)
    profiles: Relation<Profile[]>;
}

or要么

@Entity()
export class User {
    @OneToMany(() => Profile, (profile) => profile.users)
    profiles: Relation<Profile>[];
}

Or, does it even matter which way you do it?或者,您采用哪种方式甚至重要吗?

This should be the right form as you already provided:这应该是您已经提供的正确表格:

@Entity()
export class User {
    @OneToMany(() => Profile, (profile) => profile.users)
    profiles: Relation<Profile[]>;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM