Contribute Media
A thank you to everyone who makes this possible: Read More

Introduction to SQLAlchemy


Introduction to SQLAlchemy

Presented by Johnathan Ellis

SQLAlchemy is the premier SQL toolkit for Python. SQLAlchemy exposes the full power of SQL while remaining easy to use for the basics, and is flexible enough to work with existing database schemas as well as fresh designs. SQLAlchemy is used in production by leading Python web frameworks Pylons and Turbogears as well as many other web and non-web developers.

This tutorial teaches SQLAlchemy's object-relational mapping to automate painlessly loading and persisting objects from and to your database, even when complex relationships exist.

Intended Audience

Python experience: intermediate. You should already have some hands-on experience with Python and be comfortable reading modern Python code.

SQL experience: beginning. You should know how to SELECT, INSERT, and DELETE, and how to read queries involving JOINs, but we will only be reading the SQL generated by SA, not writing our own.

ORM experience: prior experience with an object-relational mapper is not necessary.

Class Outline


  • Philosophy
  • SA features


  • Introduction to ORM
  • Data Mapper pattern vs Active Record
  • Tables
  • Mapping basics


  • Conditions, operators
  • Joins
  • lazy / eager loading
  • Relation-based queries

Sessions & identity map:

  • detatching, attaching objects
  • Lifecycle relations

More Mapping:

  • one to one, many to many
  • backreferences

Extensions, etc.:

  • DDL
  • Migrate
  • FormAlchemy
  • SqlSoup
  • Elixir


Laptop with Python 2.5 or 2.6 and SQLAlchemy 0.6 installed. Code samples may also work with Python 2.4 but will not be tested on that platform. SQLAlchemy also supports Jython and cPython 3.x, but this tutorial will be using "classic" cPython.


Improve this page