find with relations returns soft-deleted entities
See original GitHub issueIssue 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
const userWithPosts = await userRepo.findOne(1, { relations: [ 'posts' ] })userWithPosts.postswill 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:
- Created 3 years ago
- Reactions:45
- Comments:22 (2 by maintainers)
Top Related StackOverflow Question
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.profilecan’t benull.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
SelectQueryBuilderandQueryBuilder.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.
I know the issue has been closed, but I think the behaviour has remained the same even in 0.2.31