python type_error ai_generated true

AttributeError: 'AsyncSession' object has no attribute 'query'

ID: python/sqlalchemy-async-session-sync-call

Also available as: JSON · Markdown · 中文
80%Fix Rate
88%Confidence
0Evidence
2024-08-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
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

  1. 95% success
    Use SQLAlchemy 2.0 style: result = await session.execute(select(User).where(User.id == 1))
  2. 50% success
    For legacy, use AsyncSession with sync_session_factory but that's not recommended.

Dead Ends

Common approaches that don't work:

  1. 100% fail

    AsyncSession doesn't have a query method; you must use select().

  2. 90% fail

    query() is not a coroutine; it doesn't exist.