r/FastAPI • u/West-Goose3582 • May 18 '26
Question Coming from ExpressJS, I love FastAPI but... do we really need two sets of models?
Coming from ExpressJS, I’ve been loving how fast and easy FastAPI is. In my Node ecosystem, I usually use Prisma with PostgreSQL, which means I don't strictly need separate models for data i/o. While I can define validation schemas (like Zod) if needed, it’s not forced on a model-by-model basis. If it's a non-mission-critical project and I know what's in the payload, I can skip validation entirely.
But in FastAPI, it feels like I'm forced to maintain two separate models: a Pydantic model for request/response validation, and a SQLAlchemy model for the database.
Does it always have to be this way? Can it just be a single model, and what is the actual industry standard here?
20
u/devatnexby May 18 '26
FastAPI didn't force you for 2 separate model, you can totally skip it for small project.
But its mostly not recommended as Python backend culture mostly lean towards separating DB models, API schemas and business logic AKA services for cleaner validation, security, and maintainability.
For small projects you can use SQLModel which let you use a single model similar to Prisma.
But most large production apps still use separate SQLAlchemy, Pydantic models because API contracts and DB structure usually grow over time. So its better to separate them in initial phase.
49
u/adiberk May 18 '26
You can use SQLModel.
But…. it is generally bad practice to have your models be directly integrated with your API. I am not saying that for initial POC or mvp it isn’t quicker. But when building something with business logic it is best avoid mixing t he database layer with the business
It is partially why I dislike sqlmodel in addition to other issues
7
u/st4reater May 18 '26
The industry standard is not tied to a language or framework.
The standard is to separate the req/res models from the data models.
What you are asking for is the ability to perform an anti pattern. Sure you may get a solution, but put some thought into why FastAPI is designed as it is
Even for quick MVP projects, it is trivial to have a DTO setup
4
u/rod_dy May 18 '26
thats funny i was trying to build the backend of my app with javascript and i hated that there was a single model. like i dont want my frontend to map to my db i just want to send a subset to the api.
3
u/amroamroamro May 18 '26
the pydantic models kinda serve two purposes: data validation obviously (both user input and returned output), this is also used by fastapi to document the openapi and generate the swagger page
the sqlalchemy models on the other hand are used for orm mapping between sql types and python
having said that, if you are coming from node and prisma, perhaps you will like this
https://prisma-client-py.readthedocs.io/en/stable/
1
u/JanGiacomelli May 18 '26
Personally, I prefer separation, as on more complex projects, the DB schema can be significantly different from the API layer schemas. But if you don't need that, just use SQLModel: https://sqlmodel.tiangolo.com/. It was built by Tiangolo as well.
1
u/KenSteel May 18 '26
We've been using SQLModel in production applications and have had a pleasant experience.
In our experience, assistive agentic tools have also been rather proficient with SQLModel, provided that we include directives of some guardrails to make sure it's using the most appropriate imports for current SQLModel best practices – since agents may sometimes look to underlying SQL Alchemy functions that are available, but usually SQLModel direct features/wrappers are what should be imported and used for all but some select circumstantial cases.
1
u/Extension-Sir-9062 May 19 '26
Absolutely! For All production projects! :) For MVPs.. maybe SQLModel, I've used it too, it's awesome but even so, schemas.py and a models.py in a simple project is not a bad idea.
1
u/maikeu May 19 '26
The sqlalchemy declarative ORM models sure do feel redundant when the only thing you're doing with then is immediately interchanging them for a corresponding pydantic model instance anyway.
For my personal projects I've started adopting the discipline of sticking with SQLalchemy core, and it feels it pays off because instead of fighting the orm , in the data layer I'm focused on getting the data I need in and out of the database.
Pydantic has me covered for converting it into well typed and serializable objects, so the orm really is just redundant.
Whereas in core, you just have namedtuple-like rows or dict-like mappings, which, if you're immediately going to cast into pydantic, well you never needed a whole declarative class to achieve that!
1
u/randomthirdworldguy May 20 '26
Your cases are for simple apps onlu
1
u/GrayWizard12345 May 21 '26
Or you can ditch ORM and rely on raw sql. More work and expertise required, but gain controll and understanding of your data layer. In fact, if you are in charge of the DB, with new projects, I'd recommend doing db first approach.
But again, it depends... For small projects that require fast deployment and minimal time to market, go for sqlmodel or two models aproach.
On projects with 20+ tables, doing raw sql is imho better. Leverage CTEs, views, triggers and so on...