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

Offset query issue while using MSSQL #21

Closed
bigg01 opened this issue Jan 18, 2022 · 1 comment · Fixed by #25
Closed

Offset query issue while using MSSQL #21

bigg01 opened this issue Jan 18, 2022 · 1 comment · Fixed by #25
Labels
enhancement New feature or request

Comments

@bigg01
Copy link
Contributor

bigg01 commented Jan 18, 2022

Error while query the Model :
sqlalchemy.exc.CompileError: MSSQL requires an order_by when using an OFFSET or a non-simple LIMIT clause

Why?
MSSQL requires an order_by when using an offset.

Setup:

  • fastapi
  • MSSQL Server 2019
  • PyODBC and SqlAlchemy

Original code:

query = select(cls.model).limit(page_size).offset((page - 1) * page_size)

My change:
https://github.com/bigg01/sqladmin/blob/main/sqladmin/models.py#L262

#  sqlalchemy.exc.CompileError: MSSQL requires an order_by when using an OFFSET or a non-simple LIMIT clause
        query = select(cls.model).order_by("id").limit(page_size).offset((page - 1) * page_size)

I think in general it would make sense to add an option for ordering ?
What do you think ?

Regards

@aminalaee
Copy link
Owner

Hey, Thank you for reporting this.

Yes I think that makes sense.
Generally the LIMIT and OFFSET wouldn't be consistent if we don't use an ORDER BY with it. I think Django has a warning about that too.

Anyway, feel free to create a PR for this but we should probably use select(cls.model).order_by(cls.pk_column) instead of hard-coding the id column.

@aminalaee aminalaee added the enhancement New feature or request label Jan 19, 2022
@aminalaee aminalaee linked a pull request Jan 19, 2022 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants