ActiveMapper is a SQLAlchemy extension developed by Jonathan LaCour which provides a declarative interface to the SQLAlchemy mapper. This page briefly covers all the extension’s features but doesn’t explain them in depth.
Note that ActiveMapper has been discontinued in favor of the Elixir declarative layer, and recent versions of SQLAlchemy come with built-in declarative support.
We’ll start off with an example:
class Demo(ActiveMapper):
class mapping:
## Showing options here with default values
#__table__ = "demo" # class_name.lower()
#__autoload__ = False"
demo_key = column(Unicode(16), primary_key=True)
name = column(Unicode(20))
active_count = column(Integer,colname="ACTIVECOUNT")
# a foreign key reference, note the string is the db column
group = column(Integer, foreign_key="group.group_id", index=True)
## This also works
#group = column(Integer, foreign_key= ForeignKey("group.group_id"),
index= True)
#one_to_one and one_to_many only need the target AM class
demo_link = one_to_one("DemoLink", backref="linked_demo")
# A many_to_many mapping requires the target ActiveMapper class and
# an intermediate table.
user = many_to_many("User",demo_user)
Similar to SQLObject, you declare a class that inherits from ActiveMapper. Unlike SQLObject, you must declare an internal class called mapping, which will contain all your column definitions. column() itself is a straight wrapper around the standard SQLAlchemy Column and takes the same arguments.
The relationship columns, one_to_one and many_to_many are a bit more restrictive, they take (only) the following arguments:
Most of this is demonstrated by the Identity classes created during quickstart.