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
这是什么?SQLModel 是一个 Python 库,让你用 Python 对象来操作 SQL 数据库,而不是手写 SQL。它构建在 Pydantic 和 SQLAlchemy 之上,追求简单、兼容和稳定。
那它是一个库,不是框架?它和 SQLAlchemy、Pydantic 是什么关系?它是一个库:在 Pydantic 和 SQLAlchemy 之上的一层轻量封装。你定义的类同时是 SQLModel、SQLAlchemy 和 Pydantic 的模型,所以两者的全部能力在底层依然可用。
怎么把它加到我的项目里?安装 uv 并把 SQLModel 添加到你的项目,或者直接在虚拟环境里运行 pip install sqlmodel。Pydantic 和 SQLAlchemy 会随之自动安装。
我需要预先装好什么?只需要一个较新的、目前仍在维护的 Python 版本。SQLModel 依赖 Pydantic 和 SQLAlchemy,但安装 SQLModel 时它们会自动一起装上。
有什么需要注意的地方吗——我是不是还得学 SQLAlchemy 本身?它用合理的默认配置让代码保持简短,日常使用不需要深入掌握 SQLAlchemy。由于它只是一层轻量封装,遇到高级场景时,SQLAlchemy 和 Pydantic 的全部能力依然随时可用。
我是不是必须在开发 FastAPI 应用,用它才有意义?不是。它由 FastAPI 的作者创建,目的是简化 FastAPI 应用中的 SQL 操作,但它是一个通用的 Python 库,任何需要操作 SQL 数据库的项目都可以用。
可以在我自己的项目里免费使用吗?可以。SQLModel 是基于 MIT 许可证的开源项目,可以免费使用,包括商业用途。
在代码里定义一张表是什么样子的?你只需写一个普通的 Python 类:这个类就相当于一张 SQL 表,类的每个属性就是一列。每一行数据就是这个类的一个实例,所以你可以用常规的 Python 代码来创建和查询数据。
为什么不直接用 SQLAlchemy 加 Pydantic,而要选它?通常你得把数据定义两遍:一遍给 SQLAlchemy,一遍给 Pydantic。SQLModel 只需一份类型标注就能同时完成这两件事,在保持与两者完全兼容的同时,把重复代码降到最少。
能把现有的 SQLAlchemy 应用迁移过来吗?可以。SQLModel 的模型同时也是一个 SQLAlchemy 模型,所以它可以和你现有的 SQLAlchemy 模型配合使用,迁移应用起来很直接。
它把数据存在哪种数据库里?各种 SQL 数据库都行。README 里的快速示例是存到 SQLite 文件,而且因为底层是 SQLAlchemy,它支持的其他 SQL 数据库也都可以使用。
Creator
fastapi
Size
8.5 MB
Stars
18.4K
Age
5y
Commits · 90 Days