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)

Most upvoted comments

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.

app = FastAPI(lifespan=lifespan)
app.add_middleware(PrometheusMetricsMiddleware, service="notify")
app.mount("/metrics", make_asgi_app())

app.include_router(notify(db, auth, config))
add_pagination(app)

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:

    def read_messages_for_user(
        self,
        params: CursorParams,
        user_id: int,
    ) -> CursorPage[MessageResponse]:
        with self.db.get_session() as session:
            msgs: CursorPage[Message] = messages.read_all(
                session, user_id, params
            )

            transformed: List[Message] = [
                m.to_JSON_with_files() for m in msgs.items
            ]

        # A helper function to turn CursorPage[T] into CursorPage[U]
        return update_items(msgs, params, transformed)

Router function:

    @router.get("/v1/messages/{user_id}")
    async def get_messages_for_user(
        handler: Annotated[HandlerV1, Depends(get_handler_v1)],
        user_id: int,
        params: CursorParams = Depends(),
    ) -> CursorPage[MessageResponse]:
        return handler.read_messages_for_user(params, user_id)

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.