Help us!

Take some time to transcribe PyCon 2014 talks! Click on the "Share" button below the video and then "Subtitle" to get started.

Introduction to SQLAlchemy

Description

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

Introduction:

  • Philosophy
  • SA features

Fundamentals:

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

Queries:

  • 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

Requirements

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.