How to Use Async SQLAlchemy in FastAPI

preview_player
Показать описание
In this video, I'll demonstrate how to use async Sqlalchemy in FastAPI by converting an existing sync version of Sqlalchemy to Async.

Рекомендации по теме
Комментарии
Автор

Thanks for the explanation. Illustrating a more complex query and a full CRUD example would be great.

gerardorosiles
Автор

can you help in how we can integrate alembic into this

jijojoseph
Автор

in this approach will be one Session per request?

ineps
Автор

Good overview! I agree with your distaste of the new sql alchemy approach. I've used pre 2.0 for a few years now and have found it very easy and comfortable, but I do see the benefit of this approach in verbosity. Hopefully it'll make stuff like joins and other weird query gimmicks easier than the old approach

Iamjafex
Автор

instead of doing

results = await db.execute(select(User))
users = results.scalars().all()

you can also do this if it makes it look any better

users = await db.scalars(select(User))
users = users.all()

SAsquirtle
Автор

aiomysql extension for sqlalchemy does not have async_sessionmaker! what should I use?

default_youtube_profile
Автор

Great video, thank you. What do you think about using asyncio?
result = await asyncio.gather(*tasks)

And tasks could be like:
tasks = [get_users()]

In that way you don't need to change the logic to access the DB, the responsibility for converting the get_users to async will be for asyncio. This may work for projects that already have a lot of logic using sync access to the DB so they don't need to refactor all the logic they have to access the DB.

mauricioyepes
Автор

Can we use same for production? Also what will be the driver for postgresql?

salmanmushtaq
Автор

Nice one. I am coming back to python after years of NodeJS and others lol!

Learn_with_cosmos
Автор

Is there one session per request in this example?

sampri
Автор

Is putting base.metadata.create_all method in function returning session a good approach?
Isn't it gonna run everytime you init session, which occurs on every endpoint?
And it's run_sync. Doesn't seem efficient.
I think it should be separated into a separate function and ran once on the app startup, no?

ReRubis
Автор

Great video,
Actually, I have an question.
when I use AsyncSession and call await db.execute(""" some procedures """) and that result of the procedure is multiple result sets.
so how to get several sets?

uclmpwx
Автор

Does this solution cover a rollback? get_db() -> AsyncSession, on which you will use transaction eventually. If you call flush() somewhere in the call stack and an exception is raised, this won't call a rollback, right? Thanks.

danielfajt
Автор

I understand that nobody's probably gonna answer me soon here, but I don't understand why metadata.create_all() is placed into get_db() function. Like, isn't create_all() method is should only be used once while creating database, not when accesing it?

Useroftherisingsun
Автор

Would you happen to know if using the @asynccontextmanager decorator from the built-in contextlib is of any use when creating AsyncSession objects?

According to the SQLAlchemy documentation, it seems that the async_sessionmaker is already handling the teardown.

I am having trouble deciding if I should use the asynccontextmanager or not.

CodeTesting-pdtd
Автор

Is it safe to use Aync SQLAlchemy, do we run into race conditions and Is it production safe?
Kindly reply

moneeshkumar
Автор

What's the difference using async db and sync db? like, if I want I can use SYNC sqlalchemy with ASYNC fastapi, right?

joaovictor-dlve
Автор

Can I use migrations with sqlalchemy assincronous?

joaovictor-dlve
Автор

That's weird, my AsyncSession does not have the .query()

wattsfield
Автор

Ill just stick to synchronous computing, if i wanted efficiency i would use rust and not python :)

fumano