diff options
Diffstat (limited to 'lib/sqlalchemy/orm/mapper.py')
-rw-r--r-- | lib/sqlalchemy/orm/mapper.py | 127 |
1 files changed, 69 insertions, 58 deletions
diff --git a/lib/sqlalchemy/orm/mapper.py b/lib/sqlalchemy/orm/mapper.py index fcd7e18ac..7e5166393 100644 --- a/lib/sqlalchemy/orm/mapper.py +++ b/lib/sqlalchemy/orm/mapper.py @@ -214,13 +214,14 @@ class Mapper(_InspectionAttr): :param 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. + 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. - .. versionadded:: 0.9.4 - added :paramref:`.mapper.confirm_deleted_rows` - as well as conditional matched row checking on delete. + .. versionadded:: 0.9.4 - added + :paramref:`.mapper.confirm_deleted_rows` as well as conditional + matched row checking on delete. :param eager_defaults: if True, the ORM will immediately fetch the value of server-generated default values after an INSERT or UPDATE, @@ -230,8 +231,8 @@ class Mapper(_InspectionAttr): 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 + 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. @@ -269,10 +270,10 @@ class Mapper(_InspectionAttr): define how the two tables are joined; defaults to a natural join between the two tables. - :param inherit_foreign_keys: When ``inherit_condition`` is used and the - columns present are missing a :class:`.ForeignKey` configuration, - this parameter can be used to specify which columns are "foreign". - In most cases can be left as ``None``. + :param inherit_foreign_keys: When ``inherit_condition`` is used and + the columns present are missing a :class:`.ForeignKey` + configuration, this parameter can be used to specify which columns + are "foreign". In most cases can be left as ``None``. :param legacy_is_orphan: Boolean, defaults to ``False``. When ``True``, specifies that "legacy" orphan consideration @@ -280,12 +281,12 @@ class Mapper(_InspectionAttr): that a pending (that is, not persistent) object is auto-expunged from an owning :class:`.Session` only when it is de-associated from *all* parents that specify a ``delete-orphan`` cascade towards - this mapper. The new default behavior is that the object is auto-expunged - when it is de-associated with *any* of its parents that specify - ``delete-orphan`` cascade. This behavior is more consistent with - that of a persistent object, and allows behavior to be consistent - in more scenarios independently of whether or not an orphanable - object has been flushed yet or not. + this mapper. The new default behavior is that the object is + auto-expunged when it is de-associated with *any* of its parents + that specify ``delete-orphan`` cascade. This behavior is more + consistent with that of a persistent object, and allows behavior to + be consistent in more scenarios independently of whether or not an + orphanable object has been flushed yet or not. See the change note and example at :ref:`legacy_is_orphan_addition` for more detail on this change. @@ -296,9 +297,9 @@ class Mapper(_InspectionAttr): is expunged from the :class:`.Session` as soon as it is de-associated from any of its orphan-enabled parents. Previously, the pending object would be expunged only if de-associated - from all of its orphan-enabled parents. The new flag ``legacy_is_orphan`` - is added to :func:`.orm.mapper` which re-establishes the - legacy behavior. + from all of its orphan-enabled parents. The new flag + ``legacy_is_orphan`` is added to :func:`.orm.mapper` which + re-establishes the legacy behavior. :param non_primary: Specify that this :class:`.Mapper` is in addition to the "primary" mapper, that is, the one used for persistence. @@ -447,8 +448,8 @@ class Mapper(_InspectionAttr): based on all those :class:`.MapperProperty` instances declared in the declared class body. - :param primary_key: A list of :class:`.Column` objects which define the - primary key to be used against this mapper's selectable unit. + :param primary_key: A list of :class:`.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. @@ -478,13 +479,13 @@ class Mapper(_InspectionAttr): 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``. + 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. - .. versionadded:: 0.9.0 ``version_id_generator`` supports server-side - version number generation. + .. versionadded:: 0.9.0 ``version_id_generator`` supports + server-side version number generation. .. seealso:: @@ -505,7 +506,8 @@ class Mapper(_InspectionAttr): .. seealso:: - :ref:`with_polymorphic` - discussion of polymorphic querying techniques. + :ref:`with_polymorphic` - discussion of polymorphic querying + techniques. """ @@ -905,7 +907,7 @@ class Mapper(_InspectionAttr): 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: + elif self.local_table is not self.inherits.local_table: if self.concrete: self.mapped_table = self.local_table for mapper in self.iterate_to_root(): @@ -926,9 +928,10 @@ class Mapper(_InspectionAttr): self.inherit_condition) fks = util.to_set(self.inherit_foreign_keys) - self._inherits_equated_pairs = sql_util.criterion_as_pairs( - self.mapped_table.onclause, - consider_as_foreign_keys=fks) + self._inherits_equated_pairs = \ + sql_util.criterion_as_pairs( + self.mapped_table.onclause, + consider_as_foreign_keys=fks) else: self.mapped_table = self.local_table @@ -1375,8 +1378,7 @@ class Mapper(_InspectionAttr): if isinstance(col, schema.Column) and ( self.with_polymorphic is None or self.with_polymorphic[1]. - corresponding_column(col) is None - ): + corresponding_column(col) is None): raise sa_exc.InvalidRequestError( "Could not map polymorphic_on column " "'%s' to the mapped table - polymorphic " @@ -1441,8 +1443,10 @@ class Mapper(_InspectionAttr): if setter: def _set_polymorphic_identity(state): dict_ = state.dict - state.get_impl(polymorphic_key).set(state, dict_, - state.manager.mapper.polymorphic_identity, None) + state.get_impl(polymorphic_key).set( + state, dict_, + state.manager.mapper.polymorphic_identity, + None) def _validate_polymorphic_identity(mapper, state, dict_): if polymorphic_key in dict_ and \ @@ -1458,7 +1462,8 @@ class Mapper(_InspectionAttr): ) self._set_polymorphic_identity = _set_polymorphic_identity - self._validate_polymorphic_identity = _validate_polymorphic_identity + self._validate_polymorphic_identity = \ + _validate_polymorphic_identity else: self._set_polymorphic_identity = None @@ -1977,15 +1982,16 @@ class Mapper(_InspectionAttr): """A namespace of all :class:`._InspectionAttr` attributes associated with the mapped class. - These attributes are in all cases Python :term:`descriptors` associated - with the mapped class or its superclasses. + 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 - :class:`._InspectionAttr`. This includes :class:`.QueryableAttribute`, - as well as extension types such as :class:`.hybrid_property`, - :class:`.hybrid_method` and :class:`.AssociationProxy`. + :class:`._InspectionAttr`. This includes + :class:`.QueryableAttribute`, as well as extension types such as + :class:`.hybrid_property`, :class:`.hybrid_method` and + :class:`.AssociationProxy`. To distinguish between mapped attributes and extension attributes, the attribute :attr:`._InspectionAttr.extension_type` will refer @@ -1993,8 +1999,9 @@ class Mapper(_InspectionAttr): When dealing with a :class:`.QueryableAttribute`, the :attr:`.QueryableAttribute.property` attribute refers to the - :class:`.MapperProperty` property, which is what you get when referring - to the collection of mapped properties via :attr:`.Mapper.attrs`. + :class:`.MapperProperty` property, which is what you get when + referring to the collection of mapped properties via + :attr:`.Mapper.attrs`. .. versionadded:: 0.8.0 @@ -2235,11 +2242,11 @@ class Mapper(_InspectionAttr): """Return an identity-map key for use in storing/retrieving an item from the identity map. - :param row: A :class:`.RowProxy` instance. The columns which are mapped - by this :class:`.Mapper` should be locatable in the row, preferably - via the :class:`.Column` object directly (as is the case when a - :func:`.select` construct is executed), or via string names of the form - ``<tablename>_<colname>``. + :param row: A :class:`.RowProxy` instance. The columns which are + mapped by this :class:`.Mapper` should be locatable in the row, + preferably via the :class:`.Column` object directly (as is the case + when a :func:`.select` construct is executed), or via string names of + the form ``<tablename>_<colname>``. """ pk_cols = self.primary_key @@ -2306,8 +2313,9 @@ class Mapper(_InspectionAttr): for col in self.primary_key ] - def _get_state_attr_by_column(self, state, dict_, column, - passive=attributes.PASSIVE_RETURN_NEVER_SET): + def _get_state_attr_by_column( + self, state, dict_, column, + passive=attributes.PASSIVE_RETURN_NEVER_SET): prop = self._columntoproperty[column] return state.manager[prop.key].impl.get(state, dict_, passive=passive) @@ -2320,9 +2328,9 @@ class Mapper(_InspectionAttr): dict_ = attributes.instance_dict(obj) return self._get_committed_state_attr_by_column(state, dict_, column) - def _get_committed_state_attr_by_column(self, state, dict_, - column, - passive=attributes.PASSIVE_RETURN_NEVER_SET): + def _get_committed_state_attr_by_column( + self, state, dict_, column, + passive=attributes.PASSIVE_RETURN_NEVER_SET): prop = self._columntoproperty[column] return state.manager[prop.key].impl.\ @@ -2384,7 +2392,8 @@ class Mapper(_InspectionAttr): for mapper in reversed(list(self.iterate_to_root())): if mapper.local_table in tables: start = True - elif not isinstance(mapper.local_table, expression.TableClause): + elif not isinstance(mapper.local_table, + expression.TableClause): return None if start and not mapper.single: allconds.append(visitors.cloned_traverse( @@ -2435,8 +2444,9 @@ class Mapper(_InspectionAttr): prop = iterator.popleft() if type_ not in prop.cascade: continue - queue = deque(prop.cascade_iterator(type_, parent_state, - parent_dict, visited_states, halt_on)) + queue = deque(prop.cascade_iterator( + type_, parent_state, parent_dict, + visited_states, halt_on)) if queue: visitables.append((queue, mpp, None, None)) elif item_type is mpp: @@ -2519,7 +2529,8 @@ class Mapper(_InspectionAttr): if m._inherits_equated_pairs and \ cols.intersection( util.reduce(set.union, - [l.proxy_set for l, r in m._inherits_equated_pairs]) + [l.proxy_set for l, r in + m._inherits_equated_pairs]) ): result[table].append((m, m._inherits_equated_pairs)) |