sqlmodel

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

https://github.com/fastapi/sqlmodel

Science Score: 54.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
    Found CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Committers with academic emails
    1 of 93 committers (1.1%) from academic institutions
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (11.3%) to scientific vocabulary

Keywords

fastapi json json-schema pydantic python sql sqlalchemy

Keywords from Contributors

asyncio redoc asgi python-types openapi3 starlette swagger-ui uvicorn traefik tortoise-orm
Last synced: 6 months ago · JSON representation ·

Repository

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

Basic Info
Statistics
  • Stars: 16,705
  • Watchers: 139
  • Forks: 752
  • Open Issues: 137
  • Releases: 23
Topics
fastapi json json-schema pydantic python sql sqlalchemy
Created over 4 years ago · Last pushed 6 months ago
Metadata Files
Readme Contributing Funding License Citation Security

README.md

SQLModel

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

Test Publish 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:

  • Intuitive to write: Great editor support. Completion everywhere. Less time debugging. Designed to be easy to use and learn. Less time reading docs.
  • Easy to use: It has sensible defaults and does a lot of work underneath to simplify the code you write.
  • Compatible: It is designed to be compatible with FastAPI, Pydantic, and SQLAlchemy.
  • Extensible: You have all the power of SQLAlchemy and Pydantic underneath.
  • Short: Minimize code duplication. A single type annotation does a lot of work. No need to duplicate models in SQLAlchemy and Pydantic.

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

Make sure you create a virtual environment, activate it, and then install SQLModel, for example with:

```console $ pip install sqlmodel ---> 100% Successfully installed sqlmodel ```

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:

  • id
  • name
  • secret_name
  • age

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:

```Python from sqlmodel import Field, SQLModel

class Hero(SQLModel, table=True): id: int | None = Field(default=None, primarykey=True) name: str secretname: 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:

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

```Python hllines="16 19 21-25" from sqlmodel import Field, Session, SQLModel, createengine

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

hero1 = Hero(name="Deadpond", secretname="Dive Wilson") hero2 = Hero(name="Spider-Boy", secretname="Pedro Parqueador") hero3 = Hero(name="Rusty-Man", secretname="Tommy Sharp", age=48)

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

SQLModel.metadata.create_all(engine)

with Session(engine) as session: session.add(hero1) session.add(hero2) 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:

```Python hllines="13-17" from sqlmodel import Field, Session, SQLModel, createengine, select

class Hero(SQLModel, table=True): id: int | None = Field(default=None, primarykey=True) name: str secretname: 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.

Owner

  • Name: FastAPI
  • Login: fastapi
  • Kind: organization

FastAPI and friends open source projects. Created and managed by @tiangolo.

Citation (CITATION.cff)

# This CITATION.cff file was generated with cffinit.
# Visit https://bit.ly/cffinit to generate yours today!

cff-version: 1.2.0
title: SQLModel
message: >-
  If you use this software, please cite it using the
  metadata from this file.
type: software
authors:
  - given-names: Sebastián
    family-names: Ramírez
    email: tiangolo@gmail.com
identifiers:
repository-code: 'https://github.com/fastapi/sqlmodel'
url: 'https://sqlmodel.tiangolo.com'
abstract: >-
  SQLModel, SQL databases in Python, designed for
  simplicity, compatibility, and robustness.
keywords:
  - fastapi
  - pydantic
  - sqlalchemy
license: MIT

GitHub Events

Total
  • Create event: 103
  • Release event: 2
  • Issues event: 27
  • Watch event: 2,157
  • Delete event: 89
  • Issue comment event: 701
  • Push event: 229
  • Pull request review comment event: 59
  • Pull request review event: 106
  • Pull request event: 346
  • Fork event: 146
Last Year
  • Create event: 103
  • Release event: 2
  • Issues event: 27
  • Watch event: 2,157
  • Delete event: 89
  • Issue comment event: 701
  • Push event: 229
  • Pull request review comment event: 59
  • Pull request review event: 106
  • Pull request event: 346
  • Fork event: 146

Committers

Last synced: 9 months ago

All Time
  • Total Commits: 690
  • Total Committers: 93
  • Avg Commits per committer: 7.419
  • Development Distribution Score (DDS): 0.551
Past Year
  • Commits: 282
  • Committers: 25
  • Avg Commits per committer: 11.28
  • Development Distribution Score (DDS): 0.518
Top Committers
Name Email Commits
github-actions g****s@g****m 310
Sebastián Ramírez t****o@g****m 207
dependabot[bot] 4****] 47
pre-commit-ci[bot] 6****] 13
Sofie Van Landeghem s****g 8
Esteban Maya e****9@g****m 6
Alejandra 9****v 5
Jorge Alvarado a****e@g****m 4
Daniil Fajnberg 6****g 3
byrman c****n@n****l 3
Brett Cannon b****t@p****g 2
Jack Homan h****k@g****m 1
Hao Wang h****y@g****m 1
Gal Bracha g****a@g****m 1
Fedor Kuznetsov 5****t 1
Fardad13 3****3 1
Evgeniy Lupashin 3****t 1
Evangelos Anagnostopoulos a****s@w****m 1
Evan Grim e****n@m****m 1
Sebastian Marines 1****s 1
Jakob Jul Elben e****l@g****m 1
Jerry Wu j****y@y****e 1
Joe Mudryk m****e@g****m 1
Joel Pérez Izquierdo j****1@g****m 1
Jon Michaelchuck 5****k 1
Jonas Krüger Svensson j****s@h****m 1
Kian-Meng Ang k****g@g****m 1
Lehoczky Zoltán i****n@g****m 1
Leynier Gutiérrez González l****1@g****m 1
Feanil Patel f****l@t****g 1
and 63 more...

Issues and Pull Requests

Last synced: 6 months ago

All Time
  • Total issues: 122
  • Total pull requests: 554
  • Average time to close issues: over 1 year
  • Average time to close pull requests: about 2 months
  • Total issue authors: 110
  • Total pull request authors: 108
  • Average comments per issue: 7.72
  • Average comments per pull request: 1.65
  • Merged pull requests: 223
  • Bot issues: 0
  • Bot pull requests: 189
Past Year
  • Issues: 11
  • Pull requests: 406
  • Average time to close issues: about 1 month
  • Average time to close pull requests: 23 days
  • Issue authors: 10
  • Pull request authors: 54
  • Average comments per issue: 1.55
  • Average comments per pull request: 1.45
  • Merged pull requests: 143
  • Bot issues: 0
  • Bot pull requests: 158
Top Authors
Issue Authors
  • tiangolo (4)
  • clstaudt (3)
  • jd-solanki (2)
  • sorasful (2)
  • peterHoburg (2)
  • bolinocroustibat (2)
  • gregsifr (2)
  • christianholland (2)
  • Matthieu-Tinycoaching (2)
  • Udayaprasad (1)
  • regainOWO (1)
  • deZakelijke (1)
  • Goldziher (1)
  • cdwilson (1)
  • angel-langdon (1)
Pull Request Authors
  • dependabot[bot] (165)
  • tiangolo (105)
  • alissadb (29)
  • pre-commit-ci[bot] (24)
  • svlandeg (24)
  • Nimitha-jagadeesha (13)
  • bharara (11)
  • jryusuf (11)
  • ryangalamb (6)
  • bilalqv (6)
  • AlanBogarin (6)
  • alejsdev (4)
  • Foxerine (4)
  • GiorgioPorgio (4)
  • brettcannon (4)
Top Labels
Issue Labels
question (89) answered (12) investigate (11) feature (11) docs (5) bug (3) confirmed (1) polar (1) waiting (1)
Pull Request Labels
internal (245) dependencies (165) docs (140) python (132) github_actions (32) feature (25) bug (11) refactor (7) investigate (6) upgrade (4) waiting (2) breaking (1)

Packages

  • Total packages: 3
  • Total downloads:
    • pypi 5,500,885 last-month
  • Total docker downloads: 863,110
  • Total dependent packages: 195
    (may contain duplicates)
  • Total dependent repositories: 852
    (may contain duplicates)
  • Total versions: 37
  • Total maintainers: 2
pypi.org: sqlmodel

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

  • Versions: 27
  • Dependent Packages: 195
  • Dependent Repositories: 852
  • Downloads: 5,498,661 Last month
  • Docker Downloads: 863,110
Rankings
Dependent packages count: 0.2%
Stargazers count: 0.2%
Dependent repos count: 0.4%
Downloads: 0.4%
Average: 0.8%
Docker downloads count: 1.1%
Forks count: 2.4%
Maintainers (1)
Last synced: 6 months ago
pypi.org: sqlmodel_polymorphic_support

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

  • Versions: 1
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Downloads: 184 Last month
Rankings
Stargazers count: 0.5%
Forks count: 2.3%
Dependent packages count: 8.7%
Average: 15.1%
Dependent repos count: 49.0%
Maintainers (1)
Last synced: 6 months ago
pypi.org: sqlmodel-slim

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

  • Versions: 9
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Downloads: 2,040 Last month
Rankings
Stargazers count: 0.2%
Forks count: 2.3%
Dependent packages count: 10.1%
Average: 19.9%
Dependent repos count: 67.1%
Maintainers (1)
Last synced: 6 months ago

Dependencies

.github/actions/comment-docs-preview-in-pr/action.yml actions
  • Dockerfile * docker
.github/workflows/build-docs.yml actions
  • actions/cache v3 composite
  • actions/checkout v4 composite
  • actions/setup-python v4 composite
  • actions/upload-artifact v3 composite
  • dorny/paths-filter v2 composite
  • re-actors/alls-green release/v1 composite
.github/workflows/deploy-docs.yml actions
  • ./.github/actions/comment-docs-preview-in-pr * composite
  • actions/checkout v4 composite
  • cloudflare/pages-action v1 composite
  • dawidd6/action-download-artifact v2.28.0 composite
.github/workflows/issue-manager.yml actions
  • tiangolo/issue-manager 0.4.0 composite
.github/workflows/latest-changes.yml actions
  • actions/checkout v4 composite
  • docker://tiangolo/latest-changes 0.0.3 composite
  • mxschmitt/action-tmate v3 composite
.github/workflows/publish.yml actions
  • actions/cache v3 composite
  • actions/checkout v4 composite
  • actions/setup-python v4 composite
  • mxschmitt/action-tmate v3 composite
.github/workflows/smokeshow.yml actions
  • actions/setup-python v4 composite
  • dawidd6/action-download-artifact v2.28.0 composite
.github/workflows/test.yml actions
  • actions/cache v3 composite
  • actions/checkout v4 composite
  • actions/download-artifact v3 composite
  • actions/setup-python v4 composite
  • actions/upload-artifact v3 composite
  • mxschmitt/action-tmate v3 composite
  • re-actors/alls-green release/v1 composite
.github/actions/comment-docs-preview-in-pr/Dockerfile docker
  • python 3.7 build
pyproject.toml pypi
  • SQLAlchemy >=1.4.36,<2.0.0
  • pydantic ^1.8.2
  • python ^3.7
  • sqlalchemy2-stubs *