Opinionated best practices and conventions for building production FastAPI applications, drawn from startup experience.
fastapi/sqlmodel
SQLModel is a Python library for working with SQL databases using Python classes, built on Pydantic and SQLAlchemy and designed to pair with FastAPI.
- Stars
- 18,447
- Forks
- 893
- Open issues
- 53
- Last commit
- 7 days ago
What is it?
SQLModel lets you define database tables as Python classes using type annotations. A single class serves as both a SQLAlchemy table model and a Pydantic data model, so you don't have to write the same schema twice for database access and request or response validation.
The library is a thin layer over Pydantic and SQLAlchemy and keeps both APIs available. You can query with a select-based syntax, run sessions, and use SQLAlchemy features underneath when needed. Table creation is done through the standard SQLModel metadata, and the examples in the project use SQLite.
SQLModel was created by the author of FastAPI and is designed to be compatible with it. Installation is via pip or uv, and the project is MIT licensed.
Key features
- 01Defines tables and Pydantic models with one Python class using type annotations.
- 02Provides select-based queries and sessions built on SQLAlchemy.
- 03Works with FastAPI for request and response models.
- 04Keeps full access to underlying SQLAlchemy and Pydantic features.
- 05Offers editor autocompletion and inline type errors.
Use cases
- Building a FastAPI service that reads and writes a SQL database.
- Reducing duplicated model definitions between ORM tables and API schemas.
- Migrating an existing SQLAlchemy application toward Pydantic-based models.
Quick start
- 1
Install uv from the official installation guide
- 2
Add SQLModel to your project
uv add sqlmodel
Topics
FAQ
- What is sqlmodel?
- SQLModel is a Python library for working with SQL databases using Python classes, built on Pydantic and SQLAlchemy and designed to pair with FastAPI. SQLModel lets you define database tables as Python classes using type annotations. A single class serves as both a SQLAlchemy table model and a Pydantic data model, so you don't have to write the same schema twice for database access and request or response validation.
- Is sqlmodel free to use? What license does it have?
- sqlmodel is open source under the MIT license. Check the license terms before using it in commercial products.
- Is sqlmodel still maintained?
- Actively maintained. The last commit to sqlmodel was on Oct 15, 2026.
- What can I use sqlmodel for?
- Building a FastAPI service that reads and writes a SQL database. Reducing duplicated model definitions between ORM tables and API schemas. Migrating an existing SQLAlchemy application toward Pydantic-based models.
- How do I install sqlmodel?
- Install uv from the official installation guide Add SQLModel to your project: uv add sqlmodel
- Where can I get sqlmodel?
- The source code and installation instructions are on GitHub: https://github.com/fastapi/sqlmodel
Summary generated by AI from the project's README. GitHub stats refresh daily.
Related projects
TypeScript ORM for Node.js that gives type-safe database access across PostgreSQL, MySQL, SQLite, MongoDB and other databases.
A full-featured ORM for Go that maps structs to database tables and supports associations, hooks, transactions and migrations.
TypeScript ORM for Node.js built on Data Mapper, Unit of Work and Identity Map patterns, supporting SQL and MongoDB.
A Python library that constrains LLM outputs during generation so they match a given type, regex, JSON schema, or grammar.
A Python web framework for building APIs with standard type hints, offering automatic validation, OpenAPI docs and high performance.