SQLAlchemy provides an extremely powerful method of defining schemas, performing queries, and manipulating data, both at the ORM level and at the SQL level. SQLAlchemy also provides several extensions to this core behavior. We have already seen one of these extensions, SqlSoup, discussed in Chapter 10. One of the nice things about the SQLAlchemy extensions package is that it provides a “proving ground” for functionality that may eventually make it into the core SQLAlchemy packages. When this occurs (the functionality of an extension is absorbed into the core feature set of SQLAlchemy), the extension is deprecated and eventually removed.
This chapter discusses the two remaining nondeprecated extensions available in SQLAlchemy 0.4, sqlalchemy.ext.associationproxy and sqlalchemy.ext.orderinglist. We will also describe the deprecated extensions, focusing on how to achieve the same functionality using “core” SQLAlchemy.
The association proxy extension allows our mapped classes to have attributes that are proxied from related objects. One place where this is useful is when we have two tables related via an association table that contains extra information in addition to linking the two tables. For instance, suppose we have a database containing the following schema:
user_table = Table( 'user', metadata, Column('id', Integer, primary_key=True), Column('user_name', String(255), unique=True), Column('password', String(255))) brand_table ...