Skip to content
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

Count query for fetchPage is incorrect for queries with raw select and groupBy #2092

Open
squgeim opened this issue Nov 28, 2020 · 0 comments

Comments

@squgeim
Copy link

squgeim commented Nov 28, 2020

Introduction

For a query that contains a raw select and is grouped by those columns, the count query generated by fetchPage is incorrect and fails. This issues was introduced in our system after we upgraded Bookshelf from 0.10.3 to 1.2.0, and knex from 0.13.0 to 0.21.12.

Issue Description

We have queries that extracts year and month from a single date column. We then group them by year and month and select some aggregate functions. When we do fetchPage, the count query generate selects count(distinct("year", "month")) directly from the table, which fails because these columns do not exist.

I am not sure about the implementation so can't comment on what the fix would be, but it clear that this worked as expected in the older version so it should be a bug.

Steps to reproduce issue

This is a simplified query that replicates the bug (Postgres):

AvgTouchesToEnroll.query((qb) => {
  qb.where('user_id', query.userId);
  qb.select(
    knex.raw(
      'extract(year from date) :: integer as year, extract(month from date) :: integer as month, sum(avg_touches) as avg_touches, sum(count) as count'
    )
  );
  qb.groupBy('year');
  qb.groupBy('month');
}).fetchPage({
  page: query.page,
  pageSize: query.pageSize,
});

The generated count query and error is:

select count(distinct("year", "month")) from "average_touches_to_enroll" where "user_id" = $1 - column "year" does not exist

Expected behaviour

It should return the requested page with pagination data.

Actual behaviour

It fails with an SQL error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant