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
Recent Distinct Count change broke existing query. #3182
Comments
Reproduction? We have tests like this: https://github.com/mikro-orm/mikro-orm/blob/master/tests/QueryBuilder.test.ts#L881-L886 (works the same without |
Reproduced it, but I'm not entirely sure if this is actually a bug or just a weird interaction. tl;dr: the combination of
|
Yeah you dont need both, Btw the same reproduces if you await such QB too, as awaiting count QB will call |
Thanks for the quick fix! |
In my case, I wanted to join and group the results by id as well as selecting fields conditionally, so the solution proposed above and in here didn't work. So I ended up with this: const qb = this.em
.createQueryBuilder(Role, 'role')
.where({ account, isActive: true });
if (filters?.name?.length) {
qb.andWhere({ name: { $ilike: `%${filters.name}%` } });
}
const total = await qb.getCount();
qb.select([
'id',
'name',
'description',
'isActive',
'isAdmin',
'permissions',
'createdAt',
'updatedAt'
])
.orderBy([
{ name: filters?.nameOrder },
{ createdAt: filters?.createdAtOrder }
])
.limit(filters.pageSize)
.offset(getOffset(filters.pageIndex, filters.pageSize));
if (filters?.includeUserCount) {
qb.addSelect(['count(users) as "userCount"']);
qb.leftJoin('role.users', 'users').groupBy('role.id');
}
const roles = await qb.execute(); where I first get the count, then the actual results. Generated queries: select count(*) as "count" from "roles" as "role" where "role"."account_id" = 'ffb2d02d-5a70-40b1-a62f-90d6c515b4ab' and "role"."is_active" = true
--
select "role"."id", "role"."name", "role"."description", "role"."is_active", "role"."is_admin", "role"."permissions", "role"."created_at", "role"."updated_at", count(users) as "userCount" from "roles" as "role" left join "user_profiles" as "users" on "role"."id" = "users"."role_id" where "role"."account_id" = 'ffb2d02d-5a70-40b1-a62f-90d6c515b4ab' and "role"."is_active" = true group by "role"."id" order by "role"."name" asc, "role"."created_at" desc limit 20 |
Describe the bug
The change in eebe34d broke some existing queries for us. Downgrading
@mikro-orm/knex
to5.1.4
works as expected.Stack trace
Versions
The text was updated successfully, but these errors were encountered: