find with relations returns soft-deleted entities

See original GitHub issue

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:

Apologies for opening an issue for this, it seems like I am probably missing something very obvious but essentially the issue is that soft-deleted entities are being returned as part of relations.

Example:

Say we have “User” and “Post” - user has many posts

  1. const userWithPosts = await userRepo.findOne(1, { relations: [ 'posts' ] })
  2. userWithPosts.posts will contain all posts, even ones that have been deleted

Am I missing something obvious here? This functionality works as expected when not using relations (e.g. in the above example, postRepo.find() would only return non-deleted items) but when using relations I am running into this issue.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:45
  • Comments:22 (2 by maintainers)

github_iconTop GitHub Comments

24reactions
nebkatcommented, Nov 27, 2020

This is simply because the functionality is not implemented. The code that normally adds the filter for deleted items is here, and it does so for the main entity in the query only, not for the relations:

https://github.com/typeorm/typeorm/blob/c4a36da62593469436b074873eba186f0f8b990d/src/query-builder/QueryBuilder.ts#L663-L672

It makes sense to filter out deleted One-to-Many/Many-to-Many relations, but it might not be the expected default behavior for a One-to-One/Many-to-One relation. If a user’s “profile” has been deleted, you might still expect the relation to be loaded, especially if user.profile can’t be null.

If someone wants to implement an option for this you can send in a PR and link to this issue, it shouldn’t be too difficult. Start with SelectQueryBuilder and QueryBuilder.


BTW, please avoid commenting just to say you have the problem. The original issue has described what is wrong, unless you have something useful to add just 👍 it.

14reactions
lorenzosignoretticommented, Feb 28, 2021

I know the issue has been closed, but I think the behaviour has remained the same even in 0.2.31

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I get soft deleted entity from typeorm postgreSQL?
I found out there is another solution. You can use the querybuilders .withDeleted() method to also return soft deleted entities. Example
Read more >
Relations - typeorm - GitBook
orphanedRowAction: "nullify" | "delete" | "soft-delete" | disable - When a parent is saved (cascading enabled) without a child/children that still exists in ......
Read more >
Find Options | TypeORM Docs
withDeleted - include entities which have been soft deleted with softDelete or softRemove , e.g. have their @DeleteDateColumn column set.
Read more >
Introducing the EfCore.SoftDeleteServices library to automate ...
This is useful in places when another part of your code accesses the dependent relationships of an entity that was soft deleted. That...
Read more >
Soft Delete | Nestjs-query - Blog
This feature does not delete records but instead updates the column ... Before enabling soft deletes you must add the DeletedDateColumn to your...
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