I have these 2 entities that reference each other twice:
@Entity('contents')
export class Content extends BaseEntity {
@PrimaryGeneratedColumn('uuid')
id: string;
@OneToOne(() => Folder, (folder) => folder.content, {
nullable: true,
eager: true,
cascade: true,
})
folder?: Folder;
@ManyToOne(() => Folder, (folder) => folder.contents)
@JoinColumn([{ name: 'parentFolderId', referencedColumnName: 'id' }])
parentFolder?: Folder;
}
@Entity('folders')
export class Folder extends BaseEntity {
@PrimaryGeneratedColumn('uuid')
id: string;
@OneToMany(() => Content, (content) => content.parentFolder)
contents: Content[];
@OneToOne(() => Content, (content) => content.folder, {
onDelete: 'CASCADE',
orphanedRowAction: 'delete',
})
@JoinColumn([{ name: 'contentId', referencedColumnName: 'id' }])
content: Content;
}
As you can see, there seems to be no circular dependency between those 2 entities. I made sure that the 2 properties aren't named the same so that TypeORM does not mix up, and I made sure each relationship is correctly defined on both sides.
However, when I try to save a Content
entity, setting the folder
property so that it's cascade inserted, I get a Cyclic dependency: Folder
error. Does anyone know why ?