question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

GROUP BY removed in count query when using `getManyAndCount()`

See original GitHub issue

Issue 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:open
  • Created 4 years ago
  • Reactions:7
  • Comments:12

github_iconTop GitHub Comments

3reactions
noneedinmagiccommented, Mar 14, 2021

@pleerock sorry if mentioned elsewhere, but is this lib is still in active support and what might you say about this one issue?

2reactions
lukzardcommented, Mar 8, 2021
Read more comments on GitHub >

github_iconTop 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 >

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