Ditching Boilerplate: Streamlining FastAPI with SQLModel

ArjanCodes////3 min read

Overview

Modern API development often feels like a redundant exercise in data modeling. Traditionally, developers building with find themselves writing two distinct sets of models: classes for the database schema and models for request validation and response serialization. This "double definition" creates a maintenance burden where every change must be mirrored across both systems. solves this by merging these two worlds, allowing you to define a single class that serves as both a database table and a data validation model.

Prerequisites

To follow this guide, you should be comfortable with (3.10+ recommended), basic REST API concepts, and the fundamentals of . Familiarity with asynchronous programming and type hints will help you understand the framework's internal logic.

Key Libraries & Tools

  • SQLModel: The star of the show. It sits on top of SQLAlchemy and Pydantic to unify data schemas.
  • FastAPI: The high-performance web framework used to build our API endpoints.
  • Uvicorn: An ASGI server implementation to run our application.
  • uv: A high-speed Python package installer and resolver used to manage the project environment.

Code Walkthrough

Converting a legacy SQLAlchemy setup to SQLModel drastically reduces the surface area of your code. Let's look at how we define a Hero model that acts as both our table and our schema.

from typing import Optional
from sqlmodel import Field, SQLModel, create_engine, Session, select

class Hero(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True, index=True)
    name: str
    secret_name: str
    age: Optional[int] = None

In this snippet, table=True tells SQLModel to create a database table for this class. Note how we use standard Python type hints. The Field function allows us to inject database-specific constraints like primary_key without losing Pydantic's validation power. To interact with the database in a FastAPI route, the session management becomes much cleaner:

@app.post("/heroes/", response_model=Hero)
def create_hero(hero: Hero, session: Session = Depends(get_session)):
    session.add(hero)
    session.commit()
    session.refresh(hero)
    return hero

We no longer need to map a Pydantic object to a separate SQLAlchemy object. The hero object passed into the function is already compatible with the database session.

Relationship Management

SQLModel handles complex relationships, such as many-to-many links, using a dedicated Link model. This prevents data redundancy by storing associations in a join table while keeping the object-oriented interface clean.

class HeroMissionLink(SQLModel, table=True):
    hero_id: Optional[int] = Field(default=None, foreign_key="hero.id", primary_key=True)
    mission_id: Optional[int] = Field(default=None, foreign_key="mission.id", primary_key=True)

Tips & Gotchas

While unifying models is efficient, it introduces a risk of tight coupling. If your database model is exactly the same as your API response, you might accidentally leak sensitive fields like hashed passwords or internal IDs. To prevent this, use inheritance: create a HeroBase for shared fields, then extend it into a Hero (with table=True) and a HeroPublic (for safe API responses). This gives you the best of both worlds: reduced boilerplate with explicit security boundaries.

Topic DensityMention share of the most discussed topics · 7 mentions across 7 distinct topics
14%· products
14%· products
14%· languages
14%· languages
14%· products
Other topics
29%
End of Article
Source video
Ditching Boilerplate: Streamlining FastAPI with SQLModel

SQLAlchemy vs SQLModel: Which Should You Use?

Watch

ArjanCodes // 19:50

On this channel, I post videos about programming and software design to help you take your coding skills to the next level. I'm an entrepreneur and a university lecturer in computer science, with more than 20 years of experience in software development and design. If you're a software developer and you want to improve your development skills, and learn more about programming in general, make sure to subscribe for helpful videos. I post a video here every Friday. If you have any suggestion for a topic you'd like me to cover, just leave a comment on any of my videos and I'll take it under consideration. Thanks for watching!

What they talk about
AI and Agentic Coding News
Who and what they mention most
Python
31.3%5
Python
25.0%4
Python
18.8%3
Pydantic
12.5%2
3 min read0%
3 min read