본문 바로가기

Python/SQLAlchemy

(스크랩 ) ★★ sqlalchemy async session execute 'where', 'order by', 'limit' (feat. Top N & Bottom N 쿼리)

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/

 

Async SQLAlchemy with FastAPI

Before we look at the example, there are some important information about the new SQLAlchemy 1.4 release: In this post I will use the new async capabilities of the ORM layer, together with the new 2.0 style queries. We will create a simple FastAPI applicat

stribny.name