python
runtime_error
ai_generated
true
sqlalchemy.exc.MissingGreenlet: greenlet_spawn 未被调用;此处无法调用 await_only()。
sqlalchemy.exc.MissingGreenlet: greenlet_spawn has not been called; can't call await_only() here.
ID: python/fastapi-sqlalchemy-async-session-not-bound
80%修复率
85%置信度
0证据数
2024-11-18首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.x | active | — | — | — |
根因分析
在异步会话之外的 greenlet 上下文中对 AsyncSession 的关系进行延迟加载,例如在模板渲染或同步代码中。
English
Lazy-loading a relationship on an AsyncSession outside an awaited greenlet context, e.g. in template rendering or sync code.
解决方案
-
94% 成功率
from sqlalchemy.orm import selectinload result = await session.execute(select(User).options(selectinload(User.posts)))
-
88% 成功率
await session.refresh(user, ['posts'])
无效尝试
常见但无效的做法:
-
80% 失败
Dynamic still requires I/O; the greenlet error persists.
-
85% 失败
Blocks the event loop and breaks FastAPI async handling.