i have three entities TestUser , TestProfile and TestPhoto in which TestUser has a OneToOne relationship with TestProfile and TestProfiles has a OneToOne relationship with TestPhoto and at the las TestPhoto has this ManyToOne relationship with User which might has not been created yet
im using cascade when defining my entites and i wish to have them all get created with a single call in my UserService but facing this Cyclic dependency: "TestPhoto" Error and had no progress since then , i see its not probably what is should do in real life scenarios but apart from that ,any possible hack for it or its just fundamentally not possible?
@Entity()
@Unique(["name"])
export class TestUser {
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
@OneToOne(() => TestProfile,{
cascade:true,
nullable:true
})
@JoinColumn()
profile: TestProfile;
@Column({nullable:true})
profileId: number
@OneToMany(() => TestPhoto, photo => photo.user)
photos: TestPhoto[];
}
@Entity()
export class TestProfile {
@PrimaryGeneratedColumn()
id: number;
@Column()
gender: string;
@OneToOne(type=>TestPhoto,{
cascade:true,
nullable:true
})
@JoinColumn()
photo: TestPhoto;
@Column({nullable:true})
photoId: number
}
@Entity()
export class TestPhoto {
@PrimaryGeneratedColumn()
id: number;
@Column()
url: string;
@ManyToOne(() => TestUser, user => user.photos,{
cascade:true,
nullable:true
})
user: TestUser;
@Column({nullable:true})
userId: number;
}
and in my UserService abstracted the calls as followed
const user = new TestUser();
const profile1 = new TestProfile();
const photo1 = new TestPhoto();
photo1.user = user;
profile1.photo = photo1;
user.profile = profile1
await connection.manager.save(user);
Does these entities are living in the same file?
I use import type
TS's feature to resolve cyclic dependencies at module resolution level. I'm not sure if that is your case tho.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With