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
View all comments
Show parent comment
4
u/atxgossiphound Jan 08 '25
This is why we use it. Once I started using Pydantic, I immediately looked to see how I could instrument things to work better with SQLAlchemy. Turns out SQLModel already did that and did a decent job of it.
It's an incredibly thin veneer on both Pydantic and SQLAlchmeny. If you already know both, it's easy to get around it's limitations.
One thing I did early that I don't do anymore is have every class inherit from SQLModel (the docs imply that there's no harm in doing this). If something will always just be a Python/JSON data structure and never be a SQL table, I inherit from BaseModel. We use JSON columns extensively and for longer term maintenance, it keeps the intent of each class clearer.
Is it a table? SQLModel. Is it Python/JSON? BaseModel.