Skip to content
tezvyn:

Explain SQLAlchemy ORM vs Pydantic models in FastAPI

Source: fastapi.tiangolo.comEasyHow cards are made

Explain SQLAlchemy ORM vs Pydantic models in FastAPI

This tests separation of database schema from API contracts. A strong answer distinguishes SQLAlchemy table rows from Pydantic validation and OpenAPI generation, and notes that create schemas exclude auto-generated IDs.

What's really being asked

This question probes whether you understand layer separation in a modern Python web stack. The interviewer wants to see that you recognize SQLAlchemy belongs to the persistence layer and Pydantic belongs to the API contract layer, and that conflating them creates coupling, security gaps, and documentation bugs.

The full answer

First, the SQLAlchemy ORM model is a declarative mapping to a database table; it handles columns, relationships, indexes, and session-bound object state. Second, the Pydantic model is a data validation and serialization schema; it enforces types, constraints, and default values at the HTTP boundary and automatically generates OpenAPI documentation for the FastAPI UI. Third, a dedicated create schema is used because the client should not supply server-generated fields such as primary keys, created_at timestamps, or computed columns; the endpoint accepts the Pydantic create model, maps its fields to a new SQLAlchemy instance, commits the session, and returns a public Pydantic model that includes the generated fields. Fourth, keeping them separate lets the database schema evolve independently from the API contract; you can rename a column or add a foreign key without breaking client-facing payloads.

The mistakes people make

Claiming that SQLAlchemy and Pydantic are interchangeable or that one should be eliminated to reduce duplication. Suggesting that the ORM model can be used directly as the request body type, which leaks database internals and prevents omitting sensitive or generated fields. Asserting that code duplication is always bad without acknowledging that accidental coupling is worse. Confusing SQLModel with raw SQLAlchemy and failing to explain why FastAPI documentation relies on Pydantic types.

What usually comes next

How would you handle a case where the database column names differ from the API field names? What if you need to expose a field in the API that is computed from multiple columns? How does SQLModel change this picture? Can you use Pydantic v2 computed fields to bridge the gap? How do you handle circular dependencies between ORM relationships and Pydantic nested models?

A concrete example

Imagine a Hero table with id, name, secret_name, and age. The SQLAlchemy model maps all four columns. The Pydantic HeroCreate model includes only name and age because secret_name is internal and id is auto-incremented. The POST endpoint accepts HeroCreate, instantiates a SQLAlchemy Hero, adds it to the session, commits, and returns a HeroPublic model containing id, name, and age while omitting secret_name. This pattern prevents the client from forging an ID or reading a secret identity.

Interview question

In FastAPI, why should a POST endpoint use a dedicated Pydantic model instead of the SQLAlchemy ORM model for the request body?

  • a.Using the ORM model directly couples the API contract to the database schema and lets clients set server-generated fields like IDs.Correct
  • b.Pydantic models handle database persistence and migrations, while SQLAlchemy models validate incoming HTTP data.
  • c.SQLAlchemy models lack Python type hints, so FastAPI cannot generate OpenAPI documentation from them.
  • d.The ORM model should be reused directly to avoid code duplication between the database and API layers.
Why?

Using the ORM model as a request body leaks database internals and allows clients to forge server-generated fields like primary keys. Option D is tempting because reducing duplication seems desirable, but accidental coupling between the API contract and database schema creates larger maintenance and security risks.

Just read this? Test yourself on what you have been reading.

Read the original → fastapi.tiangolo.com

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on python — each one lists the topics its interview covers.

See open roles