I try to delete user's profile when user was deleted. But it's delete nothing on profile.
User Entity
@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
@OneToOne(type => Profile, profile => profile.user,
{ eager: true, cascade: true, onDelete: 'CASCADE' })
@JoinColumn()
profile: Profile;
}
Profile Entity
@Entity()
export class Profile {
@PrimaryGeneratedColumn()
id: number;
@Column()
gender: string;
@Column()
photo: string;
@OneToOne(type => User, user => user.profile)
user: User;
}
How I delete a user.
const userRepository = connection.getRepository(User);
const user = await userRepository.findOne({ where: {id: 'someuserId'}, relations: ["profile"] });
await user.remove()
User was removed but nothing happen on user's profile.
Can anyone explain how to delete relationship on one-to-one?
User
@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
@OneToOne(type => Profile, profile => profile.user,
{ eager: true, onDelete: 'CASCADE' })
@JoinColumn()
profile: Profile;
}
Profile
@Entity()
export class Profile {
@PrimaryGeneratedColumn()
id: number;
@Column()
gender: string;
@Column()
photo: string;
@OneToOne(type => User, user => user.profile, { onDelete: "CASCADE" })
user: User;
}
I ran this code for removing entries from both the entities.
const userRepository = connection.getRepository(User);
const user = await userRepository.findOne({ where: {id: 'someuserId'} });
const profileRepository = connection.getRepository(Profile);
const profile = await profileRepository.findOne({ where: {id: user.profile.id} });
await profileRepository.remove(profile)
This is the expected behaviour for one-to-one relation. You have to delete referencing side to take cascade deletion to take in effect. discussion link
edit: raised issue on github
I recently found myself in the same spot. I created a inheritance relationship in my database. Multiple entities reference one common entity which provides basic attributes for each specialized table. For example: Common attribute holder table "Car" and specialized tables "Truck", "Bus". Now I wanted to delete the corresponding "Car"-entity whenever a "Truck" or "Bus" is deleted.
In typeorm the specialized tables should reference the common attribute table. To make the CASCADE DELETE work, you have to specify the side which holds the id using the @JoinColumn() annotation. Then you have to specify the onDelete: "CASCADE" on the One-to-One relationship of the same side as the @JoinColumn() annotation.
To validate the created table you can use SHOW CREATE TABLE table_name in your database. There you will see if the CASCADE DELETE is present on the correct foreign key constraint. (It should be on the table holding the foreign key).
That means, it is not required to set the CASCADE DELETE on both sides.
(see Gaurav Sharma's answer - modified)
// referenced table
@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
// you can use eager on the none-owning side
// this can be very helpful for inheritance
@OneToOne(type => Profile, profile => profile.user, { eager: true })
profile: Profile;
}
// owning side - foreign key in this table
@Entity()
export class Profile {
@PrimaryGeneratedColumn()
id: number;
@Column()
gender: string;
@Column()
photo: string;
// specify onDelete here because owning side FK constraint would fail
// and it needs to know what is going to happen
@OneToOne(type => User, user => user.profile, { onDelete: "CASCADE" })
@JoinColumn()
user: User;
}
From here on you would create a new user. Set the saved user in the profile to save and save the profile. Deleting the profile will not delete the user. Deleting the user will delete the profile.
I also looked into CASCADE DELETE from both sides. From what I have found, you would have to specify the @JoinColumn() annotation on both sides, declaring onDelete: "CASCADE" on both sides. This will require at least two saves for new entities and I did not find further information in the docs concerning the bidirectional CASCADE DELETE. I would consider deleting manually in these situations because it might become very unclear what is going to happen and when entities need to be saved.
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