SQLAlchemy Async ORM is Finally Here!

Ahmed Nafies
5 min readMay 23, 2021

[Tutorial] Here is how to work with the new async SQLAlchemy ORM

I think the whole python web development community has been waiting for this change for a while and I personally was extremely happy to hear about the long awaited async orm functionality in SQLAlchemy when the beta version was released on February 3rd in the start of this year and official release v1.4.0 on 15th of March.

Before we dive into SQLAlchemy’s async orm, there has been a bunch of async orm tools that are worth mentioning: Tortoise ORM, Pony ORM, PeeWee and Gino. While playing with these tools, I would say that Pony ORM was my favorite, specifically that its syntax was really pythonic.

Let’s install the new SQLAlchemy,

$ pip install sqlalchemy asyncpg

We will use dockerand postgres, let’s run our database in the background.

docker run \
— rm \
— name postgres \
-p 5432:5432 \
-e POSTGRES_USER=postgres \
-e POSTGRES_PASSWORD=postgres \
-e POSTGRES_DB=postgres \
-d postgres

Let’s test accessing the db shell

$ docker exec -it postgres psql -U postgres

You can exit the shell by

postgres=# \q

--

--