-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
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
find with relations returns soft-deleted entities #6265
find with relations returns soft-deleted entities #6265
Comments
This behaviour is still a blocker for me at the moment. I'm wondering if anyone knows if it is intended, and if so what is the workaround? Or if it is known to be a bug I can look to make a PR for this one if someone can point me in the right direction. Thanks again, I know your team must be very busy. |
@Ollie1700 we are seeing the same thing |
Same as #5838 |
Same here. |
@Ollie1700 @chenfanggm is the field you use the @DeleteDateColumn() named 'deletedAt'? Anything else wont work. |
Yea, I have named the @DeleteDateColumn() as @field() @field()
Even my Thanks. |
I'm seeing a similar issue and also am using a column named `deletedAt |
Seeing the same problem |
Same problem, some solution? |
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: typeorm/src/query-builder/QueryBuilder.ts Lines 663 to 672 in c4a36da
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 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 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. |
What is the status of this? Seems like a fundamental feature to have would like to work on it but is anyone already fixing this? |
I am not aware of anyone working on this, you can submit a PR if you want. |
…ties This new feature changes the behavior of typeorm to allow avoiding entities that have soft delete close: typeorm#6265
…ties This new feature changes the behavior of typeorm to allow avoiding entities that have soft delete close: typeorm#6265
…ties This new feature changes the behavior of typeorm to allow avoiding entities that have soft delete close: typeorm#6265
Seeing the same problem |
…ties This new feature changes the behavior of typeorm to allow avoiding entities that have soft delete close: typeorm#6265
…ties This new feature changes the behavior of typeorm to allow avoiding entities that have soft delete close: typeorm#6265
…ties This new feature changes the behavior of typeorm to allow avoiding entities that have soft delete close: typeorm#6265
I know the issue has been closed, but I think the behaviour has remained the same even in 0.2.31 |
@lorenzosignoretti exactly the same ting happening for me, it can be related with the skip/limit I also include maybe? |
it's not working neither using find options nor query builder, it's frustrating that I need to write a query with 6 joins manually :( |
Worked for me when using the query builder and placing the withDeleted function at the top like this
but still not working with find options |
in most cases, you have to use innerJoinAndSelect instead of leftJoinAndSelect. |
Is there any workaround for this issue? It seems weird that such a basic feature is missing. |
Is there any update for this issue ? I'm facing the same. |
But I want the |
For someone who need load soft-deleted relations, this is my approach. async listAll(
dto: PageableDto,
where?: FindConditions<T>[] | string | ObjectLiteral,
relations?: string[],
select?: any[]
): Promise<PaginatedRes<T>> {
where = where || {};
const total = await this.repo.count({ where });
const order: any = dto.by && dto.sort ? { [dto.by]: dto.sort } : {};
// fix *where* to exclude soft-deleted
if (Array.isArray(where)) {
where.forEach((cond: ObjectLiteral) => {
cond.deletedDate = IsNull();
});
} else if (typeof where != 'string') {
where.deletedDate = IsNull();
}
const options: FindManyOptions<T> = {
withDeleted: true, // force load relations include soft-deleted
where,
relations,
select,
order
};
if (dto.limit != -1) {
options.take = dto.limit;
options.skip = (dto.page - 1) * dto.limit;
}
const results = await this.repo.find(options);
const pages = dto.limit == -1 ? 1 : Math.ceil(total / dto.limit);
return {
results,
page: dto.page,
pages,
limit: dto.limit,
total
};
} |
any solution? :/ |
Same here 😬 |
same |
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
const userWithPosts = await userRepo.findOne(1, { relations: [ 'posts' ] })
userWithPosts.posts
will contain all posts, even ones that have been deletedAm 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.The text was updated successfully, but these errors were encountered: