Python/SQLAlchemy
(스크랩 ) ★★ sqlalchemy async session execute 'where', 'order by', 'limit' (feat. Top N & Bottom N 쿼리)
산을넘는다
2022. 1. 18. 14:18
async def get_biggest_cities(session: AsyncSession) -> list[City]:
result = await session.execute(select(City).order_by(City.population.desc()).limit(20))
return result.scalars().all()
where까지 붙이면, 아래처럼 하면 될듯
async def get_biggest_cities(session: AsyncSession) -> list[City]:
result = await session.execute(select(City).where(City.name == 'seoul').order_by(City.population.desc()).limit(20))
return result.scalars().all()
https://stribny.name/blog/fastapi-asyncalchemy/