GROUP BY removed in count query when using `getManyAndCount()`
See original GitHub issueIssue type:
[ ] question [x] 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:
[ ] latest
[ ] @next
[x] 0.2.14
(or put your version here)
Steps to reproduce or a small repository showing the problem:
I’m creating a query as follows:
const builder = this.recipeRepository.createQueryBuilder("r");
builder.where("r.store = :storeId", { storeId });
builder.leftJoinAndSelect("r.ratings", "rs", "rs.sessionId = :sessionId", {
sessionId
});
let groupBy = "(r.id, rs.id)";
const property = 'slug';
builder.innerJoin("r.filters", "rf");
const distinctLabelsQuery = await this.filterRepository
.createQueryBuilder("f")
.select("ARRAY_AGG(DISTINCT f.label)", "distinct_labels_filtered")
.where(`f.${property} in (:...slugs)`, { slugs: options.filterIds });
builder
.andWhere(`rf.${property} in (:...filterIds)`, {
filterIds: options.filterIds
})
.groupBy(groupBy)
.having(`ARRAY_AGG(DISTINCT rf.label) = (${distinctLabelsQuery.getQuery()})`)
.setParameters(distinctLabelsQuery.getParameters());
builder
.loadRelationIdAndMap("filters", "r.filters")
.orderBy('r."updatedAt"', "DESC");
const [items, total] = await builder
.limit(limit)
.offset(offset)
.getManyAndCount();
Which produces this SQL for the general query:
SELECT "r"."id" AS "r_id",
"r"."createdAt" AS "r_createdAt",
"r"."updatedAt" AS "r_updatedAt",
"r"."name" AS "r_name",
"r"."slug" AS "r_slug",
"r"."description" AS "r_description",
"r"."notes" AS "r_notes",
"r"."imageUrls" AS "r_imageUrls",
"r"."ingredients" AS "r_ingredients",
"r"."instructions" AS "r_instructions",
"r"."prepSeconds" AS "r_prepSeconds",
"r"."cookSeconds" AS "r_cookSeconds",
"r"."yield" AS "r_yield",
"r"."rating" AS "r_rating",
"r"."ratingCount" AS "r_ratingCount",
"r"."keywords" AS "r_keywords",
"r"."draft" AS "r_draft",
"r"."storeId" AS "r_storeId",
"r"."creatorId" AS "r_creatorId",
"r"."cuisineId" AS "r_cuisineId",
"rs"."id" AS "rs_id",
"rs"."createdAt" AS "rs_createdAt",
"rs"."updatedAt" AS "rs_updatedAt",
"rs"."ip" AS "rs_ip",
"rs"."value" AS "rs_value",
"rs"."sessionId" AS "rs_sessionId",
"rs"."recipeId" AS "rs_recipeId"
FROM "recipe" "r"
LEFT JOIN "recipe_rating" "rs" ON "rs"."recipeId"="r"."id"
AND ("rs"."sessionId" = '014299ab-caeb-43ae-b4ef-78c6fc91b95a')
INNER JOIN "recipes_filters_join" "r_rf" ON "r_rf"."recipeId"="r"."id"
INNER JOIN "recipe_filter" "rf" ON "rf"."id"="r_rf"."recipeFilterId"
WHERE "r"."storeId" = 1
AND "rf"."id" IN (38,
5)
AND "r"."draft" = false
GROUP BY ("r"."id",
"rs"."id")
HAVING ARRAY_AGG(DISTINCT "rf"."label") =
(SELECT ARRAY_AGG(DISTINCT "f"."label") AS "distinct_labels_filtered"
FROM "recipe_filter" "f"
WHERE "f"."id" IN (38,
5))
ORDER BY r."updatedAt" DESC
LIMIT 100 -- PARAMETERS: ["",1,38,5,false,38,5]
That returns 0 rows.
However, the count query following it (SQL produced):
SELECT COUNT(DISTINCT("r"."id")) AS "cnt"
FROM "recipe" "r"
LEFT JOIN "recipe_rating" "rs" ON "rs"."recipeId"="r"."id"
AND ("rs"."sessionId" = '014299ab-caeb-43ae-b4ef-78c6fc91b95a')
INNER JOIN "recipes_filters_join" "r_rf" ON "r_rf"."recipeId"="r"."id"
INNER JOIN "recipe_filter" "rf" ON "rf"."id"="r_rf"."recipeFilterId"
WHERE "r"."storeId" = 1
AND "rf"."id" IN (38,
5)
AND "r"."draft" = false
HAVING ARRAY_AGG(DISTINCT "rf"."label") =
(SELECT ARRAY_AGG(DISTINCT "f"."label") AS "distinct_labels_filtered"
FROM "recipe_filter" "f"
WHERE "f"."id" IN (38,
5)) -- PARAMETERS: ["014299ab-caeb-43ae-b4ef-78c6fc91b95a",1,38,5,false,38,5]
That returns a count of 2.
I’ve determine that’s because the GROUP BY
clause is removed. This seems like a bug.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:7
- Comments:12
Top Results From Across the Web
How to retrieve data according relation condition
I made the next query that works fine when the product has an image, but I don't know how to ignore the image...
Read more >Select using Query Builder - typeorm - GitBook
How to create and use a QueryBuilder ; SelectQueryBuilder · SELECT queries. Example: ·.createQueryBuilder() .select("user") ; InsertQueryBuilder · INSERT queries.
Read more >How to use getManyAndCount function in SelectQueryBuilder
const [list, count] = await query.skip((page - 1) * pageSize).take(pageSize) .getManyAndCount();
Read more >SQL GROUP BY query to count records within a ... - Plus2net
Sql group by command to count records in each group along with where clause. ... GROUP BY SQL query to get number of...
Read more >SelectQueryBuilder | typeorm
Executes sql generated by query builder and returns object with raw results and entities created from them. Parameters. queryRunner: QueryRunner. Returns ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@pleerock sorry if mentioned elsewhere, but is this lib is still in active support and what might you say about this one issue?
Issue seems at this line as groupBy has no params https://github.com/typeorm/typeorm/blob/3d876c61fafc815e429c68f4f4e1ab79e47c7b9c/src/query-builder/SelectQueryBuilder.ts#L1888
Can someone fix this?