SQLModel combines Pydantic and SQLAlchemy to cut code duplication.
GitHub RepoImpressions92
View on GitHub
@githubprojectsPost Author

SQLModel: The Best of Pydantic and SQLAlchemy, No Duplication

You know that feeling when you define a Pydantic model for your API request/response, then have to write a nearly identical SQLAlchemy model for your database? It's a rite of passage for Python backend devs, but it's also a real time sink.

SQLModel cuts that out. It's a library from the creator of FastAPI that merges Pydantic and SQLAlchemy into one unified model definition. You write your schema once, and it works for both data validation and database interaction.

What It Does

SQLModel is a Python library that integrates Pydantic and SQLAlchemy. Instead of maintaining two separate model classes (one for validation, one for ORM), you define a single class that inherits from SQLModel.

from sqlmodel import SQLModel, Field

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

This one class can:

  • Validate data (like Pydantic)
  • Create database tables and query rows (like SQLAlchemy)
  • Generate JSON schemas (like Pydantic)

It's not a wrapper -- it's a genuine merge. SQLModel uses Pydantic under the hood for validation and SQLAlchemy for the ORM, but combines them at the class level so you never write the same field twice.

Why It's Cool

  • Zero duplication. Your model definitions are the single source of truth. Change a field in one place, and it updates everywhere.
  • Full IDE support. Because it's pure Python classes with type hints, your editor gives you autocomplete, refactoring, and type checking out of the box.
  • Works with FastAPI seamlessly. You can use a SQLModel instance directly as a request body or response model in FastAPI endpoints. No conversion or serialization boilerplate.
  • Optional table=True. Not every model needs a database table. You can use table=False (the default) for simple data validation classes, keeping the same syntax.
  • Session handling is simple. SQLModel includes a Session and select that feel natural to anyone who's used SQLAlchemy, but with less ceremony.

How to Try It

Install it with pip:

pip install sqlmodel

Then you can define a model, create a database engine, and start querying in about 10 lines:

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

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

engine = create_engine("sqlite:///database.db")
SQLModel.metadata.create_all(engine)

with Session(engine) as session:
    hero = Hero(name="Spider-Boy", secret_name="Pedro", age=20)
    session.add(hero)
    session.commit()

Check the full docs and examples on GitHub:
https://github.com/fastapi/sqlmodel

Final Thoughts

SQLModel won't replace SQLAlchemy for complex database setups or raw flexibility. But for 90% of web applications, especially those using FastAPI, it's a clear win. Less code, fewer bugs, and faster development.

If you're tired of maintaining parallel model layers, give it a shot. You'll probably end up deleting a lot of files.


Follow us at @githubprojects for more developer tools and projects.

Back to Projects
Last updated: June 7, 2026 at 05:42 AM