sqlmodel README

The project's README from github.com/fastapi/sqlmodel. As one markdown file: README.md.

SQLModel

SQLModel, SQL databases in Python, designed for simplicity, compatibility, and robustness.

Test Coverage Package version


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:

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:

$ 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:

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:

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:

hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson")
hero_2 = Hero(name="Spider-Boy", secret_name="Pedro Parqueador")
hero_3 = Hero(name="Rusty-Man", secret_name="Tommy Sharp", age=48)

This way, you can use conventional Python code with classes and instances that represent tables and rows, and that way communicate with the SQL database.

Editor Support

Everything is designed for you to get the best developer experience possible, with the best editor support.

Including autocompletion:

And inline errors:

Write to the Database

You can learn a lot more about SQLModel by quickly following the tutorial, but if you need a taste right now of how to put all that together and save to the database, you can do this:

from sqlmodel import Field, Session, SQLModel, create_engine


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


hero_1 = Hero(name="Deadpond", secret_name="Dive Wilson")
hero_2 = Hero(name="Spider-Boy", secret_name="Pedro Parqueador")
hero_3 = Hero(name="Rusty-Man", secret_name="Tommy Sharp", age=48)


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


SQLModel.metadata.create_all(engine)

with Session(engine) as session:
    session.add(hero_1)
    session.add(hero_2)
    session.add(hero_3)
    session.commit()

That will save a SQLite database with the 3 heroes.

Select from the Database

Then you could write queries to select from that same database, for example with:

from sqlmodel import Field, Session, SQLModel, 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")

with Session(engine) as session:
    statement = select(Hero).where(Hero.name == "Spider-Boy")
    hero = session.exec(statement).first()
    print(hero)

Editor Support Everywhere

SQLModel was carefully designed to give you the best developer experience and editor support, even after selecting data from the database:

SQLAlchemy and Pydantic

That class Hero is a SQLModel model.

But at the same time, ✨ it is a SQLAlchemy model ✨. So, you can combine it and use it with other SQLAlchemy models, or you could easily migrate applications with SQLAlchemy to SQLModel.

And at the same time, ✨ it is also a Pydantic model ✨. You can use inheritance with it to define all your data models while avoiding code duplication. That makes it very easy to use with FastAPI.

License

This project is licensed under the terms of the MIT license.

sqlmodel (fastapi/sqlmodel)

Searching Database…

18.4K ·GitHub
What is this?SQLModel is a Python library for interacting with SQL databases using Python objects instead of raw SQL. It is built on Pydantic and SQLAlchemy and designed for simplicity, compatibility, and robustness.
So it is a library, not a framework? And how does it relate to SQLAlchemy and Pydantic?It is a library: a thin layer on top of Pydantic and SQLAlchemy. The classes you define are at the same time SQLModel, SQLAlchemy, and Pydantic models, so the full power of both stays available underneath.
How do I add it to a project?Install uv and add SQLModel to your project, or simply run pip install sqlmodel inside a virtual environment. Pydantic and SQLAlchemy are installed automatically along with it.
What do I need to have installed first?Only a recent, currently supported version of Python. SQLModel requires Pydantic and SQLAlchemy, but both come automatically when you install SQLModel.
Is there a catch — do I still need to learn SQLAlchemy itself?It keeps your code short with sensible defaults, so deep SQLAlchemy knowledge is not needed for everyday use. Because it is a thin layer, the full power of SQLAlchemy and Pydantic is still there for advanced cases.
Do I have to be building a FastAPI app for this to make sense?No. It was created by the FastAPI author to simplify SQL in FastAPI apps, but it is a general Python library you can use in any project that works with a SQL database.
Can I use it in my own projects for free?Yes. SQLModel is open source under the MIT license, so it is free to use, including commercially.
What does defining a table look like in code?You write a regular Python class: the class is the equivalent of a SQL table, and each class attribute is a column. Rows are just instances of that class, so you create and query data with conventional Python code.
Why choose it over plain SQLAlchemy plus Pydantic?You normally define your data twice, once for SQLAlchemy and once for Pydantic. SQLModel needs a single type annotation to do both jobs, minimizing code duplication while staying fully compatible with each.
Can I migrate an existing SQLAlchemy app to it?Yes. A SQLModel model is also a SQLAlchemy model, so you can combine it with your existing SQLAlchemy models, which makes moving an application over straightforward.
Which database does it store data in?SQL databases in general. The README quick example saves to a SQLite file, and since SQLAlchemy sits underneath, other SQL databases it supports are reachable too.
Creator
fastapi
Size
8.5 MB
Stars
18.4K
Age
5y
Commits · 90 Days