fastapi /sqlmodel
SQL databases in Python, designed for simplicity, compatibility, and robustness.
★ 18,618⑂ 898👁 128⑃ 1,163● 352▤ 49
More from this author
+9
Daily
18,618
stars
898
forks
128
watchers
1,163
prs
352
issues
49
releases
README
SQLModel, SQL databases in Python, designed for simplicity, compatibility, and robustness.
---
Documentation: https://sqlmodel.tiangolo.com
Source Code: https://github.com/fastapi/sqlmodel
---
SQLModel is a library for interacting with SQL databases from Python code, with Python objects. It is designed to be intuitive, easy to use, highly compatible, and robust.
SQLModel is based on Python type annotations, and powered by Pydantic and SQLAlchemy.
The key features are:
Intuitive to write: Great editor support. Completion everywhere. Less time debugging. Designed to be easy to use and learn. Less time reading docs.
Easy to use: It has sensible defaults and does a lot of work underneath to simplify the code you write.
Compatible: It is designed to be compatible with FastAPI, Pydantic, and SQLAlchemy.
Extensible: You have all the power of SQLAlchemy and Pydantic underneath.
Short: Minimize code duplication. A single type annotation does a lot of work. No need to duplicate models in SQLAlchemy and Pydantic.
Sponsors
SQL Databases in FastAPI
SQLModel is designed to simplify interacting with SQL databases in FastAPI applications, it was created by the same author. 😁
It combines SQLAlchemy and Pydantic and tries to simplify the code you write as much as possible, allowing you to reduce the code duplication to a minimum, but while getting the best developer experience possible.
SQLModel is, in fact, a thin layer on top of Pydantic and SQLAlchemy, carefully designed to be compatible with both.
Requirements
A recent and currently supported version of Python.
As SQLModel is based on Pydantic and SQLAlchemy, it requires them. They will be automatically installed when you install SQLModel.
Installation
First, install uv, and then add SQLModel to your project:
console
$ uv add sqlmodel
--- 100%
If you prefer to use pip, install sqlmodel inside a virtual environment. See the installation guide for the alternative steps.
Example
For an introduction to databases, SQL, and everything else, see the SQLModel documentation.
Here's a quick example. ✨
A SQL Table
Imagine you have a SQL table called hero with:
id
name
secret_name
age
And you want it to have this data:
| id | name | secret_name | age |
-----|------|-------------|------|
| 1 | Deadpond | Dive Wilson | null |
| 2 | Spider-Boy | Pedro Parqueador | null |
| 3 | Rusty-Man | Tommy Sharp | 48 |
Create a SQLModel Model
Then you could create a SQLModel model like this:
Python
from sqlmodel import Field, SQLModel
class Hero(SQLModel, table=True):
id: int | None = Field(default=None, primary_key=True)
name: str
secret_name: str
age: int | None = None
That class Hero is a SQLModel model, the equivalent of a SQL table in Python code.
And each of those class attributes is equivalent to each table column.
Create Rows
Then you could create each row of the table as an instance of the model:
Python
hero_1 = Hero(nTop issues
Discussions
#2138pytest FastAPI - InvalidRequestError collecting tests1 comments · 2026-11-13#2125Why are `select()`'s scalar TypeVars constrained rather than bounded?1 comments · 2026-11-03#2121Mr. Hong3 comments · 2026-11-01#2119-0 comments · 2026-11-01#2117Can you override what SQLModel does to convert from the Python type to the SQLAlchemy type?1 comments · 2026-10-23#2116Frozen field assignment raises ValidationError but mutates ORM state and persists on commit5 comments · 2026-10-17#2105SQLAlchemy 2.1 compatibility3 comments · 2026-09-25#2093SQLModelConfig is not publicly exported while SQLModel annotates model_config with it, rejecting public ConfigDict/dict spelling under ty4 comments · 2026-09-11
Latest release
0.0.50 0.0.50 2026-11-13SQLModel includes an official skill that helps coding agents use new features and recommended patterns for your installed version. Install it with uvx library-skills in your project. See Develop with AI. Breaking Changes 💥 Drop support for Python 3.10. PR 2139 by @tiangolo.