SoftDelete in relationships cascade does not work
See original GitHub issueIssue type:
[ ] question [x] bug report [ ] feature request [ ] documentation issue
Database system/driver:
[ ] cordova
[ ] mongodb
[ ] mssql
[x] mysql
/ mariadb
[ ] oracle
[ ] postgres
[ ] cockroachdb
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo
TypeORM version:
[ ] latest
[ ] @next
[ ] 0.2.24
(or put your version here)
In one to many relationship the sofdelete does not update the related entity:
The Gestion entity has a one-to-many relationship with the Incedence entity
Gestion entity
....
@DeleteDateColumn({ type: "datetime" })
deletedAt: Date;
....
@OneToMany(type => Incidence, incidence => incidence.gestion,{
cascade: true
})
incidences: Incidence[];
Inverse relationship
Incidence entity
...
@DeleteDateColumn({ type: "datetime" })
deletedAt: Date;
...
@ManyToOne(type => Gestion, gestion => gestion.incidences)
gestion: Gestion;
await gestRepository.softDelete({id:gestionId})
The deleteAt column of the Gestion entity is set correctly with the date but the related entity Incidence is not affected, it remains null
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:22 (3 by maintainers)
Top Results From Across the Web
SoftDelete and cascade - Laracasts
Is there a way to 'delete' (softdelete) or 'set null' related models when deleting (softdelete) the parent model automatically?
Read more >Soft delete with cascade deletion doesn't work as expected
The problem is that the last line of code, "entity2Repo.Count().ShouldBe(0);" assertion is broken, it is actually 1 instead of 0, IsDeleted ( ...
Read more >Soft delete cascade in PostgreSQL and YugabyteDB
I've run this on YugabyteDB to verify that it works the same as in PostgreSQL. Of course, no suprise, YugabyteDB re-uses the postgres...
Read more >dyrynda/laravel-cascade-soft-deletes - Packagist
A LogicException will be triggered if the model does not use the Illuminate\Database\Eloquent\SoftDeletes trait, or if any of the defined ...
Read more >SoftDelete | Ebean
A soft delete will cascade along the same relationships as a hard delete as long as the beans support soft delete. If the...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Immediately after posting I discovered that if I include the relations in my original query then the cascade deletes will work.
For example, the following did not soft delete the children:
Then by adding the relations it did:
My entities are as follows:
Also to note, the only way to get the cascade to work properly is to use
softRemove
and pass the entire entity in with all children. e.g.repo.softRemove(parent)
whereparent
contains all children. I suppose this makes sense, otherwise thesoftRemove
method would have to perform additional queries for all children and nested children, but the behavior isn’t very intuitive or helpful seeing as one could simply query the children separately and pass them torepo.softRemove()
.Ok, in your example it works if I change something on parent. In this case, I don’t want to change anything except deletedAt which is done by softRemove().
It’s a part of a feature that receive an event when a guild leave, find if settings for this guild exist, if yes softRemove it and softremove all guild’s members settings too.
Because softRemove() change something on the parent (the deletedAt column), I was thinking this will propagate the fact that parent is softRemoved and childrens need to be softRemoved too.