class Mapper(ORMFromClauseRole, ORMEntityColumnsClauseRole, sql_base.MemoizedHasCacheKey, InspectionAttr):
Defines an association between a Python class and a database table or other relational structure, so that ORM operations against the class may proceed.
The _orm.Mapper
object is instantiated using mapping methods
present on the _orm.registry
object. For information
about instantiating new _orm.Mapper
objects, see
:ref:`orm_mapping_classes_toplevel`.
Method | __clause_element__ |
Undocumented |
Method | __init__ |
Direct constructor for a new _orm.Mapper object. |
Method | __repr__ |
Undocumented |
Method | __str__ |
Undocumented |
Method | _acceptable_polymorphic_identities |
Undocumented |
Method | _adapt_inherited_property |
Undocumented |
Method | _add_with_polymorphic_subclass |
Undocumented |
Method | _all_pk_cols |
Undocumented |
Method | _canload |
Undocumented |
Method | _check_configure |
Undocumented |
Method | _columns_plus_keys |
Undocumented |
Method | _compiled_cache |
Undocumented |
Method | _configure_class_instrumentation |
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. |
Method | _configure_inheritance |
Configure settings related to inheriting and/or inherited mappers being present. |
Method | _configure_pks |
Undocumented |
Method | _configure_polymorphic_setter |
Configure an attribute on the mapper representing the 'polymorphic_on' column, if applicable, and not already generated by _configure_properties (which is typical). |
Method | _configure_properties |
Undocumented |
Method | _configure_property |
Undocumented |
Method | _dataclass_fields |
Undocumented |
Method | _equivalent_columns |
No summary |
Method | _expire_memoizations |
Undocumented |
Method | _filter_properties |
Undocumented |
Method | _gen_cache_key |
return an optional cache key. |
Method | _get_clause |
create a "get clause" based on the primary key. this is used by query.get() and many-to-one lazyloads to load this item by primary key. |
Method | _get_committed_attr_by_column |
Undocumented |
Method | _get_committed_state_attr_by_column |
Undocumented |
Method | _get_state_attr_by_column |
Undocumented |
Method | _identity_key_from_state |
Undocumented |
Method | _identity_key_props |
Undocumented |
Method | _insert_cols_as_none |
Undocumented |
Method | _insert_cols_evaluating_none |
Undocumented |
Method | _is_orphan |
Undocumented |
Method | _is_userland_descriptor |
Undocumented |
Method | _iterate_polymorphic_properties |
Return an iterator of MapperProperty objects which will render into a SELECT. |
Method | _iterate_to_target_viawpoly |
Undocumented |
Method | _log |
Undocumented |
Method | _log_debug |
Undocumented |
Method | _mappers_from_spec |
given a with_polymorphic() argument, return the set of mappers it represents. |
Method | _memo |
Undocumented |
Method | _optimized_get_statement |
assemble a WHERE clause which retrieves a given state by primary key, using a minimized set of tables. |
Method | _persistent_sortkey_fn |
Undocumented |
Method | _pk_attr_keys_by_table |
Undocumented |
Method | _pk_keys_by_table |
Undocumented |
Method | _polymorphic_adapter |
Undocumented |
Method | _polymorphic_properties |
Undocumented |
Method | _post_configure_properties |
Call the init() method on all MapperProperties attached to this mapper. |
Method | _post_inspect |
This hook is invoked by attribute inspection. |
Method | _primary_key_propkeys |
Undocumented |
Method | _prop_set |
Undocumented |
Method | _property_from_column |
generate/update a .ColumnProperty given a _schema.Column object. |
Method | _propkey_to_col |
Undocumented |
Method | _result_has_identity_key |
Undocumented |
Method | _selectable_from_mappers |
given a list of mappers (assumed to be within this mapper's inheritance hierarchy), construct an outerjoin amongst those mapper's mapped tables. |
Method | _server_default_cols |
Undocumented |
Method | _server_default_plus_onupdate_propkeys |
Undocumented |
Method | _server_onupdate_default_cols |
Undocumented |
Method | _set_committed_state_attr_by_column |
Undocumented |
Method | _set_concrete_base |
Set the given _orm.Mapper as the 'inherits' for this _orm.Mapper , assuming this _orm.Mapper is concrete and does not already have an inherits. |
Method | _set_dispose_flags |
Undocumented |
Method | _set_polymorphic_on |
Undocumented |
Method | _set_state_attr_by_column |
Undocumented |
Method | _set_with_polymorphic |
Undocumented |
Method | _should_exclude |
determine whether a particular property should be implicitly present on the class. |
Method | _should_selectin_load |
Undocumented |
Method | _should_undefer_in_wildcard |
Undocumented |
Method | _single_table_criterion |
Undocumented |
Method | _sorted_tables |
Undocumented |
Method | _subclass_load_via_in |
Assemble a that can load the columns local to this subclass as a SELECT with IN. |
Method | _subclass_load_via_in_mapper |
Undocumented |
Method | _version_id_prop |
Undocumented |
Method | _with_polymorphic_args |
Undocumented |
Method | _with_polymorphic_mappers |
Undocumented |
Method | _with_polymorphic_selectable |
Undocumented |
Method | add_properties |
Add the given dictionary of properties to this mapper, using add_property . |
Method | add_property |
Add an individual MapperProperty to this mapper. |
Method | all_orm_descriptors |
A namespace of all .InspectionAttr attributes associated with the mapped class. |
Method | attrs |
A namespace of all .MapperProperty objects associated this mapper. |
Method | cascade_iterator |
Iterate each element and its mapper in an object graph, for all relationships that meet the given cascade rule. |
Method | column_attrs |
Return a namespace of all .ColumnProperty properties maintained by this _orm.Mapper . |
Method | common_parent |
Return true if the given mapper shares a common inherited parent as this mapper. |
Method | composites |
Return a namespace of all .CompositeProperty properties maintained by this _orm.Mapper . |
Method | get_property |
return a MapperProperty associated with the given key. |
Method | get_property_by_column |
Given a _schema.Column object, return the .MapperProperty which maps this column. |
Method | has_property |
Undocumented |
Method | identity_key_from_instance |
Return the identity key for the given instance, based on its primary key attributes. |
Method | identity_key_from_primary_key |
Return an identity-map key for use in storing/retrieving an item from an identity map. |
Method | identity_key_from_row |
Return an identity-map key for use in storing/retrieving an item from the identity map. |
Method | is_sibling |
return true if the other mapper is an inheriting sibling to this one. common parent but different branch |
Method | isa |
Return True if the this mapper inherits from the given mapper. |
Method | iterate_to_root |
Undocumented |
Method | polymorphic_iterator |
Iterate through the collection including this mapper and all descendant mappers. |
Method | primary_key_from_instance |
Return the list of primary key values for the given instance. |
Method | primary_mapper |
Return the primary mapper corresponding to this mapper's class key (class). |
Method | relationships |
A namespace of all .RelationshipProperty properties maintained by this _orm.Mapper . |
Method | self_and_descendants |
The collection including this mapper and all descendant mappers. |
Method | synonyms |
Return a namespace of all .SynonymProperty properties maintained by this _orm.Mapper . |
Class Variable | is_mapper |
Part of the inspection API. |
Class Variable | represents_outer_join |
Undocumented |
Instance Variable | _all_tables |
Undocumented |
Instance Variable | _clause_adapter |
Undocumented |
Instance Variable | _cols_by_table |
Undocumented |
Instance Variable | _columntoproperty |
Undocumented |
Instance Variable | _compiled_cache_size |
Undocumented |
Instance Variable | _delete_orphans |
Undocumented |
Instance Variable | _dependency_processors |
Undocumented |
Instance Variable | _dispose_called |
Undocumented |
Instance Variable | _identity_class |
Undocumented |
Instance Variable | _inheriting_mappers |
Undocumented |
Instance Variable | _inherits_equated_pairs |
Undocumented |
Instance Variable | _init_properties |
Undocumented |
Instance Variable | _memoized_values |
Undocumented |
Instance Variable | _pks_by_table |
Undocumented |
Instance Variable | _primary_key_argument |
Undocumented |
Instance Variable | _props |
Undocumented |
Instance Variable | _readonly_props |
Undocumented |
Instance Variable | _ready_for_configure |
Undocumented |
Instance Variable | _reconstructor |
Undocumented |
Instance Variable | _requires_row_aliasing |
Undocumented |
Instance Variable | _set_polymorphic_identity |
Undocumented |
Instance Variable | _sort_key |
Undocumented |
Instance Variable | _validate_polymorphic_identity |
Undocumented |
Instance Variable | allow_partial_pks |
Undocumented |
Instance Variable | always_refresh |
Undocumented |
Instance Variable | base_mapper |
The base-most _orm.Mapper in an inheritance chain. |
Instance Variable | batch |
Undocumented |
Instance Variable | c |
A synonym for _orm.Mapper.columns . |
Instance Variable | class_ |
The Python class which this _orm.Mapper maps. |
Instance Variable | class_manager |
The .ClassManager which maintains event listeners and class-bound descriptors for this _orm.Mapper . |
Instance Variable | column_prefix |
Undocumented |
Instance Variable | columns |
A collection of _schema.Column or other scalar expression objects maintained by this _orm.Mapper . |
Instance Variable | concrete |
Represent True if this _orm.Mapper is a concrete inheritance mapper. |
Instance Variable | configured |
Represent True if this _orm.Mapper has been configured. |
Instance Variable | confirm_deleted_rows |
Undocumented |
Instance Variable | eager_defaults |
Undocumented |
Instance Variable | exclude_properties |
Undocumented |
Instance Variable | include_properties |
Undocumented |
Instance Variable | inherit_condition |
Undocumented |
Instance Variable | inherit_foreign_keys |
Undocumented |
Instance Variable | inherits |
References the _orm.Mapper which this _orm.Mapper inherits from, if any. |
Instance Variable | legacy_is_orphan |
Undocumented |
Instance Variable | local_table |
The _expression.Selectable which this _orm.Mapper manages. |
Instance Variable | non_primary |
Represent True if this _orm.Mapper is a "non-primary" mapper, e.g. a mapper that is used only to select rows but not for persistence management. |
Instance Variable | passive_deletes |
Undocumented |
Instance Variable | passive_updates |
Undocumented |
Instance Variable | persist_selectable |
The _expression.Selectable to which this _orm.Mapper is mapped. |
Instance Variable | polymorphic_identity |
Represent an identifier which is matched against the _orm.Mapper.polymorphic_on column during result row loading. |
Instance Variable | polymorphic_load |
Undocumented |
Instance Variable | polymorphic_map |
A mapping of "polymorphic identity" identifiers mapped to _orm.Mapper instances, within an inheritance scenario. |
Instance Variable | polymorphic_on |
The _schema.Column or SQL expression specified as the polymorphic_on argument for this _orm.Mapper , within an inheritance scenario. |
Instance Variable | primary_key |
No summary |
Instance Variable | registry |
Undocumented |
Instance Variable | single |
Represent True if this _orm.Mapper is a single table inheritance mapper. |
Instance Variable | tables |
An iterable containing the collection of _schema.Table objects which this _orm.Mapper is aware of. |
Instance Variable | validators |
An immutable dictionary of attributes which have been decorated using the _orm.validates decorator. |
Instance Variable | version_id_col |
Undocumented |
Instance Variable | version_id_generator |
Undocumented |
Instance Variable | version_id_prop |
Undocumented |
Instance Variable | with_polymorphic |
Undocumented |
Property | _all_column_expressions |
Undocumented |
Property | _log_desc |
Undocumented |
Property | _path_registry |
Undocumented |
Property | _table_to_equated |
memoized map of tables to collections of columns to be synchronized upwards to the base mapper. |
Property | entity |
Part of the inspection API. |
Property | entity_namespace |
Undocumented |
Property | iterate_properties |
return an iterator of all MapperProperty objects. |
Property | mapped_table |
Undocumented |
Property | mapper |
Part of the inspection API. |
Property | primary_base_mapper |
Undocumented |
Property | select_identity_token |
Undocumented |
Property | selectable |
The _schema.FromClause construct this _orm.Mapper selects from by default. |
Inherited from ORMFromClauseRole
:
Class Variable | _role_name |
Undocumented |
Inherited from StrictFromClauseRole
(via ORMFromClauseRole
):
Property | description |
Undocumented |
Inherited from FromClauseRole
(via ORMFromClauseRole
, StrictFromClauseRole
):
Class Variable | _is_subquery |
Undocumented |
Property | _hide_froms |
Undocumented |
Inherited from ColumnsClauseRole
(via ORMFromClauseRole
, StrictFromClauseRole
, FromClauseRole
):
Property | _select_iterable |
Undocumented |
Inherited from AllowsLambdaRole
(via ORMFromClauseRole
, StrictFromClauseRole
, FromClauseRole
, ColumnsClauseRole
):
Class Variable | allows_lambda |
Undocumented |
Inherited from UsesInspection
(via ORMFromClauseRole
, StrictFromClauseRole
, FromClauseRole
, ColumnsClauseRole
):
Class Variable | uses_inspection |
Undocumented |
Inherited from SQLRole
(via ORMFromClauseRole
, StrictFromClauseRole
, FromClauseRole
, ColumnsClauseRole
, ColumnListRole
):
Class Variable | allows_lambda |
Undocumented |
Class Variable | uses_inspection |
Undocumented |
Inherited from AllowsLambdaRole
(via ORMFromClauseRole
, StrictFromClauseRole
, FromClauseRole
, JoinTargetRole
):
Class Variable | allows_lambda |
Undocumented |
Inherited from UsesInspection
(via ORMFromClauseRole
, StrictFromClauseRole
, FromClauseRole
, JoinTargetRole
):
Class Variable | uses_inspection |
Undocumented |
Inherited from SQLRole
(via ORMFromClauseRole
, StrictFromClauseRole
, FromClauseRole
, JoinTargetRole
, StructuralRole
):
Class Variable | allows_lambda |
Undocumented |
Class Variable | uses_inspection |
Undocumented |
Inherited from ORMEntityColumnsClauseRole
:
Class Variable | _role_name |
Undocumented |
Inherited from ColumnsClauseRole
(via ORMEntityColumnsClauseRole
, ORMColumnsClauseRole
):
Property | _select_iterable |
Undocumented |
Inherited from AllowsLambdaRole
(via ORMEntityColumnsClauseRole
, ORMColumnsClauseRole
, ColumnsClauseRole
):
Class Variable | allows_lambda |
Undocumented |
Inherited from UsesInspection
(via ORMEntityColumnsClauseRole
, ORMColumnsClauseRole
, ColumnsClauseRole
):
Class Variable | uses_inspection |
Undocumented |
Inherited from SQLRole
(via ORMEntityColumnsClauseRole
, ORMColumnsClauseRole
, ColumnsClauseRole
, ColumnListRole
):
Class Variable | allows_lambda |
Undocumented |
Class Variable | uses_inspection |
Undocumented |
Inherited from MemoizedHasCacheKey
:
Method | _generate_cache_key |
return a cache key. |
Inherited from HasCacheKey
(via MemoizedHasCacheKey
):
Class Variable | inherit_cache |
Indicate if this .HasCacheKey instance should make use of the cache key generation scheme used by its immediate superclass. |
Class Method | _generate_cache_attrs |
generate cache key dispatcher for a new class. |
Class Method | _generate_cache_key_for_object |
Undocumented |
Class Variable | __slots__ |
Undocumented |
Class Variable | _hierarchy_supports_caching |
private attribute which may be set to False to prevent the inherit_cache warning from being emitted for a hierarchy of subclasses. |
Class Variable | _is_has_cache_key |
Undocumented |
Direct constructor for a new _orm.Mapper
object.
The _orm.mapper
function is normally invoked through the
use of the _orm.registry
object through either the
:ref:`Declarative <orm_declarative_mapping>` or
:ref:`Imperative <orm_imperative_mapping>` mapping styles.
_orm.mapper
function should not
be called directly for classical mapping; for a classical mapping
configuration, use the _orm.registry.map_imperatively
method. The _orm.mapper
function may become private in a
future release.Parameters documented below may be passed to either the
_orm.registry.map_imperatively
method, or may be passed in the
__mapper_args__ declarative class attribute described at
:ref:`orm_declarative_mapper_options`.
Parameters | |
class_ | The class to be mapped. When using Declarative, this argument is automatically passed as the declared class itself. |
local_table | The _schema.Table or other selectable
to which the class is mapped. May be None if
this mapper inherits from another mapper using single-table
inheritance. When using Declarative, this argument is
automatically passed by the extension, based on what
is configured via the __table__ argument or via the
_schema.Table
produced as a result of the __tablename__
and _schema.Column arguments present. |
properties | A dictionary mapping the string names of object
attributes to .MapperProperty instances, which define the
persistence behavior of that attribute. Note that
_schema.Column
objects present in
the mapped _schema.Table are automatically placed into
ColumnProperty instances upon mapping, unless overridden.
When using Declarative, this argument is passed automatically,
based on all those .MapperProperty instances declared
in the declared class body. |
primary_key | A list of _schema.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. |
non_primary |
See Also :ref:`relationship_aliased_class` - the new pattern that removes the need for the :paramref:`_orm.Mapper.non_primary` flag. |
inherits | A mapped class or the corresponding
See Also |
inherit_condition | For joined table inheritance, a SQL expression 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 columns present are missing a _schema.ForeignKey
configuration, this parameter can be used to specify which columns
are "foreign". In most cases can be left as None. |
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.Query.populate_existing . |
version_id_col | A See Also :ref:`mapper_version_counter` - discussion of version counting and rationale. |
version_id_generator | Define how new version ids should be generated. Defaults to None, which indicates that a simple integer counting scheme be employed. To provide a custom versioning scheme, provide a callable function of the form: def generate_version(version): return next_version Alternatively, server-side versioning functions such as triggers, or programmatic versioning schemes outside of the version id generator may be used, by specifying the value False. Please see :ref:`server_side_version_counter` for a discussion of important points when using this option.
New in version 0.9.0: version_id_generator supports
server-side version number generation.
|
polymorphic_on | Specifies the column, attribute, or SQL expression used to determine the target class for an incoming row, when inheriting classes are present. This value is commonly a class Employee(Base): __tablename__ = 'employee' id = Column(Integer, primary_key=True) discriminator = Column(String(50)) __mapper_args__ = { "polymorphic_on":discriminator, "polymorphic_identity":"employee" } It may also be specified
as a SQL expression, as in this example where we
use the class Employee(Base): __tablename__ = 'employee' id = Column(Integer, primary_key=True) discriminator = Column(String(50)) __mapper_args__ = { "polymorphic_on":case([ (discriminator == "EN", "engineer"), (discriminator == "MA", "manager"), ], else_="employee"), "polymorphic_identity":"employee" } It may also refer to any attribute
configured with class Employee(Base): __tablename__ = 'employee' id = Column(Integer, primary_key=True) discriminator = Column(String(50)) employee_type = column_property( case([ (discriminator == "EN", "engineer"), (discriminator == "MA", "manager"), ], else_="employee") ) __mapper_args__ = { "polymorphic_on":employee_type, "polymorphic_identity":"employee" } When setting polymorphic_on to reference an
attribute or expression that's not present in the
locally mapped from sqlalchemy import event from sqlalchemy.orm import object_mapper @event.listens_for(Employee, "init", propagate=True) def set_identity(instance, *arg, **kw): mapper = object_mapper(instance) instance.discriminator = mapper.polymorphic_identity Where above, we assign the value of polymorphic_identity for the mapped class to the discriminator attribute, thus persisting the value to the discriminator column in the database. Warning Currently, only one discriminator column may be set, typically on the base-most class in the hierarchy. "Cascading" polymorphic columns are not yet supported. See Also |
_polymorphic_map | Undocumented |
polymorphic_identity | Specifies the value which identifies this particular class as returned by the column expression referred to by the polymorphic_on setting. As rows are received, the value corresponding to the polymorphic_on column expression is compared to this value, indicating which subclass should be used for the newly reconstructed object. |
concrete | If True, indicates this mapper should use concrete table inheritance with its parent mapper. See the section :ref:`concrete_inheritance` for an example. |
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. See Also :ref:`with_polymorphic` - discussion of polymorphic querying techniques. |
polymorphic_load |
New in version 1.2.
|
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 | Defaults to True, indicating 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. This is used in the extremely rare case that a
.MapperEvents listener requires being called
in between individual row persistence operations. |
column_prefix | A string which will be prepended
to the mapped attribute name when See the section :ref:`column_prefix` for an example. |
include_properties | An inclusive list or set of string column names to map. See :ref:`include_exclude_cols` for an example. |
exclude_properties | A list or set of string column names to be excluded from mapping. See :ref:`include_exclude_cols` for an example. |
passive_updates | Indicates UPDATE behavior of foreign key columns when a primary key column changes on a joined-table inheritance mapping. Defaults to True. 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 columns on joined-table rows. 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 unit of work process will emit an UPDATE statement for the dependent columns during a primary key change. See Also :ref:`passive_updates` - description of a similar feature as
used with :paramref:`.mapper.passive_deletes` - supporting ON DELETE CASCADE for joined-table inheritance mappers |
passive_deletes | Indicates DELETE behavior of foreign key columns when a joined-table inheritance entity is being deleted. Defaults to False for a base mapper; for an inheriting mapper, defaults to False unless the value is set to True on the superclass mapper. When True, it is assumed that ON DELETE CASCADE is configured on the foreign key relationships that link this mapper's table to its superclass table, so that when the unit of work attempts to delete the entity, it need only emit a DELETE statement for the superclass table, and not this table. When False, a DELETE statement is emitted for this mapper's table individually. If the primary key attributes local to this table are unloaded, then a SELECT must be emitted in order to validate these attributes; note that the primary key columns of a joined-table subclass are not part of the "primary key" of the object as a whole. Note that a value of True is always forced onto the subclass mappers; that is, it's not possible for a superclass to specify passive_deletes without this taking effect for all subclass mappers.
New in version 1.1.
See Also :ref:`passive_deletes` - description of similar feature as
used with :paramref:`.mapper.passive_updates` - supporting ON UPDATE CASCADE for joined-table inheritance mappers |
confirm_deleted_rows | defaults to True; when a DELETE occurs of one more rows based on specific primary keys, a warning is emitted when the number of rows matched does not equal the number of rows expected. This parameter may be set to False to handle the case where database ON DELETE CASCADE rules may be deleting some of those rows automatically. The warning may be changed to an exception in a future release.
New in version 0.9.4: - added
:paramref:`.mapper.confirm_deleted_rows` as well as conditional
matched row checking on delete.
|
eager_defaults | if True, the ORM will immediately fetch the value of server-generated default values after an INSERT or UPDATE, rather than leaving them as expired to be fetched on next access. This can be used for event schemes where the server-generated values are needed immediately before the flush completes. By default, this scheme will emit an individual SELECT statement per row inserted or updated, which note can add significant performance overhead. However, if the target database supports :term:`RETURNING`, the default values will be returned inline with the INSERT or UPDATE statement, which can greatly enhance performance for an application that needs frequent access to just-generated server defaults. See Also
Changed in version 0.9.0: The eager_defaults option can now
make use of :term:`RETURNING` for backends which support it.
|
legacy_is_orphan | Boolean, defaults to False.
When True, specifies that "legacy" orphan consideration
is to be applied to objects mapped by this mapper, which means
that a pending (that is, not persistent) object is auto-expunged
from an owning See the change note and example at :ref:`legacy_is_orphan_addition` for more detail on this change. |
_compiled_cache_size | Undocumented |
Undocumented
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.
Configure an attribute on the mapper representing the 'polymorphic_on' column, if applicable, and not already generated by _configure_properties (which is typical).
Also create a setter function which will assign this attribute to the value of the 'polymorphic_identity' upon instance construction, also if applicable. This routine will run when an instance is created.
Undocumented
Create a map of all equivalent columns, based on the determination of column pairs that are equated to one another based on inherit condition. This is designed to work with the queries that util.polymorphic_union comes up with, which often don't include the columns from the base table directly (including the subclass table columns only).
The resulting structure is a dictionary of columns mapped to lists of equivalent columns, e.g.:
{ tablea.col1: {tableb.col1, tablec.col1}, tablea.col2: {tabled.col2} }
return an optional cache key.
The cache key is a tuple which can contain any series of objects that are hashable and also identifies this object uniquely within the presence of a larger SQL expression or statement, for the purposes of caching the resulting query.
The cache key should be based on the SQL compiled structure that would ultimately be produced. That is, two structures that are composed in exactly the same way should produce the same cache key; any difference in the structures that would affect the SQL string or the type handlers should result in a different cache key.
If a structure cannot produce a useful cache key, the NO_CACHE symbol should be added to the anon_map and the method should return None.
Undocumented
Undocumented
given a with_polymorphic() argument, return the set of mappers it represents.
Trims the list of mappers to just those represented within the given selectable, if present. This helps some more legacy-ish mappings.
assemble a WHERE clause which retrieves a given state by primary key, using a minimized set of tables.
Applies to a joined-table inheritance mapper where the requested attribute names are only present on joined tables, not the base table. The WHERE clause attempts to include only those tables to minimize joins.
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.
This hook is invoked by attribute inspection.
E.g. when Query calls:
coercions.expect(roles.ColumnsClauseRole, ent, keep_inspect=True)
This allows the inspection process run a configure mappers hook.
.ColumnProperty
given a
_schema.Column
object._orm.Mapper
as the 'inherits' for this
_orm.Mapper
, assuming this _orm.Mapper
is concrete
and does not already have an inherits.determine whether a particular property should be implicitly present on the class.
This occurs when properties are propagated from an inherited class, or are applied from the columns present in the mapped table.
add_property
.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.
A namespace of all .InspectionAttr
attributes associated
with the mapped class.
These attributes are in all cases Python :term:`descriptors` associated with the mapped class or its superclasses.
This namespace includes attributes that are mapped to the class
as well as attributes declared by extension modules.
It includes any Python descriptor type that inherits from
.InspectionAttr
. This includes
.QueryableAttribute
, as well as extension types such as
.hybrid_property
, .hybrid_method
and
.AssociationProxy
.
To distinguish between mapped attributes and extension attributes,
the attribute .InspectionAttr.extension_type
will refer
to a constant that distinguishes between different extension types.
The sorting of the attributes is based on the following rules:
The above process produces an ordering that is deterministic in terms of the order in which attributes were assigned to the class.
_orm.Mapper.all_orm_descriptors
.When dealing with a .QueryableAttribute
, the
.QueryableAttribute.property
attribute refers to the
.MapperProperty
property, which is what you get when
referring to the collection of mapped properties via
_orm.Mapper.attrs
.
Warning
The _orm.Mapper.all_orm_descriptors
accessor namespace is an
instance of .OrderedProperties
. This is
a dictionary-like object which includes a small number of
named methods such as .OrderedProperties.items
and .OrderedProperties.values
. When
accessing attributes dynamically, favor using the dict-access
scheme, e.g. mapper.all_orm_descriptors[somename] over
getattr(mapper.all_orm_descriptors, somename) to avoid name
collisions.
See Also
_orm.Mapper.attrs
A namespace of all .MapperProperty
objects
associated this mapper.
This is an object that provides each property based on
its key name. For instance, the mapper for a
User class which has User.name attribute would
provide mapper.attrs.name, which would be the
.ColumnProperty
representing the name
column. The namespace object can also be iterated,
which would yield each .MapperProperty
.
_orm.Mapper
has several pre-filtered views
of this attribute which limit the types of properties
returned, including .synonyms
, .column_attrs
,
.relationships
, and .composites
.
Warning
The _orm.Mapper.attrs
accessor namespace is an
instance of .OrderedProperties
. This is
a dictionary-like object which includes a small number of
named methods such as .OrderedProperties.items
and .OrderedProperties.values
. When
accessing attributes dynamically, favor using the dict-access
scheme, e.g. mapper.attrs[somename] over
getattr(mapper.attrs, somename) to avoid name collisions.
See Also
_orm.Mapper.all_orm_descriptors
Iterate each element and its mapper in an object graph, for all relationships that meet the given cascade rule.
See Also
:ref:`faq_walk_objects` - illustrates a generic function to traverse all objects without relying on cascades.
Parameters | |
type_ | The name of the cascade rule (i.e. "save-update", "delete", etc.). Note the "all" cascade is not accepted here. For a generic object traversal function, see :ref:`faq_walk_objects`. |
state | The lead InstanceState. child items will be processed per the relationships defined for this object's mapper. |
halt_on | Undocumented |
Returns | |
the method yields individual object instances. |
Return a namespace of all .ColumnProperty
properties maintained by this _orm.Mapper
.
See Also
_orm.Mapper.attrs
- namespace of all
.MapperProperty
objects.
Return a namespace of all .CompositeProperty
properties maintained by this _orm.Mapper
.
See Also
_orm.Mapper.attrs
- namespace of all
.MapperProperty
objects.
_schema.Column
object, return the
.MapperProperty
which maps this column.Return the identity key for the given instance, based on its primary key attributes.
If the instance's state is expired, calling this method
will result in a database check to see if the object has been deleted.
If the row no longer exists,
~sqlalchemy.orm.exc.ObjectDeletedError
is raised.
This value is typically also found on the instance state under the
attribute name key
.
Parameters | |
primary_key | A list of values indicating the identifier. |
identity_token | Undocumented |
Parameters | |
row | A .Row instance. The columns which are
mapped by this _orm.Mapper should be locatable in the row,
preferably via the _schema.Column
object directly (as is the case
when a _expression.select construct is executed), or
via string names of the form <tablename>_<colname>. |
identity_token | Undocumented |
adapter | Undocumented |
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().
Return the list of primary key values for the given instance.
If the instance's state is expired, calling this method
will result in a database check to see if the object has been deleted.
If the row no longer exists,
~sqlalchemy.orm.exc.ObjectDeletedError
is raised.
A namespace of all .RelationshipProperty
properties
maintained by this _orm.Mapper
.
Warning
the _orm.Mapper.relationships
accessor namespace is an
instance of .OrderedProperties
. This is
a dictionary-like object which includes a small number of
named methods such as .OrderedProperties.items
and .OrderedProperties.values
. When
accessing attributes dynamically, favor using the dict-access
scheme, e.g. mapper.relationships[somename] over
getattr(mapper.relationships, somename) to avoid name
collisions.
See Also
_orm.Mapper.attrs
- namespace of all
.MapperProperty
objects.
The collection including this mapper and all descendant mappers.
This includes not just the immediately inheriting mappers but all their inheriting mappers as well.
Return a namespace of all .SynonymProperty
properties maintained by this _orm.Mapper
.
See Also
_orm.Mapper.attrs
- namespace of all
.MapperProperty
objects.
The base-most _orm.Mapper
in an inheritance chain.
In a non-inheriting scenario, this attribute will always be this
_orm.Mapper
. In an inheritance scenario, it references
the _orm.Mapper
which is parent to all other _orm.Mapper
objects in the inheritance chain.
This is a read only attribute determined during mapper construction. Behavior is undefined if directly modified.
The Python class which this _orm.Mapper
maps.
This is a read only attribute determined during mapper construction. Behavior is undefined if directly modified.
The .ClassManager
which maintains event listeners
and class-bound descriptors for this _orm.Mapper
.
This is a read only attribute determined during mapper construction. Behavior is undefined if directly modified.
A collection of _schema.Column
or other scalar expression
objects maintained by this _orm.Mapper
.
The collection behaves the same as that of the c attribute on
any _schema.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
_schema.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.
Represent True if this _orm.Mapper
is a concrete
inheritance mapper.
This is a read only attribute determined during mapper construction. Behavior is undefined if directly modified.
bool
=
Represent True if this _orm.Mapper
has been configured.
This is a read only attribute determined during mapper construction. Behavior is undefined if directly modified.
See Also
.configure_mappers
.
References the _orm.Mapper
which this _orm.Mapper
inherits from, if any.
This is a read only attribute determined during mapper construction. Behavior is undefined if directly modified.
The _expression.Selectable
which this _orm.Mapper
manages.
Typically is an instance of _schema.Table
or
_expression.Alias
.
May also be None.
The "local" table is the
selectable that the _orm.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 _orm.Mapper
represents. If this mapper is a
single-table inheriting mapper, local_table will be None.
See Also
_orm.Mapper.persist_selectable
.
Represent True if this _orm.Mapper
is a "non-primary"
mapper, e.g. a mapper that is used only to select rows but not for
persistence management.
This is a read only attribute determined during mapper construction. Behavior is undefined if directly modified.
The _expression.Selectable
to which this _orm.Mapper
is mapped.
Typically an instance of _schema.Table
,
_expression.Join
, or _expression.Alias
.
The _orm.Mapper.persist_selectable
is separate from
_orm.Mapper.selectable
in that the former represents columns
that are mapped on this class or its superclasses, whereas the
latter may be a "polymorphic" selectable that contains additional columns
which are in fact mapped on subclasses only.
"persist selectable" is the "thing the mapper writes to" and "selectable" is the "thing the mapper selects from".
_orm.Mapper.persist_selectable
is also separate from
_orm.Mapper.local_table
, which represents the set of columns that
are locally mapped on this class directly.
See Also
_orm.Mapper.selectable
.
_orm.Mapper.local_table
.
Represent an identifier which is matched against the
_orm.Mapper.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
_orm.Mapper.polymorphic_on
.
This is a read only attribute determined during mapper construction. Behavior is undefined if directly modified.
A mapping of "polymorphic identity" identifiers mapped to
_orm.Mapper
instances, within an inheritance scenario.
The identifiers can be of any type which is comparable to the
type of column represented by _orm.Mapper.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.
The _schema.Column
or SQL expression specified as the
polymorphic_on argument
for this _orm.Mapper
, within an inheritance scenario.
This attribute is normally a _schema.Column
instance but
may also be an expression, such as one derived from
.cast
.
This is a read only attribute determined during mapper construction. Behavior is undefined if directly modified.
An iterable containing the collection of _schema.Column
objects
which comprise the 'primary key' of the mapped table, from the
perspective of this _orm.Mapper
.
This list is against the selectable in
_orm.Mapper.persist_selectable
.
In the case of inheriting mappers, some columns may be managed by a
superclass mapper. For example, in the case of a
_expression.Join
, the
primary key is determined by all of the primary key columns across all
tables referenced by the _expression.Join
.
The list is also not necessarily the same as the primary key column
collection associated with the underlying tables; the _orm.Mapper
features a primary_key argument that can override what the
_orm.Mapper
considers as primary key columns.
This is a read only attribute determined during mapper construction. Behavior is undefined if directly modified.
bool
=
Represent True if this _orm.Mapper
is a single table
inheritance mapper.
_orm.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.
An iterable containing the collection of _schema.Table
objects
which this _orm.Mapper
is aware of.
If the mapper is mapped to a _expression.Join
, or an
_expression.Alias
representing a _expression.Select
, the individual
_schema.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.
An immutable dictionary of attributes which have been decorated
using the _orm.validates
decorator.
The dictionary contains string attribute names as keys mapped to the actual validation method.