我把这两个实体联系在一起:
@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;
}
但是,当用户采取行动时,我想记录消息实体。我尝试在消息中添加一个额外的关系,如下所示:
@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;
}
但是,当尝试保存一个填充了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
设置cascade: false
不会引发错误,但也不会保存相关记录。
我遗漏了什么?有什么办法我仍然可以拥有cascade: true
并与实体有双重关系吗?还是我必须手动保存相关记录?
发布于 2022-04-12 12:04:24
如果出现此问题,在@ManyToOne
@OneToMany
关系中,可以通过删除关系中没有cascade
的一方来解决。错误消失了,级联起作用了。
https://stackoverflow.com/questions/68734399
复制相似问题