Skip to content

SQLModel

A Python library for working with SQL databases using Python classes, built on Pydantic and SQLAlchemy.

Stars
18.5k
Forks
894
Licence
MIT
Language
Python
Last update
4 days ago
Type
Library

What it is

SQLModel lets you define database tables as Python classes with type annotations, so you write one model instead of separate SQLAlchemy and Pydantic models. Each class maps to a table and each attribute maps to a column. It is designed to work well with the FastAPI web framework.

  • Define a table such as a hero or user as a Python class and save rows to a SQLite database
  • Run select queries with a where clause and get back Python objects
  • Reuse one model as both a database table and a FastAPI request or response schema

Great for

  • Python developers building FastAPI applications that use a SQL database
  • Developers who already know SQLAlchemy or Pydantic and want less duplicated model code
  • People who want editor autocompletion and type hints when querying a database
  • Projects that start with SQLite and may later use another SQL database

Not ideal for

  • People who are new to databases and SQL, since the README points to separate documentation for those basics
  • Projects that need full low-level control over SQLAlchemy behavior, where using SQLAlchemy directly may be simpler
  • Non-Python projects, because the library is Python-only
Step-by-step guide: how to use SQLModelInstall, first run, examples, tips and FAQs

How to get it

  1. 1

    Install with a package manager

    You'll need: Python (a recent, currently supported version) · uv or pip for installing packages

    Add SQLModel to a uv project

    uv add sqlmodel
  2. 2

    Run it

    • Install SQLModel with uv add sqlmodel
    • Define a model class with table=True, as in the README's Hero example
    • Create an engine with create_engine("sqlite:///database.db")
    • Call SQLModel.metadata.create_all(engine) to create the tables
    • Add rows inside a Session and call session.commit()
  3. 3

    Get the most out of it

    • Use the Pydantic inheritance pattern to define separate read, create, and update models that share fields
    • Combine SQLModel classes with existing SQLAlchemy models, since each class is also a SQLAlchemy model
    • Use the official documentation at sqlmodel.tiangolo.com for the full tutorial

Downloads

Strengths

  • Uses one class for both database tables and data validation, which reduces duplicated code
  • Built on established libraries, Pydantic and SQLAlchemy, and designed to be compatible with both
  • Good editor support, including autocompletion and inline errors, as shown in the README

Trade-offs

  • It is a layer on top of Pydantic and SQLAlchemy, so you inherit their complexity and version constraints
  • The README is truncated and says little about advanced features or database support beyond SQLite
  • Latest release is 0.0.48, which signals a pre-1.0 version number

How it compares

ProjectStarsTypeLevelUpdatedBest for
sqlmodel (this)18.5kLibrarybeginner4 days agoPython developers building FastAPI applications that use a SQL database
sqlalchemy12.3kLibraryintermediate4 days agoPython developers building applications backed by a SQL database
pydantic29.3kLibrarybeginner4 days agoPython developers who already use type hints and want runtime checks on incoming data
peewee12kLibrarybeginner5 days agoPython developers who want a lightweight ORM instead of a large framework
sequelize30.3kLibraryintermediate5 days agoNode.js backend developers who use a relational SQL database
knex20.3kLibraryintermediate4 months agoNode.js developers who need to talk to a SQL database from application code

Stats synced from GitHub. The assessment above was drafted with AI from the project's README on 2026-10-29 and may miss recent changes — check the README before relying on a command. MIT licence: free to use, modify and distribute, including commercially, as long as you keep the copyright notice.