fastapi-pagination icon indicating copy to clipboard operation
fastapi-pagination copied to clipboard

TypeError: unhashable type: 'dict' when trying to use Sqlalchemy 2.0 syntax

Open sahil-kumar-1129 opened this issue 1 year ago • 7 comments

Hi team, I am currently using fastapi-pagination v0.11.4 and trying to upgrade my sqlalchemy queries to be 2.0 compliant which means converting Query objects to Select objects. This meant I needed to use the paginate function from ext.sqlalchmey_future.py instead of ext.sqlalchemy.py. The migration was going smooth until I need to paginate some queries on table with JSON columns. Then I started receiving the error TypeError: unhashable type: 'dict' trying to return that column by passing my Select query into paginate from ext.sqlalchmey_future.py. Is there a good work around for this issue/if I upgrade the package to a certain version, will it fix this issue and still allow me to continue with my upgrade of the sqlachemy quries?

sahil-kumar-1129 avatar Nov 13 '23 20:11 sahil-kumar-1129

Hi @sahil-kumar-1129,

Could you please try to use unique=False parameter?

uriyyo avatar Nov 15 '23 13:11 uriyyo

There is no unique parameter. This is the function code:

def paginate( conn: Union[Connection, Engine, Session], query: Select, params: Optional[AbstractParams] = None, ) -> AbstractPage: params = resolve_params(params)

total = conn.scalar(select(func.count()).select_from(query.subquery()))
items = conn.execute(paginate_query(query, params))

return create_page(items.scalars().unique().all(), total, params)

We're using v0.11.4

sahil-kumar-1129 avatar Nov 15 '23 15:11 sahil-kumar-1129

You need to upgrade to latest fastapi-pagination version

uriyyo avatar Nov 15 '23 15:11 uriyyo

I believe we got this working using unique=False with sqlalchemy_future as the paginator on 0.11.4. We are working towards an upgrade path to the latest version. Thank you!

Also noting distinct() is used with the SQLAlchemy select query.

jshields avatar Nov 16 '23 14:11 jshields

Hi @sahil-kumar-1129,

If it's not possible to upgrade to newest version you can use this function:

from typing import Union, Optional, Any

from sqlalchemy import select, func
from sqlalchemy.orm import Session
from sqlalchemy.engine import Engine, Connection
from sqlalchemy.sql import Select

from fastapi_pagination import resolve_params, create_page
from fastapi_pagination.bases import AbstractParams
from fastapi_pagination.ext.sqlalchemy import paginate_query


def paginate(
    conn: Union[Connection, Engine, Session],
    query: Select,
    params: Optional[AbstractParams] = None,
    *,
    unique: bool = True,
) -> Any:
    params = resolve_params(params)

    total = conn.scalar(select(func.count()).select_from(query.subquery()))
    items = conn.execute(paginate_query(query, params))

    if unique:
        items = items.unique()

    return create_page(items.all(), total, params)

uriyyo avatar Nov 25 '23 11:11 uriyyo

Any updates? Can I close this issue?

uriyyo avatar Dec 17 '23 14:12 uriyyo

Using unique=False with sqlalchemy_future as the paginator on version 0.11.4, there is no issue. Thanks. This issue can be closed 👍

jshields avatar Jan 04 '24 22:01 jshields