A frequently asked question on the SQLAlchemy Discussion Group and on the IRC channel (#sqlalchemy on freenode) is how to go about implementing a custom Table object, which automatically adds specific columns to every table.
For example, if you want all of your tables to contain created_at and updated_at columns, you might be inclined to try and subclass Table. Unfortunately, Table is not easy to subclass due to the metaclass which controls its creational pattern. Instead we can use a factory function to handle it for us:
def TimeStampTable(*args, **kwargs):
args = args + (
Column('created_at', DateTime, nullable=False, default=datetime.now),
Column('updated_at', DateTime, onupdate=datetime.now)
)
return Table(*args, **kwargs)
This function simply adds the extra columns to the Table args and returns the new Table. We can then use it in our code like so:
my_table = TimeStampTable('my_table', metadata,
Column('id', Integer, primary_key=True)
)
I’ve been using this approach on a recent project and it works quite well. This idea comes from Mike Bayer.


