Skip to content

Conversation

@davidgf
Copy link
Contributor

@davidgf davidgf commented Feb 19, 2019

Introduction

The pagination plugin is returning the wrong rowCount when the query contains a SELECT DISTINCT statement.

Motivation

In one of the projects we're working on, we need to paginate different occurrences of a column within a table. When we reached the last page, we've noticed that we were still getting pagination details. Not only so, but if we requested a page with an offset greater that the total, we still got pagination details along with an empty set of results. This was due to calculating the rowCount incorrectly.

Fixes #1948.

Proposed solution

When counting the total number of rows, the pagination plugin is excluding all columns statements from the grouping columns, even when they are part of a distinct statement. Therefore, the resulting count query selects all the records in the table, rather than only unique occurrences of a column in that table, and returning a wrong rowCount. The proposed solution simply keeps columns statements when distinct is true.

Copy link
Member

@ricardograca ricardograca left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. Thanks for the fix.

@ricardograca ricardograca merged commit b686fb7 into bookshelf:master Feb 19, 2019
@davidgf davidgf deleted the fix-pagination-for-distinct-selects branch February 19, 2019 13:45
@davidgf
Copy link
Contributor Author

davidgf commented Feb 20, 2019

Thank you for accepting it 😄 Do you know when the fix could be released in npm?

@ricardograca
Copy link
Member

Possibly in the next version (1.0.0), if not earlier.

@ricardograca
Copy link
Member

This was released in version 0.15.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Pagination details are wrong when selecting distinct values of a column

2 participants