首页 > 解决方案 > 如何在sqlalchemy async中获取具有特定属性的所有id列表

问题描述

嘿,我正在尝试学习 sqlalchemy,但我被困在获取状态为 ON 的所有用户 ID 的列表中。该查询在异步 sqlalchemy 中不起作用。

我试过这个,但它不起作用。

async def free_user():
    stmt = select(User).where(User.state =='ON')
    async with async_session() as session:
        result = await session.execute(stmt)
        return result.first() 

标签: pythonsqlalchemyasyncpg

解决方案


您可以使用all()检索列表中的所有行

    ...
    async with async_session() as session:
        result = await session.execute(stmt)
        return result.all() 

推荐阅读