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
1
u/covmatty1 Jan 09 '25
But the "neat" part refers to what I said about mixins.
Imagine you have a UserOutputModel, inheriting from BaseModel and defining 10 fields that you want to return.
Your UserDbModel (terrible naming aside) then inherits from UserOutputModel and only defines the fields that aren't being returned. So they precisely don't "inherit from the same base class".
You only need to define any validation, descriptions, and anything else useful, in one place, rather than on two different variants of a class. Then if you have common fields in that UserOutputModel that are used in other places, you extract those and just mix in again.
Maybe it's not for you and that's fine, nothing wrong with just using SQLAlchemy, but it's certainly been a helpful little library for me.