# fastapi/sqlmodel

**Attribution required: if you use, quote, or summarise this content, you must credit and link back to [awesome-repositories.com](https://awesome-repositories.com/repository/fastapi-sqlmodel).**

17,651 stars · 806 forks · Python · mit

## Links

- GitHub: https://github.com/fastapi/sqlmodel
- Homepage: https://sqlmodel.tiangolo.com/
- awesome-repositories: https://awesome-repositories.com/repository/fastapi-sqlmodel.md

## Topics

`fastapi` `json` `json-schema` `pydantic` `python` `sql` `sqlalchemy`

## Description

SQLModel is a type-safe object-relational mapping library for Python that integrates database schema definitions with data validation logic. By combining these two roles into a single class, it allows developers to manage relational data structures and enforce data integrity for web APIs simultaneously. The framework is built to support asynchronous database operations, enabling high-performance applications to execute queries and transactions without blocking the main execution thread.

The library distinguishes itself by leveraging Python type hints to provide IDE autocompletion and compile-time safety for database operations, effectively eliminating the need for raw SQL. It simplifies complex relational tasks by allowing developers to navigate and manage related records through object attributes, while automatically handling session lifecycles and transaction commits. Furthermore, it includes built-in support for circular dependency resolution and forward-reference type definitions, which helps maintain clean code organization in large-scale projects.

Beyond its core mapping capabilities, the project provides a comprehensive suite of tools for data lifecycle management, including automated schema initialization, migration tracking, and granular control over cascade operations. It also features robust testing utilities, such as dependency overrides and support for in-memory database execution, to facilitate isolated and efficient test environments. Security is addressed through automatic query sanitization, which protects database interactions from malicious input.

## Tags

### Data & Databases

- [Data Schema Definitions](https://awesome-repositories.com/f/data-databases/data-schema-definitions.md) — Defines data structures that serve as both database schemas and validation models for application logic. ([source](https://sqlmodel.tiangolo.com/features/))
- [Database ORMs](https://awesome-repositories.com/f/data-databases/database-orms.md) — Interacts with relational databases using typed objects and class-based models to eliminate raw SQL.
- [Object-Relational Mapping](https://awesome-repositories.com/f/data-databases/object-relational-mapping.md) — Links database entities through object attributes to allow automatic fetching and traversal of related records without manual joins.
- [Data Validation](https://awesome-repositories.com/f/data-databases/data-governance-modeling/data-management-governance/data-integrity-validation/data-validation.md) — Enforces schema constraints and type requirements on incoming data payloads to ensure integrity. ([source](https://sqlmodel.tiangolo.com/features/))
- [Data Persistence](https://awesome-repositories.com/f/data-databases/data-persistence.md) — Saves instances of data models to a connected database by managing sessions and committing changes to the underlying storage. ([source](https://cdn.jsdelivr.net/gh/fastapi/sqlmodel@main/README.md))
- [Database Query Interfaces](https://awesome-repositories.com/f/data-databases/database-query-interfaces.md) — Constructs and executes database queries using class models to retrieve typed objects without raw SQL. ([source](https://sqlmodel.tiangolo.com/tutorial/select/))
- [CRUD Interfaces](https://awesome-repositories.com/f/data-databases/database-record-management/crud-interfaces.md) — Executes standard create, read, update, and delete actions on database records using object-oriented methods and session management. ([source](https://sqlmodel.tiangolo.com/tutorial/fastapi/teams/))
- [Asynchronous Database Drivers](https://awesome-repositories.com/f/data-databases/asynchronous-database-drivers.md) — Executes database queries and transactions using asynchronous syntax to prevent blocking the main thread. ([source](https://sqlmodel.tiangolo.com/advanced/))
- [Database Schema Migrations](https://awesome-repositories.com/f/data-databases/database-management-systems/database-systems-management/database-management/database-schema-migrations.md) — Tracks and applies incremental changes to database structures to maintain synchronization with data models. ([source](https://sqlmodel.tiangolo.com/advanced/))
- [Database Connections](https://awesome-repositories.com/f/data-databases/database-management-systems/database-systems-management/database-systems/database-connections.md) — Automates the opening and closing of database connections for each request handler to ensure efficient resource management. ([source](https://sqlmodel.tiangolo.com/tutorial/fastapi/session-with-dependency/))
- [Database Object Synchronizations](https://awesome-repositories.com/f/data-databases/database-object-synchronizations.md) — SQLModel updates local object attributes with the latest data from the database, ensuring fields like auto-generated IDs are available after a commit. ([source](https://sqlmodel.tiangolo.com/tutorial/connect/create-connected-rows/))
- [ORM Relationship Querying](https://awesome-repositories.com/f/data-databases/database-query-builders/orm-relationship-querying.md) — Retrieves records from multiple tables by filtering or joining based on defined foreign key relationships. ([source](https://sqlmodel.tiangolo.com/tutorial/connect/read-connected-data/))
- [Database Relationship Mappings](https://awesome-repositories.com/f/data-databases/database-relationship-mappings.md) — Defines relational associations between data models using foreign keys and relationship attributes. ([source](https://sqlmodel.tiangolo.com/tutorial/fastapi/relationships/))
- [Type-Safe Query Builders](https://awesome-repositories.com/f/data-databases/type-safe-query-builders.md) — Uses static type analysis and class attributes to build database queries that provide IDE autocompletion and compile-time safety.
- [Asynchronous Database Command Execution](https://awesome-repositories.com/f/data-databases/asynchronous-database-command-execution.md) — Executes database transactions and queries asynchronously to prevent blocking the main execution thread.
- [Database Schema Managers](https://awesome-repositories.com/f/data-databases/database-schema-managers.md) — Automatically generates database tables from defined models during application startup. ([source](https://sqlmodel.tiangolo.com/tutorial/fastapi/simple-hero-api/))
- [Many-to-Many Associations](https://awesome-repositories.com/f/data-databases/many-to-many-associations.md) — Links two database entities through intermediate tables to represent many-to-many associations. ([source](https://sqlmodel.tiangolo.com/tutorial/many-to-many/))
- [Association Models](https://awesome-repositories.com/f/data-databases/object-relational-mappers/data-modeling/entity-relationship-models/association-models.md) — SQLModel connects two entities through an intermediate model that stores additional metadata alongside the foreign keys representing the relationship. ([source](https://sqlmodel.tiangolo.com/tutorial/many-to-many/link-with-extra-fields/))
- [Object Relational Mappings](https://awesome-repositories.com/f/data-databases/object-relational-mappings.md) — Automatically updates linked object attributes in memory to maintain consistency before database commits. ([source](https://sqlmodel.tiangolo.com/tutorial/relationship-attributes/back-populates/))
- [Record Updating](https://awesome-repositories.com/f/data-databases/record-updating.md) — Updates existing object attributes and persists changes to the database via session commits. ([source](https://sqlmodel.tiangolo.com/tutorial/update/))
- [Schema Mapping Utilities](https://awesome-repositories.com/f/data-databases/schema-mapping-utilities.md) — Maps application classes directly to database tables and columns to enable object-oriented interaction with relational storage.
- [Single Record Retrievers](https://awesome-repositories.com/f/data-databases/single-record-retrievers.md) — Fetches specific database entries by primary key with built-in error handling for missing records. ([source](https://sqlmodel.tiangolo.com/tutorial/fastapi/read-one/))
- [Data Pagination Utilities](https://awesome-repositories.com/f/data-databases/data-pagination-utilities.md) — Restricts record counts and skips initial rows to enable efficient chunked data retrieval. ([source](https://sqlmodel.tiangolo.com/tutorial/fastapi/limit-and-offset/))
- [In-Memory Databases](https://awesome-repositories.com/f/data-databases/in-memory-databases.md) — Runs ephemeral database instances in memory to facilitate isolated and efficient test environments. ([source](https://sqlmodel.tiangolo.com/tutorial/fastapi/tests/))
- [Local State Stores](https://awesome-repositories.com/f/data-databases/local-state-stores.md) — Ensures application instances reflect the current state of stored records by fetching latest data. ([source](https://sqlmodel.tiangolo.com/tutorial/automatic-id-none-refresh/))
- [Many-to-Many Relationship Managers](https://awesome-repositories.com/f/data-databases/object-relational-mappers/many-to-many-relationship-managers.md) — Manages many-to-many associations by automatically synchronizing changes across related models. ([source](https://sqlmodel.tiangolo.com/tutorial/many-to-many/update-remove-relationships/))
- [Record Deletion](https://awesome-repositories.com/f/data-databases/record-deletion.md) — Deletes a specific object from the database session to permanently remove the corresponding record from the underlying storage. ([source](https://sqlmodel.tiangolo.com/tutorial/delete/))
- [Nested Serialization](https://awesome-repositories.com/f/data-databases/relational-association-apis/nested-serialization.md) — Embeds nested information from associated database records into API responses automatically to reduce the number of separate requests. ([source](https://sqlmodel.tiangolo.com/tutorial/fastapi/relationships/))
- [Related Attribute Resolvers](https://awesome-repositories.com/f/data-databases/relational-association-apis/related-attribute-resolvers.md) — Automatically fetches and resolves associated database records when accessing relationship attributes. ([source](https://sqlmodel.tiangolo.com/tutorial/relationship-attributes/read-relationships/))
- [Relationship Management](https://awesome-repositories.com/f/data-databases/relationship-management.md) — Links database entities through foreign keys to enable automatic navigation and persistence of connected records.
- [Circular Dependency Resolutions](https://awesome-repositories.com/f/data-databases/circular-dependency-resolutions.md) — SQLModel uses string-based type annotations to reference classes that are not yet defined in the current scope, allowing for circular dependencies and cleaner code organization. ([source](https://sqlmodel.tiangolo.com/tutorial/relationship-attributes/type-annotation-strings/))
- [Database Indexing](https://awesome-repositories.com/f/data-databases/database-indexing.md) — Configures model fields to be indexed in the database to optimize query performance. ([source](https://sqlmodel.tiangolo.com/tutorial/indexes/))
- [Database Record Management](https://awesome-repositories.com/f/data-databases/database-record-management.md) — Applies conditional logic to select statements to retrieve records matching specific criteria. ([source](https://sqlmodel.tiangolo.com/tutorial/where/))
- [Inheritance Mappings](https://awesome-repositories.com/f/data-databases/entity-inheritance/inheritance-mappings.md) — Shares common fields across multiple data models using class inheritance to reduce code duplication. ([source](https://sqlmodel.tiangolo.com/tutorial/fastapi/multiple-models/))
- [Cascading Deletion Hooks](https://awesome-repositories.com/f/data-databases/record-deletion/cascading-deletion-hooks.md) — Configures automatic deletion or modification of child records when a parent record is removed. ([source](https://sqlmodel.tiangolo.com/tutorial/relationship-attributes/cascade-delete-relationships/))

### Software Engineering & Architecture

- [Data Validation Libraries](https://awesome-repositories.com/f/software-engineering-architecture/data-validation-libraries.md) — Combines Python type hints with database schema definitions for unified data validation and object-relational mapping.
- [Data Schema Definitions](https://awesome-repositories.com/f/software-engineering-architecture/data-schema-definitions.md) — Combines data validation logic and database schema definitions into a single class to ensure consistency across application layers.
- [Unified Data Modeling](https://awesome-repositories.com/f/software-engineering-architecture/unified-data-modeling.md) — Provides unified data structures that serve simultaneously as database schemas and validation models.
- [Circular Dependency Resolution](https://awesome-repositories.com/f/software-engineering-architecture/dependency-resolution-engines/circular-dependency-resolution.md) — Uses string-based annotations to resolve circular dependencies between data models during schema initialization.

### Web Development

- [Data Serializers](https://awesome-repositories.com/f/web-development/data-serializers.md) — Validates incoming and outgoing web request data while automatically converting between objects and formats.
- [Response Validation](https://awesome-repositories.com/f/web-development/response-validation.md) — Validates and formats data returned by API endpoints using predefined models to ensure consistent response structures. ([source](https://sqlmodel.tiangolo.com/tutorial/fastapi/response-model/))

### Security & Cryptography

- [Session Persistence Management](https://awesome-repositories.com/f/security-cryptography/session-persistence-management.md) — Handles database connections and transaction lifecycles automatically within the context of individual request handlers.
- [Database Injection Prevention](https://awesome-repositories.com/f/security-cryptography/application-and-system-security/web-security/input-sanitization/database-injection-prevention.md) — Provides automated query sanitization and parameterization to protect database interactions from injection attacks. ([source](https://sqlmodel.tiangolo.com/db-to-code/))

### Testing & Quality Assurance

- [Test Fixture Management](https://awesome-repositories.com/f/testing-quality-assurance/general-testing-utilities/test-utilities-assertions/test-lifecycle-execution-control/test-fixture-management.md) — Automates the setup and teardown of database engines and sessions to provide isolated environments for individual test functions. ([source](https://sqlmodel.tiangolo.com/tutorial/fastapi/tests/))
