【问题标题】:How to get all list of id with specific properties in sqlalchemy async如何在sqlalchemy async中获取具有特定属性的所有id列表
【发布时间】:2021-09-01 00:29:47
【问题描述】:

嘿,我正在尝试学习 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() 

【问题讨论】:

    标签: python sqlalchemy asyncpg


    【解决方案1】:

    您可以使用all()retrieve all rows in a list

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

    【讨论】:

      【解决方案2】:

      正确的代码

      async def free_user():
          stmt = select(User).where(User.state == 'ON')
          async with async_session() as session:
              result = await session.execute(stmt)
              return [user.user_id for user in result.scalars()]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-03-04
        • 1970-01-01
        • 2011-01-17
        • 2021-01-07
        • 2019-04-29
        • 2016-06-15
        • 1970-01-01
        相关资源
        最近更新 更多