fastapi

sqlmodel: Build Python Models for SQL Databases

SQLModel combines Pydantic-style data models with SQLAlchemy database mapping in one Python library. It suits FastAPI developers and Python teams who want typed models for validation and database access without maintaining duplicate definitions.

Stars
18.4k
Forks
889
Stars, last 8 days
+33

Our take

Health score

78/100

How it is scored
Activity
73
Community
89
Issues
77
Pull requests
74

+33 stars in the last 8 days

  • Closes issues faster than 83% of the projects we track
  • Releases more often than 78% of the projects we track
  • More contributors than 77% of the projects we track

Overview

SQLModel is a Python library for defining SQL database tables and working with database records through typed Python models. It builds on Pydantic and SQLAlchemy, aiming to reduce duplicated model definitions while retaining compatibility with both libraries.

It is especially relevant to FastAPI applications, where the same model can support database persistence and data validation. The project is a thin layer over established libraries, so it can also fit Python applications that use SQLAlchemy without FastAPI.

Key Features

  • Define table models and columns with Python type annotations.
  • Use models that work with both SQLAlchemy and Pydantic.
  • Create database tables and manage records through SQLModel sessions.
  • Build queries with SQLModel's select interface.
  • Retain access to SQLAlchemy and Pydantic capabilities underneath.
  • Use models with FastAPI and benefit from editor completion and inline type feedback.

Use Cases

  • FastAPI developers can use shared models for request data and database tables, reducing duplicated definitions.
  • Python developers starting a small SQL-backed application can define typed models and interact with a database through familiar Python code.
  • Teams with SQLAlchemy applications can consider SQLModel when they want a more concise model layer while keeping SQLAlchemy compatibility.
  • Developers who rely on editor completion and type annotations can use SQLModel to make database model fields clearer in their code.

Getting Started

Install with uv add sqlmodel. See the README and official documentation for setup and usage details.

Considerations

  • Requires a recent, currently supported Python version.
  • Pydantic and SQLAlchemy are required dependencies and are installed with SQLModel.
  • SQLModel builds on Pydantic and SQLAlchemy, so its design and behavior are closely tied to those libraries. Consult their documentation when you need capabilities beyond SQLModel's simplified interface.

Found this useful?

Share it with someone who would like sqlmodel.

Comparisons