FastAPI & SQLModel - Database Interaction in FastAPI apps with SQLModel

In this video, we refactor the Pydantic models from the previous video into SQLModel classes that can interact with our database and define the schema of our tables.
We'll see how to re-write our FastAPI handler functions to use this database.
▶️ Playlist: • FastAPI - Complete Cou...
☕️ 𝗕𝘂𝘆 𝗺𝗲 𝗮 𝗰𝗼𝗳𝗳𝗲𝗲:
To support the channel and encourage new videos, please consider buying me a coffee here:
ko-fi.com/bugbytes
📌 𝗖𝗵𝗮𝗽𝘁𝗲𝗿𝘀:
00:00 Intro
00:35 SQLModel intro
05:18 Create relationships between SQLModel classes
08:18 Creating database connection
13:04 Using database in FastAPI handler functions
▶️ Full Playlist:
• FastAPI - Complete Cou...
𝗦𝗼𝗰𝗶𝗮𝗹 𝗠𝗲𝗱𝗶𝗮:
📖 Blog: bugbytes.io/posts/
👾 Github: github.com/bugbytes-io/
🐦 Twitter: / bugbytesio
📚 𝗙𝘂𝗿𝘁𝗵𝗲𝗿 𝗿𝗲𝗮𝗱𝗶𝗻𝗴 𝗮𝗻𝗱 𝗶𝗻𝗳𝗼𝗿𝗺𝗮𝘁𝗶𝗼𝗻:
SQLModel: sqlmodel.tiangolo.com/
FastAPI Lifespan Event: fastapi.tiangolo.com/advanced...
#python #fastapi #webdevelopment

Пікірлер: 34

  • @dixon1e
    @dixon1eАй бұрын

    This is incredibly well produced and deeply informative. Thank you.

  • @bugbytes3923

    @bugbytes3923

    Ай бұрын

    Thanks a lot, delighted to hear that! Cheers!

  • @opticonor
    @opticonor2 ай бұрын

    Lots of value in this video, thanks!!

  • @bugbytes3923

    @bugbytes3923

    2 ай бұрын

    Thanks a lot!

  • @djtoon8412
    @djtoon8412Ай бұрын

    also setting up with postgres docker and traefik .Also an additional content can be sending emails and show how to create email templates

  • @AmoahDevLabs
    @AmoahDevLabs2 ай бұрын

    great one.

  • @bugbytes3923

    @bugbytes3923

    2 ай бұрын

    Thanks as always!

  • @adhd_arti
    @adhd_arti2 ай бұрын

    Best content!

  • @bugbytes3923

    @bugbytes3923

    2 ай бұрын

    Thanks a lot!

  • @thierryyolepiot9951
    @thierryyolepiot99518 күн бұрын

    Can you show hoz to set up FastAPI with MySQL and PostGreSQL (especially with the async features)?

  • @kushaldotel3147
    @kushaldotel31472 ай бұрын

    Love from Nepal❤

  • @bugbytes3923

    @bugbytes3923

    2 ай бұрын

    Thanks a lot!

  • @ayushshende4290
    @ayushshende4290Ай бұрын

    A video on env config and file structure for bigger projects would also be helpful

  • @recaseng
    @recasengАй бұрын

    Please do more on the crud operations as well as error handlings please

  • @juvewan
    @juvewan2 ай бұрын

    Endpoint functions are defined as `async def`, but the db operations inside are all sync, including the get_session depency. This is bad in a real project. async def endpoints are running in an event loop, time cosuming db operations are not awaited, so they will block the event loop.

  • @zoxxar

    @zoxxar

    2 ай бұрын

    can you give an example of changing a DB operation to support async?

  • @tascsolutions6483
    @tascsolutions6483Ай бұрын

    Great content! Can you please explain why some tutorials use pydantic models(schemas) AND sql models (models)? I find this confusing and causing issues.

  • @matis9783

    @matis9783

    Ай бұрын

    Maybe single responsibility principle

  • @TOn-fx2gr

    @TOn-fx2gr

    27 күн бұрын

    That's how it worked in the paste but then the developer of fastapi created sqlmodel which use pydantic and sqlalchemy under the hood to make our life much easier ,i think most people in youtube are not aware that sqlmodel exist

  • @djtoon8412
    @djtoon84122 ай бұрын

    can we also get one for microservices using gRPC

  • @farzadmf
    @farzadmf2 ай бұрын

    Instead of converting the type to a string, you can do `from __future__ import annotations`

  • @catchychazz
    @catchychazz2 ай бұрын

    Is SQLModel still necessary with SQLAlchemy 2.0?

  • @beefbox
    @beefbox2 ай бұрын

    Hey, do you consider making an unpoly video? I feel like the library is so underrated.

  • @bugbytes3923

    @bugbytes3923

    2 ай бұрын

    It has been on the list for a while, yeah. I'll try and get that done soon.

  • @gerrior
    @gerrior22 күн бұрын

    Timestamp: 18:30. Even after adding `None` to the `band_id` at runtime I'm getting "pydantic_core: 1 validation error for AlbumBase" "band_id missing". I am using Progres instead of SQLite.

  • @fernandohtr

    @fernandohtr

    17 күн бұрын

    I also had a problem around this video time. The error returned was not so clear. To solve the problem in my case, I had to transform the "album.release_date" from string to datetime.date. It was something more or less like this: if band_data.albums: for album in band_data.albums: raw_album_date = album.release_date.split("-") year = int(raw_album_date[0]) month = int(raw_album_date[1]) day = int(raw_album_date[2]) album_date = date(year, month, day) album_obj = Album( title=album.title, release_date=album_date, band=band ) session.add(album_obj)

  • @Ceddybaer

    @Ceddybaer

    15 күн бұрын

    I ran into the same issue on SQLite. I fixed it by adding "default=None" to the parameters of Field().

  • @thierryyolepiot9951

    @thierryyolepiot9951

    8 күн бұрын

    @@Ceddybaer I added "default=None" to the parameters of Field() on line 23, ie band_id: int | None = Field(default=None, foreign_key="band.id") But the error still remains

  • @alexandrodisla6285
    @alexandrodisla62852 ай бұрын

    restAPI,sqlmodel,alembic. docker .compose and metrics

  • @bugbytes3923

    @bugbytes3923

    2 ай бұрын

    Alembic + FastAPI + SQLModel early this week! Thanks.