首页 > 数据库 >sqlalchemy 一对多查询报错 in await_only raise exc.MissingGreenlet( sqlalchemy.exc.MissingGreenlet: gree

sqlalchemy 一对多查询报错 in await_only raise exc.MissingGreenlet( sqlalchemy.exc.MissingGreenlet: gree

时间:2023-02-20 15:55:27浏览次数:71  
标签:sqlalchemy exc models await only MissingGreenlet

报错

in await_only
    raise exc.MissingGreenlet(
sqlalchemy.exc.MissingGreenlet: greenlet_spawn has not been called; can't call await_only() here. Was IO attempted in an unexpected place? (Background on this error 
at: https://sqlalche.me/e/20/xd2s)

解决方案

https://stackoverflow.com/questions/68195361/how-to-properly-handle-many-to-many-in-async-sqlalchemy

import sqlalchemy as sa
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import selectinload
from . import models

async def get_parent_prefetch_children(db: AsyncSession, parent_id: int) -> models.Parent:
    result = await db.execute(
        sa.select(models.Parent).where(models.Parent.id == parent_id).options(
            selectinload(models.Parent.children)
        )
    )
    return result.scalar()

标签:sqlalchemy,exc,models,await,only,MissingGreenlet
From: https://www.cnblogs.com/zhuoss/p/17137709.html

相关文章