uriyyo / fastapi-pagination

FastAPI pagination 📖
https://uriyyo-fastapi-pagination.netlify.app/
MIT License
1.1k stars 127 forks source link

columns argument to select() must be a Python list or other iterable when using sqlalchemy #964

Closed LeoQuote closed 6 months ago

LeoQuote commented 6 months ago
routers/user.py:23: in user_list
    return paginate(session.query(HelpdeskSysuser))
/usr/local/lib/python3.9/site-packages/fastapi_pagination/ext/sqlalchemy.py:221: in paginate
    return exec_pagination(query, params, conn, transformer, additional_data, subquery_count, unique, async_=False)
/usr/local/lib/python3.9/site-packages/fastapi_pagination/ext/sqlalchemy.py:101: in exec_pagination
    total = conn.scalar(count_query(query, use_subquery=subquery_count))
/usr/local/lib/python3.9/site-packages/fastapi_pagination/ext/sqlalchemy.py:67: in count_query
    return select(func.count()).select_from(query.subquery())
<string>:2: in select
    ???
<string>:2: in __init__
    ???
/usr/local/lib/python3.9/site-packages/sqlalchemy/util/deprecations.py:139: in warned
    return fn(*args, **kwargs)
/usr/local/lib/python3.9/site-packages/sqlalchemy/sql/selectable.py:3107: in __init__
    util.raise_(
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

    def raise_(
        exception, with_traceback=None, replace_context=None, from_=False
    ):
        r"""implement "raise" with cause support.

        :param exception: exception to raise
        :param with_traceback: will call exception.with_traceback()
        :param replace_context: an as-yet-unsupported feature.  This is
         an exception object which we are "replacing", e.g., it's our
         "cause" but we don't want it printed.    Basically just what
         ``__suppress_context__`` does but we don't want to suppress
         the enclosing context, if any.  So for now we make it the
         cause.
        :param from\_: the cause.  this actually sets the cause and doesn't
         hope to hide it someday.

        """
        if with_traceback is not None:
            exception = exception.with_traceback(with_traceback)

        if from_ is not False:
            exception.__cause__ = from_
        elif replace_context is not None:
            # no good solution here, we would like to have the exception
            # have only the context of replace_context.__context__ so that the
            # intermediary exception does not change, but we can't figure
            # that out.
            exception.__cause__ = replace_context

        try:
>           raise exception
E           sqlalchemy.exc.ArgumentError: columns argument to select() must be a Python list or other iterable

/usr/local/lib/python3.9/site-packages/sqlalchemy/util/compat.py:182: ArgumentError

my code:

@router.get("/api/user", response_model=Page[UserInfo])
def user_list(session: Session = Depends(get_db)):
    return paginate(session.query(HelpdeskSysuser))

add_pagination(router)

basically just copied from the sqlalchemy example

pip list:

SQLAlchemy         1.3.24
fastapi            0.105.0
fastapi-pagination 0.12.14
LeoQuote commented 6 months ago

anyone encoutered this, use

fastapi-pagination
sqlalchemy>=2.0

in your requirements.txt , instead of fastapi-pagination[sqlalchemy] , fastapi-pagnination claims to be compatible with both sqlalchemy 1. and 2. , appeareantly there're some issues with sqlalchemy 1. , using 2. would be a better choice.