{"name":"SQLModel: Simplifying SQL Databases in Python with Pydantic and SQLAlchemy","description":"SQLModel is a Python library designed for intuitive, compatible, and robust interaction with SQL databases. Built on Pydantic and SQLAlchemy, it streamlines database operations, especially within FastAPI applications, by leveraging Python type annotations. It aims to minimize code duplication and enhance developer experience with excellent editor support.","github":"https://github.com/fastapi/sqlmodel","url":"https://osrepos.com/repo/fastapi-sqlmodel","source":"osrepos.com","sourceDescription":"This repository profile is provided by osrepos.com, an open source repository discovery platform.","repositoryProfile":"https://osrepos.com/repo/fastapi-sqlmodel","generatedFor":"open source discovery and AI-assisted research","markdown":"https://osrepos.com/repo/fastapi-sqlmodel.md","json":"https://osrepos.com/repo/fastapi-sqlmodel.json","topics":["Python","FastAPI","SQL","SQLAlchemy","Pydantic","ORM","Database","JSON"],"keywords":["Python","FastAPI","SQL","SQLAlchemy","Pydantic","ORM","Database","JSON"],"stars":null,"summary":"SQLModel is a Python library designed for intuitive, compatible, and robust interaction with SQL databases. Built on Pydantic and SQLAlchemy, it streamlines database operations, especially within FastAPI applications, by leveraging Python type annotations. It aims to minimize code duplication and enhance developer experience with excellent editor support.","content":"## Introduction\n\nSQLModel is a powerful Python library for interacting with SQL databases, designed for simplicity, compatibility, and robustness. It builds upon the strengths of Pydantic for data validation and SQLAlchemy for database interaction, offering a seamless experience for developers. Created by the author of FastAPI, SQLModel is particularly well-suited for FastAPI applications, aiming to reduce code duplication and enhance developer experience.\n\nKey features of SQLModel include:\n\n*   **Intuitive to write**: Excellent editor support with autocompletion everywhere, reducing debugging time and making it easy to learn.\n*   **Easy to use**: Sensible defaults simplify the code you write, handling much of the underlying complexity.\n*   **Compatible**: Designed for high compatibility with FastAPI, Pydantic, and SQLAlchemy.\n*   **Extensible**: Provides access to the full power of SQLAlchemy and Pydantic when needed.\n*   **Short**: Minimizes code duplication, allowing a single type annotation to perform extensive work without needing separate SQLAlchemy and Pydantic models.\n\n## Installation\n\nTo get started with SQLModel, ensure you have a virtual environment set up. You can install it using pip:\n\nconsole\npip install sqlmodel\n\n\n## Examples\n\nSQLModel simplifies common database operations. Here are some quick examples to illustrate its usage.\n\n### Create a SQLModel Model\n\nDefine your database table structure using a Python class that inherits from `SQLModel` and sets `table=True`. Each class attribute corresponds to a table column.\n\npython\nfrom sqlmodel import Field, SQLModel\n\n\nclass Hero(SQLModel, table=True):\n    id: int | None = Field(default=None, primary_key=True)\n    name: str\n    secret_name: str\n    age: int | None = None\n\n\n### Create Rows\n\nCreate instances of your SQLModel class to represent rows in your table.\n\npython\nhero_1 = Hero(name=\"Deadpond\", secret_name=\"Dive Wilson\")\nhero_2 = Hero(name=\"Spider-Boy\", secret_name=\"Pedro Parqueador\")\nhero_3 = Hero(name=\"Rusty-Man\", secret_name=\"Tommy Sharp\", age=48)\n\n\n### Write to the Database\n\nCombine your model definitions and instances with an engine and session to persist data to a database. This example uses SQLite.\n\npython\nfrom sqlmodel import Field, Session, SQLModel, create_engine\n\n\nclass Hero(SQLModel, table=True):\n    id: int | None = Field(default=None, primary_key=True)\n    name: str\n    secret_name: str\n    age: int | None = None\n\n\nhero_1 = Hero(name=\"Deadpond\", secret_name=\"Dive Wilson\")\nhero_2 = Hero(name=\"Spider-Boy\", secret_name=\"Pedro Parqueador\")\nhero_3 = Hero(name=\"Rusty-Man\", secret_name=\"Tommy Sharp\", age=48)\n\n\nengine = create_engine(\"sqlite:///database.db\")\n\n\nSQLModel.metadata.create_all(engine);\n\nwith Session(engine) as session:\n    session.add(hero_1)\n    session.add(hero_2)\n    session.add(hero_3)\n    session.commit()\n\n\n### Select from the Database\n\nQuery data from your database using the `select` function and session. SQLModel ensures you retain excellent editor support even after retrieving data.\n\npython\nfrom sqlmodel import Field, Session, SQLModel, create_engine, select\n\n\nclass Hero(SQLModel, table=True):\n    id: int | None = Field(default=None, primary_key=True)\n    name: str\n    secret_name: str\n    age: int | None = None\n\n\nengine = create_engine(\"sqlite:///database.db\")\n\nwith Session(engine) as session:\n    statement = select(Hero).where(Hero.name == \"Spider-Boy\")\n    hero = session.exec(statement).first()\n    print(hero)\n\n\n## Why Use SQLModel\n\nSQLModel offers several compelling reasons for Python developers, especially those working with FastAPI:\n\n*   **Unified Models**: It acts as both a SQLAlchemy model and a Pydantic model, eliminating the need to define separate models for database interaction and data validation/serialization. This significantly reduces code duplication.\n*   **Exceptional Developer Experience**: Leveraging Python type annotations, SQLModel provides excellent editor support, including autocompletion and inline error checking, both when defining models and when querying data.\n*   **FastAPI Integration**: Designed by the creator of FastAPI, SQLModel provides a natural and highly compatible way to integrate SQL databases into FastAPI applications.\n*   **Robust and Extensible**: While simplifying common tasks, it retains the full power and extensibility of SQLAlchemy and Pydantic underneath, allowing for complex scenarios when needed.\n\n## Links\n\nFor more detailed information and comprehensive guides, refer to the official documentation and source code:\n\n*   [Official Documentation](https://sqlmodel.tiangolo.com){:target=\"_blank\"}\n*   [Source Code on GitHub](https://github.com/fastapi/sqlmodel){:target=\"_blank\"}","metrics":{"detailViews":2,"githubClicks":1},"dates":{"published":null,"modified":"2025-12-08T20:00:56.000Z"}}