adonisjs / lucid

AdonisJS SQL ORM. Supports PostgreSQL, MySQL, MSSQL, Redshift, SQLite and many more
https://lucid.adonisjs.com/
MIT License
1.03k stars 189 forks source link

Pagination doesn't take distinct queries into account #912

Closed andrew-connell closed 1 year ago

andrew-connell commented 1 year ago

Package version

18.3.0

Node.js and npm version

Node 18.13.0, npm 9.3.0

Sample Code (to reproduce the issue)

await Record.query().distinct('vehicle_id').paginate(1)

It produces the following queries.

SELECT COUNT(*) AS "total" FROM "records"
SELECT DISTINCT "vehicle_id" FROM "records" LIMIT 20

The first one should be SELECT COUNT(DISTINCT("vehicle_id")) AS "total" FROM "records"

It's an easy fix but I'm no expert on the underlying system so my code probably isn't amazing.

But in the paginate function I do const distinct = this.knexQuery['_statements'].find((statement) => statement.distinct)?.value and then if it exists, take that into consideration with the query

stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

andrew-connell commented 1 year ago

Well it definitely still exists as an issue

stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.