typeorm: Cascade delete doesn't work in one-to-one relationship
Issue type:
[ ] question [x] bug report [ ] feature request [ ] documentation issue
Database system/driver:
[ ] cordova
[ ] mongodb
[ ] mssql
[ ] mysql / mariadb
[ ] oracle
[x] postgres
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo
TypeORM version:
[x] latest
[ ] @next
[ ] 0.x.x (or put your version here)
Steps to reproduce or a small repository showing the problem:
Not sure it’s a bug or a feature, but here it goes: For the one-to-one relationship, cascade delete works only for the inverse side.
Having entities like this:
@Entity()
class User {
@PrimaryGeneratedColumn('uuid')
id!: string
@OneToOne(
() => Profile,
profile => profile.user,
{ cascade: true, onDelete: 'CASCADE' },
)
@JoinColumn()
profile!: Profile
}
@Entity()
class Profile {
@PrimaryGeneratedColumn('uuid')
id!: string
@OneToOne(
() => User,
user => user.profile,
{ onDelete: 'CASCADE' }, // This line doesn't actually make any difference
)
user!: User
}
what happens is:
await getManager().remove(someProfile) // The profile gets deleted along with the user
but
await getManager().remove(someUser) // The user gets deleted, but the profile stays in place
How I would like it to work, is the related entities to be actually removed when I define onDelete: 'CASCADE' on both sides of the relation.
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 5
- Comments: 22 (2 by maintainers)
@jalooc do you know how SQL’s
onDeleteworks? Because it requires this understanding to understand how it works in typeorm (it works same way)Put your
@JoinColumn()on the inverse side (on Profile).If you do this it will create a circular dependence, so it doesn’t works
This is still an issue! Tried mostly all. I can’t logically assign the ownership to the child side to handle case 😕
it’s still a problem
Still an issue
still not working in 2023, Dec 14.
@pleerock I know how SQL’s
ON DELETEworks, but wasn’t aware that it’s the only thing typeorm does when settingonDelete: "CASCADE". Now I know 😃So, to get it right - there’s no way to perform a cascade delete on one-to-one relation from both sides then? All I’m left with is programatic delete of both entities?
This is still an issue to this day, why was the ticket closed?
STILL AN ISSUE HELP!
I was also able to replicate the issue. The cascase on delete does not seem to work on onetoone relatioship.
yeah, still not working!
Sill not working 😦
++
Why is this closed, however problem is still persist after 2 years?
@jalooc yes, exactly. There is opened issue on programatic cascade remove, but there is no time for its implementation yet (#2784).