Skip to content

Commit f4b1d06

Browse files
committed
Fix adding engines to SQLAlchemyPanel from AsyncSession
1 parent 4311ef5 commit f4b1d06

1 file changed

Lines changed: 15 additions & 5 deletions

File tree

debug_toolbar/panels/sqlalchemy.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from fastapi.dependencies.utils import solve_dependencies
77
from sqlalchemy import event
88
from sqlalchemy.engine import Connection, Engine, ExecutionContext
9+
from sqlalchemy.ext.asyncio import AsyncSession
910
from sqlalchemy.orm import Session
1011

1112
from debug_toolbar.panels.sql import SQLPanel
@@ -41,6 +42,12 @@ def after_execute(self, context: ExecutionContext, **kwargs: t.Any) -> None:
4142
self.add_query(str(context.engine.url), query)
4243

4344
async def add_engines(self, request: Request):
45+
def add_bind_to_engines(bind: Connection | Engine):
46+
if isinstance(bind, Connection):
47+
self.engines.add(bind.engine)
48+
else:
49+
self.engines.add(bind)
50+
4451
route = request["route"]
4552

4653
if hasattr(route, "dependant"):
@@ -55,13 +62,16 @@ async def add_engines(self, request: Request):
5562
pass
5663
else:
5764
for value in solved_result[0].values():
65+
if isinstance(value, AsyncSession):
66+
value = getattr(value, "sync_session")
5867
if isinstance(value, Session):
59-
bind = value.get_bind()
60-
61-
if isinstance(bind, Connection):
62-
self.engines.add(bind.engine)
68+
binds = getattr(value, "_Session__binds")
69+
if binds:
70+
for bind in binds.values():
71+
add_bind_to_engines(bind)
6372
else:
64-
self.engines.add(bind)
73+
bind = value.get_bind()
74+
add_bind_to_engines(bind)
6575

6676
async def process_request(self, request: Request) -> Response:
6777
await self.add_engines(request)

0 commit comments

Comments
 (0)