https://github.com/tiangolo/sqlmodel Skip to content Sign up * Why GitHub? Features - + Mobile - + Actions - + Codespaces - + Packages - + Security - + Code review - + Issues - + Integrations - + GitHub Sponsors - + Customer stories- * Team * Enterprise * Explore + Explore GitHub - Learn and contribute + Topics - + Collections - + Trending - + Learning Lab - + Open source guides - Connect with others + The ReadME Project - + Events - + Community forum - + GitHub Education - + GitHub Stars program - * Marketplace * Pricing Plans - + Compare plans - + Contact Sales - + Education - [ ] * # In this repository All GitHub | Jump to | * No suggested jump to results * # In this repository All GitHub | Jump to | * # In this user All GitHub | Jump to | * # In this repository All GitHub | Jump to | Sign in Sign up {{ message }} tiangolo / sqlmodel * Sponsor Sponsor tiangolo/sqlmodel * Notifications * Star 511 * Fork 9 SQL databases in Python, designed for simplicity, compatibility, and robustness. sqlmodel.tiangolo.com/ MIT License 511 stars 9 forks Star Notifications * Code * Issues 0 * Pull requests 2 * Actions * Projects 0 * Wiki * Security * Insights More * Code * Issues * Pull requests * Actions * Projects * Wiki * Security * Insights main Switch branches/tags [ ] Branches Tags Could not load branches Nothing to show {{ refName }} default View all branches Could not load tags Nothing to show {{ refName }} default View all tags 1 branch 2 tags Code * Clone HTTPS GitHub CLI [https://github.com/t] Use Git or checkout with SVN using the web URL. [gh repo clone tiango] Work fast with our official CLI. Learn more. * Open with GitHub Desktop * Download ZIP Launching GitHub Desktop If nothing happens, download GitHub Desktop and try again. Go back Launching GitHub Desktop If nothing happens, download GitHub Desktop and try again. Go back Launching Xcode If nothing happens, download Xcode and try again. Go back Launching Visual Studio Code Your codespace will open once ready. There was a problem preparing your codespace, please try again. Latest commit @tiangolo tiangolo Release version 0.0.3 ... af03df8 Aug 24, 2021 Release version 0.0.3 af03df8 Git stats * 39 commits Files Permalink Failed to load latest commit information. Type Name Latest commit message Commit time .github Fix CI installs and tests (#2) Aug 24, 2021 docs Release version 0.0.3 Aug 24, 2021 docs_src Add docs annotations for source examples Aug 24, 2021 scripts Fix CI installs and tests (#2) Aug 24, 2021 sqlmodel Release version 0.0.3 Aug 24, 2021 tests Format tests, remove double imports Aug 24, 2021 .flake8 Add Flake8 config Aug 24, 2021 .gitignore Add .gitignore Aug 24, 2021 CONTRIBUTING.md Add CONTRIBUTING.md Aug 24, 2021 LICENSE Add license Aug 24, 2021 README.md Add README.md Aug 24, 2021 SECURITY.md Add security policy Aug 24, 2021 mkdocs.yml Add MkDocs config Aug 24, 2021 pyproject.toml [?] Update and relax specification range for sqlalchemy-stubs (#4) Aug 24, 2021 View code [ ] SQL Databases in FastAPI Requirements Installation Example A SQL Table Create a SQLModel Model Create Rows Editor Support Write to the Database Select from the Database Editor Support Everywhere SQLAlchemy and Pydantic License 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/tiangolo/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. SQL Databases in FastAPI [6874747073] 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 (right now, Python supports versions 3.6 and above). As SQLModel is based on Pydantic and SQLAlchemy, it requires them. They will be automatically installed when you install SQLModel. Installation $ 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: from typing import Optional from sqlmodel import Field, SQLModel class Hero(SQLModel, table=True): id: Optional[int] = Field(default=None, primary_key=True) name: str secret_name: str age: Optional[int] = 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: [6874747073] And inline errors: [6874747073] 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 typing import Optional from sqlmodel import Field, Session, SQLModel, create_engine class Hero(SQLModel, table=True): id: Optional[int] = Field(default=None, primary_key=True) name: str secret_name: str age: Optional[int] = 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 typing import Optional from sqlmodel import Field, Session, SQLModel, create_engine, select class Hero(SQLModel, table=True): id: Optional[int] = Field(default=None, primary_key=True) name: str secret_name: str age: Optional[int] = 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: [6874747073] 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. About SQL databases in Python, designed for simplicity, compatibility, and robustness. sqlmodel.tiangolo.com/ Topics python json sqlalchemy sql json-schema pydantic fastapi Resources Readme License MIT License Releases 2 0.0.3 Latest Aug 24, 2021 + 1 release Sponsor this project Sponsor Learn more about GitHub Sponsors Packages 0 No packages published Languages * Python 99.0% * Other 1.0% * (c) 2021 GitHub, Inc. * Terms * Privacy * Security * Status * Docs * Contact GitHub * Pricing * API * Training * Blog * About You can't perform that action at this time. You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.