question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SoftDelete in relationships cascade does not work

See original GitHub issue

Issue 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:open
  • Created 3 years ago
  • Reactions:1
  • Comments:22 (3 by maintainers)

github_iconTop GitHub Comments

42reactions
mikehrothcommented, Aug 17, 2020

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:

const parent = await repo.findOneOrFail({ id })               
await repo.softRemove(parent)         

Then by adding the relations it did:

const parent = await repo.findOneOrFail({ id }, {
    relations: ['children']
}) 
repo.softRemove(parent)

My entities are as follows:


// Parent
  @OneToMany(
    (type) => Child, 
    (joint) => child.parent,
    { cascade: true }
  )
  public children: Child[];

// Child
  @ManyToOne(
    (type) => Parent, 
    (parent) => parent.children
  )
  public parent!: Parent;

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) where parent contains all children. I suppose this makes sense, otherwise the softRemove 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 to repo.softRemove().

7reactions
xavierdemoorcommented, Nov 25, 2020

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found