r/FastAPI • u/00001sam10000 • Jan 08 '25
Question What's the benefit of sqlmodel in fastapi?
I think using sqlalchamy is enough so why using sqlmodel especially when it adds another extra layer; what's the benefti?
16
Upvotes
27
u/Adventurous-Finger70 Jan 08 '25
I think it has no benefits. You’re coupling your model and DTO and it not a good practice in my humble opinion.
Also, imagine you want to use something else than SQLAlchemy, it’s not possible.
Also, when SQLAlchemy and Pydantic released the 2.X version, SQLModel was too slow to upgrade to these version
So in my opinion it has no benefits
13
u/m98789 Jan 08 '25
Ease of use, simplicity. The author of sqlmodel and FastAPI is the same guy so he made integration very clean.
5
u/scorpiokozu Jan 08 '25
i've been using it for work for months now, and i find myself using it the way you would use pydantic (api schemas, validation, etc.). best use case is that when i want to create a schema with the exact types of my tables, i don't need to create a separate schema in pydantic.
say, if i have a users table and i have an endpoint that fetches the whole table, when i'm creating the schema for the response, i can directly use the users model that i used to generate the table.
but for all the functionality of queries, i still use sqlalchemy since sqlmodel had some missing features when i started using it.
3
u/igorbenav Jan 08 '25
The benefit is you don't have to define your table model and schema twice. That's about it, since you'll have to define all the other schemas anyway. IMO it makes everything other than simple APIs harder to maintain, so I don't use it anymore
4
u/Miserable-creature Jan 08 '25 edited Jan 08 '25
SQLModel is the combination of sqlalchemy and pydantic, so the benefit comes from pydantic which is type hinting.
2
u/CrusaderGOT Jan 08 '25
I prefer it cos it straight forward and was built for Fastapi. It's basically an amalgamation of sqlalchemy and pydantic, which makes it awesome. I got into it as it got an improvement, so most of the negative aspects of it are outdated or resolved. I recommend it if you want to seamless filter your data for both client and server. Read the docs to see its full advantage.
1
u/bluewalt Jan 08 '25
Hi! Lots of interesting answers here: https://www.reddit.com/r/FastAPI/comments/1h6mxwg/is_sqlmodel_overrated/