Skip to main content

SQLAlchemy

The Python SQL Toolkit and Object Relational Mapper

Overview

SQLAlchemy is the Python SQL toolkit and Object Relational Mapper that gives application developers the full power and flexibility of SQL. SQLAlchemy provides a full suite of well-known enterprise-level persistence patterns, designed for efficient and high-performing database access, adapted into a simple and Pythonic domain language.

Installation

Get started with pip, conda, or your preferred package manager

Quick Start

Build your first SQLAlchemy application in minutes

ORM Tutorial

Learn the Object Relational Mapper from the ground up

Core Tutorial

Explore the SQL Expression Language

Key Features

Industrial Strength ORM

Built from the core on the identity map, unit of work, and data mapper patterns. These patterns allow transparent persistence of objects using a declarative configuration system. Domain models can be constructed and manipulated naturally, and changes are synchronized with the current transaction automatically.

Relationally-Oriented Query System

Exposes the full range of SQL’s capabilities explicitly, including joins, subqueries, correlation, and most everything else, in terms of the object model. Writing queries with the ORM uses the same techniques of relational composition you use when writing SQL. While you can drop into literal SQL at any time, it’s virtually never needed.

Comprehensive Eager Loading

A comprehensive and flexible system of eager loading for related collections and objects. Collections are cached within a session, and can be loaded on individual access, all at once using joins, or by query per collection across the full result set.

Core SQL Construction System

The SQLAlchemy Core is separate from the ORM and is a full database abstraction layer in its own right, and includes an extensible Python-based SQL expression language, schema metadata, connection pooling, type coercion, and custom types.

Database Introspection and Generation

Database schemas can be “reflected” in one step into Python structures representing database metadata; those same structures can then generate CREATE statements right back out - all within the Core, independent of the ORM.

Philosophy

SQLAlchemy aims to accommodate both object-oriented and relational paradigms, providing an open-ended set of patterns that allow developers to construct a custom mediation layer between domain models and relational schemas.
Key Principles:
  • SQL databases behave less like object collections the more size and performance matter; object collections behave less like tables the more abstraction matters. SQLAlchemy accommodates both principles.
  • An ORM doesn’t need to hide the “R”. A relational database provides rich, set-based functionality that should be fully exposed. SQLAlchemy’s ORM turns the “object relational impedance” issue into a distant memory.
  • Developer control is paramount. In all cases, developers make all decisions regarding design, structure, and naming conventions of both the object model and the relational schema. SQLAlchemy only provides the means to automate execution.
  • Full query control. There’s no such thing as “the ORM generated a bad query” - you retain full control over query structure, joins, subqueries, correlation, and column selection.
  • Transactions are the norm. With SQLAlchemy’s ORM, nothing goes to permanent storage until commit() is called, encouraging consistent transaction boundaries.
  • Security by default. Bound parameters are used to the greatest degree possible, allowing query optimizers to cache query plans effectively and making SQL injection attacks a non-issue.

Version Information

SQLAlchemy 2.1 requires Python 3.10 or higher. The library depends on typing-extensions >= 4.6.0.
Supported Python versions:
  • Python 3.10
  • Python 3.11
  • Python 3.12
  • Python 3.13
  • Python 3.14
  • PyPy (CPython and PyPy implementations supported)

Get Started

Ready to dive in? Follow our installation guide to set up SQLAlchemy, then jump into the quick start tutorial to build your first application.

License

SQLAlchemy is distributed under the MIT License.