The Python SQL Toolkit and Object Relational Mapper
SQLAlchemy is the Python SQL toolkit and Object Relational Mapper that gives application developers the full power and flexibility of SQL.
It 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.
The SQLAlchemy SQL Toolkit and Object Relational Mapper is a comprehensive set of tools for working with databases and Python. It has several distinct areas of functionality which can be used individually or combined together. Its major components are illustrated below, with component dependencies organized into layers:
Above, the two most significant front-facing portions of SQLAlchemy are the Object Relational Mapper (ORM) and the Core.
Core contains the breadth of SQLAlchemy’s SQL and database integration and description services, the most prominent part of this being the SQL Expression Language.
SQLAlchemy Core is the foundational architecture for SQLAlchemy as a “database toolkit”. The library provides tools for managing connectivity to a database, interacting with database queries and results, and programmatic construction of SQL statements.
Sections that have a dark blue border on the right will discuss concepts that are primarily Core-only; when using the ORM, these concepts are still in play but are less often explicit in user code.
Sections that have a light blue border on the left will discuss concepts that are primarily ORM-only. Core-only users can skip these.
The documentation is separated into four sections:
SQLAlchemy 1.4 / 2.0 Tutorial – this all-new tutorial for the 1.4/2.0 series of SQLAlchemy introduces the entire library holistically, starting from a description of Core and working more and more towards ORM-specific concepts. New users, as well as users coming from 1.x style, who wish to work in 2.0 style should start here.
SQLAlchemy ORM – In this section, reference documentation for the ORM is presented; this section also includes the now-legacy Object Relational Tutorial (1.x API).
SQLAlchemy Core – Here, reference documentation for everything else within Core is presented; section also includes the legacy SQL Expression Language Tutorial (1.x API). SQLAlchemy engine, connection, and pooling services are also described here.
Dialects – Provides reference documentation for all dialect implementations, including DBAPI specifics.
Here, the Object Relational Mapper is introduced and fully described. If you want to work with higher-level SQL which is constructed automatically for you, as well as automated persistence of Python objects, proceed first to the tutorial.
The tutorial will present both concepts in the natural order that they should be learned, first with a mostly-Core-centric approach and then spanning out into more ORM-centric concepts.
The major sections of this tutorial are as follows:
Establishing Connectivity – the Engine – all SQLAlchemy applications start with an
Engineobject; here’s how to create one.
Working with Transactions and the DBAPI – the usage API of the
Engineand its related objects
Resultare presented here. This content is Core-centric however ORM users will want to be familiar with at least the
Working with Database Metadata – SQLAlchemy’s SQL abstractions as well as the ORM rely upon a system of defining database schema constructs as Python objects. This section introduces how to do that from both a Core and an ORM perspective.
Working with Data – here we learn how to create, select, update and delete data in the database. The so-called CRUD operations here are given in terms of SQLAlchemy Core with links out towards their ORM counterparts. The SELECT operation that is introduced in detail at Selecting Rows with Core or ORM applies equally well to Core and ORM.
Data Manipulation with the ORM covers the persistence framework of the ORM; basically the ORM-centric ways to insert, update and delete, as well as how to handle transactions.
Working with Related Objects introduces the concept of the
relationship()construct and provides a brief overview of how it’s used, with links to deeper documentation.
Further Reading lists a series of major top-level documentation sections which fully document the concepts introduced in this tutorial.
Data Manipulation with the ORM
The previous section Working with Data remained focused on the SQL Expression Language from a Core perspective, in order to provide continuity across the major SQL statement constructs. This section will then build out the lifecycle of the
Sessionand how it interacts with these constructs.
Prerequisite Sections – the ORM focused part of the tutorial builds upon two previous ORM-centric sections in this document:
Executing with an ORM Session – introduces how to make an ORM
Defining Table Metadata with the ORM – where we set up our ORM mappings of the
Selecting ORM Entities and Columns – a few examples on how to run SELECT statements for entities like
Data Mapper Pattern
The Data Mapper Pattern is an architectural pattern introduced by Martin Fowler in his book Patterns of Enterprise Application Architecture. A Data Mapper is a type of Data Access Layer that performs bi-directional transfer of data between objects in memory and persistent storage. With the Data Mapper Pattern, the in-memory objects have no idea there is a database, and the database schema is unaware of any objects that use it. This separation is the main purpose of a Data Mapper.
Relational algebra is a procedural query language, which takes instances of relations as input and yields instances of relations as output. It uses operators to perform queries. An operator can be either unary or binary. They accept relations as their input and yield relations as their output. Relational algebra is performed recursively on a relation and intermediate results are also considered relations.
The fundamental operations of relational algebra are as follows −
- Set different
- Cartesian product
#!/usr/bin/env python3 # -*- coding: utf-8 -*- from sqlalchemy import Column, String, create_engine from sqlalchemy.orm import sessionmaker from sqlalchemy.ext.declarative import declarative_base # 创建对象的基类: Base = declarative_base() # 定义User对象: class User(Base): # 表的名字: __tablename__ = 'user' # 表的结构: id = Column(String(20), primary_key=True) name = Column(String(20)) # 初始化数据库连接: engine = create_engine('mysql+mysqlconnector://root:password@localhost:3306/test') # 创建DBSession类型: DBSession = sessionmaker(bind=engine) # 创建session对象: session = DBSession() # 创建新User对象: new_user = User(id='5', name='Bob') # 添加到session: session.add(new_user) # 提交即保存到数据库: session.commit() # 关闭session: session.close() # 创建Session: session = DBSession() # 创建Query查询，filter是where条件，最后调用one()返回唯一行，如果调用all()则返回所有行: user = session.query(User).filter(User.id=='5').one() # 打印类型和对象的name属性: print('type:', type(user)) print('name:', user.name) # 关闭Session: session.close()
TP – TUTORIAL
SQLAlchemy is a popular SQL toolkit and Object Relational Mapper. It is written in Python and gives full power and flexibility of SQL to an application developer. It is an open source and cross-platform software released under MIT license.
SQLAlchemy is famous for its object-relational mapper (ORM), using which, classes can be mapped to the database, thereby allowing the object model and database schema to develop in a cleanly decoupled way from the beginning.
As size and performance of SQL databases start to matter, they behave less like object collections. On the other hand, as abstraction in object collections starts to matter, they behave less like tables and rows. SQLAlchemy aims to accommodate both of these principles.
For this reason, it has adopted the data mapper pattern (like Hibernate) rather than the active record pattern used by a number of other ORMs. Databases and SQL will be viewed in a different perspective using SQLAlchemy.
Michael Bayer is the original author of SQLAlchemy. Its initial version was released in February 2006. Latest version is numbered as 1.2.7, released as recently as in April 2018.