# SQLAlchemy 0.7 Documentation

Search:
Release: 0.7.3 | Release Date: October 16, 2011

# Mapper Configuration

This section describes a variety of configurational patterns that are usable with mappers. It assumes you’ve worked through Object Relational Tutorial and know how to construct and use rudimentary mappers and relationships.

## Classical Mappings

Recall from Object Relational Tutorial that we normally use the Declarative system to define mappings. The “Classical Mapping” refers to the process of defining `Table` metadata and mapped class via `mapper()` separately. While direct usage of `mapper()` is not prominent in modern SQLAlchemy, the function can be used to create alternative mappings for an already mapped class, offers greater configurational flexibility in certain highly circular configurations, and is also at the base of alternative configurational systems not based upon Declarative. Many SQLAlchemy applications in fact use classical mappings directly for configuration.

The `User` example in the tutorial, using classical mapping, defines the `Table`, class, and `mapper()` of the class separately. Below we illustrate the full `User`/`Address` example using this style:

    from sqlalchemy import Table, MetaData, Column, ForeignKey, Integer, String
    from sqlalchemy.orm import mapper, relationship

    metadata = MetaData()

    user = Table('user', metadata,
                Column('id', Integer, primary_key=True),
                Column('name', String(50)),
                Column('fullname', String(50)),
                Column('password', String(12))
            )

    address = Table('address', metadata,
                Column('id', Integer, primary_key=True),
                Column('user_id', Integer, ForeignKey('user.id')),
                Column('email_address', String(50))
                )

    class User(object):
        def __init__(self, name, fullname, password):
            self.name = name
            self.fullname = fullname
            self.password = password

    class Address(object):
        def __init__(self, email_address):
            self.email_address = email_address

    mapper(User, user, properties={
        'addresses':relationship(Address, order_by=address.c.id, backref="user")
    })
    mapper(Address, address)

When the above is complete we now have a `Table`/`mapper()` setup the same as that set up using Declarative in the tutorial. Note that the mappings do not have the benefit of the instrumented `User` and `Address` classes available, nor is the “string” argument system of `relationship()` available, as this is a feature of Declarative. The `order_by` argument of the `User.addresses` relationship is defined in terms of the actual `address` table instead of the `Address` class.

It’s also worth noting that the “Classical” and “Declarative” mapping systems are not in any way exclusive of each other. The two can be mixed freely - below we can define a new class `Order` using a declarative base, which links back to `User`- no problem, except that we can’t specify `User` as a string since it’s not available in the “base” registry:

    from sqlalchemy.ext.declarative import declarative_base

    Base = declarative_base()

    class Order(Base):
        __tablename__ = 'order'

        id = Column(Integer, primary_key=True)
        user_id = Column(ForeignKey('user.id'))
        order_number = Column(String(50))
        user = relationship(User, backref="orders")

This reference document uses a mix of Declarative and classical mappings for examples. However, all patterns here apply both to the usage of explicit `mapper()` and `Table` objects as well as when using Declarative, where options that are specific to the `mapper()` function can be specified with Declarative via the `__mapper__` attribute. Any example in this section which takes a form such as:

    mapper(User, user_table, primary_key=[user_table.c.id])

Would translate into declarative as:

    class User(Base):
        __table__ = user_table
        __mapper_args__ = {
            'primary_key':[user_table.c.id]
        }

`Column` objects which are declared inline can also be used directly in `__mapper_args__`:

    class User(Base):
        __tablename__ = 'user'

        id = Column(Integer)

        __mapper_args__ = {
            'primary_key':[id]
        }

## Customizing Column Properties

The default behavior of `mapper()` is to assemble all the columns in the mapped `Table` into mapped object attributes. This behavior can be modified in several ways, as well as enhanced by SQL expressions.

### Naming Columns Distinctly from Attribute Names

A mapping by default shares the same name for a `Column` as that of the mapped attribute. The name assigned to the `Column` can be different, as we illustrate here in a Declarative mapping:

    class User(Base):
        __tablename__ = 'user'
        id = Column('user_id', Integer, primary_key=True)
        name = Column('user_name', String(50))

Where above `User.id` resolves to a column named `user_id` and `User.name` resolves to a column named `user_name`.

In a classical mapping, the `Column` objects can be placed directly in the `properties` dictionary using an alternate key:

    mapper(User, user_table, properties={
       'id': user_table.c.user_id,
       'name': user_table.c.user_name,
    })

When mapping to an already constructed `Table`, a prefix can be specified using the `column_prefix` option, which will cause the automated mapping of each `Column` to name the attribute starting with the given prefix, prepended to the actual `Column` name:

    class User(Base):
        __table__ = user_table
        __mapper_args__ = {'column_prefix':'_'}

The above will place attribute names such as `_user_id`, `_user_name`, `_password` etc. on the mapped `User` class.

The classical version of the above:

    mapper(User, user_table, column_prefix='_')

### Mapping Multiple Columns to a Single Attribute

To place multiple columns which are known to be “synonymous” based on foreign key relationship or join condition into the same mapped attribute, they can be mapped as a list. Below we map to a `join()`:

    from sqlalchemy import join, Table, Column, String, Integer, ForeignKey
    from sqlalchemy.ext.declarative import declarative_base

    Base = declarative_base()

    user_table = Table('user', Base.metadata,
                Column('id', Integer, primary_key=True),
                Column('name', String(50)),
                Column('fullname', String(50)),
                Column('password', String(12))
            )

    address_table = Table('address', Base.metadata,
                Column('id', Integer, primary_key=True),
                Column('user_id', Integer, ForeignKey('user.id')),
                Column('email_address', String(50))
                )

    # "user JOIN address ON user.id=address.user_id"
    useraddress = join(user_table, address_table, \
        user_table.c.id == address_table.c.user_id)

    class User(Base):
        __table__ = useraddress

        # assign "user.id", "address.user_id" to the
        # "id" attribute
        id = [user_table.c.id, address_table.c.user_id]

        # assign "address.id" to the "address_id"
        # attribute, to avoid name conflicts
        address_id = address_table.c.id

In the above mapping, the value assigned to `user.id` will also be persisted to the `address.user_id` column during a flush. The two columns are also not independently queryable from the perspective of the mapped class (they of course are still available from their original tables).

Classical version:

    mapper(User, useraddress, properties={
        'id':[user_table.c.id, address_table.c.user_id],
        'address_id':address_table.c.id
    })

For further examples on this particular use case, see Mapping a Class against Multiple Tables.

### Using column_property for column level options

The mapping of a `Column` with a particular `mapper()` can be customized using the `orm.column_property()` function. This function explicitly creates the `ColumnProperty` object which handles the job of mapping a `Column`, instead of relying upon the `mapper()` function to create it automatically. Used with Declarative, the `Column` can be embedded directly into the function:

    from sqlalchemy.orm import column_property

    class User(Base):
        __tablename__ = 'user'

        id = Column(Integer, primary_key=True)
        name = column_property(Column(String(50)), active_history=True)

Or with a classical mapping, in the `properties` dictionary:

    from sqlalchemy.orm import column_property

    mapper(User, user, properties={
        'name':column_property(user.c.name, active_history=True)
    })

Further examples of `orm.column_property()` are at SQL Expressions as Mapped Attributes.

`sqlalchemy.orm.``column_property`(*args, **kwargs)

  Provide a column-level property for use with a Mapper.

  Column-based properties can normally be applied to the mapper’s `properties` dictionary using the `Column` element directly. Use this function when the given column is not directly present within the mapper’s selectable; examples include SQL expressions, functions, and scalar SELECT queries.

  Columns that aren’t present in the mapper’s selectable won’t be persisted by the mapper and are effectively “read-only” attributes.

  Parameters: | *cols – list of Column objects to be mapped. active_history=False – When `True`, indicates that the “previous” value for a scalar attribute should be loaded when replaced, if not already loaded. Normally, history tracking logic for simple non-primary-key scalar values only needs to be aware of the “new” value in order to perform a flush. This flag is available for applications that make use of `attributes.get_history()` which also need to know the “previous” value of the attribute. (new in 0.6.6) comparator_factory – a class which extends `ColumnProperty.Comparator` which provides custom SQL clause generation for comparison operations. group – a group name for this property when marked as deferred. deferred – when True, the column property is “deferred”, meaning that it does not load immediately, and is instead loaded when the attribute is first accessed on an instance. See also `deferred()`. doc – optional string that will be applied as the doc on the class-bound descriptor. expire_on_flush=True – Disable expiry on flush. A column_property() which refers to a SQL expression (and not a single table-bound column) is considered to be a “read only” property; populating it has no effect on the state of data, and it can only return database state. For this reason a column_property()’s value is expired whenever the parent object is involved in a flush, that is, has any kind of “dirty” state within a flush. Setting this parameter to `False` will have the effect of leaving any existing value present after the flush proceeds. Note however that the `Session` with default expiration settings still expires all attributes after a `Session.commit()` call, however. New in 0.7.3. extension – an `AttributeExtension` instance, or list of extensions, which will be prepended to the list of attribute listeners for the resulting descriptor placed on the class. Deprecated. Please see `AttributeEvents`.

### Mapping a Subset of Table Columns

To reference a subset of columns referenced by a table as mapped attributes, use the `include_properties` or `exclude_properties` arguments. For example:

    mapper(User, user_table, include_properties=['user_id', 'user_name'])

...will map the `User` class to the `user_table` table, only including the “user_id” and “user_name” columns - the rest are not refererenced. Similarly:

    mapper(Address, address_table,
                exclude_properties=['street', 'city', 'state', 'zip'])

...will map the `Address` class to the `address_table` table, including all columns present except “street”, “city”, “state”, and “zip”.

When this mapping is used, the columns that are not included will not be referenced in any SELECT statements emitted by `Query`, nor will there be any mapped attribute on the mapped class which represents the column; assigning an attribute of that name will have no effect beyond that of a normal Python attribute assignment.

In some cases, multiple columns may have the same name, such as when mapping to a join of two or more tables that share some column name. To exclude or include individual columns, `Column` objects may also be placed within the “include_properties” and “exclude_properties” collections (new feature as of 0.6.4):

    mapper(UserAddress, user_table.join(addresse_table),
                exclude_properties=[address_table.c.id],
                primary_key=[user_table.c.id]
            )

It should be noted that insert and update defaults configured on individal `Column` objects, such as those configured by the “default”, “update”, “server_default” and “server_onupdate” arguments, will continue to function normally even if those `Column` objects are not mapped. This functionality is part of the SQL expression and execution system and occurs below the level of the ORM.

## Deferred Column Loading

This feature allows particular columns of a table be loaded only upon direct access, instead of when the entity is queried using `Query`. This feature is useful when one wants to avoid loading a large text or binary field into memory when it’s not needed. Individual columns can be lazy loaded by themselves or placed into groups that lazy-load together, using the `orm.deferred()` function to mark them as “deferred”. In the example below, we define a mapping that will load each of `.excerpt` and `.photo` in separate, individual-row SELECT statements when each attribute is first referenced on the individual object instance:

    from sqlalchemy.orm import deferred
    from sqlalchemy import Integer, String, Text, Binary, Column

    class Book(Base):
        __tablename__ = 'book'

        book_id = Column(Integer, primary_key=True)
        title = Column(String(200), nullable=False)
        summary = Column(String(2000))
        excerpt = deferred(Column(Text))
        photo = deferred(Column(Binary))

Classical mappings as always place the usage of `orm.deferred()` in the `properties` dictionary against the table-bound `Column`:

    mapper(Book, book_table, properties={
        'photo':deferred(book_table.c.photo)
    })

Deferred columns can be associated with a “group” name, so that they load together when any of them are first accessed. The example below defines a mapping with a `photos` deferred group. When one `.photo` is accessed, all three photos will be loaded in one SELECT statement. The `.excerpt` will be loaded separately when it is accessed:

    class Book(Base):
        __tablename__ = 'book'

        book_id = Column(Integer, primary_key=True)
        title = Column(String(200), nullable=False)
        summary = Column(String(2000))
        excerpt = deferred(Column(Text))
        photo1 = deferred(Column(Binary), group='photos')
        photo2 = deferred(Column(Binary), group='photos')
        photo3 = deferred(Column(Binary), group='photos')

You can defer or undefer columns at the `Query` level using the `orm.defer()` and `orm.undefer()` query options:

    from sqlalchemy.orm import defer, undefer

    query = session.query(Book)
    query.options(defer('summary')).all()
    query.options(undefer('excerpt')).all()

And an entire “deferred group”, i.e. which uses the `group` keyword argument to `orm.deferred()`, can be undeferred using `orm.undefer_group()`, sending in the group name:

    from sqlalchemy.orm import undefer_group

    query = session.query(Book)
    query.options(undefer_group('photos')).all()

### Column Deferral API

`sqlalchemy.orm.``deferred`(*columns, **kwargs)

  Return a `DeferredColumnProperty`, which indicates this object attributes should only be loaded from its corresponding table column when first accessed.

  Used with the “properties” dictionary sent to `mapper()`.

  See also:

  Deferred Column Loading

`sqlalchemy.orm.``defer`(*key)

  Return a `MapperOption` that will convert the column property of the given name into a deferred load.

  Used with `Query.options()`.

  e.g.:

      from sqlalchemy.orm import defer

      query(MyClass).options(defer("attribute_one"),
                          defer("attribute_two"))

  A class bound descriptor is also accepted:

      query(MyClass).options(
                          defer(MyClass.attribute_one),
                          defer(MyClass.attribute_two))

  A “path” can be specified onto a related or collection object using a dotted name. The `orm.defer()` option will be applied to that object when loaded:

      query(MyClass).options(
                          defer("related.attribute_one"),
                          defer("related.attribute_two"))

  To specify a path via class, send multiple arguments:

      query(MyClass).options(
                          defer(MyClass.related, MyOtherClass.attribute_one),
                          defer(MyClass.related, MyOtherClass.attribute_two))

  See also:

  Deferred Column Loading

  Parameters: | *key – A key representing an individual path. Multiple entries are accepted to allow a multiple-token path for a single target, not multiple targets.

`sqlalchemy.orm.``undefer`(*key)

  Return a `MapperOption` that will convert the column property of the given name into a non-deferred (regular column) load.

  Used with `Query.options()`.

  e.g.:

      from sqlalchemy.orm import undefer

      query(MyClass).options(undefer("attribute_one"),
                              undefer("attribute_two"))

  A class bound descriptor is also accepted:

      query(MyClass).options(
                          undefer(MyClass.attribute_one),
                          undefer(MyClass.attribute_two))

  A “path” can be specified onto a related or collection object using a dotted name. The `orm.undefer()` option will be applied to that object when loaded:

      query(MyClass).options(
                          undefer("related.attribute_one"),
                          undefer("related.attribute_two"))

  To specify a path via class, send multiple arguments:

      query(MyClass).options(
                          undefer(MyClass.related, MyOtherClass.attribute_one),
                          undefer(MyClass.related, MyOtherClass.attribute_two))

  See also:

  `orm.undefer_group()` as a means to “undefer” a group of attributes at once.

  Deferred Column Loading

  Parameters: | *key – A key representing an individual path. Multiple entries are accepted to allow a multiple-token path for a single target, not multiple targets.

`sqlalchemy.orm.``undefer_group`(name)

  Return a `MapperOption` that will convert the given group of deferred column properties into a non-deferred (regular column) load.

  Used with `Query.options()`.

  e.g.:

      query(MyClass).options(undefer("group_one"))

  See also:

  Deferred Column Loading

  Parameters: | name – String name of the deferred group. This name is established using the “group” name to the `orm.deferred()` configurational function.

## SQL Expressions as Mapped Attributes

Any SQL expression that relates to the primary mapped selectable can be mapped as a read-only attribute which will be bundled into the SELECT emitted for the target mapper when rows are loaded. This effect is achieved using the `orm.column_property()` function. Any scalar-returning `ClauseElement` may be used:

    from sqlalchemy.orm import column_property

    class User(Base):
        __tablename__ = 'user'
        id = Column(Integer, primary_key=True)
        firstname = Column(String(50))
        lastname = Column(String(50))
        fullname = column_property(firstname + " " + lastname)

Correlated subqueries may be used as well. Below we use the `select()` construct to create a SELECT that links together the count of `Address` objects available for a particular `User`:

    from sqlalchemy.orm import column_property
    from sqlalchemy import select, func
    from sqlalchemy import Column, Integer, String, ForeignKey

    from sqlalchemy.ext.declarative import declarative_base

    Base = declarative_base()

    class Address(Base):
        __tablename__ = 'address'
        id = Column(Integer, primary_key=True)
        user_id = Column(Integer, ForeignKey('user.id'))

    class User(Base):
        __tablename__ = 'user'
        id = Column(Integer, primary_key=True)
        address_count = column_property(
            select([func.count(Address.id)]).\
                where(Address.user_id==id)
        )

If import issues prevent the `column_property()` from being defined inline with the class, it can be assigned to the class after both are configured. In Declarative this has the effect of calling `Mapper.add_property()` to add an additional property after the fact:

    User.address_count = column_property(
            select([func.count(Address.id)]).\
                where(Address.user_id==User.id)
        )

For many-to-many relationships, use `and_()` to join the fields of the association table to both tables in a relation, illustrated here with a classical mapping:

    from sqlalchemy import and_

    mapper(Author, authors, properties={
        'book_count': column_property(
                            select([func.count(books.c.id)],
                                and_(
                                    book_authors.c.author_id==authors.c.id,
                                    book_authors.c.book_id==books.c.id
                                )))
        })

### Alternatives to column_property()

`orm.column_property()` is used to provide the effect of a SQL expression that is actively rendered into the SELECT generated for a particular mapped class. For the typical attribute that represents a composed value, it’s often simpler and more efficient to just define it as a Python property, which is evaluated as it is invoked on instances after they’ve been loaded:

    class User(Base):
        __tablename__ = 'user'
        id = Column(Integer, primary_key=True)
        firstname = Column(String(50))
        lastname = Column(String(50))

        @property
        def fullname(self):
            return self.firstname + " " + self.lastname

To emit SQL queries from within a @property, the `Session` associated with the instance can be acquired using `object_session()`, which will provide the appropriate transactional context from which to emit a statement:

    from sqlalchemy.orm import object_session
    from sqlalchemy import select, func

    class User(Base):
        __tablename__ = 'user'
        id = Column(Integer, primary_key=True)
        firstname = Column(String(50))
        lastname = Column(String(50))

        @property
        def address_count(self):
            return object_session(self).\
                scalar(
                    select([func.count(Address.id)]).\
                        where(Address.user_id==self.id)
                )

For more information on using descriptors, including how they can be smoothly integrated into SQL expressions, see Using Descriptors.

## Changing Attribute Behavior

### Simple Validators

A quick way to add a “validation” routine to an attribute is to use the `validates()` decorator. An attribute validator can raise an exception, halting the process of mutating the attribute’s value, or can change the given value into something different. Validators, like all attribute extensions, are only called by normal userland code; they are not issued when the ORM is populating the object:

    from sqlalchemy.orm import validates

    class EmailAddress(Base):
        __tablename__ = 'address'

        id = Column(Integer, primary_key=True)
        email = Column(String)

        @validates('email')
        def validate_email(self, key, address):
            assert '@' in address
            return address

Validators also receive collection events, when items are added to a collection:

    from sqlalchemy.orm import validates

    class User(Base):
        # ...

        addresses = relationship("Address")

        @validates('addresses')
        def validate_address(self, key, address):
            assert '@' in address.email
            return address

Note that the `validates()` decorator is a convenience function built on top of attribute events. An application that requires more control over configuration of attribute change behavior can make use of this system, described at `AttributeEvents`.

`sqlalchemy.orm.``validates`(*names)

  Decorate a method as a ‘validator’ for one or more named properties.

  Designates a method as a validator, a method which receives the name of the attribute as well as a value to be assigned, or in the case of a collection, the value to be added to the collection. The function can then raise validation exceptions to halt the process from continuing (where Python’s built-in `ValueError` and `AssertionError` exceptions are reasonable choices), or can modify or replace the value before proceeding. The function should otherwise return the given value.

  Note that a validator for a collection cannot issue a load of that collection within the validation routine - this usage raises an assertion to avoid recursion overflows. This is a reentrant condition which is not supported.

### Using Descriptors

A more comprehensive way to produce modified behavior for an attribute is to use descriptors. These are commonly used in Python using the `property()` function. The standard SQLAlchemy technique for descriptors is to create a plain descriptor, and to have it read/write from a mapped attribute with a different name. Below we illustrate this using Python 2.6-style properties:

    class EmailAddress(Base):
        __tablename__ = 'email_address'

        id = Column(Integer, primary_key=True)

        # name the attribute with an underscore,
        # different from the column name
        _email = Column("email", String)

        # then create an ".email" attribute
        # to get/set "._email"
        @property
        def email(self):
            return self._email

        @email.setter
        def email(self, email):
            self._email = email

The approach above will work, but there’s more we can add. While our `EmailAddress` object will shuttle the value through the `email` descriptor and into the `_email` mapped attribute, the class level `EmailAddress.email` attribute does not have the usual expression semantics usable with `Query`. To provide these, we instead use the `hybrid` extension as follows:

    from sqlalchemy.ext.hybrid import hybrid_property

    class EmailAddress(Base):
        __tablename__ = 'email_address'

        id = Column(Integer, primary_key=True)

        _email = Column("email", String)

        @hybrid_property
        def email(self):
            return self._email

        @email.setter
        def email(self, email):
            self._email = email

The `.email` attribute, in addition to providing getter/setter behavior when we have an instance of `EmailAddress`, also provides a SQL expression when used at the class level, that is, from the `EmailAddress` class directly:

    from sqlalchemy.orm import Session
    session = Session()

    sqladdress = session.query(EmailAddress).\
                     filter(EmailAddress.email == 'address@example.com').\
                     one()
    SELECT address.email AS address_email, address.id AS address_id
    FROM address
    WHERE address.email = ?
    ('address@example.com',)
    address.email = 'otheraddress@example.com'
    sqlsession.commit()
    UPDATE address SET email=? WHERE address.id = ?
    ('otheraddress@example.com', 1)
    COMMIT

The `hybrid_property` also allows us to change the behavior of the attribute, including defining separate behaviors when the attribute is accessed at the instance level versus at the class/expression level, using the `hybrid_property.expression()` modifier. Such as, if we wanted to add a host name automatically, we might define two sets of string manipulation logic:

    class EmailAddress(Base):
        __tablename__ = 'email_address'

        id = Column(Integer, primary_key=True)

        _email = Column("email", String)

        @hybrid_property
        def email(self):
            """Return the value of _email up until the last twelve
            characters."""

            return self._email[:-12]

        @email.setter
        def email(self, email):
            """Set the value of _email, tacking on the twelve character
            value @example.com."""

            self._email = email + "@example.com"

        @email.expression
        def email(cls):
            """Produce a SQL expression that represents the value
            of the _email column, minus the last twelve characters."""

            return func.substr(cls._email, 0, func.length(cls._email) - 12)

Above, accessing the `email` property of an instance of `EmailAddress` will return the value of the `_email` attribute, removing or adding the hostname `@example.com` from the value. When we query against the `email` attribute, a SQL function is rendered which produces the same effect:

    sqladdress = session.query(EmailAddress).filter(EmailAddress.email == 'address').one()
    SELECT address.email AS address_email, address.id AS address_id
    FROM address
    WHERE substr(address.email, ?, length(address.email) - ?) = ?
    (0, 12, 'address')

Read more about Hybrids at Hybrid Attributes.

### Synonyms

Synonyms are a mapper-level construct that applies expression behavior to a descriptor based attribute. The functionality of synonym is superceded as of 0.7 by hybrid attributes.

`sqlalchemy.orm.``synonym`(name, map_column=False, descriptor=None, comparator_factory=None, doc=None)

  Denote an attribute name as a synonym to a mapped property.

  Note

  `synonym()` is superseded as of 0.7 by the `hybrid` extension. See the documentation for hybrids at Hybrid Attributes.

  Used with the `properties` dictionary sent to `mapper()`:

      class MyClass(object):
          def _get_status(self):
              return self._status
          def _set_status(self, value):
              self._status = value
          status = property(_get_status, _set_status)

      mapper(MyClass, sometable, properties={
          "status":synonym("_status", map_column=True)
      })

  Above, the `status` attribute of MyClass will produce expression behavior against the table column named `status`, using the Python attribute `_status` on the mapped class to represent the underlying value.

  Parameters: | name – the name of the existing mapped property, which can be any other `MapperProperty` including column-based properties and relationships. map_column – if `True`, an additional `ColumnProperty` is created on the mapper automatically, using the synonym’s name as the keyname of the property, and the keyname of this `synonym()` as the name of the column to map.

### Custom Comparators

The expressions returned by comparison operations, such as `User.name=='ed'`, can be customized, by implementing an object that explicitly defines each comparison method needed.

This is a relatively rare use case which generally applies only to highly customized types. Usually, custom SQL behaviors can be associated with a mapped class by composing together the classes’ existing mapped attributes with other expression components, using either mapped SQL expressions as those described in SQL Expressions as Mapped Attributes, or so-called “hybrid” attributes as described at Hybrid Attributes. Those approaches should be considered first before resorting to custom comparison objects.

Each of `orm.column_property()`, `composite()`, `relationship()`, and `comparable_property()` accept an argument called `comparator_factory`. A subclass of `PropComparator` can be provided for this argument, which can then reimplement basic Python comparison methods such as `__eq__()`, `__ne__()`, `__lt__()`, and so on.

It’s best to subclass the `PropComparator` subclass provided by each type of property. For example, to allow a column-mapped attribute to do case-insensitive comparison:

    from sqlalchemy.orm.properties import ColumnProperty
    from sqlalchemy.sql import func

    class MyComparator(ColumnProperty.Comparator):
        def __eq__(self, other):
            return func.lower(self.__clause_element__()) == func.lower(other)

    mapper(EmailAddress, address_table, properties={
        'email':column_property(address_table.c.email,
                                comparator_factory=MyComparator)
    })

Above, comparisons on the `email` column are wrapped in the SQL lower() function to produce case-insensitive matching:

    >>> str(EmailAddress.email == 'SomeAddress@foo.com')
    lower(address.email) = lower(:lower_1)

When building a `PropComparator`, the `__clause_element__()` method should be used in order to acquire the underlying mapped column. This will return a column that is appropriately wrapped in any kind of subquery or aliasing that has been applied in the context of the generated SQL statement.

`sqlalchemy.orm.``comparable_property`(comparator_factory, descriptor=None)

  Provides a method of applying a `PropComparator` to any Python descriptor attribute.

  Note

  `comparable_property()` is superseded as of 0.7 by the `hybrid` extension. See the example at Building Custom Comparators.

  Allows a regular Python @property (descriptor) to be used in queries and SQL constructs like a managed attribute. comparable_property wraps a descriptor with a proxy that directs operator overrides such as == (__eq__) to the supplied comparator but proxies everything else through to the original descriptor. Used with the `properties` dictionary sent to `mapper()`:

      from sqlalchemy.orm import mapper, comparable_property
      from sqlalchemy.orm.interfaces import PropComparator
      from sqlalchemy.sql import func
      from sqlalchemy import Table, MetaData, Integer, String, Column

      metadata = MetaData()

      word_table = Table('word', metadata,
        Column('id', Integer, primary_key=True),
        Column('word', String(200), nullable=False)
      )

      class CaseInsensitiveComparator(PropComparator):
          def __clause_element__(self):
              return self.prop

          def __eq__(self, other):
              return func.lower(self.__clause_element__()) == func.lower(other)

      class SearchWord(object):
          pass

      mapper(SearchWord, word_table, properties={
               'word_insensitive': comparable_property(CaseInsensitiveComparator)
              })

  A mapping like the above allows the `word_insensitive` attribute to render an expression like:

      >>> print SearchWord.word_insensitive == "Trucks"
      lower(:lower_1) = lower(:lower_2)

  Parameters: | comparator_factory – A PropComparator subclass or factory that defines operator behavior for this property. descriptor – Optional when used in a `properties={}` declaration. The Python descriptor or property to layer comparison behavior on top of. The like-named descriptor will be automatically retreived from the mapped class if left blank in a `properties` declaration.

## Composite Column Types

Sets of columns can be associated with a single user-defined datatype. The ORM provides a single attribute which represents the group of columns using the class you provide.

Note

As of SQLAlchemy 0.7, composites have been simplified such that they no longer “conceal” the underlying column based attributes. Additionally, in-place mutation is no longer automatic; see the section below on enabling mutability to support tracking of in-place changes.

A simple example represents pairs of columns as a `Point` object. `Point` represents such a pair as `.x` and `.y`:

    class Point(object):
        def __init__(self, x, y):
            self.x = x
            self.y = y

        def __composite_values__(self):
            return self.x, self.y

        def __repr__(self):
            return "Point(x=%r, y=%r)" % (self.x, self.y)

        def __eq__(self, other):
            return isinstance(other, Point) and \
                other.x == self.x and \
                other.y == self.y

        def __ne__(self, other):
            return not self.__eq__(other)

The requirements for the custom datatype class are that it have a constructor which accepts positional arguments corresponding to its column format, and also provides a method `__composite_values__()` which returns the state of the object as a list or tuple, in order of its column-based attributes. It also should supply adequate `__eq__()` and `__ne__()` methods which test the equality of two instances.

We will create a mapping to a table `vertice`, which represents two points as `x1/y1` and `x2/y2`. These are created normally as `Column` objects. Then, the `composite()` function is used to assign new attributes that will represent sets of columns via the `Point` class:

    from sqlalchemy import Column, Integer
    from sqlalchemy.orm import composite
    from sqlalchemy.ext.declarative import declarative_base

    Base = declarative_base()

    class Vertex(Base):
        __tablename__ = 'vertice'

        id = Column(Integer, primary_key=True)
        x1 = Column(Integer)
        y1 = Column(Integer)
        x2 = Column(Integer)
        y2 = Column(Integer)

        start = composite(Point, x1, y1)
        end = composite(Point, x2, y2)

A classical mapping above would define each `composite()` against the existing table:

    mapper(Vertex, vertice_table, properties={
        'start':composite(Point, vertice_table.c.x1, vertice_table.c.y1),
        'end':composite(Point, vertice_table.c.x2, vertice_table.c.y2),
    })

We can now persist and use `Vertex` instances, as well as query for them, using the `.start` and `.end` attributes against ad-hoc `Point` instances:

    >>> v = Vertex(start=Point(3, 4), end=Point(5, 6))
    >>> session.add(v)
    >>> q = session.query(Vertex).filter(Vertex.start == Point(3, 4))
    sql>>> print q.first().start
    BEGIN (implicit)
    INSERT INTO vertice (x1, y1, x2, y2) VALUES (?, ?, ?, ?)
    (3, 4, 5, 6)
    SELECT vertice.id AS vertice_id,
            vertice.x1 AS vertice_x1,
            vertice.y1 AS vertice_y1,
            vertice.x2 AS vertice_x2,
            vertice.y2 AS vertice_y2
    FROM vertice
    WHERE vertice.x1 = ? AND vertice.y1 = ?
     LIMIT ? OFFSET ?
    (3, 4, 1, 0)Point(x=3, y=4)

`sqlalchemy.orm.``composite`(class_, *cols, **kwargs)

  Return a composite column-based property for use with a Mapper.

  See the mapping documention section Composite Column Types for a full usage example.

  Parameters: | class_ – The “composite type” class. *cols – List of Column objects to be mapped. active_history=False – When `True`, indicates that the “previous” value for a scalar attribute should be loaded when replaced, if not already loaded. See the same flag on `column_property()`. (This flag becomes meaningful specifically for `composite()` in 0.7 - previously it was a placeholder). group – A group name for this property when marked as deferred. deferred – When True, the column property is “deferred”, meaning that it does not load immediately, and is instead loaded when the attribute is first accessed on an instance. See also `deferred()`. comparator_factory – a class which extends `CompositeProperty.Comparator` which provides custom SQL clause generation for comparison operations. doc – optional string that will be applied as the doc on the class-bound descriptor. extension – an `AttributeExtension` instance, or list of extensions, which will be prepended to the list of attribute listeners for the resulting descriptor placed on the class. Deprecated. Please see `AttributeEvents`.

### Tracking In-Place Mutations on Composites

As of SQLAlchemy 0.7, in-place changes to an existing composite value are not tracked automatically. Instead, the composite class needs to provide events to its parent object explicitly. This task is largely automated via the usage of the `MutableComposite` mixin, which uses events to associate each user-defined composite object with all parent associations. Please see the example in Establishing Mutability on Composites.

### Redefining Comparison Operations for Composites

The “equals” comparison operation by default produces an AND of all corresponding columns equated to one another. This can be changed using the `comparator_factory`, described in Custom Comparators. Below we illustrate the “greater than” operator, implementing the same expression that the base “greater than” does:

    from sqlalchemy.orm.properties import CompositeProperty
    from sqlalchemy import sql

    class PointComparator(CompositeProperty.Comparator):
        def __gt__(self, other):
            """redefine the 'greater than' operation"""

            return sql.and_(*[a>b for a, b in
                              zip(self.__clause_element__().clauses,
                                  other.__composite_values__())])

    class Vertex(Base):
        ___tablename__ = 'vertice'

        id = Column(Integer, primary_key=True)
        x1 = Column(Integer)
        y1 = Column(Integer)
        x2 = Column(Integer)
        y2 = Column(Integer)

        start = composite(Point, x1, y1,
                            comparator_factory=PointComparator)
        end = composite(Point, x2, y2,
                            comparator_factory=PointComparator)

## Mapping a Class against Multiple Tables

Mappers can be constructed against arbitrary relational units (called `Selectables`) as well as plain `Tables`. For example, The `join` keyword from the SQL package creates a neat selectable unit comprised of multiple tables, complete with its own composite primary key, which can be passed in to a mapper as the table.

    from sqlalchemy.orm import mapper
    from sqlalchemy.sql import join

    class AddressUser(object):
        pass

    # define a Join
    j = join(user_table, address_table)

    # map to it - the identity of an AddressUser object will be
    # based on (user_id, address_id) since those are the primary keys involved
    mapper(AddressUser, j, properties={
        'user_id': [user_table.c.user_id, address_table.c.user_id]
    })

Note that the list of columns is equivalent to the usage of `orm.column_property()` with multiple columns:

    from sqlalchemy.orm import mapper, column_property

    mapper(AddressUser, j, properties={
        'user_id': column_property(user_table.c.user_id, address_table.c.user_id)
    })

The usage of `orm.column_property()` is required when using declarative to map to multiple columns, since the declarative class parser won’t recognize a plain list of columns:

    from sqlalchemy.ext.declarative import declarative_base

    Base = declarative_base()

    class AddressUser(Base):
        __table__ = j

        user_id = column_property(user_table.c.user_id, address_table.c.user_id)

A second example:

    from sqlalchemy.sql import join

    # many-to-many join on an association table
    j = join(user_table, userkeywords,
            user_table.c.user_id==userkeywords.c.user_id).join(keywords,
               userkeywords.c.keyword_id==keywords.c.keyword_id)

    # a class
    class KeywordUser(object):
        pass

    # map to it - the identity of a KeywordUser object will be
    # (user_id, keyword_id) since those are the primary keys involved
    mapper(KeywordUser, j, properties={
        'user_id': [user_table.c.user_id, userkeywords.c.user_id],
        'keyword_id': [userkeywords.c.keyword_id, keywords.c.keyword_id]
    })

In both examples above, “composite” columns were added as properties to the mappers; these are aggregations of multiple columns into one mapper property, which instructs the mapper to keep both of those columns set at the same value.

## Mapping a Class against Arbitrary Selects

Similar to mapping against a join, a plain `select()` object can be used with a mapper as well. Below, an example select which contains two aggregate functions and a group_by is mapped to a class:

    from sqlalchemy import select, func

    subq = select([
                func.count(orders.c.id).label('order_count'),
                func.max(orders.c.price).label('highest_order'),
                orders.c.customer_id
                ]).group_by(orders.c.customer_id).alias()

    s = select([customers,subq]).\
                where(customers.c.customer_id==subq.c.customer_id)
    class Customer(object):
        pass

    mapper(Customer, s)

Above, the “customers” table is joined against the “orders” table to produce a full row for each customer row, the total count of related rows in the “orders” table, and the highest price in the “orders” table. That query is then mapped against the Customer class. New instances of Customer will contain attributes for each column in the “customers” table as well as an “order_count” and “highest_order” attribute. Updates to the Customer object will only be reflected in the “customers” table and not the “orders” table. This is because the primary key columns of the “orders” table are not represented in this mapper and therefore the table is not affected by save or delete operations.

## Multiple Mappers for One Class

The first mapper created for a certain class is known as that class’s “primary mapper.” Other mappers can be created as well on the “load side” - these are called secondary mappers. This is a mapper that must be constructed with the keyword argument `non_primary=True`, and represents a load-only mapper. Objects that are loaded with a secondary mapper will have their save operation processed by the primary mapper. It is also invalid to add new `relationship()` objects to a non-primary mapper. To use this mapper with the Session, specify it to the `query` method:

example:

    # primary mapper
    mapper(User, user_table)

    # make a secondary mapper to load User against a join
    othermapper = mapper(User, user_table.join(someothertable), non_primary=True)

    # select
    result = session.query(othermapper).select()

The “non primary mapper” is a rarely needed feature of SQLAlchemy; in most cases, the `Query` object can produce any kind of query that’s desired. It’s recommended that a straight `Query` be used in place of a non-primary mapper unless the mapper approach is absolutely needed. Current use cases for the “non primary mapper” are when you want to map the class to a particular select statement or view to which additional query criterion can be added, and for when the particular mapped select statement or view is to be placed in a `relationship()` of a parent mapper.

## Multiple “Persistence” Mappers for One Class

The non_primary mapper defines alternate mappers for the purposes of loading objects. What if we want the same class to be persisted differently, such as to different tables ? SQLAlchemy refers to this as the “entity name” pattern, and in Python one can use a recipe which creates anonymous subclasses which are distinctly mapped. See the recipe at Entity Name.

## Constructors and Object Initialization

Mapping imposes no restrictions or requirements on the constructor (`__init__`) method for the class. You are free to require any arguments for the function that you wish, assign attributes to the instance that are unknown to the ORM, and generally do anything else you would normally do when writing a constructor for a Python class.

The SQLAlchemy ORM does not call `__init__` when recreating objects from database rows. The ORM’s process is somewhat akin to the Python standard library’s `pickle` module, invoking the low level `__new__` method and then quietly restoring attributes directly on the instance rather than calling `__init__`.

If you need to do some setup on database-loaded instances before they’re ready to use, you can use the `@reconstructor` decorator to tag a method as the ORM counterpart to `__init__`. SQLAlchemy will call this method with no arguments every time it loads or reconstructs one of your instances. This is useful for recreating transient properties that are normally assigned in your `__init__`:

    from sqlalchemy import orm

    class MyMappedClass(object):
        def __init__(self, data):
            self.data = data
            # we need stuff on all instances, but not in the database.
            self.stuff = []

        @orm.reconstructor
        def init_on_load(self):
            self.stuff = []

When `obj = MyMappedClass()` is executed, Python calls the `__init__` method as normal and the `data` argument is required. When instances are loaded during a `Query` operation as in `query(MyMappedClass).one()`, `init_on_load` is called instead.

Any method may be tagged as the `reconstructor()`, even the `__init__` method. SQLAlchemy will call the reconstructor method with no arguments. Scalar (non-collection) database-mapped attributes of the instance will be available for use within the function. Eagerly-loaded collections are generally not yet available and will usually only contain the first element. ORM state changes made to objects at this stage will not be recorded for the next flush() operation, so the activity within a reconstructor should be conservative.

While the ORM does not call your `__init__` method, it will modify the class’s `__init__` slightly. The method is lightly wrapped to act as a trigger for the ORM, allowing mappers to be compiled automatically and will fire a `init_instance()` event that `MapperExtension` objects may listen for. `MapperExtension` objects can also listen for a `reconstruct_instance` event, analogous to the `reconstructor()` decorator above.

`sqlalchemy.orm.``reconstructor`(fn)

  Decorate a method as the ‘reconstructor’ hook.

  Designates a method as the “reconstructor”, an `__init__`-like method that will be called by the ORM after the instance has been loaded from the database or otherwise reconstituted.

  The reconstructor will be invoked with no arguments. Scalar (non-collection) database-mapped attributes of the instance will be available for use within the function. Eagerly-loaded collections are generally not yet available and will usually only contain the first element. ORM state changes made to objects at this stage will not be recorded for the next flush() operation, so the activity within a reconstructor should be conservative.

## Class Mapping API

`sqlalchemy.orm.``mapper`(class_, local_table=None, *args, **params)

  Return a new `Mapper` object.

  - Parameters: | class_ – The class to be mapped. local_table – The table to which the class is mapped, or None if this mapper inherits from another mapper using concrete table inheritance. always_refresh – If True, all query operations for this mapped class will overwrite all data within object instances that already exist within the session, erasing any in-memory changes with whatever information was loaded from the database. Usage of this flag is highly discouraged; as an alternative, see the method `Query.populate_existing()`. allow_null_pks – This flag is deprecated - this is stated as allow_partial_pks which defaults to True. allow_partial_pks – Defaults to True. Indicates that a composite primary key with some NULL values should be considered as possibly existing within the database. This affects whether a mapper will assign an incoming row to an existing identity, as well as if `Session.merge()` will check the database first for a particular primary key value. A “partial primary key” can occur if one has mapped to an OUTER JOIN, for example. batch – Indicates that save operations of multiple entities can be batched together for efficiency. setting to False indicates that an instance will be fully saved before saving the next instance, which includes inserting/updating all table rows corresponding to the entity as well as calling all `MapperExtension` methods corresponding to the save operation. column_prefix – A string which will be prepended to the key name of all `Column` objects when creating column-based properties from the given `Table`. Does not affect explicitly specified column-based properties concrete – If True, indicates this mapper should use concrete table inheritance with its parent mapper. exclude_properties – A list or set of string column names to be excluded from mapping. As of SQLAlchemy 0.6.4, this collection may also include `Column` objects. Columns named or present in this list will not be automatically mapped. Note that neither this option nor include_properties will allow one to circumvent plan Python inheritance - if mapped class `B` inherits from mapped class `A`, no combination of includes or excludes will allow `B` to have fewer properties than its superclass, `A`. extension – A `MapperExtension` instance or list of `MapperExtension` instances which will be applied to all operations by this `Mapper`. Deprecated. Please see `MapperEvents`. include_properties – An inclusive list or set of string column names to map. As of SQLAlchemy 0.6.4, this collection may also include `Column` objects in order to disambiguate between same-named columns in a selectable (such as a `join()`). If this list is not `None`, columns present in the mapped table but not named or present in this list will not be automatically mapped. See also “exclude_properties”. inherits – Another `Mapper` for which this `Mapper` will have an inheritance relationship with. inherit_condition – For joined table inheritance, a SQL expression (constructed `ClauseElement`) which will define how the two tables are joined; defaults to a natural join between the two tables. inherit_foreign_keys – When inherit_condition is used and the condition contains no ForeignKey columns, specify the “foreign” columns of the join condition in this list. else leave as None. non_primary – Construct a `Mapper` that will define only the selection of instances, not their persistence. Any number of non_primary mappers may be created for a particular class. order_by – A single `Column` or list of `Column` objects for which selection operations should use as the default ordering for entities. Defaults to the OID/ROWID of the table if any, or the first primary key column of the table. passive_updates – Indicates UPDATE behavior of foreign keys when a primary key changes on a joined-table inheritance or other joined table mapping. When True, it is assumed that ON UPDATE CASCADE is configured on the foreign key in the database, and that the database will handle propagation of an UPDATE from a source column to dependent rows. Note that with databases which enforce referential integrity (i.e. PostgreSQL, MySQL with InnoDB tables), ON UPDATE CASCADE is required for this operation. The relationship() will update the value of the attribute on related items which are locally present in the session during a flush. When False, it is assumed that the database does not enforce referential integrity and will not be issuing its own CASCADE operation for an update. The relationship() will issue the appropriate UPDATE statements to the database in response to the change of a referenced key, and items locally present in the session during a flush will also be refreshed. This flag should probably be set to False if primary key changes are expected and the database in use doesn’t support CASCADE (i.e. SQLite, MySQL MyISAM tables). Also see the passive_updates flag on `relationship()`. A future SQLAlchemy release will provide a “detect” feature for this flag. polymorphic_on – Used with mappers in an inheritance relationship, a `Column` which will identify the class/mapper combination to be used with a particular row. Requires the `polymorphic_identity` value to be set for all mappers in the inheritance hierarchy. The column specified by `polymorphic_on` is usually a column that resides directly within the base mapper’s mapped table; alternatively, it may be a column that is only present within the <selectable> portion of the `with_polymorphic` argument. polymorphic_identity – A value which will be stored in the Column denoted by polymorphic_on, corresponding to the class identity of this mapper. properties – A dictionary mapping the string names of object attributes to `MapperProperty` instances, which define the persistence behavior of that attribute. Note that the columns in the mapped table are automatically converted into `ColumnProperty` instances based on the `key` property of each `Column` (although they can be overridden using this dictionary). primary_key – A list of `Column` objects which define the primary key to be used against this mapper’s selectable unit. This is normally simply the primary key of the `local_table`, but can be overridden here. version_id_col – A `Column` which must have an integer type that will be used to keep a running version id of mapped entities in the database. this is used during save operations to ensure that no other thread or process has updated the instance during the lifetime of the entity, else a `StaleDataError` exception is thrown. version_id_generator – A callable which defines the algorithm used to generate new version ids. Defaults to an integer generator. Can be replaced with one that generates timestamps, uuids, etc. e.g.:
        import uuid

        mapper(Cls, table,
        version_id_col=table.c.version_uuid,
        version_id_generator=lambda version:uuid.uuid4().hex
        )
  The callable receives the current version identifier as its single argument. with_polymorphic – A tuple in the form `(<classes>, <selectable>)` indicating the default style of “polymorphic” loading, that is, which tables are queried at once. <classes> is any single or list of mappers and/or classes indicating the inherited classes that should be loaded at once. The special value `'*'` may be used to indicate all descending classes should be loaded immediately. The second tuple argument <selectable> indicates a selectable that will be used to query for multiple classes. Normally, it is left as None, in which case this mapper will form an outer join from the base mapper’s table to that of all desired sub-mappers. When specified, it provides the selectable to be used for polymorphic loading. When with_polymorphic includes mappers which load from a “concrete” inheriting table, the <selectable> argument is required, since it usually requires more complex UNION queries.

`sqlalchemy.orm.``object_mapper`(instance)

  Given an object, return the primary Mapper associated with the object instance.

  Raises UnmappedInstanceError if no mapping is configured.

`sqlalchemy.orm.``class_mapper`(class_, compile=True)

  Given a class, return the primary `Mapper` associated with the key.

  Raises `UnmappedClassError` if no mapping is configured on the given class, or `ArgumentError` if a non-class object is passed.

`sqlalchemy.orm.``compile_mappers`()

  Initialize the inter-mapper relationships of all mappers that have been defined.

  Deprecated since version 0.7: `compile_mappers()` is renamed to `configure_mappers()`

`sqlalchemy.orm.``configure_mappers`()

  Initialize the inter-mapper relationships of all mappers that have been constructed thus far.

  This function can be called any number of times, but in most cases is handled internally.

`sqlalchemy.orm.``clear_mappers`()

  Remove all mappers from all classes.

  This function removes all instrumentation from classes and disposes of their associated mappers. Once called, the classes are unmapped and can be later re-mapped with new mappers.

  `clear_mappers()` is not for normal use, as there is literally no valid usage for it outside of very specific testing scenarios. Normally, mappers are permanent structural components of user-defined classes, and are never discarded independently of their class. If a mapped class itself is garbage collected, its mapper is automatically disposed of as well. As such, `clear_mappers()` is only for usage in test suites that re-use the same classes with different mappings, which is itself an extremely rare use case - the only such use case is in fact SQLAlchemy’s own test suite, and possibly the test suites of other ORM extension libraries which intend to test various combinations of mapper construction upon a fixed set of classes.

`sqlalchemy.orm.util.``identity_key`(*args, **kwargs)

  Get an identity key.

  Valid call signatures:

  - `identity_key(class, ident)`

    class

      mapped class (must be a positional argument)

    ident

      primary key, if the key is composite this is a tuple

  - `identity_key(instance=instance)`

    instance

      object instance (must be given as a keyword arg)

  - `identity_key(class, row=row)`

    class

      mapped class (must be a positional argument)

    row

      result proxy row (must be given as a keyword arg)

`sqlalchemy.orm.util.``polymorphic_union`(table_map, typecolname, aliasname='p_union', cast_nulls=True)

  Create a `UNION` statement used by a polymorphic mapper.

  See Concrete Table Inheritance for an example of how this is used.

  Parameters: | table_map – mapping of polymorphic identities to `Table` objects. typecolname – string name of a “discriminator” column, which will be derived from the query, producing the polymorphic identity for each row. If `None`, no polymorphic discriminator is generated. aliasname – name of the `alias()` construct generated. cast_nulls – if True, non-existent columns, which are represented as labeled NULLs, will be passed into CAST. This is a legacy behavior that is problematic on some backends such as Oracle - in which case it can be set to False.

class `sqlalchemy.orm.mapper.``Mapper`(class_, local_table, properties=None, primary_key=None, non_primary=False, inherits=None, inherit_condition=None, inherit_foreign_keys=None, extension=None, order_by=False, always_refresh=False, version_id_col=None, version_id_generator=None, polymorphic_on=None, _polymorphic_map=None, polymorphic_identity=None, concrete=False, with_polymorphic=None, allow_null_pks=None, allow_partial_pks=True, batch=True, column_prefix=None, include_properties=None, exclude_properties=None, passive_updates=True, eager_defaults=False, _compiled_cache_size=100)

  Define the correlation of class attributes to database table columns.

  Instances of this class should be constructed via the `mapper()` function.

  `__init__`(class_, local_table, properties=None, primary_key=None, non_primary=False, inherits=None, inherit_condition=None, inherit_foreign_keys=None, extension=None, order_by=False, always_refresh=False, version_id_col=None, version_id_generator=None, polymorphic_on=None, _polymorphic_map=None, polymorphic_identity=None, concrete=False, with_polymorphic=None, allow_null_pks=None, allow_partial_pks=True, batch=True, column_prefix=None, include_properties=None, exclude_properties=None, passive_updates=True, eager_defaults=False, _compiled_cache_size=100)

    Construct a new mapper.

    Mappers are normally constructed via the `mapper()` function. See for details.

  `add_properties`(dict_of_properties)

    Add the given dictionary of properties to this mapper, using add_property.

  `add_property`(key, prop)

    Add an individual MapperProperty to this mapper.

    If the mapper has not been configured yet, just adds the property to the initial properties dictionary sent to the constructor. If this Mapper has already been configured, then the given MapperProperty is configured immediately.

  `base_mapper`

    The base-most `Mapper` in an inheritance chain.

    In a non-inheriting scenario, this attribute will always be this `Mapper`. In an inheritance scenario, it references the `Mapper` which is parent to all other `Mapper` objects in the inheritance chain.

    This is a read only attribute determined during mapper construction. Behavior is undefined if directly modified.

  `c`

    A synonym for `columns`.

  `cascade_iterator`(type_, state, halt_on=None)

    Iterate each element and its mapper in an object graph, for all relationships that meet the given cascade rule.

    Parameters: | type – The name of the cascade rule (i.e. save-update, delete, etc.) state – The lead InstanceState. child items will be processed per the relationships defined for this object’s mapper.

    the return value are object instances; this provides a strong reference so that they don’t fall out of scope immediately.

  `class_`

    The Python class which this `Mapper` maps.

    This is a read only attribute determined during mapper construction. Behavior is undefined if directly modified.

  `class_manager`

    The `ClassManager` which maintains event listeners and class-bound descriptors for this `Mapper`.

    This is a read only attribute determined during mapper construction. Behavior is undefined if directly modified.

  `columns`

    A collection of `Column` or other scalar expression objects maintained by this `Mapper`.

    The collection behaves the same as that of the `c` attribute on any `Table` object, except that only those columns included in this mapping are present, and are keyed based on the attribute name defined in the mapping, not necessarily the `key` attribute of the `Column` itself. Additionally, scalar expressions mapped by `column_property()` are also present here.

    This is a read only attribute determined during mapper construction. Behavior is undefined if directly modified.

  `common_parent`(other)

    Return true if the given mapper shares a common inherited parent as this mapper.

  `compile`()

    Initialize the inter-mapper relationships of all mappers that

    Deprecated since version 0.7: `Mapper.compile()` is replaced by `configure_mappers()`

    have been constructed thus far.

  `compiled`

    Deprecated since version 0.7: `Mapper.compiled` is replaced by `Mapper.configured`

  `concrete`

    Represent `True` if this `Mapper` is a concrete inheritance mapper.

    This is a read only attribute determined during mapper construction. Behavior is undefined if directly modified.

  `configured`

    Represent `True` if this `Mapper` has been configured.

    This is a read only attribute determined during mapper construction. Behavior is undefined if directly modified.

    See also `configure_mappers()`.

  `get_property`(key, _compile_mappers=True)

    return a MapperProperty associated with the given key.

  `get_property_by_column`(column)

    Given a `Column` object, return the `MapperProperty` which maps this column.

  `identity_key_from_instance`(instance)

    Return the identity key for the given instance, based on its primary key attributes.

    This value is typically also found on the instance state under the attribute name key.

  `identity_key_from_primary_key`(primary_key)

    Return an identity-map key for use in storing/retrieving an item from an identity map.

    primary_key
      A list of values indicating the identifier.
  `identity_key_from_row`(row, adapter=None)

    Return an identity-map key for use in storing/retrieving an item from the identity map.

    row
      A `sqlalchemy.engine.base.RowProxy` instance or a dictionary corresponding result-set `ColumnElement` instances to their values within a row.
  `inherits`

    References the `Mapper` which this `Mapper` inherits from, if any.

    This is a read only attribute determined during mapper construction. Behavior is undefined if directly modified.

  `isa`(other)

    Return True if the this mapper inherits from the given mapper.

  `iterate_properties`

    return an iterator of all MapperProperty objects.

  `local_table`

    The `Selectable` which this `Mapper` manages.

    Typically is an instance of `Table` or `Alias`. May also be `None`.

    The “local” table is the selectable that the `Mapper` is directly responsible for managing from an attribute access and flush perspective. For non-inheriting mappers, the local table is the same as the “mapped” table. For joined-table inheritance mappers, local_table will be the particular sub-table of the overall “join” which this `Mapper` represents. If this mapper is a single-table inheriting mapper, local_table will be `None`.

    See also `mapped_table`.

  `mapped_table`

    The `Selectable` to which this `Mapper` is mapped.

    Typically an instance of `Table`, `Join`, or `Alias`.

    The “mapped” table is the selectable that the mapper selects from during queries. For non-inheriting mappers, the mapped table is the same as the “local” table. For joined-table inheritance mappers, mapped_table references the full `Join` representing full rows for this particular subclass. For single-table inheritance mappers, mapped_table references the base table.

    See also `local_table`.

  `non_primary`

    Represent `True` if this `Mapper` is a “non-primary” mapper, e.g. a mapper that is used only to selet rows but not for persistence management.

    This is a read only attribute determined during mapper construction. Behavior is undefined if directly modified.

  `polymorphic_identity`

    Represent an identifier which is matched against the `polymorphic_on` column during result row loading.

    Used only with inheritance, this object can be of any type which is comparable to the type of column represented by `polymorphic_on`.

    This is a read only attribute determined during mapper construction. Behavior is undefined if directly modified.

  `polymorphic_iterator`()

    Iterate through the collection including this mapper and all descendant mappers.

    This includes not just the immediately inheriting mappers but all their inheriting mappers as well.

    To iterate through an entire hierarchy, use `mapper.base_mapper.polymorphic_iterator()`.

  `polymorphic_map`

    A mapping of “polymorphic identity” identifiers mapped to `Mapper` instances, within an inheritance scenario.

    The identifiers can be of any type which is comparable to the type of column represented by `polymorphic_on`.

    An inheritance chain of mappers will all reference the same polymorphic map object. The object is used to correlate incoming result rows to target mappers.

    This is a read only attribute determined during mapper construction. Behavior is undefined if directly modified.

  `polymorphic_on`

    The `Column` specified as the `polymorphic_on` column for this `Mapper`, within an inheritance scenario.

    This attribute may also be of other types besides `Column` in a future SQLAlchemy release.

    This is a read only attribute determined during mapper construction. Behavior is undefined if directly modified.

  `primary_key`

    An iterable containing the collection of `Column` objects which comprise the ‘primary key’ of the mapped table, from the perspective of this `Mapper`.

    This list is against the selectable in `mapped_table`. In the case of inheriting mappers, some columns may be managed by a superclass mapper. For example, in the case of a `Join`, the primary key is determined by all of the primary key columns across all tables referenced by the `Join`.

    The list is also not necessarily the same as the primary key column collection associated with the underlying tables; the `Mapper` features a `primary_key` argument that can override what the `Mapper` considers as primary key columns.

    This is a read only attribute determined during mapper construction. Behavior is undefined if directly modified.

  `primary_key_from_instance`(instance)

    Return the list of primary key values for the given instance.

  `primary_mapper`()

    Return the primary mapper corresponding to this mapper’s class key (class).

  `self_and_descendants`

    The collection including this mapper and all descendant mappers.

    This includes not just the immediately inheriting mappers but all their inheriting mappers as well.

  `single`

    Represent `True` if this `Mapper` is a single table inheritance mapper.

    `local_table` will be `None` if this flag is set.

    This is a read only attribute determined during mapper construction. Behavior is undefined if directly modified.

  `tables`

    An iterable containing the collection of `Table` objects which this `Mapper` is aware of.

    If the mapper is mapped to a `Join`, or an `Alias` representing a `Select`, the individual `Table` objects that comprise the full construct will be represented here.

    This is a read only attribute determined during mapper construction. Behavior is undefined if directly modified.

  `validators`

    An immutable dictionary of attributes which have been decorated using the `validates()` decorator.

    The dictionary contains string attribute names as keys mapped to the actual validation method.