Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cascade softRemove on relations #30

Closed
Abdizriel opened this issue Apr 21, 2020 · 12 comments
Closed

Cascade softRemove on relations #30

Abdizriel opened this issue Apr 21, 2020 · 12 comments

Comments

@Abdizriel
Copy link

Issue type:

[x] question
[ ] bug report
[ ] feature request
[ ] documentation issue

Database system/driver:

[ ] cordova
[ ] mongodb
[ ] mssql
[ ] mysql / mariadb
[ ] oracle
[x] postgres
[ ] cockroachdb
[ ] 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:

I have an issue with a cascade softRemove on relations. This is the entry for a Client table

  @Field(_ => [Patient])
  @OneToMany(
    _ => Patient,
    patient => patient.owner,
    { lazy: true, cascade: true  },
  )
  public patients: Lazy<Patient[]>

So when i do softRemove on a client entry it should do softRemove on all patients. This is how I do softRemove on my service:

  async removeById(id: string) {
    const client = await this.getById(id)
    return this.clientRepository.softRemove(client)
  }

It soft removed a client correctly however all patients where client is the owner are not soft removed. Could you point where the error is? I have even tried this but it also didn't worked:

  @Field(_ => [Patient])
  @OneToMany(
    _ => Patient,
    patient => patient.owner,
    { lazy: true, cascade: true, onDelete: 'CASCADE'  },
  )
  public patients: Lazy<Patient[]>

or even on the Patient table which also didn't worked

  @Field(_ => Client)
  @ManyToOne(_ => Client, { lazy: true, cascade: true })
  public owner: Lazy<Client>
@iWinston
Copy link
Owner

Hi, can you show me the code of this.getById() ?
The softRemove only works when you pull the patients with the owner at the same time.

I am sorry for missing the notify.

@ruslanguns
Copy link

@iWinston Is cascade supported in Typeorm or only in Typeorm+? I mentioned it at : typeorm/typeorm#5034 (comment)

@Abdizriel
Copy link
Author

@iWinston this is the code fot this.getById()

async getById(id: string): Promise<Client> {
    return this.clientRepository.findOne({ id, tenant: this.tenant })
  }

@ruslanguns
Copy link

@Abdizriel how did you solve the circular dependency by using cascade: true in both the entity and the child entity? and what does @type decorator does?

@Abdizriel
Copy link
Author

@ruslanguns I had to remove one cascade true and I did it from a child (Patient)

@iWinston
Copy link
Owner

@Abdizriel Maybe you can try this:

async getById(id: string): Promise<Client> {
    return this.clientRepository.findOne({
        where: { id, tenant: this.tenant },
        relations: ['patients']
     })
  }

@ruslanguns
Copy link

ruslanguns commented Apr 29, 2020

@iWinston How can I find softdeleted users?

.... I just edited my comment at typeorm/typeorm#5034 (comment)

@Abdizriel
Copy link
Author

@iWinston Yeah, that one worked however then in case of softRemove I have to add everywhere relations since it doesn't work with lazy loading.

@ruslanguns
Copy link

Nevermind, just discovered it by myself using { withDeleted: true } .... thank you @iWinston for this great feature!!

@iWinston
Copy link
Owner

@Abdizriel Maybe you can try eager relations if you want it to be loaded automatically.

@Abdizriel
Copy link
Author

Abdizriel commented May 7, 2020

@iWinston Sorry for late reply, it worked as you said! Thank you very much for help with this. One last question - How I can include soft deleted entry from relations to a response from repository? Since I can do it for a main entity { withDeleted: true }

@iWinston
Copy link
Owner

iWinston commented May 9, 2020

@Abdizriel It will always include soft deleted entry from relations to a response from repository by default.
The default global condition of non-deleted is just applied to the main entity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants