SQLModel lets Python applications talk to SQL databases through typed classes instead of separate ORM and validation layers. One model class can represent a table row, a SQLAlchemy model, and a Pydantic schema.
Overview
SQLModel is a Python library for accessing SQL databases with ordinary classes and instances that stand in for tables and rows. It is built on type annotations and sits on top of Pydantic and SQLAlchemy, with the goal of staying intuitive, compatible, and robust.
You define models as Python classes whose attributes map to table columns, then create row objects as instances and persist or query them through a database engine and session. The documentation walks through creating tables, inserting heroes into SQLite, and selecting rows with query helpers.
The same model class acts as a SQLAlchemy model and a Pydantic model, so you can reuse definitions across validation and persistence and avoid maintaining parallel SQLAlchemy and Pydantic types. It is intended to work alongside FastAPI, Pydantic, and SQLAlchemy, including mixing with existing SQLAlchemy models or migrating SQLAlchemy apps.
Defaults and internal wiring aim to keep application code short while still exposing SQLAlchemy and Pydantic capabilities when you need them. The project emphasizes editor support such as autocompletion and inline type errors on model definitions and query results.
Install it as a package dependency with uv or pip inside a Python environment; Pydantic and SQLAlchemy are pulled in automatically. It targets current supported Python versions and is aimed especially at simplifying database access in FastAPI applications.
Key features
- Python type annotations for models
- Built on Pydantic and SQLAlchemy
- Compatible with FastAPI, Pydantic, and SQLAlchemy
- One class for ORM table and Pydantic model
- Sensible defaults with less boilerplate
- Sessions and engines for reads and writes
- Editor autocompletion and inline errors
- Works with existing SQLAlchemy models
Use cases
- SQL-backed FastAPI applications
- Mapping SQL tables to Python classes
- Sharing models between validation and persistence
- Inserting and querying rows with sessions
- Gradual migration from SQLAlchemy