python
type_error
ai_generated
true
AttributeError: 'AsyncSession' object has no attribute 'query'
ID: python/sqlalchemy-async-session-sync-call
80%Fix Rate
88%Confidence
0Evidence
2024-08-01First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 1.4.x | active | — | — | — |
| 2.0.x | active | — | — | — |
Root Cause
Using synchronous session methods like session.query() on an AsyncSession; async sessions require awaitable methods.
generic中文
在 AsyncSession 上使用同步会话方法如 session.query();异步会话需要使用可等待的方法。
Workarounds
-
95% success
Use SQLAlchemy 2.0 style: result = await session.execute(select(User).where(User.id == 1))
-
50% success
For legacy, use AsyncSession with sync_session_factory but that's not recommended.
Dead Ends
Common approaches that don't work:
-
100% fail
AsyncSession doesn't have a query method; you must use select().
-
90% fail
query() is not a coroutine; it doesn't exist.