nestjsx / nestjs-typeorm-paginate

:page_with_curl: Pagination response object function + types for typeorm + nestjs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

paginateRaw enforcing groupBy when it is not needed

GhettoBurger996 opened this issue · comments

I have the following query I would like to get the facilities along with the AVG review of each when done like this WITHOUT the pagianteRaw or pagiante function it return the results as expected.

const facilities = await this.connection
        .getRepository(Facility)
        .createQueryBuilder("facilities")
        .select(["facilities.facilityUuid"])
        .leftJoin("facilities.facilityPrice", "fcp")
        .leftJoin("facilities.reviews", "facreviews")
        .addSelect([
          "fcp.facilityPriceUuid",
          `AVG(facreviews.rating) AS "averageRating"`,
        ])
        .groupBy("facilities.facilityUuid")
        .addGroupBy("fcp.facilityPriceUuid")
        .getRawMany();

      return facilities;

The body ends up looking like so,

    {
        "facilities_facilityUuid": "5d996b76-e2ba-4c24-b5c2-bdc53f8f3d4d",
        "fcp_facilityPriceUuid": "5d996b76-e2ba-4c24-b5c2-bdc53f8f3d4d",
        "averageRating": 3.57
    }

The issue is when using the same query and passing it into the paginateRaw or paginate function. It returns the following error. It expects an extra query like so .addGroupBy("facreviews.reviewUuid")


query failed: SELECT COUNT(*) AS "value" FROM (SELECT * FROM "facilities" "facilities" LEFT JOIN "facility_price" "fcp" ON "fcp"."facilityPriceUuid"="facilities"."facilityUuid"  LEFT JOIN "reviews" "facreviews" ON "facreviews"."facilityUuid"="facilities"."facilityUuid" GROUP BY "facilities"."facilityUuid", "fcp"."facilityPriceUuid") "uniqueTableAlias"
error: error: column "facreviews.reviewUuid" must appear in the GROUP BY clause or be used in an aggregate function

I cant group by review, since if I do it will return all the reviews posted, and instead of a single facility with its average review it ends up getting a dozen of entries. Again typeorm performs as intended despite it not being grouped by reviews.

This has been open since January 2022 and there is no solution for this?