Databases With SQLModel (Connection, Lifespan Events, And Models) - FastAPI Beyond CRUD (Part 5)

preview_player
Показать описание
In this video, we connect our application to a PostgreSQL database using SQLModel as well as an async DB API (Asyncpg). We look at different concepts involved like creating a database model, choosing when to connect to that database, and so on. We look at FastAPI lifespan events that can help us set up the database connection at the start of the application.
#fastapi #sqlmodel #python #apidevelopment

SOURCE CODE:

COURSE WEBSITE:

PLEASE DONATE TO THE CHANNEL THROUGH:
BUY ME A COFFEE:

PATREON:

You can also consider Becoming a member of this channel.

FOLLOW ME:

X:

GITHUB:

Timestamps:
(00:00:00) Introduction
(00:04:05) Creating a database
(00:05:59) Environment variables with Pydantic Settings
(00:15:10) Create the engine object
(00:20:20) Lifespan events
(00:26:40) Connect to database using lifespan event
(00:31:42) Create a database model
(00:43:17) Create models in database
(00:47:34) Confirm tables in database
(00:48:49) Conclusion
Рекомендации по теме
Комментарии
Автор

Hi guys, I later on realized that there is a mistake I made at 41:22. Where I forgot to add sa_column. so it should be sa_column=Column(pg.TIMESTAMP, default=datetime.now)

Also change the primary_key to have the default being uuid.uuid4 instead of uuid.uuid4()

SsaliJonathan
Автор

This is an amazing video sir, they are very clear and amazing sir, thank you so much sir for this

KayongoJohnsonBrian-ys
Автор

This is the simplest fastapi video I have seen. Very detailed and straight to the point.

davidessien
Автор

those who are facing problems with neondb, remove the sslmode=required parameter at the end of the connection string, it worked for me, from my understanding we are not handling ssl in the create_engine function so it was throwing an error,
also don't forget to add asyncpg to the start of the neondb url as shown in the video.
These videos are great, loving them so far

gadzrux
Автор

bro your accent is awesome when you are saying "our" just come to my ears like el agua in spanish

hallosf
Автор

Suivre tes tutorials est un vraiment instructif.

Pense à nous présenter des astuces concernant l'intégration de FASTAPI avec DOCKER + FASTAPI with NoSQL database

Bonne chance pour la suite.

suen-tech
Автор

great video, took twice as long setting this up to work async with sqlite db instead of postgress but great work, one question why not use factory_default instead of default?

zach-ee
Автор

Cool tutorial, well ordered, coherent and well presented! Just one question though, my Python version is 3.12.2 and says Books (from src.books.models import Books ) is unused in async def init_db(): I think I agree. The line "await is magic because it does not explicitly refer to our Books model. I think it should.

densondube
Автор

at 46:47 the init_db function is producing error, and application startup failed again and again, what to do?

ShashankSingh-ljfd
Автор

Interesting video as always.
My question is, won't the database be overwritten each time the server starts or add/remove a field in the model? Or is it that under the hood, it's preforming migration like alembic?

TheAremoh
Автор

using sqlalchemy also we define table model and response model, what is the benefit of using sqlmodel. I think even syntax of sqlalchemy model is easier to user and remember.

thelifehackerpro
Автор

What exactly is the difference between the async approach of working with the db and the sync approach of working with the db, I've seen both of them on the Internet

ayushshende
Автор

What is the use of life_span function in our case, its just checking db connectivity?

thelifehackerpro
Автор

Great series of tutorial, I am facing an error while connecting to DB using lifespan event, I'm getting error like: "The asyncio extension requires an async driver to be used. The loaded 'psycopg2' is not async."

ObserverGuy
Автор

Bro make one video of fastapi, llm, docker production grade development

devdeee
Автор

Using SqlAlchemy also we create model class as table, and response model, then why use sqlmodel? benefits?

thelifehackerpro
Автор

How can I have psql in my termninal ? do I need to install something ?

sahilkatle
Автор

and please tell what username and password we have to use in database url, is it the same as our localsystem one's or we have to create on postgres

ShashankSingh-ljfd
Автор

Hi Jonathan! Thanks for the awesome series — it's been super helpful so far. 🙌
Quick question around the 20-minute mark during the “Create the engine object” section:
You wrote:
engine = AsyncEngine(create_engine(url=Config.DATABASE_URL, echo=True))
Just wondering — why did you choose to manually wrap create_engine() in AsyncEngine, instead of using the built-in async version like:
from sqlalchemy.ext.asyncio import create_async_engine
engine = create_async_engine(Config.DATABASE_URL, echo=True)
Is there a specific reason or benefit to doing it your way?

Thanks again for the great content — learning a lot!

aminmirzakuchaki
Автор

I am getting error
raise AsyncConnection context has not been started and object has not been awaited
what to do in this?

HabibUrRahman-muzf
visit shbcf.ru