# mapper.py
# Copyright (C) 2005, 2006, 2007, 2008, 2009 Michael Bayer mike_mp@zzzcomputing.com
#
# This module is part of SQLAlchemy and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php
"""Logic to map Python classes to and from selectables.
Defines the :class:`~sqlalchemy.orm.mapper.Mapper` class, the central configurational
unit which associates a class with a database table.
This is a semi-private module; the main configurational API of the ORM is
available in :class:`~sqlalchemy.orm.`.
"""
import types
import weakref
import operator
from itertools import chain
deque = __import__('collections').deque
from sqlalchemy import sql, util, log, exc as sa_exc
from sqlalchemy.sql import expression, visitors, operators, util as sqlutil
from sqlalchemy.orm import attributes, exc, sync
from sqlalchemy.orm.interfaces import (
MapperProperty, EXT_CONTINUE, PropComparator
)
from sqlalchemy.orm.util import (
ExtensionCarrier, _INSTRUMENTOR, _class_to_mapper, _state_has_identity,
_state_mapper, class_mapper, instance_str, state_str,
)
__all__ = (
'Mapper',
'_mapper_registry',
'class_mapper',
'object_mapper',
)
_mapper_registry = weakref.WeakKeyDictionary()
_new_mappers = False
_already_compiling = False
# a list of MapperExtensions that will be installed in all mappers by default
global_extensions = []
# a constant returned by _get_attr_by_column to indicate
# this mapper is not handling an attribute for a particular
# column
NO_ATTRIBUTE = util.symbol('NO_ATTRIBUTE')
# lock used to synchronize the "mapper compile" step
_COMPILE_MUTEX = util.threading.RLock()
# initialize these lazily
ColumnProperty = None
SynonymProperty = None
ComparableProperty = None
RelationProperty = None
ConcreteInheritedProperty = None
_expire_state = None
_state_session = None
class Mapper(object):
"""Define the correlation of class attributes to database table
columns.
Instances of this class should be constructed via the
:func:`~sqlalchemy.orm.mapper` function.
"""
def __init__(self,
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,
polymorphic_on=None,
_polymorphic_map=None,
polymorphic_identity=None,
polymorphic_fetch=None,
concrete=False,
select_table=None,
with_polymorphic=None,
allow_null_pks=False,
batch=True,
column_prefix=None,
include_properties=None,
exclude_properties=None,
eager_defaults=False):
"""Construct a new mapper.
Mappers are normally constructed via the :func:`~sqlalchemy.orm.mapper`
function. See for details.
"""
self.class_ = util.assert_arg_type(class_, type, 'class_')
self.class_manager = None
self.primary_key_argument = primary_key
self.non_primary = non_primary
if order_by:
self.order_by = util.to_list(order_by)
else:
self.order_by = order_by
self.always_refresh = always_refresh
self.version_id_col = version_id_col
self.concrete = concrete
self.single = False
self.inherits = inherits
self.local_table = local_table
self.inherit_condition = inherit_condition
self.inherit_foreign_keys = inherit_foreign_keys
self.extension = extension
self._init_properties = properties or {}
self.allow_null_pks = allow_null_pks
self.delete_orphans = []
self.batch = batch
self.eager_defaults = eager_defaults
self.column_prefix = column_prefix
self.polymorphic_on = polymorphic_on
self._dependency_processors = []
self._validators = {}
self._clause_adapter = None
self._requires_row_aliasing = False
self._inherits_equated_pairs = None
self.select_table = select_table
if select_table:
util.warn_deprecated('select_table option is deprecated. Use with_polymorphic=("*", selectable) '
'instead.')
if with_polymorphic:
raise sa_exc.ArgumentError("select_table can't be used with "
"with_polymorphic (they define conflicting settings)")
self.with_polymorphic = ('*', select_table)
else:
if with_polymorphic == '*':
self.with_polymorphic = ('*', None)
elif isinstance(with_polymorphic, (tuple, list)):
if isinstance(with_polymorphic[0], (basestring, tuple, list)):
self.with_polymorphic = with_polymorphic
else:
self.with_polymorphic = (with_polymorphic, None)
elif with_polymorphic is not None:
raise sa_exc.ArgumentError("Invalid setting for with_polymorphic")
else:
self.with_polymorphic = None
if isinstance(self.local_table, expression._SelectBaseMixin):
util.warn("mapper %s creating an alias for the given "
"selectable. References to the original selectable "
"may be misinterpreted by queries, polymorphic_on, etc. "
" Consider passing an explicit selectable.alias() construct instead." % self)
self.local_table = self.local_table.alias()
if self.with_polymorphic and isinstance(self.with_polymorphic[1], expression._SelectBaseMixin):
self.with_polymorphic = (self.with_polymorphic[0], self.with_polymorphic[1].alias())
# our 'polymorphic identity', a string name that when located in a result set row
# indicates this Mapper should be used to construct the object instance for that row.
self.polymorphic_identity = polymorphic_identity
if polymorphic_fetch:
util.warn_deprecated('polymorphic_fetch option is deprecated. Unloaded columns '
'load as deferred in all cases; loading can be controlled '
'using the "with_polymorphic" option.')
# a dictionary of 'polymorphic identity' names, associating those names with
# Mappers that will be used to construct object instances upon a select operation.
if _polymorphic_map is None:
self.polymorphic_map = {}
else:
self.polymorphic_map = _polymorphic_map
self.include_properties = include_properties
self.exclude_properties = exclude_properties
self.compiled = False
self._configure_inheritance()
self._configure_extensions()
self._configure_class_instrumentation()
self._configure_properties()
self._configure_pks()
global _new_mappers
_new_mappers = True
self._log("constructed")
# configurational / mutating methods. not threadsafe
# except for compile().
def _configure_inheritance(self):
"""Configure settings related to inherting and/or inherited mappers being present."""
# a set of all mappers which inherit from this one.
self._inheriting_mappers = set()
if self.inherits:
if isinstance(self.inherits, type):
self.inherits = class_mapper(self.inherits, compile=False)
if not issubclass(self.class_, self.inherits.class_):
raise sa_exc.ArgumentError(
"Class '%s' does not inherit from '%s'" %
(self.class_.__name__, self.inherits.class_.__name__))
if self.non_primary != self.inherits.non_primary:
np = not self.non_primary and "primary" or "non-primary"
raise sa_exc.ArgumentError("Inheritance of %s mapper for class '%s' is "
"only allowed from a %s mapper" % (np, self.class_.__name__, np))
# inherit_condition is optional.
if self.local_table is None:
self.local_table = self.inherits.local_table
self.mapped_table = self.inherits.mapped_table
self.single = True
elif not self.local_table is self.inherits.local_table:
if self.concrete:
self.mapped_table = self.local_table
for mapper in self.iterate_to_root():
if mapper.polymorphic_on:
mapper._requires_row_aliasing = True
else:
if not self.inherit_condition:
# figure out inherit condition from our table to the immediate table
# of the inherited mapper, not its full table which could pull in other
# stuff we dont want (allows test/inheritance.InheritTest4 to pass)
self.inherit_condition = sqlutil.join_condition(self.inherits.local_table, self.local_table)
self.mapped_table = sql.join(self.inherits.mapped_table, self.local_table, self.inherit_condition)
fks = util.to_set(self.inherit_foreign_keys)
self._inherits_equated_pairs = sqlutil.criterion_as_pairs(self.mapped_table.onclause, consider_as_foreign_keys=fks)
else:
self.mapped_table = self.local_table
if self.polymorphic_identity and not self.concrete:
self._identity_class = self.inherits._identity_class
else:
self._identity_class = self.class_
if self.version_id_col is None:
self.version_id_col = self.inherits.version_id_col
for mapper in self.iterate_to_root():
util.reset_memoized(mapper, '_equivalent_columns')
util.reset_memoized(mapper, '_sorted_tables')
if self.order_by is False and not self.concrete and self.inherits.order_by is not False:
self.order_by = self.inherits.order_by
self.polymorphic_map = self.inherits.polymorphic_map
self.batch = self.inherits.batch
self.inherits._inheriting_mappers.add(self)
self.base_mapper = self.inherits.base_mapper
self._all_tables = self.inherits._all_tables
if self.polymorphic_identity is not None:
self.polymorphic_map[self.polymorphic_identity] = self
if not self.polymorphic_on:
for mapper in self.iterate_to_root():
# try to set up polymorphic on using correesponding_column(); else leave
# as None
if mapper.polymorphic_on:
self.polymorphic_on = self.mapped_table.corresponding_column(mapper.polymorphic_on)
break
else:
self._all_tables = set()
self.base_mapper = self
self.mapped_table = self.local_table
if self.polymorphic_identity:
self.polymorphic_map[self.polymorphic_identity] = self
self._identity_class = self.class_
if self.mapped_table is None:
raise sa_exc.ArgumentError("Mapper '%s' does not have a mapped_table specified." % self)
def _configure_extensions(self):
"""Go through the global_extensions list as well as the list
of ``MapperExtensions`` specified for this ``Mapper`` and
creates a linked list of those extensions.
"""
extlist = util.OrderedSet()
extension = self.extension
if extension:
for ext_obj in util.to_list(extension):
# local MapperExtensions have already instrumented the class
extlist.add(ext_obj)
if self.inherits:
for ext in self.inherits.extension:
if ext not in extlist:
extlist.add(ext)
else:
for ext in global_extensions:
if isinstance(ext, type):
ext = ext()
if ext not in extlist:
extlist.add(ext)
self.extension = ExtensionCarrier()
for ext in extlist:
self.extension.append(ext)
def _configure_class_instrumentation(self):
"""If this mapper is to be a primary mapper (i.e. the
non_primary flag is not set), associate this Mapper with the
given class_ and entity name.
Subsequent calls to ``class_mapper()`` for the class_/entity
name combination will return this mapper. Also decorate the
`__init__` method on the mapped class to include optional
auto-session attachment logic.
"""
manager = attributes.manager_of_class(self.class_)
if self.non_primary:
if not manager or manager.mapper is None:
raise sa_exc.InvalidRequestError(
"Class %s has no primary mapper configured. Configure "
"a primary mapper first before setting up a non primary "
"Mapper.")
self.class_manager = manager
_mapper_registry[self] = True
return
if manager is not None:
assert manager.class_ is self.class_
if manager.mapper:
raise sa_exc.ArgumentError(
"Class '%s' already has a primary mapper defined. "
"Use non_primary=True to "
"create a non primary Mapper. clear_mappers() will "
"remove *all* current mappers from all classes." %
self.class_)
#else:
# a ClassManager may already exist as
# ClassManager.instrument_attribute() creates
# new managers for each subclass if they don't yet exist.
_mapper_registry[self] = True
self.extension.instrument_class(self, self.class_)
if manager is None:
manager = attributes.register_class(self.class_,
deferred_scalar_loader = _load_scalar_attributes
)
self.class_manager = manager
manager.mapper = self
# The remaining members can be added by any mapper, e_name None or not.
if manager.info.get(_INSTRUMENTOR, False):
return
event_registry = manager.events
event_registry.add_listener('on_init', _event_on_init)
event_registry.add_listener('on_init_failure', _event_on_init_failure)
event_registry.add_listener('on_resurrect', _event_on_resurrect)
for key, method in util.iterate_attributes(self.class_):
if isinstance(method, types.FunctionType):
if hasattr(method, '__sa_reconstructor__'):
event_registry.add_listener('on_load', method)
elif hasattr(method, '__sa_validators__'):
for name in method.__sa_validators__:
self._validators[name] = method
if 'reconstruct_instance' in self.extension:
def reconstruct(instance):
self.extension.reconstruct_instance(self, instance)
event_registry.add_listener('on_load', reconstruct)
manager.info[_INSTRUMENTOR] = self
def dispose(self):
# Disable any attribute-based compilation.
self.compiled = True
if hasattr(self, '_compile_failed'):
del self._compile_failed
if not self.non_primary and self.class_manager.mapper is self:
attributes.unregister_class(self.class_)
def _configure_pks(self):
self.tables = sqlutil.find_tables(self.mapped_table)
if not self.tables:
raise sa_exc.InvalidRequestError("Could not find any Table objects in mapped table '%s'" % str(self.mapped_table))
self._pks_by_table = {}
self._cols_by_table = {}
all_cols = util.column_set(chain(*[col.proxy_set for col in self._columntoproperty]))
pk_cols = util.column_set(c for c in all_cols if c.primary_key)
# identify primary key columns which are also mapped by this mapper.
tables = set(self.tables + [self.mapped_table])
self._all_tables.update(tables)
for t in tables:
if t.primary_key and pk_cols.issuperset(t.primary_key):
# ordering is important since it determines the ordering of mapper.primary_key (and therefore query.get())
self._pks_by_table[t] = util.ordered_column_set(t.primary_key).intersection(pk_cols)
self._cols_by_table[t] = util.ordered_column_set(t.c).intersection(all_cols)
# determine cols that aren't expressed within our tables; mark these
# as "read only" properties which are refreshed upon INSERT/UPDATE
self._readonly_props = set(
self._columntoproperty[col]
for col in self._columntoproperty
if not hasattr(col, 'table') or col.table not in self._cols_by_table)
# if explicit PK argument sent, add those columns to the primary key mappings
if self.primary_key_argument:
for k in self.primary_key_argument:
if k.table not in self._pks_by_table:
self._pks_by_table[k.table] = util.OrderedSet()
self._pks_by_table[k.table].add(k)
if self.mapped_table not in self._pks_by_table or len(self._pks_by_table[self.mapped_table]) == 0:
raise sa_exc.ArgumentError("Mapper %s could not assemble any primary "
"key columns for mapped table '%s'" % (self, self.mapped_table.description))
if self.inherits and not self.concrete and not self.primary_key_argument:
# if inheriting, the "primary key" for this mapper is that of the inheriting (unless concrete or explicit)
self.primary_key = self.inherits.primary_key
else:
# determine primary key from argument or mapped_table pks - reduce to the minimal set of columns
if self.primary_key_argument:
primary_key = sqlutil.reduce_columns(
[self.mapped_table.corresponding_column(c) for c in self.primary_key_argument],
ignore_nonexistent_tables=True)
else:
primary_key = sqlutil.reduce_columns(
self._pks_by_table[self.mapped_table], ignore_nonexistent_tables=True)
if len(primary_key) == 0:
raise sa_exc.ArgumentError("Mapper %s could not assemble any primary "
"key columns for mapped table '%s'" % (self, self.mapped_table.description))
self.primary_key = primary_key
self._log("Identified primary key columns: " + str(primary_key))
def _configure_properties(self):
# Column and other ClauseElement objects which are mapped
self.columns = self.c = util.OrderedProperties()
# object attribute names mapped to MapperProperty objects
self._props = util.OrderedDict()
# table columns mapped to lists of MapperProperty objects
# using a list allows a single column to be defined as
# populating multiple object attributes
self._columntoproperty = util.column_dict()
# load custom properties
if self._init_properties:
for key, prop in self._init_properties.iteritems():
self._configure_property(key, prop, False)
# pull properties from the inherited mapper if any.
if self.inherits:
for key, prop in self.inherits._props.iteritems():
if key not in self._props and not self._should_exclude(key, key, local=False):
self._adapt_inherited_property(key, prop, False)
# create properties for each column in the mapped table,
# for those columns which don't already map to a property
for column in self.mapped_table.columns:
if column in self._columntoproperty:
continue
column_key = (self.column_prefix or '') + column.key
if self._should_exclude(column.key, column_key, local=self.local_table.c.contains_column(column)):
continue
# adjust the "key" used for this column to that
# of the inheriting mapper
for mapper in self.iterate_to_root():
if column in mapper._columntoproperty:
column_key = mapper._columntoproperty[column].key
self._configure_property(column_key, column, init=False, setparent=True)
# do a special check for the "discriminiator" column, as it may only be present
# in the 'with_polymorphic' selectable but we need it for the base mapper
if self.polymorphic_on and self.polymorphic_on not in self._columntoproperty:
col = self.mapped_table.corresponding_column(self.polymorphic_on)
if not col:
dont_instrument = True
col = self.polymorphic_on
else:
dont_instrument = False
if self._should_exclude(col.key, col.key, local=False):
raise sa_exc.InvalidRequestError("Cannot exclude or override the discriminator column %r" % col.key)
self._configure_property(col.key, ColumnProperty(col, _no_instrument=dont_instrument), init=False, setparent=True)
def _adapt_inherited_property(self, key, prop, init):
if not self.concrete:
self._configure_property(key, prop, init=False, setparent=False)
elif key not in self._props:
self._configure_property(key, ConcreteInheritedProperty(), init=init, setparent=True)
def _configure_property(self, key, prop, init=True, setparent=True):
self._log("_configure_property(%s, %s)" % (key, prop.__class__.__name__))
if not isinstance(prop, MapperProperty):
# we were passed a Column or a list of Columns; generate a ColumnProperty
columns = util.to_list(prop)
column = columns[0]
if not expression.is_column(column):
raise sa_exc.ArgumentError("%s=%r is not an instance of MapperProperty or Column" % (key, prop))
prop = self._props.get(key, None)
if isinstance(prop, ColumnProperty):
# TODO: the "property already exists" case is still not well defined here.
# assuming single-column, etc.
if prop.parent is not self:
# existing ColumnProperty from an inheriting mapper.
# make a copy and append our column to it
prop = prop.copy()
prop.columns.append(column)
self._log("appending to existing ColumnProperty %s" % (key))
elif prop is None or isinstance(prop, ConcreteInheritedProperty):
mapped_column = []
for c in columns:
mc = self.mapped_table.corresponding_column(c)
if not mc:
raise sa_exc.ArgumentError("Column '%s' is not represented in mapper's table. "
"Use the `column_property()` function to force this column "
"to be mapped as a read-only attribute." % c)
mapped_column.append(mc)
prop = ColumnProperty(*mapped_column)
else:
raise sa_exc.ArgumentError("WARNING: column '%s' conflicts with property '%r'. "
"To resolve this, map the column to the class under a different "
"name in the 'properties' dictionary. Or, to remove all awareness "
"of the column entirely (including its availability as a foreign key), "
"use the 'include_properties' or 'exclude_properties' mapper arguments "
"to control specifically which table columns get mapped." % (column.key, prop))
if isinstance(prop, ColumnProperty):
col = self.mapped_table.corresponding_column(prop.columns[0])
# col might not be present! the selectable given to the mapper need not include "deferred"
# columns (included in zblog tests)
if col is None:
col = prop.columns[0]
# column is coming in after _readonly_props was initialized; check
# for 'readonly'
if hasattr(self, '_readonly_props') and \
(not hasattr(col, 'table') or col.table not in self._cols_by_table):
self._readonly_props.add(prop)
else:
# if column is coming in after _cols_by_table was initialized, ensure the col is in the
# right set
if hasattr(self, '_cols_by_table') and col.table in self._cols_by_table and col not in self._cols_by_table[col.table]:
self._cols_by_table[col.table].add(col)
# if this ColumnProperty represents the "polymorphic discriminator"
# column, mark it. We'll need this when rendering columns
# in SELECT statements.
if not hasattr(prop, '_is_polymorphic_discriminator'):
prop._is_polymorphic_discriminator = (col is self.polymorphic_on or prop.columns[0] is self.polymorphic_on)
self.columns[key] = col
for col in prop.columns:
for col in col.proxy_set:
self._columntoproperty[col] = prop
elif isinstance(prop, (ComparableProperty, SynonymProperty)) and setparent:
if prop.descriptor is None:
desc = getattr(self.class_, key, None)
if self._is_userland_descriptor(desc):
prop.descriptor = desc
if getattr(prop, 'map_column', False):
if key not in self.mapped_table.c:
raise sa_exc.ArgumentError(
"Can't compile synonym '%s': no column on table '%s' named '%s'"
% (prop.name, self.mapped_table.description, key))
self._configure_property(prop.name, ColumnProperty(self.mapped_table.c[key]), init=init, setparent=setparent)
self._props[key] = prop
prop.key = key
if setparent:
prop.set_parent(self)
if not self.non_primary:
prop.instrument_class(self)
for mapper in self._inheriting_mappers:
mapper._adapt_inherited_property(key, prop, init)
if init:
prop.init()
prop.post_instrument_class(self)
def compile(self):
"""Compile this mapper and all other non-compiled mappers.
This method checks the local compiled status as well as for
any new mappers that have been defined, and is safe to call
repeatedly.
"""
global _new_mappers
if self.compiled and not _new_mappers:
return self
_COMPILE_MUTEX.acquire()
try:
try:
global _already_compiling
if _already_compiling:
# re-entrance to compile() occurs rarely, when a class-mapped construct is
# used within a ForeignKey, something that is possible
# when using the declarative layer
self._post_configure_properties()
return
_already_compiling = True
try:
# double-check inside mutex
if self.compiled and not _new_mappers:
return self
# initialize properties on all mappers
for mapper in list(_mapper_registry):
if getattr(mapper, '_compile_failed', False):
raise sa_exc.InvalidRequestError("One or more mappers failed to compile. Exception was probably "
"suppressed within a hasattr() call. "
"Message was: %s" % mapper._compile_failed)
if not mapper.compiled:
mapper._post_configure_properties()
_new_mappers = False
return self
finally:
_already_compiling = False
except:
import sys
exc = sys.exc_info()[1]
self._compile_failed = exc
raise
finally:
_COMPILE_MUTEX.release()
def _post_configure_properties(self):
"""Call the ``init()`` method on all ``MapperProperties``
attached to this mapper.
This is a deferred configuration step which is intended
to execute once all mappers have been constructed.
"""
self._log("_post_configure_properties() started")
l = [(key, prop) for key, prop in self._props.iteritems()]
for key, prop in l:
self._log("initialize prop " + key)
if prop.parent is self and not prop._compile_started:
prop.init()
if prop._compile_finished:
prop.post_instrument_class(self)
self._log("_post_configure_properties() complete")
self.compiled = True
def add_properties(self, dict_of_properties):
"""Add the given dictionary of properties to this mapper,
using `add_property`.
"""
for key, value in dict_of_properties.iteritems():
self.add_property(key, value)
def add_property(self, key, prop):
"""Add an individual MapperProperty to this mapper.
If the mapper has not been compiled yet, just adds the
property to the initial properties dictionary sent to the
constructor. If this Mapper has already been compiled, then
the given MapperProperty is compiled immediately.
"""
self._init_properties[key] = prop
self._configure_property(key, prop, init=self.compiled)
# class formatting / logging.
def _log(self, msg):
if self._should_log_info:
self.logger.info(
"(" + self.class_.__name__ +
"|" +
(self.local_table and self.local_table.description or str(self.local_table)) +
(self.non_primary and "|non-primary" or "") + ") " +
msg)
def _log_debug(self, msg):
if self._should_log_debug:
self.logger.debug(
"(" + self.class_.__name__ +
"|" +
(self.local_table and self.local_table.description or str(self.local_table)) +
(self.non_primary and "|non-primary" or "") + ") " +
msg)
def __repr__(self):
return '