fastapi-pagination: Cursor not working with sqlalchemy
Using fastapi_pagination 0.12.34. I have tested also with 0.12.31.
Following the guidance here, I have a function:
from fastapi_pagination.cursor import CursorPage, CursorParams
from fastapi_pagination.ext.sqlalchemy import paginate
from sqlalchemy import select
from sqlalchemy.orm import Session
def read_all(
session: Session,
user_id: int,
params: CursorParams,
) -> CursorPage[Message]:
query = (
select(Message)
.filter(Message.user_id == user_id)
.order_by(Message.created.desc())
)
page: CursorPage[Message] = paginate(session, query, params)
return page
This is generating an error at the paginate function call: TypeError: Page should be used with Params
With stack:
../../../../.cache/pypoetry/virtualenvs/python-challenge-PNLqxBFX-py3.12/lib/python3.12/site-packages/fastapi_pagination/ext/sqlalchemy.py:377: in paginate
return exec_pagination(
../../../../.cache/pypoetry/virtualenvs/python-challenge-PNLqxBFX-py3.12/lib/python3.12/site-packages/fastapi_pagination/ext/sqlalchemy.py:260: in exec_pagination
return create_page(
../../../../.cache/pypoetry/virtualenvs/python-challenge-PNLqxBFX-py3.12/lib/python3.12/site-packages/fastapi_pagination/api.py:182: in create_page
return _page_val.get().create(items, **kwargs)
I’m not entirely sure why it thinks it has a Params object when the logic here clearly shows that the exec_pagination function knows that it is trying to create a CursorPage.
Any help is appreciated.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 16 (7 by maintainers)
This is working for my team as well, without
set_page, thank you very much.From my side, I’m fine with closing the issue 😊, thanks!
Pagination is being used with FastAPI.
I’m using a 3 part division of code in my microservice, router layer, handler layer and data layer. The data layer function is as above (though now with the line
set_page(CursorPage[Message])included).Handler function:
Router function:
I took out all the auth stuff for simplicity, but its basiclly just using fastapi.security to get a bearer token in the router layer, and compare the claims to the user_id to see if it is good to go.
The only thing that I can think of that is weird is that I am transforming the type of the cursor page in the handler layer. Not sure if that has some impact here.