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

Preventing negative offset by default #4335

Closed
aghArdeshir opened this issue Mar 3, 2021 · 4 comments
Closed

Preventing negative offset by default #4335

aghArdeshir opened this issue Mar 3, 2021 · 4 comments

Comments

@aghArdeshir
Copy link
Contributor

Environment

Knex version: 0.21.17
Database + version: ~#: mysql --version -> mysql Ver 14.14 Distrib 5.5.62, for debian-linux-gnu (x86_64) using readline 6.3
OS: Debian

Bug

  1. I mistakenly set a negative offset:
// ...
page(page: number = 1) {
  const offset = (page - 1) * this.limit;
  this.query.offset(offset);
}

I mistakenly passed 0 to this function, and my query became:
select * from `` order by Time desc limit 20 offset -20

  1. Error message
    Screenshot from 2021-03-03 11-34-10
    (sorry for the blue marks)

  2. Reduced test code:

import Knex from 'knex';

const knexConnection = Knex({
  client: 'mysql',
  connection: {
    host: '', //...
    user: '', //...
    password: '', //...
    database: '', //...
  },
});
const query = knexConnection('test').limit(20).offset(-20);

I believe if no negative offset is allowed in mysql query, the knex query-builder should prevent it. (instead of letting the error happen and generated by mysql)

If it is knex that should implement this, and this is known as fix that needs to be done in knex, I'd love to contribute to code.

@kibertoad
Copy link
Collaborator

@Ardeshir81 Considering that error thrown by driver in this case is pretty confusing, I think throwing an explicit error from Knex would be an improvement. PR would be most appreciated.

@aghArdeshir
Copy link
Contributor Author

That's good news @kibertoad . I'll start working on it. I'll ask your help if I had problem understanding things.

@aghArdeshir
Copy link
Contributor Author

I'm only familiar with mysql. Is negative offset invalid for all databases?

@aghArdeshir
Copy link
Contributor Author

I will close this issue as I think it is resolved now @kibertoad

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

No branches or pull requests

2 participants