Chapter 9. Elixir: A Declarative Extension to SQLAlchemy

This chapter describes Elixir, a module developed to automate some of the more common tasks in SQLAlchemy by providing a declarative layer atop “base” or “raw” SQLAlchemy. This chapter also describes the various extensions to Elixir that provide features such as encryption and versioning.

Introduction to Elixir

The Elixir module was developed as a declarative layer on top of SQLAlchemy, implementing the “active record” pattern described in Chapter 6. Elixir goes out of its way to make all of the power of SQLAlchemy available, while providing sensible default behavior with significantly less code than “raw” SQLAlchemy. This chapter describes versions 0.4 and 0.5 of Elixir, corresponding to the 0.4 version of SQLAlchemy. Differences between versions 0.4 and 0.5 are discussed in the upcoming sidebar, Differences Between Elixir 0.4 and 0.5.”

So, what exactly does Elixir do? Well, consider a simple product database. In SQLAlchemy, we might set up the products, stores, and prices with the following code:

product_table = Table( 'product', metadata, Column('sku', String(20), primary_key=True), Column('msrp', Numeric)) store_table = Table( 'store', metadata, Column('id', Integer, primary_key=True), Column('name', Unicode(255))) product_price_table = Table( 'product_price', metadata, Column('sku', None, ForeignKey('product.sku'), primary_key=True), Column('store_id', None, ForeignKey('store.id'), primary_key=True), Column('price', Numeric, ...

Get Essential SQLAlchemy now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.