sqladmin icon indicating copy to clipboard operation
sqladmin copied to clipboard

Add select_from to count query in models.py

Open estyrke opened this issue 1 month ago • 0 comments

This prevents crashes in multi-bind scenarios:

session_maker = sessionmaker(
    binds={Base: engine, BorderBase: border_engine},
    expire_on_commit=False,
)
admin = Admin(
    app,
    base_url="/admin",
    session_maker=session_maker,
)
Traceback (most recent call last):
[...]
  File ".venv/lib/python3.12/site-packages/sqladmin/authentication.py", line 72, in wrapper_decorator
    return await func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.12/site-packages/sqladmin/application.py", line 453, in list
    pagination = await model_view.list(request)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.12/site-packages/sqladmin/models.py", line 851, in list
    count = await self.count(request)
            ^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.12/site-packages/sqladmin/models.py", line 826, in count
    rows = await self._run_query(stmt)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.12/site-packages/sqladmin/models.py", line 765, in _run_query
    return await anyio.to_thread.run_sync(self._run_query_sync, stmt)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.12/site-packages/anyio/to_thread.py", line 56, in run_sync
    return await get_async_backend().run_sync_in_worker_thread(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.12/site-packages/anyio/_backends/_asyncio.py", line 2485, in run_sync_in_worker_thread
    return await future
           ^^^^^^^^^^^^
  File ".venv/lib/python3.12/site-packages/anyio/_backends/_asyncio.py", line 976, in run
    result = context.run(func, *args)
             ^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.12/site-packages/sqladmin/models.py", line 756, in _run_query_sync
    result = session.execute(stmt)
             ^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.12/site-packages/sqlalchemy/orm/session.py", line 2351, in execute
    return self._execute_internal(
           ^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.12/site-packages/sqlalchemy/orm/session.py", line 2237, in _execute_internal
    bind = self.get_bind(**bind_arguments)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.12/site-packages/sqlalchemy/orm/session.py", line 2831, in get_bind
    raise sa_exc.UnboundExecutionError(
sqlalchemy.exc.UnboundExecutionError: Could not locate a bind configured on SQL expression or this Session.

estyrke avatar Dec 02 '25 09:12 estyrke