简体   繁体   English

OneToOne 和 ManyToOne 的 TypeORM 循环依赖

[英]TypeORM cyclic dependency with OneToOne and ManyToOne

I've got these two entities related to each other:我有这两个相互关联的实体:

@Entity()
export class Message {

    // ... other columns ...

    @OneToMany(() => Action, action => action.message, { eager: true, cascade: true })
    public actions: Action[];
}
@Entity()
export class Action {

    // ... other columns ...

    @ManyToOne(() => Message, message => message.actions, { nullable: false })
    public message?: Message;
}

But I want to record on the message entity when the user takes an action.但是我想在用户采取行动时记录在消息实体上。 I try adding an extra relation to the message like this:我尝试像这样向消息添加额外的关系:

@Entity()
export class Message {

    // ... other columns ...

    @OneToMany(() => Action, action => action.message, { eager: true, cascade: true })
    public actions: Action[];

    @OneToOne(() => Action, { nullable: true })
    @JoinColumn()
    public action_taken: Action;
}

But when trying to save a new message with the actions relation populated (trying to save them all at once with cascade: true ), then I get the following error:但是,当尝试保存填充了actions关系的新消息时(尝试使用cascade: true一次保存它们),然后出现以下错误:

TypeORMError: Cyclic dependency: "Action"
    at new TypeORMError (/app/node_modules/typeorm/error/TypeORMError.js:9:28)
    at visit (/app/node_modules/typeorm/persistence/SubjectTopoligicalSorter.js:144:23)
    at visit (/app/node_modules/typeorm/persistence/SubjectTopoligicalSorter.js:160:21)
    at visit (/app/node_modules/typeorm/persistence/SubjectTopoligicalSorter.js:160:21)
    at SubjectTopoligicalSorter.toposort (/app/node_modules/typeorm/persistence/SubjectTopoligicalSorter.js:139:17)
    at SubjectTopoligicalSorter.sort (/app/node_modules/typeorm/persistence/SubjectTopoligicalSorter.js:53:45)
    at SubjectExecutor.<anonymous> (/app/node_modules/typeorm/persistence/SubjectExecutor.js:99:124)
    at step (/app/node_modules/tslib/tslib.js:143:27)
    at Object.next (/app/node_modules/tslib/tslib.js:124:57)
    at /app/node_modules/tslib/tslib.js:117:75

Setting cascade: false doesn't throw the error, but then it also doesn't save the related records.设置cascade: false不会抛出错误,但也不会保存相关记录。

What am I missing?我错过了什么? Is there a way I can still have cascade: true and have a double relation to an entity?有没有办法我仍然可以拥有cascade: true并且与实体有双重关系? Or will I have to manually save the related records?还是我必须手动保存相关记录?

Had this issue, working fine on nest CLI but give issues using serverless and webpack.有这个问题,在 nest CLI 上工作正常,但在使用无服务器和 webpack 时出现问题。 Typeorm does not work well with webpack. Typeorm 不适用于 webpack。 I used repository.save (entity) and always failed with cyclic dependency.我使用了 repository.save (entity) 并且总是因循环依赖而失败。

After switching to切换到后

repository.createQueryBuilder()
.insert()
.values(data)
.execute();

This error has been cleared.此错误已被清除。 I really recommend trying to change the repository method, because the save() method waits for the full entity set and createQueryBuilder() takes the entity values and turns them into string values, making a normal query.我真的建议尝试更改存储库方法,因为 save() 方法等待完整的实体集,而 createQueryBuilder() 获取实体值并将它们转换为字符串值,进行正常查询。

Had this problem, in a @ManyToOne @OneToMany relationship, It can be solved by removing the side of the relationship that is without cascade .有这个问题,在@ManyToOne @OneToMany关系中,可以通过删除没有cascade的关系的一侧来解决。 The error goes away, and the cascade works.错误消失,级联工作。

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

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