1# orm/mapper.py
2# Copyright (C) 2005-2026 the SQLAlchemy authors and contributors
3# <see AUTHORS file>
4#
5# This module is part of SQLAlchemy and is released under
6# the MIT License: https://www.opensource.org/licenses/mit-license.php
7# mypy: allow-untyped-defs, allow-untyped-calls
8
9"""Logic to map Python classes to and from selectables.
10
11Defines the :class:`~sqlalchemy.orm.mapper.Mapper` class, the central
12configurational unit which associates a class with a database table.
13
14This is a semi-private module; the main configurational API of the ORM is
15available in :class:`~sqlalchemy.orm.`.
16
17"""
18
19from __future__ import annotations
20
21from collections import deque
22from functools import reduce
23from itertools import chain
24import sys
25import threading
26from typing import Any
27from typing import Callable
28from typing import cast
29from typing import Collection
30from typing import Deque
31from typing import Dict
32from typing import FrozenSet
33from typing import Generic
34from typing import Iterable
35from typing import Iterator
36from typing import List
37from typing import Literal
38from typing import Mapping
39from typing import Optional
40from typing import Sequence
41from typing import Set
42from typing import Tuple
43from typing import Type
44from typing import TYPE_CHECKING
45from typing import TypeVar
46from typing import Union
47import weakref
48
49from . import attributes
50from . import exc as orm_exc
51from . import instrumentation
52from . import loading
53from . import properties
54from . import util as orm_util
55from ._typing import _O
56from .base import _class_to_mapper
57from .base import _parse_mapper_argument
58from .base import _state_mapper
59from .base import PassiveFlag
60from .base import state_str
61from .interfaces import _MappedAttribute
62from .interfaces import EXT_SKIP
63from .interfaces import InspectionAttr
64from .interfaces import MapperProperty
65from .interfaces import ORMEntityColumnsClauseRole
66from .interfaces import ORMFromClauseRole
67from .interfaces import StrategizedProperty
68from .path_registry import PathRegistry
69from .. import event
70from .. import exc as sa_exc
71from .. import inspection
72from .. import log
73from .. import schema
74from .. import sql
75from .. import util
76from ..event import dispatcher
77from ..event import EventTarget
78from ..sql import base as sql_base
79from ..sql import coercions
80from ..sql import expression
81from ..sql import operators
82from ..sql import roles
83from ..sql import TableClause
84from ..sql import util as sql_util
85from ..sql import visitors
86from ..sql.cache_key import MemoizedHasCacheKey
87from ..sql.elements import KeyedColumnElement
88from ..sql.schema import Column
89from ..sql.schema import Table
90from ..sql.selectable import LABEL_STYLE_TABLENAME_PLUS_COL
91from ..util import HasMemoized
92from ..util import HasMemoized_ro_memoized_attribute
93from ..util.typing import TupleAny
94from ..util.typing import Unpack
95
96if TYPE_CHECKING:
97 from ._typing import _IdentityKeyType
98 from ._typing import _InstanceDict
99 from ._typing import _ORMColumnExprArgument
100 from ._typing import _RegistryType
101 from .decl_api import registry
102 from .dependency import _DependencyProcessor
103 from .descriptor_props import CompositeProperty
104 from .descriptor_props import SynonymProperty
105 from .events import MapperEvents
106 from .instrumentation import ClassManager
107 from .path_registry import _CachingEntityRegistry
108 from .properties import ColumnProperty
109 from .relationships import RelationshipProperty
110 from .state import InstanceState
111 from .util import ORMAdapter
112 from ..engine import Row
113 from ..engine import RowMapping
114 from ..sql._typing import _ColumnExpressionArgument
115 from ..sql._typing import _EquivalentColumnMap
116 from ..sql.base import _EntityNamespace
117 from ..sql.base import ReadOnlyColumnCollection
118 from ..sql.elements import ColumnClause
119 from ..sql.elements import ColumnElement
120 from ..sql.selectable import FromClause
121 from ..util import OrderedSet
122
123
124_T = TypeVar("_T", bound=Any)
125_MP = TypeVar("_MP", bound="MapperProperty[Any]")
126_Fn = TypeVar("_Fn", bound="Callable[..., Any]")
127
128
129_WithPolymorphicArg = Union[
130 Literal["*"],
131 Tuple[
132 Union[Literal["*"], Sequence[Union["Mapper[Any]", Type[Any]]]],
133 Optional["FromClause"],
134 ],
135 Sequence[Union["Mapper[Any]", Type[Any]]],
136]
137
138
139_mapper_registries: weakref.WeakKeyDictionary[_RegistryType, bool] = (
140 weakref.WeakKeyDictionary()
141)
142
143
144def _all_registries() -> Set[registry]:
145 with _CONFIGURE_MUTEX:
146 return set(_mapper_registries)
147
148
149def _unconfigured_mappers() -> Iterator[Mapper[Any]]:
150 for reg in _all_registries():
151 yield from reg._mappers_to_configure()
152
153
154_already_compiling = False
155
156
157# a constant returned by _get_attr_by_column to indicate
158# this mapper is not handling an attribute for a particular
159# column
160NO_ATTRIBUTE = util.symbol("NO_ATTRIBUTE")
161
162# lock used to synchronize the "mapper configure" step
163_CONFIGURE_MUTEX = threading.RLock()
164
165
166@inspection._self_inspects
167@log.class_logger
168class Mapper(
169 ORMFromClauseRole,
170 ORMEntityColumnsClauseRole[_O],
171 MemoizedHasCacheKey,
172 InspectionAttr,
173 log.Identified,
174 inspection.Inspectable["Mapper[_O]"],
175 EventTarget,
176 Generic[_O],
177):
178 """Defines an association between a Python class and a database table or
179 other relational structure, so that ORM operations against the class may
180 proceed.
181
182 The :class:`_orm.Mapper` object is instantiated using mapping methods
183 present on the :class:`_orm.registry` object. For information
184 about instantiating new :class:`_orm.Mapper` objects, see
185 :ref:`orm_mapping_classes_toplevel`.
186
187 """
188
189 dispatch: dispatcher[Mapper[_O]]
190
191 _dispose_called = False
192 _configure_failed: Any = False
193 _ready_for_configure = False
194
195 def __init__(
196 self,
197 class_: Type[_O],
198 local_table: Optional[FromClause] = None,
199 properties: Optional[Mapping[str, MapperProperty[Any]]] = None,
200 primary_key: Optional[Iterable[_ORMColumnExprArgument[Any]]] = None,
201 inherits: Optional[Union[Mapper[Any], Type[Any]]] = None,
202 inherit_condition: Optional[_ColumnExpressionArgument[bool]] = None,
203 inherit_foreign_keys: Optional[
204 Sequence[_ORMColumnExprArgument[Any]]
205 ] = None,
206 always_refresh: bool = False,
207 version_id_col: Optional[_ORMColumnExprArgument[Any]] = None,
208 version_id_generator: Optional[
209 Union[Literal[False], Callable[[Any], Any]]
210 ] = None,
211 polymorphic_on: Optional[
212 Union[_ORMColumnExprArgument[Any], str, MapperProperty[Any]]
213 ] = None,
214 _polymorphic_map: Optional[Dict[Any, Mapper[Any]]] = None,
215 polymorphic_identity: Optional[Any] = None,
216 concrete: bool = False,
217 with_polymorphic: Optional[_WithPolymorphicArg] = None,
218 polymorphic_abstract: bool = False,
219 polymorphic_load: Optional[Literal["selectin", "inline"]] = None,
220 allow_partial_pks: bool = True,
221 batch: bool = True,
222 column_prefix: Optional[str] = None,
223 include_properties: Optional[Sequence[str]] = None,
224 exclude_properties: Optional[Sequence[str]] = None,
225 passive_updates: bool = True,
226 passive_deletes: bool = False,
227 confirm_deleted_rows: bool = True,
228 eager_defaults: Literal[True, False, "auto"] = "auto",
229 legacy_is_orphan: bool = False,
230 _compiled_cache_size: int = 100,
231 ):
232 r"""Direct constructor for a new :class:`_orm.Mapper` object.
233
234 The :class:`_orm.Mapper` constructor is not called directly, and
235 is normally invoked through the
236 use of the :class:`_orm.registry` object through either the
237 :ref:`Declarative <orm_declarative_mapping>` or
238 :ref:`Imperative <orm_imperative_mapping>` mapping styles.
239
240 .. versionchanged:: 2.0 The public facing ``mapper()`` function is
241 removed; for a classical mapping configuration, use the
242 :meth:`_orm.registry.map_imperatively` method.
243
244 Parameters documented below may be passed to either the
245 :meth:`_orm.registry.map_imperatively` method, or may be passed in the
246 ``__mapper_args__`` declarative class attribute described at
247 :ref:`orm_declarative_mapper_options`.
248
249 :param class\_: The class to be mapped. When using Declarative,
250 this argument is automatically passed as the declared class
251 itself.
252
253 :param local_table: The :class:`_schema.Table` or other
254 :class:`_sql.FromClause` (i.e. selectable) to which the class is
255 mapped. May be ``None`` if this mapper inherits from another mapper
256 using single-table inheritance. When using Declarative, this
257 argument is automatically passed by the extension, based on what is
258 configured via the :attr:`_orm.DeclarativeBase.__table__` attribute
259 or via the :class:`_schema.Table` produced as a result of
260 the :attr:`_orm.DeclarativeBase.__tablename__` attribute being
261 present.
262
263 :param polymorphic_abstract: Indicates this class will be mapped in a
264 polymorphic hierarchy, but not directly instantiated. The class is
265 mapped normally, except that it has no requirement for a
266 :paramref:`_orm.Mapper.polymorphic_identity` within an inheritance
267 hierarchy. The class however must be part of a polymorphic
268 inheritance scheme which uses
269 :paramref:`_orm.Mapper.polymorphic_on` at the base.
270
271 .. versionadded:: 2.0
272
273 .. seealso::
274
275 :ref:`orm_inheritance_abstract_poly`
276
277 :param always_refresh: If True, all query operations for this mapped
278 class will overwrite all data within object instances that already
279 exist within the session, erasing any in-memory changes with
280 whatever information was loaded from the database. Usage of this
281 flag is highly discouraged; as an alternative, see the method
282 :meth:`_query.Query.populate_existing`.
283
284 :param allow_partial_pks: Defaults to True. Indicates that a
285 composite primary key with some NULL values should be considered as
286 possibly existing within the database. This affects whether a
287 mapper will assign an incoming row to an existing identity, as well
288 as if :meth:`.Session.merge` will check the database first for a
289 particular primary key value. A "partial primary key" can occur if
290 one has mapped to an OUTER JOIN, for example.
291
292 The :paramref:`.orm.Mapper.allow_partial_pks` parameter also
293 indicates to the ORM relationship lazy loader, when loading a
294 many-to-one related object, if a composite primary key that has
295 partial NULL values should result in an attempt to load from the
296 database, or if a load attempt is not necessary.
297
298 .. versionadded:: 2.0.36 :paramref:`.orm.Mapper.allow_partial_pks`
299 is consulted by the relationship lazy loader strategy, such that
300 when set to False, a SELECT for a composite primary key that
301 has partial NULL values will not be emitted.
302
303 :param batch: Defaults to ``True``, indicating that save operations
304 of multiple entities can be batched together for efficiency.
305 Setting to False indicates
306 that an instance will be fully saved before saving the next
307 instance. This is used in the extremely rare case that a
308 :class:`.MapperEvents` listener requires being called
309 in between individual row persistence operations.
310
311 :param column_prefix: A string which will be prepended
312 to the mapped attribute name when :class:`_schema.Column`
313 objects are automatically assigned as attributes to the
314 mapped class. Does not affect :class:`.Column` objects that
315 are mapped explicitly in the :paramref:`.Mapper.properties`
316 dictionary.
317
318 This parameter is typically useful with imperative mappings
319 that keep the :class:`.Table` object separate. Below, assuming
320 the ``user_table`` :class:`.Table` object has columns named
321 ``user_id``, ``user_name``, and ``password``::
322
323 class User(Base):
324 __table__ = user_table
325 __mapper_args__ = {"column_prefix": "_"}
326
327 The above mapping will assign the ``user_id``, ``user_name``, and
328 ``password`` columns to attributes named ``_user_id``,
329 ``_user_name``, and ``_password`` on the mapped ``User`` class.
330
331 The :paramref:`.Mapper.column_prefix` parameter is uncommon in
332 modern use. For dealing with reflected tables, a more flexible
333 approach to automating a naming scheme is to intercept the
334 :class:`.Column` objects as they are reflected; see the section
335 :ref:`mapper_automated_reflection_schemes` for notes on this usage
336 pattern.
337
338 :param concrete: If True, indicates this mapper should use concrete
339 table inheritance with its parent mapper.
340
341 See the section :ref:`concrete_inheritance` for an example.
342
343 :param confirm_deleted_rows: defaults to True; when a DELETE occurs
344 of one more rows based on specific primary keys, a warning is
345 emitted when the number of rows matched does not equal the number
346 of rows expected. This parameter may be set to False to handle the
347 case where database ON DELETE CASCADE rules may be deleting some of
348 those rows automatically. The warning may be changed to an
349 exception in a future release.
350
351 :param eager_defaults: if True, the ORM will immediately fetch the
352 value of server-generated default values after an INSERT or UPDATE,
353 rather than leaving them as expired to be fetched on next access.
354 This can be used for event schemes where the server-generated values
355 are needed immediately before the flush completes.
356
357 The fetch of values occurs either by using ``RETURNING`` inline
358 with the ``INSERT`` or ``UPDATE`` statement, or by adding an
359 additional ``SELECT`` statement subsequent to the ``INSERT`` or
360 ``UPDATE``, if the backend does not support ``RETURNING``.
361
362 The use of ``RETURNING`` is extremely performant in particular for
363 ``INSERT`` statements where SQLAlchemy can take advantage of
364 :ref:`insertmanyvalues <engine_insertmanyvalues>`, whereas the use of
365 an additional ``SELECT`` is relatively poor performing, adding
366 additional SQL round trips which would be unnecessary if these new
367 attributes are not to be accessed in any case.
368
369 For this reason, :paramref:`.Mapper.eager_defaults` defaults to the
370 string value ``"auto"``, which indicates that server defaults for
371 INSERT should be fetched using ``RETURNING`` if the backing database
372 supports it and if the dialect in use supports "insertmanyreturning"
373 for an INSERT statement. If the backing database does not support
374 ``RETURNING`` or "insertmanyreturning" is not available, server
375 defaults will not be fetched.
376
377 .. versionchanged:: 2.0.0rc1 added the "auto" option for
378 :paramref:`.Mapper.eager_defaults`
379
380 .. seealso::
381
382 :ref:`orm_server_defaults`
383
384 .. versionchanged:: 2.0.0 RETURNING now works with multiple rows
385 INSERTed at once using the
386 :ref:`insertmanyvalues <engine_insertmanyvalues>` feature, which
387 among other things allows the :paramref:`.Mapper.eager_defaults`
388 feature to be very performant on supporting backends.
389
390 :param exclude_properties: A list or set of string column names to
391 be excluded from mapping.
392
393 .. seealso::
394
395 :ref:`include_exclude_cols`
396
397 :param include_properties: An inclusive list or set of string column
398 names to map.
399
400 .. seealso::
401
402 :ref:`include_exclude_cols`
403
404 :param inherits: A mapped class or the corresponding
405 :class:`_orm.Mapper`
406 of one indicating a superclass to which this :class:`_orm.Mapper`
407 should *inherit* from. The mapped class here must be a subclass
408 of the other mapper's class. When using Declarative, this argument
409 is passed automatically as a result of the natural class
410 hierarchy of the declared classes.
411
412 .. seealso::
413
414 :ref:`inheritance_toplevel`
415
416 :param inherit_condition: For joined table inheritance, a SQL
417 expression which will
418 define how the two tables are joined; defaults to a natural join
419 between the two tables.
420
421 :param inherit_foreign_keys: When ``inherit_condition`` is used and
422 the columns present are missing a :class:`_schema.ForeignKey`
423 configuration, this parameter can be used to specify which columns
424 are "foreign". In most cases can be left as ``None``.
425
426 :param legacy_is_orphan: Boolean, defaults to ``False``.
427 When ``True``, specifies that "legacy" orphan consideration
428 is to be applied to objects mapped by this mapper, which means
429 that a pending (that is, not persistent) object is auto-expunged
430 from an owning :class:`.Session` only when it is de-associated
431 from *all* parents that specify a ``delete-orphan`` cascade towards
432 this mapper. The new default behavior is that the object is
433 auto-expunged when it is de-associated with *any* of its parents
434 that specify ``delete-orphan`` cascade. This behavior is more
435 consistent with that of a persistent object, and allows behavior to
436 be consistent in more scenarios independently of whether or not an
437 orphan object has been flushed yet or not.
438
439 See the change note and example at :ref:`legacy_is_orphan_addition`
440 for more detail on this change.
441
442 :param passive_deletes: Indicates DELETE behavior of foreign key
443 columns when a joined-table inheritance entity is being deleted.
444 Defaults to ``False`` for a base mapper; for an inheriting mapper,
445 defaults to ``False`` unless the value is set to ``True``
446 on the superclass mapper.
447
448 When ``True``, it is assumed that ON DELETE CASCADE is configured
449 on the foreign key relationships that link this mapper's table
450 to its superclass table, so that when the unit of work attempts
451 to delete the entity, it need only emit a DELETE statement for the
452 superclass table, and not this table.
453
454 When ``False``, a DELETE statement is emitted for this mapper's
455 table individually. If the primary key attributes local to this
456 table are unloaded, then a SELECT must be emitted in order to
457 validate these attributes; note that the primary key columns
458 of a joined-table subclass are not part of the "primary key" of
459 the object as a whole.
460
461 Note that a value of ``True`` is **always** forced onto the
462 subclass mappers; that is, it's not possible for a superclass
463 to specify passive_deletes without this taking effect for
464 all subclass mappers.
465
466 .. seealso::
467
468 :ref:`passive_deletes` - description of similar feature as
469 used with :func:`_orm.relationship`
470
471 :paramref:`.mapper.passive_updates` - supporting ON UPDATE
472 CASCADE for joined-table inheritance mappers
473
474 :param passive_updates: Indicates UPDATE behavior of foreign key
475 columns when a primary key column changes on a joined-table
476 inheritance mapping. Defaults to ``True``.
477
478 When True, it is assumed that ON UPDATE CASCADE is configured on
479 the foreign key in the database, and that the database will handle
480 propagation of an UPDATE from a source column to dependent columns
481 on joined-table rows.
482
483 When False, it is assumed that the database does not enforce
484 referential integrity and will not be issuing its own CASCADE
485 operation for an update. The unit of work process will
486 emit an UPDATE statement for the dependent columns during a
487 primary key change.
488
489 .. seealso::
490
491 :ref:`passive_updates` - description of a similar feature as
492 used with :func:`_orm.relationship`
493
494 :paramref:`.mapper.passive_deletes` - supporting ON DELETE
495 CASCADE for joined-table inheritance mappers
496
497 :param polymorphic_load: Specifies "polymorphic loading" behavior
498 for a subclass in an inheritance hierarchy (joined and single
499 table inheritance only). Valid values are:
500
501 * "'inline'" - specifies this class should be part of
502 the "with_polymorphic" mappers, e.g. its columns will be included
503 in a SELECT query against the base.
504
505 * "'selectin'" - specifies that when instances of this class
506 are loaded, an additional SELECT will be emitted to retrieve
507 the columns specific to this subclass. The SELECT uses
508 IN to fetch multiple subclasses at once.
509
510 .. seealso::
511
512 :ref:`with_polymorphic_mapper_config`
513
514 :ref:`polymorphic_selectin`
515
516 :param polymorphic_on: Specifies the column, attribute, or
517 SQL expression used to determine the target class for an
518 incoming row, when inheriting classes are present.
519
520 May be specified as a string attribute name, or as a SQL
521 expression such as a :class:`_schema.Column` or in a Declarative
522 mapping a :func:`_orm.mapped_column` object. It is typically
523 expected that the SQL expression corresponds to a column in the
524 base-most mapped :class:`.Table`::
525
526 class Employee(Base):
527 __tablename__ = "employee"
528
529 id: Mapped[int] = mapped_column(primary_key=True)
530 discriminator: Mapped[str] = mapped_column(String(50))
531
532 __mapper_args__ = {
533 "polymorphic_on": discriminator,
534 "polymorphic_identity": "employee",
535 }
536
537 It may also be specified
538 as a SQL expression, as in this example where we
539 use the :func:`.case` construct to provide a conditional
540 approach::
541
542 class Employee(Base):
543 __tablename__ = "employee"
544
545 id: Mapped[int] = mapped_column(primary_key=True)
546 discriminator: Mapped[str] = mapped_column(String(50))
547
548 __mapper_args__ = {
549 "polymorphic_on": case(
550 (discriminator == "EN", "engineer"),
551 (discriminator == "MA", "manager"),
552 else_="employee",
553 ),
554 "polymorphic_identity": "employee",
555 }
556
557 It may also refer to any attribute using its string name,
558 which is of particular use when using annotated column
559 configurations::
560
561 class Employee(Base):
562 __tablename__ = "employee"
563
564 id: Mapped[int] = mapped_column(primary_key=True)
565 discriminator: Mapped[str]
566
567 __mapper_args__ = {
568 "polymorphic_on": "discriminator",
569 "polymorphic_identity": "employee",
570 }
571
572 When setting ``polymorphic_on`` to reference an
573 attribute or expression that's not present in the
574 locally mapped :class:`_schema.Table`, yet the value
575 of the discriminator should be persisted to the database,
576 the value of the
577 discriminator is not automatically set on new
578 instances; this must be handled by the user,
579 either through manual means or via event listeners.
580 A typical approach to establishing such a listener
581 looks like::
582
583 from sqlalchemy import event
584 from sqlalchemy.orm import object_mapper
585
586
587 @event.listens_for(Employee, "init", propagate=True)
588 def set_identity(instance, *arg, **kw):
589 mapper = object_mapper(instance)
590 instance.discriminator = mapper.polymorphic_identity
591
592 Where above, we assign the value of ``polymorphic_identity``
593 for the mapped class to the ``discriminator`` attribute,
594 thus persisting the value to the ``discriminator`` column
595 in the database.
596
597 .. warning::
598
599 Currently, **only one discriminator column may be set**, typically
600 on the base-most class in the hierarchy. "Cascading" polymorphic
601 columns are not yet supported.
602
603 .. seealso::
604
605 :ref:`inheritance_toplevel`
606
607 :param polymorphic_identity: Specifies the value which
608 identifies this particular class as returned by the column expression
609 referred to by the :paramref:`_orm.Mapper.polymorphic_on` setting. As
610 rows are received, the value corresponding to the
611 :paramref:`_orm.Mapper.polymorphic_on` column expression is compared
612 to this value, indicating which subclass should be used for the newly
613 reconstructed object.
614
615 .. seealso::
616
617 :ref:`inheritance_toplevel`
618
619 :param properties: A dictionary mapping the string names of object
620 attributes to :class:`.MapperProperty` instances, which define the
621 persistence behavior of that attribute. Note that
622 :class:`_schema.Column`
623 objects present in
624 the mapped :class:`_schema.Table` are automatically placed into
625 ``ColumnProperty`` instances upon mapping, unless overridden.
626 When using Declarative, this argument is passed automatically,
627 based on all those :class:`.MapperProperty` instances declared
628 in the declared class body.
629
630 .. seealso::
631
632 :ref:`orm_mapping_properties` - in the
633 :ref:`orm_mapping_classes_toplevel`
634
635 :param primary_key: A list of :class:`_schema.Column`
636 objects, or alternatively string names of attribute names which
637 refer to :class:`_schema.Column`, which define
638 the primary key to be used against this mapper's selectable unit.
639 This is normally simply the primary key of the ``local_table``, but
640 can be overridden here.
641
642 .. versionchanged:: 2.0.2 :paramref:`_orm.Mapper.primary_key`
643 arguments may be indicated as string attribute names as well.
644
645 .. seealso::
646
647 :ref:`mapper_primary_key` - background and example use
648
649 :param version_id_col: A :class:`_schema.Column`
650 that will be used to keep a running version id of rows
651 in the table. This is used to detect concurrent updates or
652 the presence of stale data in a flush. The methodology is to
653 detect if an UPDATE statement does not match the last known
654 version id, a
655 :class:`~sqlalchemy.orm.exc.StaleDataError` exception is
656 thrown.
657 By default, the column must be of :class:`.Integer` type,
658 unless ``version_id_generator`` specifies an alternative version
659 generator.
660
661 .. seealso::
662
663 :ref:`mapper_version_counter` - discussion of version counting
664 and rationale.
665
666 :param version_id_generator: Define how new version ids should
667 be generated. Defaults to ``None``, which indicates that
668 a simple integer counting scheme be employed. To provide a custom
669 versioning scheme, provide a callable function of the form::
670
671 def generate_version(version):
672 return next_version
673
674 Alternatively, server-side versioning functions such as triggers,
675 or programmatic versioning schemes outside of the version id
676 generator may be used, by specifying the value ``False``.
677 Please see :ref:`server_side_version_counter` for a discussion
678 of important points when using this option.
679
680 .. seealso::
681
682 :ref:`custom_version_counter`
683
684 :ref:`server_side_version_counter`
685
686
687 :param with_polymorphic: A tuple in the form ``(<classes>,
688 <selectable>)`` indicating the default style of "polymorphic"
689 loading, that is, which tables are queried at once. <classes> is
690 any single or list of mappers and/or classes indicating the
691 inherited classes that should be loaded at once. The special value
692 ``'*'`` may be used to indicate all descending classes should be
693 loaded immediately. The second tuple argument <selectable>
694 indicates a selectable that will be used to query for multiple
695 classes.
696
697 The :paramref:`_orm.Mapper.polymorphic_load` parameter may be
698 preferable over the use of :paramref:`_orm.Mapper.with_polymorphic`
699 in modern mappings to indicate a per-subclass technique of
700 indicating polymorphic loading styles.
701
702 .. seealso::
703
704 :ref:`with_polymorphic_mapper_config`
705
706 """
707 self.class_ = util.assert_arg_type(class_, type, "class_")
708 self._sort_key = "%s.%s" % (
709 self.class_.__module__,
710 self.class_.__name__,
711 )
712
713 self._primary_key_argument = util.to_list(primary_key)
714
715 self.always_refresh = always_refresh
716
717 if isinstance(version_id_col, MapperProperty):
718 self.version_id_prop = version_id_col
719 self.version_id_col = None
720 else:
721 self.version_id_col = (
722 coercions.expect(
723 roles.ColumnArgumentOrKeyRole,
724 version_id_col,
725 argname="version_id_col",
726 )
727 if version_id_col is not None
728 else None
729 )
730
731 if version_id_generator is False:
732 self.version_id_generator = False
733 elif version_id_generator is None:
734 self.version_id_generator = lambda x: (x or 0) + 1
735 else:
736 self.version_id_generator = version_id_generator
737
738 self.concrete = concrete
739 self.single = False
740
741 if inherits is not None:
742 self.inherits = _parse_mapper_argument(inherits)
743 else:
744 self.inherits = None
745
746 if local_table is not None:
747 self.local_table = coercions.expect(
748 roles.FromClauseRole,
749 local_table,
750 disable_inspection=True,
751 argname="local_table",
752 )
753 elif self.inherits:
754 # note this is a new flow as of 2.0 so that
755 # .local_table need not be Optional
756 self.local_table = self.inherits.local_table
757 self.single = True
758 else:
759 raise sa_exc.ArgumentError(
760 f"Mapper[{self.class_.__name__}(None)] has None for a "
761 "primary table argument and does not specify 'inherits'"
762 )
763
764 if inherit_condition is not None:
765 self.inherit_condition = coercions.expect(
766 roles.OnClauseRole, inherit_condition
767 )
768 else:
769 self.inherit_condition = None
770
771 self.inherit_foreign_keys = inherit_foreign_keys
772 self._init_properties = dict(properties) if properties else {}
773 self._delete_orphans = []
774 self.batch = batch
775 self.eager_defaults = eager_defaults
776 self.column_prefix = column_prefix
777
778 # interim - polymorphic_on is further refined in
779 # _configure_polymorphic_setter
780 self.polymorphic_on = (
781 coercions.expect( # type: ignore
782 roles.ColumnArgumentOrKeyRole,
783 polymorphic_on,
784 argname="polymorphic_on",
785 )
786 if polymorphic_on is not None
787 else None
788 )
789 self.polymorphic_abstract = polymorphic_abstract
790 self._dependency_processors = []
791 self.validators = util.EMPTY_DICT
792 self.passive_updates = passive_updates
793 self.passive_deletes = passive_deletes
794 self.legacy_is_orphan = legacy_is_orphan
795 self._clause_adapter = None
796 self._requires_row_aliasing = False
797 self._inherits_equated_pairs = None
798 self._memoized_values = {}
799 self._compiled_cache_size = _compiled_cache_size
800 self._reconstructor = None
801 self.allow_partial_pks = allow_partial_pks
802
803 if self.inherits and not self.concrete:
804 self.confirm_deleted_rows = False
805 else:
806 self.confirm_deleted_rows = confirm_deleted_rows
807
808 self._set_with_polymorphic(with_polymorphic)
809 self.polymorphic_load = polymorphic_load
810
811 # our 'polymorphic identity', a string name that when located in a
812 # result set row indicates this Mapper should be used to construct
813 # the object instance for that row.
814 self.polymorphic_identity = polymorphic_identity
815
816 # a dictionary of 'polymorphic identity' names, associating those
817 # names with Mappers that will be used to construct object instances
818 # upon a select operation.
819 if _polymorphic_map is None:
820 self.polymorphic_map = {}
821 else:
822 self.polymorphic_map = _polymorphic_map
823
824 if include_properties is not None:
825 self.include_properties = util.to_set(include_properties)
826 else:
827 self.include_properties = None
828 if exclude_properties:
829 self.exclude_properties = util.to_set(exclude_properties)
830 else:
831 self.exclude_properties = None
832
833 # prevent this mapper from being constructed
834 # while a configure_mappers() is occurring (and defer a
835 # configure_mappers() until construction succeeds)
836 with _CONFIGURE_MUTEX:
837 cast("MapperEvents", self.dispatch._events)._new_mapper_instance(
838 class_, self
839 )
840 self._configure_inheritance()
841 self._configure_class_instrumentation()
842 self._configure_properties()
843 self._configure_polymorphic_setter()
844 self._configure_pks()
845 self.registry._flag_new_mapper(self)
846 self._log("constructed")
847 self._expire_memoizations()
848
849 self.dispatch.after_mapper_constructed(self, self.class_)
850
851 def _prefer_eager_defaults(self, dialect, table):
852 if self.eager_defaults == "auto":
853 if not table.implicit_returning:
854 return False
855
856 return (
857 table in self._server_default_col_keys
858 and dialect.insert_executemany_returning
859 )
860 else:
861 return self.eager_defaults
862
863 def _gen_cache_key(self, anon_map, bindparams):
864 return (self,)
865
866 # ### BEGIN
867 # ATTRIBUTE DECLARATIONS START HERE
868
869 is_mapper = True
870 """Part of the inspection API."""
871
872 represents_outer_join = False
873
874 registry: _RegistryType
875
876 @property
877 def mapper(self) -> Mapper[_O]:
878 """Part of the inspection API.
879
880 Returns self.
881
882 """
883 return self
884
885 @property
886 def entity(self):
887 r"""Part of the inspection API.
888
889 Returns self.class\_.
890
891 """
892 return self.class_
893
894 class_: Type[_O]
895 """The class to which this :class:`_orm.Mapper` is mapped."""
896
897 _identity_class: Type[_O]
898
899 _delete_orphans: List[Tuple[str, Type[Any]]]
900 _dependency_processors: List[_DependencyProcessor]
901 _memoized_values: Dict[Any, Callable[[], Any]]
902 _inheriting_mappers: util.WeakSequence[Mapper[Any]]
903 _all_tables: Set[TableClause]
904 _polymorphic_attr_key: Optional[str]
905
906 _pks_by_table: Dict[FromClause, OrderedSet[ColumnClause[Any]]]
907 _cols_by_table: Dict[FromClause, OrderedSet[ColumnElement[Any]]]
908
909 _props: util.OrderedDict[str, MapperProperty[Any]]
910 _init_properties: Dict[str, MapperProperty[Any]]
911
912 _columntoproperty: _ColumnMapping
913
914 _set_polymorphic_identity: Optional[Callable[[InstanceState[_O]], None]]
915 _validate_polymorphic_identity: Optional[
916 Callable[[Mapper[_O], InstanceState[_O], _InstanceDict], None]
917 ]
918
919 tables: Sequence[TableClause]
920 """A sequence containing the collection of :class:`_schema.Table`
921 or :class:`_schema.TableClause` objects which this :class:`_orm.Mapper`
922 is aware of.
923
924 If the mapper is mapped to a :class:`_expression.Join`, or an
925 :class:`_expression.Alias`
926 representing a :class:`_expression.Select`, the individual
927 :class:`_schema.Table`
928 objects that comprise the full construct will be represented here.
929
930 This is a *read only* attribute determined during mapper construction.
931 Behavior is undefined if directly modified.
932
933 """
934
935 validators: util.immutabledict[str, Tuple[str, Dict[str, Any]]]
936 """An immutable dictionary of attributes which have been decorated
937 using the :func:`_orm.validates` decorator.
938
939 The dictionary contains string attribute names as keys
940 mapped to the actual validation method.
941
942 """
943
944 always_refresh: bool
945 allow_partial_pks: bool
946 version_id_col: Optional[ColumnElement[Any]]
947
948 with_polymorphic: Optional[
949 Tuple[
950 Union[Literal["*"], Sequence[Union[Mapper[Any], Type[Any]]]],
951 Optional[FromClause],
952 ]
953 ]
954
955 version_id_generator: Optional[Union[Literal[False], Callable[[Any], Any]]]
956
957 local_table: FromClause
958 """The immediate :class:`_expression.FromClause` to which this
959 :class:`_orm.Mapper` refers.
960
961 Typically is an instance of :class:`_schema.Table`, may be any
962 :class:`.FromClause`.
963
964 The "local" table is the
965 selectable that the :class:`_orm.Mapper` is directly responsible for
966 managing from an attribute access and flush perspective. For
967 non-inheriting mappers, :attr:`.Mapper.local_table` will be the same
968 as :attr:`.Mapper.persist_selectable`. For inheriting mappers,
969 :attr:`.Mapper.local_table` refers to the specific portion of
970 :attr:`.Mapper.persist_selectable` that includes the columns to which
971 this :class:`.Mapper` is loading/persisting, such as a particular
972 :class:`.Table` within a join.
973
974 .. seealso::
975
976 :attr:`_orm.Mapper.persist_selectable`.
977
978 :attr:`_orm.Mapper.selectable`.
979
980 """
981
982 persist_selectable: FromClause
983 """The :class:`_expression.FromClause` to which this :class:`_orm.Mapper`
984 is mapped.
985
986 Typically is an instance of :class:`_schema.Table`, may be any
987 :class:`.FromClause`.
988
989 The :attr:`_orm.Mapper.persist_selectable` is similar to
990 :attr:`.Mapper.local_table`, but represents the :class:`.FromClause` that
991 represents the inheriting class hierarchy overall in an inheritance
992 scenario.
993
994 :attr.`.Mapper.persist_selectable` is also separate from the
995 :attr:`.Mapper.selectable` attribute, the latter of which may be an
996 alternate subquery used for selecting columns.
997 :attr.`.Mapper.persist_selectable` is oriented towards columns that
998 will be written on a persist operation.
999
1000 .. seealso::
1001
1002 :attr:`_orm.Mapper.selectable`.
1003
1004 :attr:`_orm.Mapper.local_table`.
1005
1006 """
1007
1008 inherits: Optional[Mapper[Any]]
1009 """References the :class:`_orm.Mapper` which this :class:`_orm.Mapper`
1010 inherits from, if any.
1011
1012 """
1013
1014 inherit_condition: Optional[ColumnElement[bool]]
1015
1016 configured: bool = False
1017 """Represent ``True`` if this :class:`_orm.Mapper` has been configured.
1018
1019 This is a *read only* attribute determined during mapper construction.
1020 Behavior is undefined if directly modified.
1021
1022 .. seealso::
1023
1024 :func:`.configure_mappers`.
1025
1026 """
1027
1028 concrete: bool
1029 """Represent ``True`` if this :class:`_orm.Mapper` is a concrete
1030 inheritance mapper.
1031
1032 This is a *read only* attribute determined during mapper construction.
1033 Behavior is undefined if directly modified.
1034
1035 """
1036
1037 primary_key: Tuple[ColumnElement[Any], ...]
1038 """An iterable containing the collection of :class:`_schema.Column`
1039 objects
1040 which comprise the 'primary key' of the mapped table, from the
1041 perspective of this :class:`_orm.Mapper`.
1042
1043 This list is against the selectable in
1044 :attr:`_orm.Mapper.persist_selectable`.
1045 In the case of inheriting mappers, some columns may be managed by a
1046 superclass mapper. For example, in the case of a
1047 :class:`_expression.Join`, the
1048 primary key is determined by all of the primary key columns across all
1049 tables referenced by the :class:`_expression.Join`.
1050
1051 The list is also not necessarily the same as the primary key column
1052 collection associated with the underlying tables; the :class:`_orm.Mapper`
1053 features a ``primary_key`` argument that can override what the
1054 :class:`_orm.Mapper` considers as primary key columns.
1055
1056 This is a *read only* attribute determined during mapper construction.
1057 Behavior is undefined if directly modified.
1058
1059 """
1060
1061 class_manager: ClassManager[_O]
1062 """The :class:`.ClassManager` which maintains event listeners
1063 and class-bound descriptors for this :class:`_orm.Mapper`.
1064
1065 This is a *read only* attribute determined during mapper construction.
1066 Behavior is undefined if directly modified.
1067
1068 """
1069
1070 single: bool
1071 """Represent ``True`` if this :class:`_orm.Mapper` is a single table
1072 inheritance mapper.
1073
1074 :attr:`_orm.Mapper.local_table` will be ``None`` if this flag is set.
1075
1076 This is a *read only* attribute determined during mapper construction.
1077 Behavior is undefined if directly modified.
1078
1079 """
1080
1081 polymorphic_on: Optional[KeyedColumnElement[Any]]
1082 """The :class:`_schema.Column` or SQL expression specified as the
1083 ``polymorphic_on`` argument
1084 for this :class:`_orm.Mapper`, within an inheritance scenario.
1085
1086 This attribute is normally a :class:`_schema.Column` instance but
1087 may also be an expression, such as one derived from
1088 :func:`.cast`.
1089
1090 This is a *read only* attribute determined during mapper construction.
1091 Behavior is undefined if directly modified.
1092
1093 """
1094
1095 polymorphic_map: Dict[Any, Mapper[Any]]
1096 """A mapping of "polymorphic identity" identifiers mapped to
1097 :class:`_orm.Mapper` instances, within an inheritance scenario.
1098
1099 The identifiers can be of any type which is comparable to the
1100 type of column represented by :attr:`_orm.Mapper.polymorphic_on`.
1101
1102 An inheritance chain of mappers will all reference the same
1103 polymorphic map object. The object is used to correlate incoming
1104 result rows to target mappers.
1105
1106 This is a *read only* attribute determined during mapper construction.
1107 Behavior is undefined if directly modified.
1108
1109 """
1110
1111 polymorphic_identity: Optional[Any]
1112 """Represent an identifier which is matched against the
1113 :attr:`_orm.Mapper.polymorphic_on` column during result row loading.
1114
1115 Used only with inheritance, this object can be of any type which is
1116 comparable to the type of column represented by
1117 :attr:`_orm.Mapper.polymorphic_on`.
1118
1119 This is a *read only* attribute determined during mapper construction.
1120 Behavior is undefined if directly modified.
1121
1122 """
1123
1124 base_mapper: Mapper[Any]
1125 """The base-most :class:`_orm.Mapper` in an inheritance chain.
1126
1127 In a non-inheriting scenario, this attribute will always be this
1128 :class:`_orm.Mapper`. In an inheritance scenario, it references
1129 the :class:`_orm.Mapper` which is parent to all other :class:`_orm.Mapper`
1130 objects in the inheritance chain.
1131
1132 This is a *read only* attribute determined during mapper construction.
1133 Behavior is undefined if directly modified.
1134
1135 """
1136
1137 _columns: sql_base.WriteableColumnCollection[str, Column[Any]]
1138
1139 @util.memoized_property
1140 def _path_registry(self) -> _CachingEntityRegistry:
1141 return PathRegistry.per_mapper(self)
1142
1143 def _configure_inheritance(self):
1144 """Configure settings related to inheriting and/or inherited mappers
1145 being present."""
1146
1147 # a set of all mappers which inherit from this one.
1148 self._inheriting_mappers = util.WeakSequence()
1149
1150 if self.inherits:
1151 if not issubclass(self.class_, self.inherits.class_):
1152 raise sa_exc.ArgumentError(
1153 "Class '%s' does not inherit from '%s'"
1154 % (self.class_.__name__, self.inherits.class_.__name__)
1155 )
1156
1157 self.dispatch._update(self.inherits.dispatch)
1158
1159 if self.single:
1160 self.persist_selectable = self.inherits.persist_selectable
1161 elif self.local_table is not self.inherits.local_table:
1162 if self.concrete:
1163 self.persist_selectable = self.local_table
1164 for mapper in self.iterate_to_root():
1165 if mapper.polymorphic_on is not None:
1166 mapper._requires_row_aliasing = True
1167 else:
1168 if self.inherit_condition is None:
1169 # figure out inherit condition from our table to the
1170 # immediate table of the inherited mapper, not its
1171 # full table which could pull in other stuff we don't
1172 # want (allows test/inheritance.InheritTest4 to pass)
1173 try:
1174 self.inherit_condition = sql_util.join_condition(
1175 self.inherits.local_table, self.local_table
1176 )
1177 except sa_exc.NoForeignKeysError as nfe:
1178 assert self.inherits.local_table is not None
1179 assert self.local_table is not None
1180 raise sa_exc.NoForeignKeysError(
1181 "Can't determine the inherit condition "
1182 "between inherited table '%s' and "
1183 "inheriting "
1184 "table '%s'; tables have no "
1185 "foreign key relationships established. "
1186 "Please ensure the inheriting table has "
1187 "a foreign key relationship to the "
1188 "inherited "
1189 "table, or provide an "
1190 "'on clause' using "
1191 "the 'inherit_condition' mapper argument."
1192 % (
1193 self.inherits.local_table.description,
1194 self.local_table.description,
1195 )
1196 ) from nfe
1197 except sa_exc.AmbiguousForeignKeysError as afe:
1198 assert self.inherits.local_table is not None
1199 assert self.local_table is not None
1200 raise sa_exc.AmbiguousForeignKeysError(
1201 "Can't determine the inherit condition "
1202 "between inherited table '%s' and "
1203 "inheriting "
1204 "table '%s'; tables have more than one "
1205 "foreign key relationship established. "
1206 "Please specify the 'on clause' using "
1207 "the 'inherit_condition' mapper argument."
1208 % (
1209 self.inherits.local_table.description,
1210 self.local_table.description,
1211 )
1212 ) from afe
1213 assert self.inherits.persist_selectable is not None
1214 self.persist_selectable = sql.join(
1215 self.inherits.persist_selectable,
1216 self.local_table,
1217 self.inherit_condition,
1218 )
1219
1220 fks = util.to_set(self.inherit_foreign_keys)
1221 self._inherits_equated_pairs = sql_util.criterion_as_pairs(
1222 self.persist_selectable.onclause,
1223 consider_as_foreign_keys=fks,
1224 )
1225 else:
1226 self.persist_selectable = self.local_table
1227
1228 if self.polymorphic_identity is None:
1229 self._identity_class = self.class_
1230
1231 if (
1232 not self.polymorphic_abstract
1233 and self.inherits.base_mapper.polymorphic_on is not None
1234 ):
1235 util.warn(
1236 f"{self} does not indicate a 'polymorphic_identity', "
1237 "yet is part of an inheritance hierarchy that has a "
1238 f"'polymorphic_on' column of "
1239 f"'{self.inherits.base_mapper.polymorphic_on}'. "
1240 "If this is an intermediary class that should not be "
1241 "instantiated, the class may either be left unmapped, "
1242 "or may include the 'polymorphic_abstract=True' "
1243 "parameter in its Mapper arguments. To leave the "
1244 "class unmapped when using Declarative, set the "
1245 "'__abstract__ = True' attribute on the class."
1246 )
1247 elif self.concrete:
1248 self._identity_class = self.class_
1249 else:
1250 self._identity_class = self.inherits._identity_class
1251
1252 if self.version_id_col is None:
1253 self.version_id_col = self.inherits.version_id_col
1254 self.version_id_generator = self.inherits.version_id_generator
1255 elif (
1256 self.inherits.version_id_col is not None
1257 and self.version_id_col is not self.inherits.version_id_col
1258 ):
1259 util.warn(
1260 "Inheriting version_id_col '%s' does not match inherited "
1261 "version_id_col '%s' and will not automatically populate "
1262 "the inherited versioning column. "
1263 "version_id_col should only be specified on "
1264 "the base-most mapper that includes versioning."
1265 % (
1266 self.version_id_col.description,
1267 self.inherits.version_id_col.description,
1268 )
1269 )
1270
1271 self.polymorphic_map = self.inherits.polymorphic_map
1272 self.batch = self.inherits.batch
1273 self.inherits._inheriting_mappers.append(self)
1274 self.base_mapper = self.inherits.base_mapper
1275 self.passive_updates = self.inherits.passive_updates
1276 self.passive_deletes = (
1277 self.inherits.passive_deletes or self.passive_deletes
1278 )
1279 self._all_tables = self.inherits._all_tables
1280
1281 if self.polymorphic_identity is not None:
1282 if self.polymorphic_identity in self.polymorphic_map:
1283 util.warn(
1284 "Reassigning polymorphic association for identity %r "
1285 "from %r to %r: Check for duplicate use of %r as "
1286 "value for polymorphic_identity."
1287 % (
1288 self.polymorphic_identity,
1289 self.polymorphic_map[self.polymorphic_identity],
1290 self,
1291 self.polymorphic_identity,
1292 )
1293 )
1294 self.polymorphic_map[self.polymorphic_identity] = self
1295
1296 if self.polymorphic_load and self.concrete:
1297 raise sa_exc.ArgumentError(
1298 "polymorphic_load is not currently supported "
1299 "with concrete table inheritance"
1300 )
1301 if self.polymorphic_load == "inline":
1302 self.inherits._add_with_polymorphic_subclass(self)
1303 elif self.polymorphic_load == "selectin":
1304 pass
1305 elif self.polymorphic_load is not None:
1306 raise sa_exc.ArgumentError(
1307 "unknown argument for polymorphic_load: %r"
1308 % self.polymorphic_load
1309 )
1310
1311 else:
1312 self._all_tables = set()
1313 self.base_mapper = self
1314 assert self.local_table is not None
1315 self.persist_selectable = self.local_table
1316 if self.polymorphic_identity is not None:
1317 self.polymorphic_map[self.polymorphic_identity] = self
1318 self._identity_class = self.class_
1319
1320 if self.persist_selectable is None:
1321 raise sa_exc.ArgumentError(
1322 "Mapper '%s' does not have a persist_selectable specified."
1323 % self
1324 )
1325
1326 def _set_with_polymorphic(
1327 self, with_polymorphic: Optional[_WithPolymorphicArg]
1328 ) -> None:
1329 if with_polymorphic == "*":
1330 self.with_polymorphic = ("*", None)
1331 elif isinstance(with_polymorphic, (tuple, list)):
1332 if isinstance(with_polymorphic[0], (str, tuple, list)):
1333 self.with_polymorphic = cast(
1334 """Tuple[
1335 Union[
1336 Literal["*"],
1337 Sequence[Union["Mapper[Any]", Type[Any]]],
1338 ],
1339 Optional["FromClause"],
1340 ]""",
1341 with_polymorphic,
1342 )
1343 else:
1344 self.with_polymorphic = (with_polymorphic, None)
1345 elif with_polymorphic is not None:
1346 raise sa_exc.ArgumentError(
1347 f"Invalid setting for with_polymorphic: {with_polymorphic!r}"
1348 )
1349 else:
1350 self.with_polymorphic = None
1351
1352 if self.with_polymorphic and self.with_polymorphic[1] is not None:
1353 self.with_polymorphic = (
1354 self.with_polymorphic[0],
1355 coercions.expect(
1356 roles.FromClauseRole,
1357 self.with_polymorphic[1],
1358 ),
1359 )
1360
1361 if self.configured:
1362 self._expire_memoizations()
1363
1364 def _add_with_polymorphic_subclass(self, mapper):
1365 subcl = mapper.class_
1366 if self.with_polymorphic is None:
1367 self._set_with_polymorphic((subcl,))
1368 elif self.with_polymorphic[0] != "*":
1369 assert isinstance(self.with_polymorphic[0], tuple)
1370 self._set_with_polymorphic(
1371 (self.with_polymorphic[0] + (subcl,), self.with_polymorphic[1])
1372 )
1373
1374 def _set_concrete_base(self, mapper):
1375 """Set the given :class:`_orm.Mapper` as the 'inherits' for this
1376 :class:`_orm.Mapper`, assuming this :class:`_orm.Mapper` is concrete
1377 and does not already have an inherits."""
1378
1379 assert self.concrete
1380 assert not self.inherits
1381 assert isinstance(mapper, Mapper)
1382 self.inherits = mapper
1383 self.inherits.polymorphic_map.update(self.polymorphic_map)
1384 self.polymorphic_map = self.inherits.polymorphic_map
1385 for mapper in self.iterate_to_root():
1386 if mapper.polymorphic_on is not None:
1387 mapper._requires_row_aliasing = True
1388 self.batch = self.inherits.batch
1389 for mp in self.self_and_descendants:
1390 mp.base_mapper = self.inherits.base_mapper
1391 self.inherits._inheriting_mappers.append(self)
1392 self.passive_updates = self.inherits.passive_updates
1393 self._all_tables = self.inherits._all_tables
1394
1395 for key, prop in mapper._props.items():
1396 if key not in self._props and not self._should_exclude(
1397 key, key, local=False, column=None
1398 ):
1399 self._adapt_inherited_property(key, prop, False)
1400
1401 def _set_polymorphic_on(self, polymorphic_on):
1402 self.polymorphic_on = polymorphic_on
1403 self._configure_polymorphic_setter(True)
1404
1405 def _configure_class_instrumentation(self):
1406 """Associate this Mapper with the
1407 given class and entity name.
1408
1409 Subsequent calls to ``class_mapper()`` for the ``class_`` / ``entity``
1410 name combination will return this mapper. Also decorate the
1411 `__init__` method on the mapped class to include optional
1412 auto-session attachment logic.
1413
1414 """
1415
1416 # we expect that declarative has applied the class manager
1417 # already and set up a registry. if this is None,
1418 # this raises as of 2.0.
1419 manager = attributes.opt_manager_of_class(self.class_)
1420
1421 if manager is None or not manager.registry:
1422 raise sa_exc.InvalidRequestError(
1423 "The _mapper() function and Mapper() constructor may not be "
1424 "invoked directly outside of a declarative registry."
1425 " Please use the sqlalchemy.orm.registry.map_imperatively() "
1426 "function for a classical mapping."
1427 )
1428
1429 self.dispatch.instrument_class(self, self.class_)
1430
1431 # this invokes the class_instrument event and sets up
1432 # the __init__ method. documented behavior is that this must
1433 # occur after the instrument_class event above.
1434 # yes two events with the same two words reversed and different APIs.
1435 # :(
1436
1437 manager = instrumentation.register_class(
1438 self.class_,
1439 mapper=self,
1440 expired_attribute_loader=util.partial(
1441 loading._load_scalar_attributes, self
1442 ),
1443 # finalize flag means instrument the __init__ method
1444 # and call the class_instrument event
1445 finalize=True,
1446 )
1447
1448 self.class_manager = manager
1449
1450 assert manager.registry is not None
1451 self.registry = manager.registry
1452
1453 # The remaining members can be added by any mapper,
1454 # e_name None or not.
1455 if manager.mapper is None:
1456 return
1457
1458 event.listen(manager, "init", _event_on_init, raw=True)
1459
1460 for key, method in util.iterate_attributes(self.class_):
1461 if key == "__init__" and hasattr(method, "_sa_original_init"):
1462 method = method._sa_original_init
1463 if hasattr(method, "__func__"):
1464 method = method.__func__
1465 if callable(method):
1466 if hasattr(method, "__sa_reconstructor__"):
1467 self._reconstructor = method
1468 event.listen(manager, "load", _event_on_load, raw=True)
1469 elif hasattr(method, "__sa_validators__"):
1470 validation_opts = method.__sa_validation_opts__
1471 for name in method.__sa_validators__:
1472 if name in self.validators:
1473 raise sa_exc.InvalidRequestError(
1474 "A validation function for mapped "
1475 "attribute %r on mapper %s already exists."
1476 % (name, self)
1477 )
1478 self.validators = self.validators.union(
1479 {name: (method, validation_opts)}
1480 )
1481
1482 def _set_dispose_flags(self) -> None:
1483 self.configured = True
1484 self._ready_for_configure = True
1485 self._dispose_called = True
1486
1487 self.__dict__.pop("_configure_failed", None)
1488
1489 def _str_arg_to_mapped_col(self, argname: str, key: str) -> Column[Any]:
1490 try:
1491 prop = self._props[key]
1492 except KeyError as err:
1493 raise sa_exc.ArgumentError(
1494 f"Can't determine {argname} column '{key}' - "
1495 "no attribute is mapped to this name."
1496 ) from err
1497 try:
1498 expr = prop.expression
1499 except AttributeError as ae:
1500 raise sa_exc.ArgumentError(
1501 f"Can't determine {argname} column '{key}'; "
1502 "property does not refer to a single mapped Column"
1503 ) from ae
1504 if not isinstance(expr, Column):
1505 raise sa_exc.ArgumentError(
1506 f"Can't determine {argname} column '{key}'; "
1507 "property does not refer to a single "
1508 "mapped Column"
1509 )
1510 return expr
1511
1512 def _configure_pks(self) -> None:
1513 self.tables = sql_util.find_tables(self.persist_selectable)
1514
1515 self._all_tables.update(t for t in self.tables)
1516
1517 self._pks_by_table = {}
1518 self._cols_by_table = {}
1519
1520 all_cols = util.column_set(
1521 chain(*[col.proxy_set for col in self._columntoproperty])
1522 )
1523
1524 pk_cols = util.column_set(c for c in all_cols if c.primary_key)
1525
1526 # identify primary key columns which are also mapped by this mapper.
1527 for fc in set(self.tables).union([self.persist_selectable]):
1528 if fc.primary_key and pk_cols.issuperset(fc.primary_key):
1529 # ordering is important since it determines the ordering of
1530 # mapper.primary_key (and therefore query.get())
1531 self._pks_by_table[fc] = util.ordered_column_set( # type: ignore # noqa: E501
1532 fc.primary_key
1533 ).intersection(
1534 pk_cols
1535 )
1536 self._cols_by_table[fc] = util.ordered_column_set(fc.c).intersection( # type: ignore # noqa: E501
1537 all_cols
1538 )
1539
1540 if self._primary_key_argument:
1541 coerced_pk_arg = [
1542 (
1543 self._str_arg_to_mapped_col("primary_key", c)
1544 if isinstance(c, str)
1545 else c
1546 )
1547 for c in (
1548 coercions.expect(
1549 roles.DDLConstraintColumnRole,
1550 coerce_pk,
1551 argname="primary_key",
1552 )
1553 for coerce_pk in self._primary_key_argument
1554 )
1555 ]
1556 else:
1557 coerced_pk_arg = None
1558
1559 # if explicit PK argument sent, add those columns to the
1560 # primary key mappings
1561 if coerced_pk_arg:
1562 for k in coerced_pk_arg:
1563 if k.table not in self._pks_by_table:
1564 self._pks_by_table[k.table] = util.OrderedSet()
1565 self._pks_by_table[k.table].add(k)
1566
1567 # otherwise, see that we got a full PK for the mapped table
1568 elif (
1569 self.persist_selectable not in self._pks_by_table
1570 or len(self._pks_by_table[self.persist_selectable]) == 0
1571 ):
1572 raise sa_exc.ArgumentError(
1573 "Mapper %s could not assemble any primary "
1574 "key columns for mapped table '%s'"
1575 % (self, self.persist_selectable.description)
1576 )
1577 elif self.local_table not in self._pks_by_table and isinstance(
1578 self.local_table, schema.Table
1579 ):
1580 util.warn(
1581 "Could not assemble any primary "
1582 "keys for locally mapped table '%s' - "
1583 "no rows will be persisted in this Table."
1584 % self.local_table.description
1585 )
1586
1587 if (
1588 self.inherits
1589 and not self.concrete
1590 and not self._primary_key_argument
1591 ):
1592 # if inheriting, the "primary key" for this mapper is
1593 # that of the inheriting (unless concrete or explicit)
1594 self.primary_key = self.inherits.primary_key
1595 else:
1596 # determine primary key from argument or persist_selectable pks
1597 primary_key: Collection[ColumnElement[Any]]
1598
1599 if coerced_pk_arg:
1600 primary_key = [
1601 cc if cc is not None else c
1602 for cc, c in (
1603 (self.persist_selectable.corresponding_column(c), c)
1604 for c in coerced_pk_arg
1605 )
1606 ]
1607 else:
1608 # if heuristically determined PKs, reduce to the minimal set
1609 # of columns by eliminating FK->PK pairs for a multi-table
1610 # expression. May over-reduce for some kinds of UNIONs
1611 # / CTEs; use explicit PK argument for these special cases
1612 primary_key = sql_util.reduce_columns(
1613 self._pks_by_table[self.persist_selectable],
1614 ignore_nonexistent_tables=True,
1615 )
1616
1617 if len(primary_key) == 0:
1618 raise sa_exc.ArgumentError(
1619 "Mapper %s could not assemble any primary "
1620 "key columns for mapped table '%s'"
1621 % (self, self.persist_selectable.description)
1622 )
1623
1624 self.primary_key = tuple(primary_key)
1625 self._log("Identified primary key columns: %s", primary_key)
1626
1627 # determine cols that aren't expressed within our tables; mark these
1628 # as "read only" properties which are refreshed upon INSERT/UPDATE
1629 self._readonly_props = {
1630 self._columntoproperty[col]
1631 for col in self._columntoproperty
1632 if self._columntoproperty[col] not in self._identity_key_props
1633 and (
1634 not hasattr(col, "table")
1635 or col.table not in self._cols_by_table
1636 )
1637 }
1638
1639 def _configure_properties(self) -> None:
1640 self._columns = sql_base.WriteableColumnCollection()
1641
1642 # object attribute names mapped to MapperProperty objects
1643 self._props = util.OrderedDict()
1644
1645 # table columns mapped to MapperProperty
1646 self._columntoproperty = _ColumnMapping(self)
1647
1648 explicit_col_props_by_column: Dict[
1649 KeyedColumnElement[Any], Tuple[str, ColumnProperty[Any]]
1650 ] = {}
1651 explicit_col_props_by_key: Dict[str, ColumnProperty[Any]] = {}
1652
1653 # step 1: go through properties that were explicitly passed
1654 # in the properties dictionary. For Columns that are local, put them
1655 # aside in a separate collection we will reconcile with the Table
1656 # that's given. For other properties, set them up in _props now.
1657 if self._init_properties:
1658 for key, prop_arg in self._init_properties.items():
1659 if not isinstance(prop_arg, MapperProperty):
1660 possible_col_prop = self._make_prop_from_column(
1661 key, prop_arg
1662 )
1663 else:
1664 possible_col_prop = prop_arg
1665
1666 # issue #8705. if the explicit property is actually a
1667 # Column that is local to the local Table, don't set it up
1668 # in ._props yet, integrate it into the order given within
1669 # the Table.
1670
1671 _map_as_property_now = True
1672 if isinstance(possible_col_prop, properties.ColumnProperty):
1673 for given_col in possible_col_prop.columns:
1674 if self.local_table.c.contains_column(given_col):
1675 _map_as_property_now = False
1676 explicit_col_props_by_key[key] = possible_col_prop
1677 explicit_col_props_by_column[given_col] = (
1678 key,
1679 possible_col_prop,
1680 )
1681
1682 if _map_as_property_now:
1683 self._configure_property(
1684 key,
1685 possible_col_prop,
1686 init=False,
1687 )
1688
1689 # step 2: pull properties from the inherited mapper. reconcile
1690 # columns with those which are explicit above. for properties that
1691 # are only in the inheriting mapper, set them up as local props
1692 if self.inherits:
1693 for key, inherited_prop in self.inherits._props.items():
1694 if self._should_exclude(key, key, local=False, column=None):
1695 continue
1696
1697 incoming_prop = explicit_col_props_by_key.get(key)
1698 if incoming_prop:
1699 new_prop = self._reconcile_prop_with_incoming_columns(
1700 key,
1701 inherited_prop,
1702 warn_only=False,
1703 incoming_prop=incoming_prop,
1704 )
1705 explicit_col_props_by_key[key] = new_prop
1706
1707 for inc_col in incoming_prop.columns:
1708 explicit_col_props_by_column[inc_col] = (
1709 key,
1710 new_prop,
1711 )
1712 elif key not in self._props:
1713 self._adapt_inherited_property(key, inherited_prop, False)
1714
1715 # step 3. Iterate through all columns in the persist selectable.
1716 # this includes not only columns in the local table / fromclause,
1717 # but also those columns in the superclass table if we are joined
1718 # inh or single inh mapper. map these columns as well. additional
1719 # reconciliation against inherited columns occurs here also.
1720
1721 for column in self.persist_selectable.columns:
1722 if column in explicit_col_props_by_column:
1723 # column was explicitly passed to properties; configure
1724 # it now in the order in which it corresponds to the
1725 # Table / selectable
1726 key, prop = explicit_col_props_by_column[column]
1727 self._configure_property(key, prop, init=False)
1728 continue
1729
1730 elif column in self._columntoproperty:
1731 continue
1732
1733 column_key = (self.column_prefix or "") + column.key
1734 if self._should_exclude(
1735 column.key,
1736 column_key,
1737 local=self.local_table.c.contains_column(column),
1738 column=column,
1739 ):
1740 continue
1741
1742 # adjust the "key" used for this column to that
1743 # of the inheriting mapper
1744 for mapper in self.iterate_to_root():
1745 if column in mapper._columntoproperty:
1746 column_key = mapper._columntoproperty[column].key
1747
1748 self._configure_property(
1749 column_key,
1750 column,
1751 init=False,
1752 setparent=True,
1753 )
1754
1755 def _configure_polymorphic_setter(self, init=False):
1756 """Configure an attribute on the mapper representing the
1757 'polymorphic_on' column, if applicable, and not
1758 already generated by _configure_properties (which is typical).
1759
1760 Also create a setter function which will assign this
1761 attribute to the value of the 'polymorphic_identity'
1762 upon instance construction, also if applicable. This
1763 routine will run when an instance is created.
1764
1765 """
1766 setter = False
1767 polymorphic_key: Optional[str] = None
1768
1769 if self.polymorphic_on is not None:
1770 setter = True
1771
1772 if isinstance(self.polymorphic_on, str):
1773 # polymorphic_on specified as a string - link
1774 # it to mapped ColumnProperty
1775 try:
1776 self.polymorphic_on = self._props[self.polymorphic_on]
1777 except KeyError as err:
1778 raise sa_exc.ArgumentError(
1779 "Can't determine polymorphic_on "
1780 "value '%s' - no attribute is "
1781 "mapped to this name." % self.polymorphic_on
1782 ) from err
1783
1784 if self.polymorphic_on in self._columntoproperty:
1785 # polymorphic_on is a column that is already mapped
1786 # to a ColumnProperty
1787 prop = self._columntoproperty[self.polymorphic_on]
1788 elif isinstance(self.polymorphic_on, MapperProperty):
1789 # polymorphic_on is directly a MapperProperty,
1790 # ensure it's a ColumnProperty
1791 if not isinstance(
1792 self.polymorphic_on, properties.ColumnProperty
1793 ):
1794 raise sa_exc.ArgumentError(
1795 "Only direct column-mapped "
1796 "property or SQL expression "
1797 "can be passed for polymorphic_on"
1798 )
1799 prop = self.polymorphic_on
1800 else:
1801 # polymorphic_on is a Column or SQL expression and
1802 # doesn't appear to be mapped. this means it can be 1.
1803 # only present in the with_polymorphic selectable or
1804 # 2. a totally standalone SQL expression which we'd
1805 # hope is compatible with this mapper's persist_selectable
1806 col = self.persist_selectable.corresponding_column(
1807 self.polymorphic_on
1808 )
1809 if col is None:
1810 # polymorphic_on doesn't derive from any
1811 # column/expression isn't present in the mapped
1812 # table. we will make a "hidden" ColumnProperty
1813 # for it. Just check that if it's directly a
1814 # schema.Column and we have with_polymorphic, it's
1815 # likely a user error if the schema.Column isn't
1816 # represented somehow in either persist_selectable or
1817 # with_polymorphic. Otherwise as of 0.7.4 we
1818 # just go with it and assume the user wants it
1819 # that way (i.e. a CASE statement)
1820 setter = False
1821 instrument = False
1822 col = self.polymorphic_on
1823 if isinstance(col, schema.Column) and (
1824 self.with_polymorphic is None
1825 or self.with_polymorphic[1] is None
1826 or self.with_polymorphic[1].corresponding_column(col)
1827 is None
1828 ):
1829 raise sa_exc.InvalidRequestError(
1830 "Could not map polymorphic_on column "
1831 "'%s' to the mapped table - polymorphic "
1832 "loads will not function properly"
1833 % col.description
1834 )
1835 else:
1836 # column/expression that polymorphic_on derives from
1837 # is present in our mapped table
1838 # and is probably mapped, but polymorphic_on itself
1839 # is not. This happens when
1840 # the polymorphic_on is only directly present in the
1841 # with_polymorphic selectable, as when use
1842 # polymorphic_union.
1843 # we'll make a separate ColumnProperty for it.
1844 instrument = True
1845 key = getattr(col, "key", None)
1846 if key:
1847 if self._should_exclude(key, key, False, col):
1848 raise sa_exc.InvalidRequestError(
1849 "Cannot exclude or override the "
1850 "discriminator column %r" % key
1851 )
1852 else:
1853 self.polymorphic_on = col = col.label("_sa_polymorphic_on")
1854 key = col.key
1855
1856 prop = properties.ColumnProperty(col, _instrument=instrument)
1857 self._configure_property(key, prop, init=init, setparent=True)
1858
1859 # the actual polymorphic_on should be the first public-facing
1860 # column in the property
1861 self.polymorphic_on = prop.columns[0]
1862 polymorphic_key = prop.key
1863 else:
1864 # no polymorphic_on was set.
1865 # check inheriting mappers for one.
1866 for mapper in self.iterate_to_root():
1867 # determine if polymorphic_on of the parent
1868 # should be propagated here. If the col
1869 # is present in our mapped table, or if our mapped
1870 # table is the same as the parent (i.e. single table
1871 # inheritance), we can use it
1872 if mapper.polymorphic_on is not None:
1873 if self.persist_selectable is mapper.persist_selectable:
1874 self.polymorphic_on = mapper.polymorphic_on
1875 else:
1876 self.polymorphic_on = (
1877 self.persist_selectable
1878 ).corresponding_column(mapper.polymorphic_on)
1879 # we can use the parent mapper's _set_polymorphic_identity
1880 # directly; it ensures the polymorphic_identity of the
1881 # instance's mapper is used so is portable to subclasses.
1882 if self.polymorphic_on is not None:
1883 self._set_polymorphic_identity = (
1884 mapper._set_polymorphic_identity
1885 )
1886 self._polymorphic_attr_key = (
1887 mapper._polymorphic_attr_key
1888 )
1889 self._validate_polymorphic_identity = (
1890 mapper._validate_polymorphic_identity
1891 )
1892 else:
1893 self._set_polymorphic_identity = None
1894 self._polymorphic_attr_key = None
1895 return
1896
1897 if self.polymorphic_abstract and self.polymorphic_on is None:
1898 raise sa_exc.InvalidRequestError(
1899 "The Mapper.polymorphic_abstract parameter may only be used "
1900 "on a mapper hierarchy which includes the "
1901 "Mapper.polymorphic_on parameter at the base of the hierarchy."
1902 )
1903
1904 if setter:
1905
1906 def _set_polymorphic_identity(state):
1907 dict_ = state.dict
1908 # TODO: what happens if polymorphic_on column attribute name
1909 # does not match .key?
1910
1911 polymorphic_identity = (
1912 state.manager.mapper.polymorphic_identity
1913 )
1914 if (
1915 polymorphic_identity is None
1916 and state.manager.mapper.polymorphic_abstract
1917 ):
1918 raise sa_exc.InvalidRequestError(
1919 f"Can't instantiate class for {state.manager.mapper}; "
1920 "mapper is marked polymorphic_abstract=True"
1921 )
1922
1923 state.get_impl(polymorphic_key).set(
1924 state,
1925 dict_,
1926 polymorphic_identity,
1927 None,
1928 )
1929
1930 self._polymorphic_attr_key = polymorphic_key
1931
1932 def _validate_polymorphic_identity(mapper, state, dict_):
1933 if (
1934 polymorphic_key in dict_
1935 and dict_[polymorphic_key]
1936 not in mapper._acceptable_polymorphic_identities
1937 ):
1938 util.warn_limited(
1939 "Flushing object %s with "
1940 "incompatible polymorphic identity %r; the "
1941 "object may not refresh and/or load correctly",
1942 (state_str(state), dict_[polymorphic_key]),
1943 )
1944
1945 self._set_polymorphic_identity = _set_polymorphic_identity
1946 self._validate_polymorphic_identity = (
1947 _validate_polymorphic_identity
1948 )
1949 else:
1950 self._polymorphic_attr_key = None
1951 self._set_polymorphic_identity = None
1952
1953 _validate_polymorphic_identity = None
1954
1955 @HasMemoized.memoized_attribute
1956 def _version_id_prop(self):
1957 if self.version_id_col is not None:
1958 return self._columntoproperty[self.version_id_col]
1959 else:
1960 return None
1961
1962 @HasMemoized.memoized_attribute
1963 def _acceptable_polymorphic_identities(self):
1964 identities = set()
1965
1966 stack = deque([self])
1967 while stack:
1968 item = stack.popleft()
1969 if item.persist_selectable is self.persist_selectable:
1970 identities.add(item.polymorphic_identity)
1971 stack.extend(item._inheriting_mappers)
1972
1973 return identities
1974
1975 @HasMemoized.memoized_attribute
1976 def _prop_set(self):
1977 return frozenset(self._props.values())
1978
1979 @util.preload_module("sqlalchemy.orm.descriptor_props")
1980 def _adapt_inherited_property(self, key, prop, init):
1981 descriptor_props = util.preloaded.orm_descriptor_props
1982
1983 if not self.concrete:
1984 self._configure_property(key, prop, init=False, setparent=False)
1985 elif key not in self._props:
1986 # determine if the class implements this attribute; if not,
1987 # or if it is implemented by the attribute that is handling the
1988 # given superclass-mapped property, then we need to report that we
1989 # can't use this at the instance level since we are a concrete
1990 # mapper and we don't map this. don't trip user-defined
1991 # descriptors that might have side effects when invoked.
1992 implementing_attribute = self.class_manager._get_class_attr_mro(
1993 key, prop
1994 )
1995 if implementing_attribute is prop or (
1996 isinstance(
1997 implementing_attribute, attributes.InstrumentedAttribute
1998 )
1999 and implementing_attribute._parententity is prop.parent
2000 ):
2001 self._configure_property(
2002 key,
2003 descriptor_props.ConcreteInheritedProperty(),
2004 init=init,
2005 setparent=True,
2006 )
2007
2008 @util.preload_module("sqlalchemy.orm.descriptor_props")
2009 def _configure_property(
2010 self,
2011 key: str,
2012 prop_arg: Union[KeyedColumnElement[Any], MapperProperty[Any]],
2013 *,
2014 init: bool = True,
2015 setparent: bool = True,
2016 warn_for_existing: bool = False,
2017 ) -> MapperProperty[Any]:
2018 descriptor_props = util.preloaded.orm_descriptor_props
2019 self._log(
2020 "_configure_property(%s, %s)", key, prop_arg.__class__.__name__
2021 )
2022
2023 # early setup mode - don't assign any props, only
2024 # ensure a Column is turned into a ColumnProperty.
2025 # see #12858
2026 early_setup = not hasattr(self, "_props")
2027
2028 if not isinstance(prop_arg, MapperProperty):
2029 prop: MapperProperty[Any] = self._property_from_column(
2030 key, prop_arg, early_setup
2031 )
2032 else:
2033 prop = prop_arg
2034
2035 if early_setup:
2036 return prop
2037
2038 if isinstance(prop, properties.ColumnProperty):
2039 col = self.persist_selectable.corresponding_column(prop.columns[0])
2040
2041 # if the column is not present in the mapped table,
2042 # test if a column has been added after the fact to the
2043 # parent table (or their parent, etc.) [ticket:1570]
2044 if col is None and self.inherits:
2045 path = [self]
2046 for m in self.inherits.iterate_to_root():
2047 col = m.local_table.corresponding_column(prop.columns[0])
2048 if col is not None:
2049 for m2 in path:
2050 m2.persist_selectable._refresh_for_new_column(col)
2051 col = self.persist_selectable.corresponding_column(
2052 prop.columns[0]
2053 )
2054 break
2055 path.append(m)
2056
2057 # subquery expression, column not present in the mapped
2058 # selectable.
2059 if col is None:
2060 col = prop.columns[0]
2061
2062 # column is coming in after _readonly_props was
2063 # initialized; check for 'readonly'
2064 if hasattr(self, "_readonly_props") and (
2065 not hasattr(col, "table")
2066 or col.table not in self._cols_by_table
2067 ):
2068 self._readonly_props.add(prop)
2069
2070 else:
2071 # if column is coming in after _cols_by_table was
2072 # initialized, ensure the col is in the right set
2073 if (
2074 hasattr(self, "_cols_by_table")
2075 and col.table in self._cols_by_table
2076 and col not in self._cols_by_table[col.table]
2077 ):
2078 self._cols_by_table[col.table].add(col)
2079
2080 # if this properties.ColumnProperty represents the "polymorphic
2081 # discriminator" column, mark it. We'll need this when rendering
2082 # columns in SELECT statements.
2083 if not hasattr(prop, "_is_polymorphic_discriminator"):
2084 prop._is_polymorphic_discriminator = (
2085 col is self.polymorphic_on
2086 or prop.columns[0] is self.polymorphic_on
2087 )
2088
2089 if isinstance(col, expression.Label):
2090 # new in 1.4, get column property against expressions
2091 # to be addressable in subqueries
2092 col.key = col._tq_key_label = key
2093
2094 self._columns.add(col, key)
2095
2096 for col in prop.columns:
2097 for proxy_col in col.proxy_set:
2098 self._columntoproperty[proxy_col] = prop
2099
2100 if getattr(prop, "key", key) != key:
2101 util.warn(
2102 f"ORM mapped property {self.class_.__name__}.{prop.key} being "
2103 "assigned to attribute "
2104 f"{key!r} is already associated with "
2105 f"attribute {prop.key!r}. The attribute will be de-associated "
2106 f"from {prop.key!r}."
2107 )
2108
2109 prop.key = key
2110
2111 if setparent:
2112 prop.set_parent(self, init)
2113
2114 if key in self._props and getattr(
2115 self._props[key], "_mapped_by_synonym", False
2116 ):
2117 syn = self._props[key]._mapped_by_synonym
2118 raise sa_exc.ArgumentError(
2119 "Can't call map_column=True for synonym %r=%r, "
2120 "a ColumnProperty already exists keyed to the name "
2121 "%r for column %r" % (syn, key, key, syn)
2122 )
2123
2124 # replacement cases
2125
2126 # case one: prop is replacing a prop that we have mapped. this is
2127 # independent of whatever might be in the actual class dictionary
2128 if (
2129 key in self._props
2130 and not isinstance(
2131 self._props[key], descriptor_props.ConcreteInheritedProperty
2132 )
2133 and not isinstance(prop, descriptor_props.SynonymProperty)
2134 ):
2135 if warn_for_existing:
2136 util.warn_deprecated(
2137 f"User-placed attribute {self.class_.__name__}.{key} on "
2138 f"{self} is replacing an existing ORM-mapped attribute. "
2139 "Behavior is not fully defined in this case. This "
2140 "use is deprecated and will raise an error in a future "
2141 "release",
2142 "2.0",
2143 )
2144 oldprop = self._props[key]
2145 self._path_registry.pop(oldprop, None)
2146
2147 # case two: prop is replacing an attribute on the class of some kind.
2148 # we have to be more careful here since it's normal when using
2149 # Declarative that all the "declared attributes" on the class
2150 # get replaced.
2151 elif (
2152 warn_for_existing
2153 and self.class_.__dict__.get(key, None) is not None
2154 and not isinstance(prop, descriptor_props.SynonymProperty)
2155 and not isinstance(
2156 self._props.get(key, None),
2157 descriptor_props.ConcreteInheritedProperty,
2158 )
2159 ):
2160 util.warn_deprecated(
2161 f"User-placed attribute {self.class_.__name__}.{key} on "
2162 f"{self} is replacing an existing class-bound "
2163 "attribute of the same name. "
2164 "Behavior is not fully defined in this case. This "
2165 "use is deprecated and will raise an error in a future "
2166 "release",
2167 "2.0",
2168 )
2169
2170 self._props[key] = prop
2171
2172 prop.instrument_class(self)
2173
2174 for mapper in self._inheriting_mappers:
2175 mapper._adapt_inherited_property(key, prop, init)
2176
2177 if init:
2178 prop.init()
2179 prop.post_instrument_class(self)
2180
2181 if self.configured:
2182 self._expire_memoizations()
2183
2184 return prop
2185
2186 def _make_prop_from_column(
2187 self,
2188 key: str,
2189 column: Union[
2190 Sequence[KeyedColumnElement[Any]], KeyedColumnElement[Any]
2191 ],
2192 ) -> ColumnProperty[Any]:
2193 columns = util.to_list(column)
2194 mapped_column = []
2195 for c in columns:
2196 mc = self.persist_selectable.corresponding_column(c)
2197 if mc is None:
2198 mc = self.local_table.corresponding_column(c)
2199 if mc is not None:
2200 # if the column is in the local table but not the
2201 # mapped table, this corresponds to adding a
2202 # column after the fact to the local table.
2203 # [ticket:1523]
2204 self.persist_selectable._refresh_for_new_column(mc)
2205 mc = self.persist_selectable.corresponding_column(c)
2206 if mc is None:
2207 raise sa_exc.ArgumentError(
2208 "When configuring property '%s' on %s, "
2209 "column '%s' is not represented in the mapper's "
2210 "table. Use the `column_property()` function to "
2211 "force this column to be mapped as a read-only "
2212 "attribute." % (key, self, c)
2213 )
2214 mapped_column.append(mc)
2215 return properties.ColumnProperty(*mapped_column)
2216
2217 def _reconcile_prop_with_incoming_columns(
2218 self,
2219 key: str,
2220 existing_prop: MapperProperty[Any],
2221 warn_only: bool,
2222 incoming_prop: Optional[ColumnProperty[Any]] = None,
2223 single_column: Optional[KeyedColumnElement[Any]] = None,
2224 ) -> ColumnProperty[Any]:
2225 if incoming_prop and (
2226 self.concrete
2227 or not isinstance(existing_prop, properties.ColumnProperty)
2228 ):
2229 return incoming_prop
2230
2231 existing_column = existing_prop.columns[0]
2232
2233 if incoming_prop and existing_column in incoming_prop.columns:
2234 return incoming_prop
2235
2236 if incoming_prop is None:
2237 assert single_column is not None
2238 incoming_column = single_column
2239 equated_pair_key = (existing_prop.columns[0], incoming_column)
2240 else:
2241 assert single_column is None
2242 incoming_column = incoming_prop.columns[0]
2243 equated_pair_key = (incoming_column, existing_prop.columns[0])
2244
2245 if (
2246 (
2247 not self._inherits_equated_pairs
2248 or (equated_pair_key not in self._inherits_equated_pairs)
2249 )
2250 and not existing_column.shares_lineage(incoming_column)
2251 and existing_column is not self.version_id_col
2252 and incoming_column is not self.version_id_col
2253 ):
2254 msg = (
2255 "Implicitly combining column %s with column "
2256 "%s under attribute '%s'. Please configure one "
2257 "or more attributes for these same-named columns "
2258 "explicitly."
2259 % (
2260 existing_prop.columns[-1],
2261 incoming_column,
2262 key,
2263 )
2264 )
2265 if warn_only:
2266 util.warn(msg)
2267 else:
2268 raise sa_exc.InvalidRequestError(msg)
2269
2270 # existing properties.ColumnProperty from an inheriting
2271 # mapper. make a copy and append our column to it
2272 new_prop = existing_prop.copy()
2273
2274 new_prop.columns.insert(0, incoming_column)
2275 self._log(
2276 "inserting column to existing list "
2277 "in properties.ColumnProperty %s",
2278 key,
2279 )
2280 return new_prop # type: ignore
2281
2282 @util.preload_module("sqlalchemy.orm.descriptor_props")
2283 def _property_from_column(
2284 self, key: str, column: KeyedColumnElement[Any], early_setup: bool
2285 ) -> ColumnProperty[Any]:
2286 """generate/update a :class:`.ColumnProperty` given a
2287 :class:`_schema.Column` or other SQL expression object."""
2288
2289 descriptor_props = util.preloaded.orm_descriptor_props
2290
2291 if early_setup:
2292 prop = None
2293 else:
2294 prop = self._props.get(key)
2295
2296 if isinstance(prop, properties.ColumnProperty):
2297 return self._reconcile_prop_with_incoming_columns(
2298 key,
2299 prop,
2300 single_column=column,
2301 warn_only=prop.parent is not self,
2302 )
2303 elif prop is None or isinstance(
2304 prop, descriptor_props.ConcreteInheritedProperty
2305 ):
2306 return self._make_prop_from_column(key, column)
2307 else:
2308 raise sa_exc.ArgumentError(
2309 "WARNING: when configuring property '%s' on %s, "
2310 "column '%s' conflicts with property '%r'. "
2311 "To resolve this, map the column to the class under a "
2312 "different name in the 'properties' dictionary. Or, "
2313 "to remove all awareness of the column entirely "
2314 "(including its availability as a foreign key), "
2315 "use the 'include_properties' or 'exclude_properties' "
2316 "mapper arguments to control specifically which table "
2317 "columns get mapped." % (key, self, column.key, prop)
2318 )
2319
2320 @util.langhelpers.tag_method_for_warnings(
2321 "This warning originated from the `configure_mappers()` process, "
2322 "which was invoked automatically in response to a user-initiated "
2323 "operation.",
2324 sa_exc.SAWarning,
2325 )
2326 def _check_configure(self) -> None:
2327 if self.registry._new_mappers:
2328 _configure_registries({self.registry}, cascade=True)
2329
2330 def _post_configure_properties(self) -> None:
2331 """Call the ``init()`` method on all ``MapperProperties``
2332 attached to this mapper.
2333
2334 This is a deferred configuration step which is intended
2335 to execute once all mappers have been constructed.
2336
2337 """
2338
2339 self._log("_post_configure_properties() started")
2340 l = [(key, prop) for key, prop in self._props.items()]
2341 for key, prop in l:
2342 self._log("initialize prop %s", key)
2343
2344 if prop.parent is self and not prop._configure_started:
2345 prop.init()
2346
2347 if prop._configure_finished:
2348 prop.post_instrument_class(self)
2349
2350 self._log("_post_configure_properties() complete")
2351 self.configured = True
2352
2353 def add_properties(self, dict_of_properties):
2354 """Add the given dictionary of properties to this mapper,
2355 using `add_property`.
2356
2357 """
2358 for key, value in dict_of_properties.items():
2359 self.add_property(key, value)
2360
2361 def add_property(
2362 self, key: str, prop: Union[Column[Any], MapperProperty[Any]]
2363 ) -> None:
2364 """Add an individual MapperProperty to this mapper.
2365
2366 If the mapper has not been configured yet, just adds the
2367 property to the initial properties dictionary sent to the
2368 constructor. If this Mapper has already been configured, then
2369 the given MapperProperty is configured immediately.
2370
2371 """
2372 prop = self._configure_property(
2373 key, prop, init=self.configured, warn_for_existing=True
2374 )
2375 assert isinstance(prop, MapperProperty)
2376 self._init_properties[key] = prop
2377
2378 def _expire_memoizations(self) -> None:
2379 for mapper in self.iterate_to_root():
2380 mapper._reset_memoizations()
2381
2382 @property
2383 def _log_desc(self) -> str:
2384 return (
2385 "("
2386 + self.class_.__name__
2387 + "|"
2388 + (
2389 self.local_table is not None
2390 and self.local_table.description
2391 or str(self.local_table)
2392 )
2393 + ")"
2394 )
2395
2396 def _log(self, msg: str, *args: Any) -> None:
2397 self.logger.info("%s " + msg, *((self._log_desc,) + args))
2398
2399 def _log_debug(self, msg: str, *args: Any) -> None:
2400 self.logger.debug("%s " + msg, *((self._log_desc,) + args))
2401
2402 def __repr__(self) -> str:
2403 return "<Mapper at 0x%x; %s>" % (id(self), self.class_.__name__)
2404
2405 def __str__(self) -> str:
2406 return "Mapper[%s(%s)]" % (
2407 self.class_.__name__,
2408 (
2409 self.local_table.description
2410 if self.local_table is not None
2411 else self.persist_selectable.description
2412 ),
2413 )
2414
2415 def _is_orphan(self, state: InstanceState[_O]) -> bool:
2416 orphan_possible = False
2417 for mapper in self.iterate_to_root():
2418 for key, cls in mapper._delete_orphans:
2419 orphan_possible = True
2420
2421 has_parent = attributes.manager_of_class(cls).has_parent(
2422 state, key, optimistic=state.has_identity
2423 )
2424
2425 if self.legacy_is_orphan and has_parent:
2426 return False
2427 elif not self.legacy_is_orphan and not has_parent:
2428 return True
2429
2430 if self.legacy_is_orphan:
2431 return orphan_possible
2432 else:
2433 return False
2434
2435 def has_property(self, key: str) -> bool:
2436 return key in self._props
2437
2438 def get_property(
2439 self, key: str, _configure_mappers: bool = False
2440 ) -> MapperProperty[Any]:
2441 """return a MapperProperty associated with the given key."""
2442
2443 if _configure_mappers:
2444 self._check_configure()
2445
2446 try:
2447 return self._props[key]
2448 except KeyError as err:
2449 raise sa_exc.InvalidRequestError(
2450 f"Mapper '{self}' has no property '{key}'. If this property "
2451 "was indicated from other mappers or configure events, ensure "
2452 "registry.configure() has been called."
2453 ) from err
2454
2455 def get_property_by_column(
2456 self, column: ColumnElement[_T]
2457 ) -> MapperProperty[_T]:
2458 """Given a :class:`_schema.Column` object, return the
2459 :class:`.MapperProperty` which maps this column."""
2460
2461 return self._columntoproperty[column]
2462
2463 @HasMemoized.memoized_attribute
2464 def columns(self) -> ReadOnlyColumnCollection[str, Column[Any]]:
2465 """A collection of :class:`_schema.Column` or other scalar expression
2466 objects maintained by this :class:`_orm.Mapper`.
2467
2468 The collection behaves the same as that of the ``c`` attribute on any
2469 :class:`_schema.Table` object, except that only those columns included
2470 in this mapping are present, and are keyed based on the attribute name
2471 defined in the mapping, not necessarily the ``key`` attribute of the
2472 :class:`_schema.Column` itself. Additionally, scalar expressions
2473 mapped by :func:`.column_property` are also present here.
2474
2475 This is a *read only* attribute determined during mapper construction.
2476 Behavior is undefined if directly modified.
2477
2478 """
2479 return self._columns.as_readonly()
2480
2481 @HasMemoized.memoized_attribute
2482 def c(self) -> ReadOnlyColumnCollection[str, Column[Any]]:
2483 """A synonym for :attr:`_orm.Mapper.columns`."""
2484 return self._columns.as_readonly()
2485
2486 @property
2487 def iterate_properties(self):
2488 """return an iterator of all MapperProperty objects."""
2489
2490 return iter(self._props.values())
2491
2492 def _mappers_from_spec(
2493 self, spec: Any, selectable: Optional[FromClause]
2494 ) -> Sequence[Mapper[Any]]:
2495 """given a with_polymorphic() argument, return the set of mappers it
2496 represents.
2497
2498 Trims the list of mappers to just those represented within the given
2499 selectable, if present. This helps some more legacy-ish mappings.
2500
2501 """
2502 if spec == "*":
2503 mappers = list(self.self_and_descendants)
2504 elif spec:
2505 mapper_set: Set[Mapper[Any]] = set()
2506 for m in util.to_list(spec):
2507 m = _class_to_mapper(m)
2508 if not m.isa(self):
2509 raise sa_exc.InvalidRequestError(
2510 "%r does not inherit from %r" % (m, self)
2511 )
2512
2513 if selectable is None:
2514 mapper_set.update(m.iterate_to_root())
2515 else:
2516 mapper_set.add(m)
2517 mappers = [m for m in self.self_and_descendants if m in mapper_set]
2518 else:
2519 mappers = []
2520
2521 if selectable is not None:
2522 tables = set(
2523 sql_util.find_tables(selectable, include_aliases=True)
2524 )
2525 mappers = [m for m in mappers if m.local_table in tables]
2526 return mappers
2527
2528 def _selectable_from_mappers(
2529 self, mappers: Iterable[Mapper[Any]], innerjoin: bool
2530 ) -> FromClause:
2531 """given a list of mappers (assumed to be within this mapper's
2532 inheritance hierarchy), construct an outerjoin amongst those mapper's
2533 mapped tables.
2534
2535 """
2536 from_obj = self.persist_selectable
2537 for m in mappers:
2538 if m is self:
2539 continue
2540 if m.concrete:
2541 raise sa_exc.InvalidRequestError(
2542 "'with_polymorphic()' requires 'selectable' argument "
2543 "when concrete-inheriting mappers are used."
2544 )
2545 elif not m.single:
2546 if innerjoin:
2547 from_obj = from_obj.join(
2548 m.local_table, m.inherit_condition
2549 )
2550 else:
2551 from_obj = from_obj.outerjoin(
2552 m.local_table, m.inherit_condition
2553 )
2554
2555 return from_obj
2556
2557 @HasMemoized.memoized_attribute
2558 def _version_id_has_server_side_value(self) -> bool:
2559 vid_col = self.version_id_col
2560
2561 if vid_col is None:
2562 return False
2563
2564 elif not isinstance(vid_col, Column):
2565 return True
2566 else:
2567 return vid_col.server_default is not None or (
2568 vid_col.default is not None
2569 and (
2570 not vid_col.default.is_scalar
2571 and not vid_col.default.is_callable
2572 )
2573 )
2574
2575 @HasMemoized.memoized_attribute
2576 def _single_table_criteria_component(self):
2577 if self.single and self.inherits and self.polymorphic_on is not None:
2578
2579 hierarchy = tuple(
2580 m.polymorphic_identity
2581 for m in self.self_and_descendants
2582 if not m.polymorphic_abstract
2583 )
2584
2585 return (
2586 self.polymorphic_on._annotate(
2587 {"parententity": self, "parentmapper": self}
2588 ),
2589 hierarchy,
2590 )
2591 else:
2592 return None
2593
2594 @HasMemoized.memoized_attribute
2595 def _single_table_criterion(self):
2596 component = self._single_table_criteria_component
2597 if component is not None:
2598 return component[0].in_(component[1])
2599 else:
2600 return None
2601
2602 @HasMemoized.memoized_attribute
2603 def _has_aliased_polymorphic_fromclause(self):
2604 """return True if with_polymorphic[1] is an aliased fromclause,
2605 like a subquery.
2606
2607 As of #8168, polymorphic adaption with ORMAdapter is used only
2608 if this is present.
2609
2610 """
2611 return self.with_polymorphic and isinstance(
2612 self.with_polymorphic[1],
2613 expression.AliasedReturnsRows,
2614 )
2615
2616 @HasMemoized.memoized_attribute
2617 def _should_select_with_poly_adapter(self):
2618 """determine if _MapperEntity or _ORMColumnEntity will need to use
2619 polymorphic adaption when setting up a SELECT as well as fetching
2620 rows for mapped classes and subclasses against this Mapper.
2621
2622 moved here from context.py for #8456 to generalize the ruleset
2623 for this condition.
2624
2625 """
2626
2627 # this has been simplified as of #8456.
2628 # rule is: if we have a with_polymorphic or a concrete-style
2629 # polymorphic selectable, *or* if the base mapper has either of those,
2630 # we turn on the adaption thing. if not, we do *no* adaption.
2631 #
2632 # (UPDATE for #8168: the above comment was not accurate, as we were
2633 # still saying "do polymorphic" if we were using an auto-generated
2634 # flattened JOIN for with_polymorphic.)
2635 #
2636 # this splits the behavior among the "regular" joined inheritance
2637 # and single inheritance mappers, vs. the "weird / difficult"
2638 # concrete and joined inh mappings that use a with_polymorphic of
2639 # some kind or polymorphic_union.
2640 #
2641 # note we have some tests in test_polymorphic_rel that query against
2642 # a subclass, then refer to the superclass that has a with_polymorphic
2643 # on it (such as test_join_from_polymorphic_explicit_aliased_three).
2644 # these tests actually adapt the polymorphic selectable (like, the
2645 # UNION or the SELECT subquery with JOIN in it) to be just the simple
2646 # subclass table. Hence even if we are a "plain" inheriting mapper
2647 # but our base has a wpoly on it, we turn on adaption. This is a
2648 # legacy case we should probably disable.
2649 #
2650 #
2651 # UPDATE: simplified way more as of #8168. polymorphic adaption
2652 # is turned off even if with_polymorphic is set, as long as there
2653 # is no user-defined aliased selectable / subquery configured.
2654 # this scales back the use of polymorphic adaption in practice
2655 # to basically no cases except for concrete inheritance with a
2656 # polymorphic base class.
2657 #
2658 return (
2659 self._has_aliased_polymorphic_fromclause
2660 or self._requires_row_aliasing
2661 or (self.base_mapper._has_aliased_polymorphic_fromclause)
2662 or self.base_mapper._requires_row_aliasing
2663 )
2664
2665 @HasMemoized.memoized_attribute
2666 def _with_polymorphic_mappers(self) -> Sequence[Mapper[Any]]:
2667 self._check_configure()
2668
2669 if not self.with_polymorphic:
2670 return []
2671 return self._mappers_from_spec(*self.with_polymorphic)
2672
2673 @HasMemoized.memoized_attribute
2674 def _post_inspect(self):
2675 """This hook is invoked by attribute inspection.
2676
2677 E.g. when Query calls:
2678
2679 coercions.expect(roles.ColumnsClauseRole, ent, keep_inspect=True)
2680
2681 This allows the inspection process run a configure mappers hook.
2682
2683 """
2684 self._check_configure()
2685
2686 @HasMemoized_ro_memoized_attribute
2687 def _with_polymorphic_selectable(self) -> FromClause:
2688 if not self.with_polymorphic:
2689 return self.persist_selectable
2690
2691 spec, selectable = self.with_polymorphic
2692 if selectable is not None:
2693 return selectable
2694 else:
2695 return self._selectable_from_mappers(
2696 self._mappers_from_spec(spec, selectable), False
2697 )
2698
2699 with_polymorphic_mappers = _with_polymorphic_mappers
2700 """The list of :class:`_orm.Mapper` objects included in the
2701 default "polymorphic" query.
2702
2703 """
2704
2705 @HasMemoized_ro_memoized_attribute
2706 def _insert_cols_evaluating_none(self):
2707 return {
2708 table: frozenset(
2709 col for col in columns if col.type.should_evaluate_none
2710 )
2711 for table, columns in self._cols_by_table.items()
2712 }
2713
2714 @HasMemoized.memoized_attribute
2715 def _insert_cols_as_none(self):
2716 return {
2717 table: frozenset(
2718 col.key
2719 for col in columns
2720 if not col.primary_key
2721 and not col.server_default
2722 and not col.default
2723 and not col.type.should_evaluate_none
2724 )
2725 for table, columns in self._cols_by_table.items()
2726 }
2727
2728 @HasMemoized.memoized_attribute
2729 def _propkey_to_col(self):
2730 return {
2731 table: {self._columntoproperty[col].key: col for col in columns}
2732 for table, columns in self._cols_by_table.items()
2733 }
2734
2735 @HasMemoized.memoized_attribute
2736 def _pk_keys_by_table(self):
2737 return {
2738 table: frozenset([col.key for col in pks])
2739 for table, pks in self._pks_by_table.items()
2740 }
2741
2742 @HasMemoized.memoized_attribute
2743 def _pk_attr_keys_by_table(self):
2744 return {
2745 table: frozenset([self._columntoproperty[col].key for col in pks])
2746 for table, pks in self._pks_by_table.items()
2747 }
2748
2749 @HasMemoized.memoized_attribute
2750 def _server_default_cols(
2751 self,
2752 ) -> Mapping[FromClause, FrozenSet[Column[Any]]]:
2753 return {
2754 table: frozenset(
2755 [
2756 col
2757 for col in cast("Iterable[Column[Any]]", columns)
2758 if col.server_default is not None
2759 or (
2760 col.default is not None
2761 and col.default.is_clause_element
2762 )
2763 ]
2764 )
2765 for table, columns in self._cols_by_table.items()
2766 }
2767
2768 @HasMemoized.memoized_attribute
2769 def _server_onupdate_default_cols(
2770 self,
2771 ) -> Mapping[FromClause, FrozenSet[Column[Any]]]:
2772 return {
2773 table: frozenset(
2774 [
2775 col
2776 for col in cast("Iterable[Column[Any]]", columns)
2777 if col.server_onupdate is not None
2778 or (
2779 col.onupdate is not None
2780 and col.onupdate.is_clause_element
2781 )
2782 ]
2783 )
2784 for table, columns in self._cols_by_table.items()
2785 }
2786
2787 @HasMemoized.memoized_attribute
2788 def _server_default_col_keys(self) -> Mapping[FromClause, FrozenSet[str]]:
2789 return {
2790 table: frozenset(col.key for col in cols if col.key is not None)
2791 for table, cols in self._server_default_cols.items()
2792 }
2793
2794 @HasMemoized.memoized_attribute
2795 def _server_onupdate_default_col_keys(
2796 self,
2797 ) -> Mapping[FromClause, FrozenSet[str]]:
2798 return {
2799 table: frozenset(col.key for col in cols if col.key is not None)
2800 for table, cols in self._server_onupdate_default_cols.items()
2801 }
2802
2803 @HasMemoized.memoized_attribute
2804 def _server_default_plus_onupdate_propkeys(self) -> Set[str]:
2805 result: Set[str] = set()
2806
2807 col_to_property = self._columntoproperty
2808 for table, columns in self._server_default_cols.items():
2809 result.update(
2810 col_to_property[col].key
2811 for col in columns.intersection(col_to_property)
2812 )
2813 for table, columns in self._server_onupdate_default_cols.items():
2814 result.update(
2815 col_to_property[col].key
2816 for col in columns.intersection(col_to_property)
2817 )
2818 return result
2819
2820 @HasMemoized.memoized_instancemethod
2821 def __clause_element__(self):
2822 annotations: Dict[str, Any] = {
2823 "entity_namespace": self,
2824 "parententity": self,
2825 "parentmapper": self,
2826 }
2827 if self.persist_selectable is not self.local_table:
2828 # joined table inheritance, with polymorphic selectable,
2829 # etc.
2830 annotations["dml_table"] = self.local_table._annotate(
2831 {
2832 "entity_namespace": self,
2833 "parententity": self,
2834 "parentmapper": self,
2835 }
2836 )._set_propagate_attrs(
2837 {"compile_state_plugin": "orm", "plugin_subject": self}
2838 )
2839
2840 return self.selectable._annotate(annotations)._set_propagate_attrs(
2841 {"compile_state_plugin": "orm", "plugin_subject": self}
2842 )
2843
2844 @util.memoized_property
2845 def select_identity_token(self):
2846 return (
2847 expression.null()
2848 ._annotate(
2849 {
2850 "entity_namespace": self,
2851 "parententity": self,
2852 "parentmapper": self,
2853 "identity_token": True,
2854 }
2855 )
2856 ._set_propagate_attrs(
2857 {"compile_state_plugin": "orm", "plugin_subject": self}
2858 )
2859 )
2860
2861 @property
2862 def selectable(self) -> FromClause:
2863 """The :class:`_schema.FromClause` construct this
2864 :class:`_orm.Mapper` selects from by default.
2865
2866 Normally, this is equivalent to :attr:`.persist_selectable`, unless
2867 the ``with_polymorphic`` feature is in use, in which case the
2868 full "polymorphic" selectable is returned.
2869
2870 """
2871 return self._with_polymorphic_selectable
2872
2873 def _with_polymorphic_args(
2874 self,
2875 spec: Any = None,
2876 selectable: Union[Literal[False, None], FromClause] = False,
2877 innerjoin: bool = False,
2878 ) -> Tuple[Sequence[Mapper[Any]], FromClause]:
2879 if selectable not in (None, False):
2880 selectable = coercions.expect(
2881 roles.FromClauseRole,
2882 selectable,
2883 )
2884
2885 if self.with_polymorphic:
2886 if not spec:
2887 spec = self.with_polymorphic[0]
2888 if selectable is False:
2889 selectable = self.with_polymorphic[1]
2890 elif selectable is False:
2891 selectable = None
2892 mappers = self._mappers_from_spec(spec, selectable)
2893 if selectable is not None:
2894 return mappers, selectable
2895 else:
2896 return mappers, self._selectable_from_mappers(mappers, innerjoin)
2897
2898 @HasMemoized.memoized_attribute
2899 def _polymorphic_properties(self):
2900 return list(
2901 self._iterate_polymorphic_properties(
2902 self._with_polymorphic_mappers
2903 )
2904 )
2905
2906 @property
2907 def _all_column_expressions(self):
2908 poly_properties = self._polymorphic_properties
2909 adapter = self._polymorphic_adapter
2910
2911 return [
2912 adapter.columns[c] if adapter else c
2913 for prop in poly_properties
2914 if isinstance(prop, properties.ColumnProperty)
2915 and prop._renders_in_subqueries
2916 for c in prop.columns
2917 ]
2918
2919 def _columns_plus_keys(self, polymorphic_mappers=()):
2920 if polymorphic_mappers:
2921 poly_properties = self._iterate_polymorphic_properties(
2922 polymorphic_mappers
2923 )
2924 else:
2925 poly_properties = self._polymorphic_properties
2926
2927 return [
2928 (prop.key, prop.columns[0])
2929 for prop in poly_properties
2930 if isinstance(prop, properties.ColumnProperty)
2931 ]
2932
2933 @HasMemoized.memoized_attribute
2934 def _polymorphic_adapter(self) -> Optional[orm_util.ORMAdapter]:
2935 if self._has_aliased_polymorphic_fromclause:
2936 return orm_util.ORMAdapter(
2937 orm_util._TraceAdaptRole.MAPPER_POLYMORPHIC_ADAPTER,
2938 self,
2939 selectable=self.selectable,
2940 equivalents=self._equivalent_columns,
2941 limit_on_entity=False,
2942 )
2943 else:
2944 return None
2945
2946 def _iterate_polymorphic_properties(self, mappers=None):
2947 """Return an iterator of MapperProperty objects which will render into
2948 a SELECT."""
2949 if mappers is None:
2950 mappers = self._with_polymorphic_mappers
2951
2952 if not mappers:
2953 for c in self.iterate_properties:
2954 yield c
2955 else:
2956 # in the polymorphic case, filter out discriminator columns
2957 # from other mappers, as these are sometimes dependent on that
2958 # mapper's polymorphic selectable (which we don't want rendered)
2959 for c in util.unique_list(
2960 chain(
2961 *[
2962 list(mapper.iterate_properties)
2963 for mapper in [self] + mappers
2964 ]
2965 )
2966 ):
2967 if getattr(c, "_is_polymorphic_discriminator", False) and (
2968 self.polymorphic_on is None
2969 or c.columns[0] is not self.polymorphic_on
2970 ):
2971 continue
2972 yield c
2973
2974 @HasMemoized.memoized_attribute
2975 def attrs(self) -> util.ReadOnlyProperties[MapperProperty[Any]]:
2976 """A namespace of all :class:`.MapperProperty` objects
2977 associated this mapper.
2978
2979 This is an object that provides each property based on
2980 its key name. For instance, the mapper for a
2981 ``User`` class which has ``User.name`` attribute would
2982 provide ``mapper.attrs.name``, which would be the
2983 :class:`.ColumnProperty` representing the ``name``
2984 column. The namespace object can also be iterated,
2985 which would yield each :class:`.MapperProperty`.
2986
2987 :class:`_orm.Mapper` has several pre-filtered views
2988 of this attribute which limit the types of properties
2989 returned, including :attr:`.synonyms`, :attr:`.column_attrs`,
2990 :attr:`.relationships`, and :attr:`.composites`.
2991
2992 .. warning::
2993
2994 The :attr:`_orm.Mapper.attrs` accessor namespace is an
2995 instance of :class:`.OrderedProperties`. This is
2996 a dictionary-like object which includes a small number of
2997 named methods such as :meth:`.OrderedProperties.items`
2998 and :meth:`.OrderedProperties.values`. When
2999 accessing attributes dynamically, favor using the dict-access
3000 scheme, e.g. ``mapper.attrs[somename]`` over
3001 ``getattr(mapper.attrs, somename)`` to avoid name collisions.
3002
3003 .. seealso::
3004
3005 :attr:`_orm.Mapper.all_orm_descriptors`
3006
3007 """
3008
3009 self._check_configure()
3010 return util.ReadOnlyProperties(self._props)
3011
3012 @HasMemoized.memoized_attribute
3013 def all_orm_descriptors(self) -> util.ReadOnlyProperties[InspectionAttr]:
3014 """A namespace of all :class:`.InspectionAttr` attributes associated
3015 with the mapped class.
3016
3017 These attributes are in all cases Python :term:`descriptors`
3018 associated with the mapped class or its superclasses.
3019
3020 This namespace includes attributes that are mapped to the class
3021 as well as attributes declared by extension modules.
3022 It includes any Python descriptor type that inherits from
3023 :class:`.InspectionAttr`. This includes
3024 :class:`.QueryableAttribute`, as well as extension types such as
3025 :class:`.hybrid_property`, :class:`.hybrid_method` and
3026 :class:`.AssociationProxy`.
3027
3028 To distinguish between mapped attributes and extension attributes,
3029 the attribute :attr:`.InspectionAttr.extension_type` will refer
3030 to a constant that distinguishes between different extension types.
3031
3032 The sorting of the attributes is based on the following rules:
3033
3034 1. Iterate through the class and its superclasses in order from
3035 subclass to superclass (i.e. iterate through ``cls.__mro__``)
3036
3037 2. For each class, yield the attributes in the order in which they
3038 appear in ``__dict__``, with the exception of those in step
3039 3 below. The order will be the
3040 same as that of the class' construction, with the exception
3041 of attributes that were added after the fact by the application
3042 or the mapper.
3043
3044 3. If a certain attribute key is also in the superclass ``__dict__``,
3045 then it's included in the iteration for that class, and not the
3046 class in which it first appeared.
3047
3048 The above process produces an ordering that is deterministic in terms
3049 of the order in which attributes were assigned to the class.
3050
3051 When dealing with a :class:`.QueryableAttribute`, the
3052 :attr:`.QueryableAttribute.property` attribute refers to the
3053 :class:`.MapperProperty` property, which is what you get when
3054 referring to the collection of mapped properties via
3055 :attr:`_orm.Mapper.attrs`.
3056
3057 .. warning::
3058
3059 The :attr:`_orm.Mapper.all_orm_descriptors`
3060 accessor namespace is an
3061 instance of :class:`.OrderedProperties`. This is
3062 a dictionary-like object which includes a small number of
3063 named methods such as :meth:`.OrderedProperties.items`
3064 and :meth:`.OrderedProperties.values`. When
3065 accessing attributes dynamically, favor using the dict-access
3066 scheme, e.g. ``mapper.all_orm_descriptors[somename]`` over
3067 ``getattr(mapper.all_orm_descriptors, somename)`` to avoid name
3068 collisions.
3069
3070 .. seealso::
3071
3072 :attr:`_orm.Mapper.attrs`
3073
3074 """
3075 return util.ReadOnlyProperties(
3076 dict(self.class_manager._all_sqla_attributes())
3077 )
3078
3079 @HasMemoized.memoized_attribute
3080 @util.preload_module("sqlalchemy.orm.descriptor_props")
3081 def _pk_synonyms(self) -> Dict[str, str]:
3082 """return a dictionary of {syn_attribute_name: pk_attr_name} for
3083 all synonyms that refer to primary key columns
3084
3085 """
3086 descriptor_props = util.preloaded.orm_descriptor_props
3087
3088 pk_keys = {prop.key for prop in self._identity_key_props}
3089
3090 return {
3091 syn.key: syn.name
3092 for k, syn in self._props.items()
3093 if isinstance(syn, descriptor_props.SynonymProperty)
3094 and syn.name in pk_keys
3095 }
3096
3097 @HasMemoized.memoized_attribute
3098 @util.preload_module("sqlalchemy.orm.descriptor_props")
3099 def synonyms(self) -> util.ReadOnlyProperties[SynonymProperty[Any]]:
3100 """Return a namespace of all :class:`.Synonym`
3101 properties maintained by this :class:`_orm.Mapper`.
3102
3103 .. seealso::
3104
3105 :attr:`_orm.Mapper.attrs` - namespace of all
3106 :class:`.MapperProperty`
3107 objects.
3108
3109 """
3110 descriptor_props = util.preloaded.orm_descriptor_props
3111
3112 return self._filter_properties(descriptor_props.SynonymProperty)
3113
3114 @util.ro_non_memoized_property
3115 def entity_namespace(self) -> _EntityNamespace:
3116 return self.class_ # type: ignore[return-value]
3117
3118 @HasMemoized.memoized_attribute
3119 def column_attrs(self) -> util.ReadOnlyProperties[ColumnProperty[Any]]:
3120 """Return a namespace of all :class:`.ColumnProperty`
3121 properties maintained by this :class:`_orm.Mapper`.
3122
3123 .. seealso::
3124
3125 :attr:`_orm.Mapper.attrs` - namespace of all
3126 :class:`.MapperProperty`
3127 objects.
3128
3129 """
3130 return self._filter_properties(properties.ColumnProperty)
3131
3132 @HasMemoized.memoized_attribute
3133 @util.preload_module("sqlalchemy.orm.relationships")
3134 def relationships(
3135 self,
3136 ) -> util.ReadOnlyProperties[RelationshipProperty[Any]]:
3137 """A namespace of all :class:`.Relationship` properties
3138 maintained by this :class:`_orm.Mapper`.
3139
3140 .. warning::
3141
3142 the :attr:`_orm.Mapper.relationships` accessor namespace is an
3143 instance of :class:`.OrderedProperties`. This is
3144 a dictionary-like object which includes a small number of
3145 named methods such as :meth:`.OrderedProperties.items`
3146 and :meth:`.OrderedProperties.values`. When
3147 accessing attributes dynamically, favor using the dict-access
3148 scheme, e.g. ``mapper.relationships[somename]`` over
3149 ``getattr(mapper.relationships, somename)`` to avoid name
3150 collisions.
3151
3152 .. seealso::
3153
3154 :attr:`_orm.Mapper.attrs` - namespace of all
3155 :class:`.MapperProperty`
3156 objects.
3157
3158 """
3159 return self._filter_properties(
3160 util.preloaded.orm_relationships.RelationshipProperty
3161 )
3162
3163 @HasMemoized.memoized_attribute
3164 @util.preload_module("sqlalchemy.orm.descriptor_props")
3165 def composites(self) -> util.ReadOnlyProperties[CompositeProperty[Any]]:
3166 """Return a namespace of all :class:`.Composite`
3167 properties maintained by this :class:`_orm.Mapper`.
3168
3169 .. seealso::
3170
3171 :attr:`_orm.Mapper.attrs` - namespace of all
3172 :class:`.MapperProperty`
3173 objects.
3174
3175 """
3176 return self._filter_properties(
3177 util.preloaded.orm_descriptor_props.CompositeProperty
3178 )
3179
3180 def _filter_properties(
3181 self, type_: Type[_MP]
3182 ) -> util.ReadOnlyProperties[_MP]:
3183 self._check_configure()
3184 return util.ReadOnlyProperties(
3185 util.OrderedDict(
3186 (k, v) for k, v in self._props.items() if isinstance(v, type_)
3187 )
3188 )
3189
3190 @HasMemoized.memoized_attribute
3191 def _get_clause(self):
3192 """create a "get clause" based on the primary key. this is used
3193 by query.get() and many-to-one lazyloads to load this item
3194 by primary key.
3195
3196 """
3197 params = [
3198 (
3199 primary_key,
3200 sql.bindparam("pk_%d" % idx, type_=primary_key.type),
3201 )
3202 for idx, primary_key in enumerate(self.primary_key, 1)
3203 ]
3204 return (
3205 sql.and_(*[k == v for (k, v) in params]),
3206 util.column_dict(params),
3207 )
3208
3209 @HasMemoized.memoized_attribute
3210 def _equivalent_columns(self) -> _EquivalentColumnMap:
3211 """Create a map of all equivalent columns, based on
3212 the determination of column pairs that are equated to
3213 one another based on inherit condition. This is designed
3214 to work with the queries that util.polymorphic_union
3215 comes up with, which often don't include the columns from
3216 the base table directly (including the subclass table columns
3217 only).
3218
3219 The resulting structure is a dictionary of columns mapped
3220 to lists of equivalent columns, e.g.::
3221
3222 {tablea.col1: {tableb.col1, tablec.col1}, tablea.col2: {tabled.col2}}
3223
3224 """ # noqa: E501
3225 result: _EquivalentColumnMap = {}
3226
3227 def visit_binary(binary):
3228 if binary.operator == operators.eq:
3229 if binary.left in result:
3230 result[binary.left].add(binary.right)
3231 else:
3232 result[binary.left] = {binary.right}
3233 if binary.right in result:
3234 result[binary.right].add(binary.left)
3235 else:
3236 result[binary.right] = {binary.left}
3237
3238 for mapper in self.base_mapper.self_and_descendants:
3239 if mapper.inherit_condition is not None:
3240 visitors.traverse(
3241 mapper.inherit_condition, {}, {"binary": visit_binary}
3242 )
3243
3244 return result
3245
3246 def _is_userland_descriptor(self, assigned_name: str, obj: Any) -> bool:
3247 if isinstance(
3248 obj,
3249 (
3250 _MappedAttribute,
3251 instrumentation.ClassManager,
3252 expression.ColumnElement,
3253 ),
3254 ):
3255 return False
3256 else:
3257 return assigned_name not in self._dataclass_fields
3258
3259 @HasMemoized.memoized_attribute
3260 def _dataclass_fields(self):
3261 return [f.name for f in util.dataclass_fields(self.class_)]
3262
3263 def _should_exclude(self, name, assigned_name, local, column):
3264 """determine whether a particular property should be implicitly
3265 present on the class.
3266
3267 This occurs when properties are propagated from an inherited class, or
3268 are applied from the columns present in the mapped table.
3269
3270 """
3271
3272 if column is not None and sql_base._never_select_column(column):
3273 return True
3274
3275 # check for class-bound attributes and/or descriptors,
3276 # either local or from an inherited class
3277 # ignore dataclass field default values
3278 if local:
3279 if self.class_.__dict__.get(
3280 assigned_name, None
3281 ) is not None and self._is_userland_descriptor(
3282 assigned_name, self.class_.__dict__[assigned_name]
3283 ):
3284 return True
3285 else:
3286 attr = self.class_manager._get_class_attr_mro(assigned_name, None)
3287 if attr is not None and self._is_userland_descriptor(
3288 assigned_name, attr
3289 ):
3290 return True
3291
3292 if (
3293 self.include_properties is not None
3294 and name not in self.include_properties
3295 and (column is None or column not in self.include_properties)
3296 ):
3297 self._log("not including property %s" % (name))
3298 return True
3299
3300 if self.exclude_properties is not None and (
3301 name in self.exclude_properties
3302 or (column is not None and column in self.exclude_properties)
3303 ):
3304 self._log("excluding property %s" % (name))
3305 return True
3306
3307 return False
3308
3309 def common_parent(self, other: Mapper[Any]) -> bool:
3310 """Return true if the given mapper shares a
3311 common inherited parent as this mapper."""
3312
3313 return self.base_mapper is other.base_mapper
3314
3315 def is_sibling(self, other: Mapper[Any]) -> bool:
3316 """return true if the other mapper is an inheriting sibling to this
3317 one. common parent but different branch
3318
3319 """
3320 return (
3321 self.base_mapper is other.base_mapper
3322 and not self.isa(other)
3323 and not other.isa(self)
3324 )
3325
3326 def _canload(
3327 self, state: InstanceState[Any], allow_subtypes: bool
3328 ) -> bool:
3329 s = self.primary_mapper()
3330 if self.polymorphic_on is not None or allow_subtypes:
3331 return _state_mapper(state).isa(s)
3332 else:
3333 return _state_mapper(state) is s
3334
3335 def isa(self, other: Mapper[Any]) -> bool:
3336 """Return True if the this mapper inherits from the given mapper."""
3337
3338 m: Optional[Mapper[Any]] = self
3339 while m and m is not other:
3340 m = m.inherits
3341 return bool(m)
3342
3343 def iterate_to_root(self) -> Iterator[Mapper[Any]]:
3344 m: Optional[Mapper[Any]] = self
3345 while m:
3346 yield m
3347 m = m.inherits
3348
3349 @HasMemoized.memoized_attribute
3350 def self_and_descendants(self) -> Sequence[Mapper[Any]]:
3351 """The collection including this mapper and all descendant mappers.
3352
3353 This includes not just the immediately inheriting mappers but
3354 all their inheriting mappers as well.
3355
3356 """
3357 descendants = []
3358 stack = deque([self])
3359 while stack:
3360 item = stack.popleft()
3361 descendants.append(item)
3362 stack.extend(item._inheriting_mappers)
3363 return util.WeakSequence(descendants)
3364
3365 def polymorphic_iterator(self) -> Iterator[Mapper[Any]]:
3366 """Iterate through the collection including this mapper and
3367 all descendant mappers.
3368
3369 This includes not just the immediately inheriting mappers but
3370 all their inheriting mappers as well.
3371
3372 To iterate through an entire hierarchy, use
3373 ``mapper.base_mapper.polymorphic_iterator()``.
3374
3375 """
3376 return iter(self.self_and_descendants)
3377
3378 def primary_mapper(self) -> Mapper[Any]:
3379 """Return the primary mapper corresponding to this mapper's class key
3380 (class)."""
3381
3382 return self.class_manager.mapper
3383
3384 @property
3385 def primary_base_mapper(self) -> Mapper[Any]:
3386 return self.class_manager.mapper.base_mapper
3387
3388 def _result_has_identity_key(self, result, adapter=None):
3389 pk_cols: Sequence[ColumnElement[Any]]
3390 if adapter is not None:
3391 pk_cols = [adapter.columns[c] for c in self.primary_key]
3392 else:
3393 pk_cols = self.primary_key
3394 rk = result.keys()
3395 for col in pk_cols:
3396 if col not in rk:
3397 return False
3398 else:
3399 return True
3400
3401 def identity_key_from_row(
3402 self,
3403 row: Union[Row[Unpack[TupleAny]], RowMapping],
3404 identity_token: Optional[Any] = None,
3405 adapter: Optional[ORMAdapter] = None,
3406 ) -> _IdentityKeyType[_O]:
3407 """Return an identity-map key for use in storing/retrieving an
3408 item from the identity map.
3409
3410 :param row: A :class:`.Row` or :class:`.RowMapping` produced from a
3411 result set that selected from the ORM mapped primary key columns.
3412
3413 .. versionchanged:: 2.0
3414 :class:`.Row` or :class:`.RowMapping` are accepted
3415 for the "row" argument
3416
3417 """
3418 pk_cols: Sequence[ColumnElement[Any]]
3419 if adapter is not None:
3420 pk_cols = [adapter.columns[c] for c in self.primary_key]
3421 else:
3422 pk_cols = self.primary_key
3423
3424 mapping: RowMapping
3425 if hasattr(row, "_mapping"):
3426 mapping = row._mapping
3427 else:
3428 mapping = row # type: ignore[assignment]
3429
3430 return (
3431 self._identity_class,
3432 tuple(mapping[column] for column in pk_cols),
3433 identity_token,
3434 )
3435
3436 def identity_key_from_primary_key(
3437 self,
3438 primary_key: Tuple[Any, ...],
3439 identity_token: Optional[Any] = None,
3440 ) -> _IdentityKeyType[_O]:
3441 """Return an identity-map key for use in storing/retrieving an
3442 item from an identity map.
3443
3444 :param primary_key: A list of values indicating the identifier.
3445
3446 """
3447 return (
3448 self._identity_class,
3449 tuple(primary_key),
3450 identity_token,
3451 )
3452
3453 def identity_key_from_instance(self, instance: _O) -> _IdentityKeyType[_O]:
3454 """Return the identity key for the given instance, based on
3455 its primary key attributes.
3456
3457 If the instance's state is expired, calling this method
3458 will result in a database check to see if the object has been deleted.
3459 If the row no longer exists,
3460 :class:`~sqlalchemy.orm.exc.ObjectDeletedError` is raised.
3461
3462 This value is typically also found on the instance state under the
3463 attribute name `key`.
3464
3465 """
3466 state = attributes.instance_state(instance)
3467 return self._identity_key_from_state(state, PassiveFlag.PASSIVE_OFF)
3468
3469 def _identity_key_from_state(
3470 self,
3471 state: InstanceState[_O],
3472 passive: PassiveFlag = PassiveFlag.PASSIVE_RETURN_NO_VALUE,
3473 ) -> _IdentityKeyType[_O]:
3474 dict_ = state.dict
3475 manager = state.manager
3476 return (
3477 self._identity_class,
3478 tuple(
3479 [
3480 manager[prop.key].impl.get(state, dict_, passive)
3481 for prop in self._identity_key_props
3482 ]
3483 ),
3484 state.identity_token,
3485 )
3486
3487 def primary_key_from_instance(self, instance: _O) -> Tuple[Any, ...]:
3488 """Return the list of primary key values for the given
3489 instance.
3490
3491 If the instance's state is expired, calling this method
3492 will result in a database check to see if the object has been deleted.
3493 If the row no longer exists,
3494 :class:`~sqlalchemy.orm.exc.ObjectDeletedError` is raised.
3495
3496 """
3497 state = attributes.instance_state(instance)
3498 identity_key = self._identity_key_from_state(
3499 state, PassiveFlag.PASSIVE_OFF
3500 )
3501 return identity_key[1]
3502
3503 @HasMemoized.memoized_attribute
3504 def _persistent_sortkey_fn(self):
3505 key_fns = [col.type.sort_key_function for col in self.primary_key]
3506
3507 if set(key_fns).difference([None]):
3508
3509 def key(state):
3510 return tuple(
3511 key_fn(val) if key_fn is not None else val
3512 for key_fn, val in zip(key_fns, state.key[1])
3513 )
3514
3515 else:
3516
3517 def key(state):
3518 return state.key[1]
3519
3520 return key
3521
3522 @HasMemoized.memoized_attribute
3523 def _identity_key_props(self):
3524 return [self._columntoproperty[col] for col in self.primary_key]
3525
3526 @HasMemoized.memoized_attribute
3527 def _all_pk_cols(self):
3528 collection: Set[ColumnClause[Any]] = set()
3529 for table in self.tables:
3530 collection.update(self._pks_by_table[table])
3531 return collection
3532
3533 @HasMemoized.memoized_attribute
3534 def _should_undefer_in_wildcard(self):
3535 cols: Set[ColumnElement[Any]] = set(self.primary_key)
3536 if self.polymorphic_on is not None:
3537 cols.add(self.polymorphic_on)
3538 return cols
3539
3540 @HasMemoized.memoized_attribute
3541 def _primary_key_propkeys(self):
3542 return {self._columntoproperty[col].key for col in self._all_pk_cols}
3543
3544 def _get_state_attr_by_column(
3545 self,
3546 state: InstanceState[_O],
3547 dict_: _InstanceDict,
3548 column: ColumnElement[Any],
3549 passive: PassiveFlag = PassiveFlag.PASSIVE_RETURN_NO_VALUE,
3550 ) -> Any:
3551 prop = self._columntoproperty[column]
3552 return state.manager[prop.key].impl.get(state, dict_, passive=passive)
3553
3554 def _set_committed_state_attr_by_column(self, state, dict_, column, value):
3555 prop = self._columntoproperty[column]
3556 state.manager[prop.key].impl.set_committed_value(state, dict_, value)
3557
3558 def _set_state_attr_by_column(self, state, dict_, column, value):
3559 prop = self._columntoproperty[column]
3560 state.manager[prop.key].impl.set(state, dict_, value, None)
3561
3562 def _get_committed_attr_by_column(self, obj, column):
3563 state = attributes.instance_state(obj)
3564 dict_ = attributes.instance_dict(obj)
3565 return self._get_committed_state_attr_by_column(
3566 state, dict_, column, passive=PassiveFlag.PASSIVE_OFF
3567 )
3568
3569 def _get_committed_state_attr_by_column(
3570 self, state, dict_, column, passive=PassiveFlag.PASSIVE_RETURN_NO_VALUE
3571 ):
3572 prop = self._columntoproperty[column]
3573 return state.manager[prop.key].impl.get_committed_value(
3574 state, dict_, passive=passive
3575 )
3576
3577 def _optimized_get_statement(self, state, attribute_names):
3578 """assemble a WHERE clause which retrieves a given state by primary
3579 key, using a minimized set of tables.
3580
3581 Applies to a joined-table inheritance mapper where the
3582 requested attribute names are only present on joined tables,
3583 not the base table. The WHERE clause attempts to include
3584 only those tables to minimize joins.
3585
3586 """
3587 props = self._props
3588
3589 col_attribute_names = set(attribute_names).intersection(
3590 state.mapper.column_attrs.keys()
3591 )
3592 tables: Set[FromClause] = set(
3593 chain(
3594 *[
3595 sql_util.find_tables(c, check_columns=True)
3596 for key in col_attribute_names
3597 for c in props[key].columns
3598 ]
3599 )
3600 )
3601
3602 if self.base_mapper.local_table in tables:
3603 return None
3604
3605 def visit_binary(binary):
3606 leftcol = binary.left
3607 rightcol = binary.right
3608 if leftcol is None or rightcol is None:
3609 return
3610
3611 if leftcol.table not in tables:
3612 leftval = self._get_committed_state_attr_by_column(
3613 state,
3614 state.dict,
3615 leftcol,
3616 passive=PassiveFlag.PASSIVE_NO_INITIALIZE,
3617 )
3618 if leftval in orm_util._none_set:
3619 raise _OptGetColumnsNotAvailable()
3620 binary.left = sql.bindparam(
3621 None, leftval, type_=binary.right.type
3622 )
3623 elif rightcol.table not in tables:
3624 rightval = self._get_committed_state_attr_by_column(
3625 state,
3626 state.dict,
3627 rightcol,
3628 passive=PassiveFlag.PASSIVE_NO_INITIALIZE,
3629 )
3630 if rightval in orm_util._none_set:
3631 raise _OptGetColumnsNotAvailable()
3632 binary.right = sql.bindparam(
3633 None, rightval, type_=binary.right.type
3634 )
3635
3636 allconds: List[ColumnElement[bool]] = []
3637
3638 start = False
3639
3640 # as of #7507, from the lowest base table on upwards,
3641 # we include all intermediary tables.
3642
3643 for mapper in reversed(list(self.iterate_to_root())):
3644 if mapper.local_table in tables:
3645 start = True
3646 elif not isinstance(mapper.local_table, expression.TableClause):
3647 return None
3648 if start and not mapper.single:
3649 assert mapper.inherits
3650 assert not mapper.concrete
3651 assert mapper.inherit_condition is not None
3652 allconds.append(mapper.inherit_condition)
3653 tables.add(mapper.local_table)
3654
3655 # only the bottom table needs its criteria to be altered to fit
3656 # the primary key ident - the rest of the tables upwards to the
3657 # descendant-most class should all be present and joined to each
3658 # other.
3659 try:
3660 _traversed = visitors.cloned_traverse(
3661 allconds[0], {}, {"binary": visit_binary}
3662 )
3663 except _OptGetColumnsNotAvailable:
3664 return None
3665 else:
3666 allconds[0] = _traversed
3667
3668 cond = sql.and_(*allconds)
3669
3670 cols = []
3671 for key in col_attribute_names:
3672 cols.extend(props[key].columns)
3673 return (
3674 sql.select(*cols)
3675 .where(cond)
3676 .set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL)
3677 )
3678
3679 def _iterate_to_target_viawpoly(self, mapper):
3680 if self.isa(mapper):
3681 prev = self
3682 for m in self.iterate_to_root():
3683 yield m
3684
3685 if m is not prev and prev not in m._with_polymorphic_mappers:
3686 break
3687
3688 prev = m
3689 if m is mapper:
3690 break
3691
3692 @HasMemoized.memoized_attribute
3693 def _would_selectinload_combinations_cache(self):
3694 return {}
3695
3696 def _would_selectin_load_only_from_given_mapper(self, super_mapper):
3697 """return True if this mapper would "selectin" polymorphic load based
3698 on the given super mapper, and not from a setting from a subclass.
3699
3700 given::
3701
3702 class A: ...
3703
3704
3705 class B(A):
3706 __mapper_args__ = {"polymorphic_load": "selectin"}
3707
3708
3709 class C(B): ...
3710
3711
3712 class D(B):
3713 __mapper_args__ = {"polymorphic_load": "selectin"}
3714
3715 ``inspect(C)._would_selectin_load_only_from_given_mapper(inspect(B))``
3716 returns True, because C does selectin loading because of B's setting.
3717
3718 OTOH, ``inspect(D)
3719 ._would_selectin_load_only_from_given_mapper(inspect(B))``
3720 returns False, because D does selectin loading because of its own
3721 setting; when we are doing a selectin poly load from B, we want to
3722 filter out D because it would already have its own selectin poly load
3723 set up separately.
3724
3725 Added as part of #9373.
3726
3727 """
3728 cache = self._would_selectinload_combinations_cache
3729
3730 try:
3731 return cache[super_mapper]
3732 except KeyError:
3733 pass
3734
3735 # assert that given object is a supermapper, meaning we already
3736 # strong reference it directly or indirectly. this allows us
3737 # to not worry that we are creating new strongrefs to unrelated
3738 # mappers or other objects.
3739 assert self.isa(super_mapper)
3740
3741 mapper = super_mapper
3742 for m in self._iterate_to_target_viawpoly(mapper):
3743 if m.polymorphic_load == "selectin":
3744 retval = m is super_mapper
3745 break
3746 else:
3747 retval = False
3748
3749 cache[super_mapper] = retval
3750 return retval
3751
3752 def _should_selectin_load(self, enabled_via_opt, polymorphic_from):
3753 if not enabled_via_opt:
3754 # common case, takes place for all polymorphic loads
3755 mapper = polymorphic_from
3756 for m in self._iterate_to_target_viawpoly(mapper):
3757 if m.polymorphic_load == "selectin":
3758 return m
3759 else:
3760 # uncommon case, selectin load options were used
3761 enabled_via_opt = set(enabled_via_opt)
3762 enabled_via_opt_mappers = {e.mapper: e for e in enabled_via_opt}
3763 for entity in enabled_via_opt.union([polymorphic_from]):
3764 mapper = entity.mapper
3765 for m in self._iterate_to_target_viawpoly(mapper):
3766 if (
3767 m.polymorphic_load == "selectin"
3768 or m in enabled_via_opt_mappers
3769 ):
3770 return enabled_via_opt_mappers.get(m, m)
3771
3772 return None
3773
3774 @util.preload_module("sqlalchemy.orm.strategy_options")
3775 def _subclass_load_via_in(self, entity, polymorphic_from):
3776 """Assemble a that can load the columns local to
3777 this subclass as a SELECT with IN.
3778
3779 """
3780
3781 strategy_options = util.preloaded.orm_strategy_options
3782
3783 assert self.inherits
3784
3785 if self.polymorphic_on is not None:
3786 polymorphic_prop = self._columntoproperty[self.polymorphic_on]
3787 keep_props = set([polymorphic_prop] + self._identity_key_props)
3788 else:
3789 keep_props = set(self._identity_key_props)
3790
3791 disable_opt = strategy_options.Load(entity)
3792 enable_opt = strategy_options.Load(entity)
3793
3794 classes_to_include = {self}
3795 m: Optional[Mapper[Any]] = self.inherits
3796 while (
3797 m is not None
3798 and m is not polymorphic_from
3799 and m.polymorphic_load == "selectin"
3800 ):
3801 classes_to_include.add(m)
3802 m = m.inherits
3803
3804 for prop in self.column_attrs + self.relationships:
3805 # skip prop keys that are not instrumented on the mapped class.
3806 # this is primarily the "_sa_polymorphic_on" property that gets
3807 # created for an ad-hoc polymorphic_on SQL expression, issue #8704
3808 if prop.key not in self.class_manager:
3809 continue
3810
3811 if prop.parent in classes_to_include or prop in keep_props:
3812 # "enable" options, to turn on the properties that we want to
3813 # load by default (subject to options from the query)
3814 if not isinstance(prop, StrategizedProperty):
3815 continue
3816
3817 enable_opt = enable_opt._set_generic_strategy(
3818 # convert string name to an attribute before passing
3819 # to loader strategy. note this must be in terms
3820 # of given entity, such as AliasedClass, etc.
3821 (getattr(entity.entity_namespace, prop.key),),
3822 dict(prop.strategy_key),
3823 _reconcile_to_other=True,
3824 )
3825 else:
3826 # "disable" options, to turn off the properties from the
3827 # superclass that we *don't* want to load, applied after
3828 # the options from the query to override them
3829 disable_opt = disable_opt._set_generic_strategy(
3830 # convert string name to an attribute before passing
3831 # to loader strategy. note this must be in terms
3832 # of given entity, such as AliasedClass, etc.
3833 (getattr(entity.entity_namespace, prop.key),),
3834 {"do_nothing": True},
3835 _reconcile_to_other=False,
3836 )
3837
3838 primary_key = list(self.primary_key)
3839
3840 in_expr: ColumnElement[Any]
3841
3842 if len(primary_key) > 1:
3843 in_expr = sql.tuple_(*primary_key)
3844 else:
3845 in_expr = primary_key[0]
3846
3847 if entity.is_aliased_class:
3848 assert entity.mapper is self
3849
3850 q = sql.select(entity).set_label_style(
3851 LABEL_STYLE_TABLENAME_PLUS_COL
3852 )
3853
3854 in_expr = entity._adapter.traverse(in_expr)
3855 q = q.where(
3856 in_expr.in_(sql.bindparam("primary_keys", expanding=True))
3857 )
3858 else:
3859 q = sql.select(self).set_label_style(
3860 LABEL_STYLE_TABLENAME_PLUS_COL
3861 )
3862 q = q.where(
3863 in_expr.in_(sql.bindparam("primary_keys", expanding=True))
3864 )
3865
3866 return q, enable_opt, disable_opt
3867
3868 @HasMemoized.memoized_attribute
3869 def _subclass_load_via_in_mapper(self):
3870 # the default is loading this mapper against the basemost mapper
3871 return self._subclass_load_via_in(self, self.base_mapper)
3872
3873 def cascade_iterator(
3874 self,
3875 type_: str,
3876 state: InstanceState[_O],
3877 halt_on: Optional[Callable[[InstanceState[Any]], bool]] = None,
3878 ) -> Iterator[
3879 Tuple[object, Mapper[Any], InstanceState[Any], _InstanceDict]
3880 ]:
3881 r"""Iterate each element and its mapper in an object graph,
3882 for all relationships that meet the given cascade rule.
3883
3884 :param type\_:
3885 The name of the cascade rule (i.e. ``"save-update"``, ``"delete"``,
3886 etc.).
3887
3888 .. note:: the ``"all"`` cascade is not accepted here. For a generic
3889 object traversal function, see :ref:`faq_walk_objects`.
3890
3891 :param state:
3892 The lead InstanceState. child items will be processed per
3893 the relationships defined for this object's mapper.
3894
3895 :return: the method yields individual object instances.
3896
3897 .. seealso::
3898
3899 :ref:`unitofwork_cascades`
3900
3901 :ref:`faq_walk_objects` - illustrates a generic function to
3902 traverse all objects without relying on cascades.
3903
3904 """
3905 visited_states: Set[InstanceState[Any]] = set()
3906 prp, mpp = object(), object()
3907
3908 assert state.mapper.isa(self)
3909
3910 # this is actually a recursive structure, fully typing it seems
3911 # a little too difficult for what it's worth here
3912 visitables: Deque[
3913 Tuple[
3914 Deque[Any],
3915 object,
3916 Optional[InstanceState[Any]],
3917 Optional[_InstanceDict],
3918 ]
3919 ]
3920
3921 visitables = deque(
3922 [(deque(state.mapper._props.values()), prp, state, state.dict)]
3923 )
3924
3925 while visitables:
3926 iterator, item_type, parent_state, parent_dict = visitables[-1]
3927 if not iterator:
3928 visitables.pop()
3929 continue
3930
3931 if item_type is prp:
3932 prop = iterator.popleft()
3933 if not prop.cascade or type_ not in prop.cascade:
3934 continue
3935 assert parent_state is not None
3936 assert parent_dict is not None
3937 queue = deque(
3938 prop.cascade_iterator(
3939 type_,
3940 parent_state,
3941 parent_dict,
3942 visited_states,
3943 halt_on,
3944 )
3945 )
3946 if queue:
3947 visitables.append((queue, mpp, None, None))
3948 elif item_type is mpp:
3949 (
3950 instance,
3951 instance_mapper,
3952 corresponding_state,
3953 corresponding_dict,
3954 ) = iterator.popleft()
3955 yield (
3956 instance,
3957 instance_mapper,
3958 corresponding_state,
3959 corresponding_dict,
3960 )
3961 visitables.append(
3962 (
3963 deque(instance_mapper._props.values()),
3964 prp,
3965 corresponding_state,
3966 corresponding_dict,
3967 )
3968 )
3969
3970 @HasMemoized.memoized_attribute
3971 def _compiled_cache(self):
3972 return util.LRUCache(self._compiled_cache_size)
3973
3974 @HasMemoized.memoized_attribute
3975 def _multiple_persistence_tables(self):
3976 return len(self.tables) > 1
3977
3978 @HasMemoized.memoized_attribute
3979 def _sorted_tables(self):
3980 table_to_mapper: Dict[TableClause, Mapper[Any]] = {}
3981
3982 for mapper in self.base_mapper.self_and_descendants:
3983 for t in mapper.tables:
3984 table_to_mapper.setdefault(t, mapper)
3985
3986 extra_dependencies = []
3987 for table, mapper in table_to_mapper.items():
3988 super_ = mapper.inherits
3989 if super_:
3990 extra_dependencies.extend(
3991 [(super_table, table) for super_table in super_.tables]
3992 )
3993
3994 def skip(fk):
3995 # attempt to skip dependencies that are not
3996 # significant to the inheritance chain
3997 # for two tables that are related by inheritance.
3998 # while that dependency may be important, it's technically
3999 # not what we mean to sort on here.
4000 parent = table_to_mapper.get(fk.parent.table)
4001 dep = table_to_mapper.get(fk.column.table)
4002 if (
4003 parent is not None
4004 and dep is not None
4005 and dep is not parent
4006 and dep.inherit_condition is not None
4007 ):
4008 cols = set(sql_util._find_columns(dep.inherit_condition))
4009 if parent.inherit_condition is not None:
4010 cols = cols.union(
4011 sql_util._find_columns(parent.inherit_condition)
4012 )
4013 return fk.parent not in cols and fk.column not in cols
4014 else:
4015 return fk.parent not in cols
4016 return False
4017
4018 sorted_ = sql_util.sort_tables(
4019 table_to_mapper,
4020 skip_fn=skip,
4021 extra_dependencies=extra_dependencies,
4022 )
4023
4024 ret = util.OrderedDict()
4025 for t in sorted_:
4026 ret[t] = table_to_mapper[t]
4027 return ret
4028
4029 def _memo(self, key: Any, callable_: Callable[[], _T]) -> _T:
4030 if key in self._memoized_values:
4031 return cast(_T, self._memoized_values[key])
4032 else:
4033 self._memoized_values[key] = value = callable_()
4034 return value
4035
4036 @util.memoized_property
4037 def _table_to_equated(self):
4038 """memoized map of tables to collections of columns to be
4039 synchronized upwards to the base mapper."""
4040
4041 result: util.defaultdict[
4042 Table,
4043 List[
4044 Tuple[
4045 Mapper[Any],
4046 List[Tuple[ColumnElement[Any], ColumnElement[Any]]],
4047 ]
4048 ],
4049 ] = util.defaultdict(list)
4050
4051 def set_union(x, y):
4052 return x.union(y)
4053
4054 for table in self._sorted_tables:
4055 cols = set(table.c)
4056
4057 for m in self.iterate_to_root():
4058 if m._inherits_equated_pairs and cols.intersection(
4059 reduce(
4060 set_union,
4061 [l.proxy_set for l, r in m._inherits_equated_pairs],
4062 )
4063 ):
4064 result[table].append((m, m._inherits_equated_pairs))
4065
4066 return result
4067
4068
4069class _OptGetColumnsNotAvailable(Exception):
4070 pass
4071
4072
4073def configure_mappers() -> None:
4074 """Initialize the inter-mapper relationships of all mappers that
4075 have been constructed thus far across all :class:`_orm.registry`
4076 collections.
4077
4078 The configure step is used to reconcile and initialize the
4079 :func:`_orm.relationship` linkages between mapped classes, as well as to
4080 invoke configuration events such as the
4081 :meth:`_orm.MapperEvents.before_configured` and
4082 :meth:`_orm.MapperEvents.after_configured`, which may be used by ORM
4083 extensions or user-defined extension hooks.
4084
4085 Mapper configuration is normally invoked automatically, the first time
4086 mappings from a particular :class:`_orm.registry` are used, as well as
4087 whenever mappings are used and additional not-yet-configured mappers have
4088 been constructed. The automatic configuration process however is local only
4089 to the :class:`_orm.registry` involving the target mapper and any related
4090 :class:`_orm.registry` objects which it may depend on; this is
4091 equivalent to invoking the :meth:`_orm.registry.configure` method
4092 on a particular :class:`_orm.registry`.
4093
4094 By contrast, the :func:`_orm.configure_mappers` function will invoke the
4095 configuration process on all :class:`_orm.registry` objects that
4096 exist in memory, and may be useful for scenarios where many individual
4097 :class:`_orm.registry` objects that are nonetheless interrelated are
4098 in use.
4099
4100 .. versionchanged:: 1.4
4101
4102 As of SQLAlchemy 1.4.0b2, this function works on a
4103 per-:class:`_orm.registry` basis, locating all :class:`_orm.registry`
4104 objects present and invoking the :meth:`_orm.registry.configure` method
4105 on each. The :meth:`_orm.registry.configure` method may be preferred to
4106 limit the configuration of mappers to those local to a particular
4107 :class:`_orm.registry` and/or declarative base class.
4108
4109 Points at which automatic configuration is invoked include when a mapped
4110 class is instantiated into an instance, as well as when ORM queries
4111 are emitted using :meth:`.Session.query` or :meth:`_orm.Session.execute`
4112 with an ORM-enabled statement.
4113
4114 The mapper configure process, whether invoked by
4115 :func:`_orm.configure_mappers` or from :meth:`_orm.registry.configure`,
4116 provides several event hooks that can be used to augment the mapper
4117 configuration step. These hooks include:
4118
4119 * :meth:`.MapperEvents.before_configured` - called once before
4120 :func:`.configure_mappers` or :meth:`_orm.registry.configure` does any
4121 work; this can be used to establish additional options, properties, or
4122 related mappings before the operation proceeds.
4123
4124 * :meth:`.RegistryEvents.before_configured` - Like
4125 :meth:`.MapperEvents.before_configured`, but local to a specific
4126 :class:`_orm.registry`.
4127
4128 .. versionadded:: 2.1 - added :meth:`.RegistryEvents.before_configured`
4129
4130 * :meth:`.MapperEvents.mapper_configured` - called as each individual
4131 :class:`_orm.Mapper` is configured within the process; will include all
4132 mapper state except for backrefs set up by other mappers that are still
4133 to be configured.
4134
4135 * :meth:`.MapperEvents.after_configured` - called once after
4136 :func:`.configure_mappers` or :meth:`_orm.registry.configure` is
4137 complete; at this stage, all :class:`_orm.Mapper` objects that fall
4138 within the scope of the configuration operation will be fully configured.
4139 Note that the calling application may still have other mappings that
4140 haven't been produced yet, such as if they are in modules as yet
4141 unimported, and may also have mappings that are still to be configured,
4142 if they are in other :class:`_orm.registry` collections not part of the
4143 current scope of configuration.
4144
4145 * :meth:`.RegistryEvents.after_configured` - Like
4146 :meth:`.MapperEvents.after_configured`, but local to a specific
4147 :class:`_orm.registry`.
4148
4149 .. versionadded:: 2.1 - added :meth:`.RegistryEvents.after_configured`
4150
4151 """
4152
4153 _configure_registries(_all_registries(), cascade=True)
4154
4155
4156def _configure_registries(
4157 registries: Set[_RegistryType], cascade: bool
4158) -> None:
4159 for reg in registries:
4160 if reg._new_mappers:
4161 break
4162 else:
4163 return
4164
4165 with _CONFIGURE_MUTEX:
4166 global _already_compiling
4167 if _already_compiling:
4168 return
4169 _already_compiling = True
4170 try:
4171 # double-check inside mutex
4172 for reg in registries:
4173 if reg._new_mappers:
4174 break
4175 else:
4176 return
4177
4178 Mapper.dispatch._for_class(Mapper).before_configured() # type: ignore # noqa: E501
4179
4180 # initialize properties on all mappers
4181 # note that _mapper_registry is unordered, which
4182 # may randomly conceal/reveal issues related to
4183 # the order of mapper compilation
4184
4185 registries_configured = list(
4186 _do_configure_registries(registries, cascade)
4187 )
4188
4189 finally:
4190 _already_compiling = False
4191 for reg in registries_configured:
4192 reg.dispatch.after_configured(reg)
4193 Mapper.dispatch._for_class(Mapper).after_configured() # type: ignore
4194
4195
4196@util.preload_module("sqlalchemy.orm.decl_api")
4197def _do_configure_registries(
4198 registries: Set[_RegistryType], cascade: bool
4199) -> Iterator[registry]:
4200 registry = util.preloaded.orm_decl_api.registry
4201
4202 orig = set(registries)
4203
4204 for reg in registry._recurse_with_dependencies(registries):
4205 if reg._new_mappers:
4206 reg.dispatch.before_configured(reg)
4207
4208 has_skip = False
4209
4210 for mapper in reg._mappers_to_configure():
4211 run_configure = None
4212
4213 for fn in mapper.dispatch.before_mapper_configured:
4214 run_configure = fn(mapper, mapper.class_)
4215 if run_configure is EXT_SKIP:
4216 has_skip = True
4217 break
4218 if run_configure is EXT_SKIP:
4219 continue
4220
4221 if getattr(mapper, "_configure_failed", False):
4222 e = sa_exc.InvalidRequestError(
4223 "One or more mappers failed to initialize - "
4224 "can't proceed with initialization of other "
4225 "mappers. Triggering mapper: '%s'. "
4226 "Original exception was: %s"
4227 % (mapper, mapper._configure_failed)
4228 )
4229 e._configure_failed = mapper._configure_failed # type: ignore
4230 raise e
4231
4232 if not mapper.configured:
4233 try:
4234 mapper._post_configure_properties()
4235 mapper._expire_memoizations()
4236 mapper.dispatch.mapper_configured(mapper, mapper.class_)
4237 except Exception:
4238 exc = sys.exc_info()[1]
4239 if not hasattr(exc, "_configure_failed"):
4240 mapper._configure_failed = exc
4241 raise
4242
4243 if reg._new_mappers:
4244 yield reg
4245 if not has_skip:
4246 reg._new_mappers = False
4247
4248 if not cascade and reg._dependencies.difference(orig):
4249 raise sa_exc.InvalidRequestError(
4250 "configure was called with cascade=False but "
4251 "additional registries remain"
4252 )
4253
4254
4255@util.preload_module("sqlalchemy.orm.decl_api")
4256def _dispose_registries(registries: Set[_RegistryType], cascade: bool) -> None:
4257 registry = util.preloaded.orm_decl_api.registry
4258
4259 orig = set(registries)
4260
4261 for reg in registry._recurse_with_dependents(registries):
4262 if not cascade and reg._dependents.difference(orig):
4263 raise sa_exc.InvalidRequestError(
4264 "Registry has dependent registries that are not disposed; "
4265 "pass cascade=True to clear these also"
4266 )
4267
4268 while reg._managers:
4269 try:
4270 manager, _ = reg._managers.popitem()
4271 except KeyError:
4272 # guard against race between while and popitem
4273 pass
4274 else:
4275 reg._dispose_manager_and_mapper(manager)
4276
4277 reg._dependents.clear()
4278 for dep in reg._dependencies:
4279 dep._dependents.discard(reg)
4280 reg._dependencies.clear()
4281 # this wasn't done in the 1.3 clear_mappers() and in fact it
4282 # was a bug, as it could cause configure_mappers() to invoke
4283 # the "before_configured" event even though mappers had all been
4284 # disposed.
4285 reg._new_mappers = False
4286
4287
4288def reconstructor(fn: _Fn) -> _Fn:
4289 """Decorate a method as the 'reconstructor' hook.
4290
4291 Designates a single method as the "reconstructor", an ``__init__``-like
4292 method that will be called by the ORM after the instance has been
4293 loaded from the database or otherwise reconstituted.
4294
4295 .. tip::
4296
4297 The :func:`_orm.reconstructor` decorator makes use of the
4298 :meth:`_orm.InstanceEvents.load` event hook, which can be
4299 used directly.
4300
4301 The reconstructor will be invoked with no arguments. Scalar
4302 (non-collection) database-mapped attributes of the instance will
4303 be available for use within the function. Eagerly-loaded
4304 collections are generally not yet available and will usually only
4305 contain the first element. ORM state changes made to objects at
4306 this stage will not be recorded for the next flush() operation, so
4307 the activity within a reconstructor should be conservative.
4308
4309 .. seealso::
4310
4311 :meth:`.InstanceEvents.load`
4312
4313 """
4314 fn.__sa_reconstructor__ = True # type: ignore[attr-defined]
4315 return fn
4316
4317
4318def validates(
4319 *names: str, include_removes: bool = False, include_backrefs: bool = True
4320) -> Callable[[_Fn], _Fn]:
4321 r"""Decorate a method as a 'validator' for one or more named properties.
4322
4323 Designates a method as a validator, a method which receives the
4324 name of the attribute as well as a value to be assigned, or in the
4325 case of a collection, the value to be added to the collection.
4326 The function can then raise validation exceptions to halt the
4327 process from continuing (where Python's built-in ``ValueError``
4328 and ``AssertionError`` exceptions are reasonable choices), or can
4329 modify or replace the value before proceeding. The function should
4330 otherwise return the given value.
4331
4332 Note that a validator for a collection **cannot** issue a load of that
4333 collection within the validation routine - this usage raises
4334 an assertion to avoid recursion overflows. This is a reentrant
4335 condition which is not supported.
4336
4337 :param \*names: list of attribute names to be validated.
4338 :param include_removes: if True, "remove" events will be
4339 sent as well - the validation function must accept an additional
4340 argument "is_remove" which will be a boolean.
4341
4342 :param include_backrefs: defaults to ``True``; if ``False``, the
4343 validation function will not emit if the originator is an attribute
4344 event related via a backref. This can be used for bi-directional
4345 :func:`.validates` usage where only one validator should emit per
4346 attribute operation.
4347
4348 .. versionchanged:: 2.0.16 This parameter inadvertently defaulted to
4349 ``False`` for releases 2.0.0 through 2.0.15. Its correct default
4350 of ``True`` is restored in 2.0.16.
4351
4352 .. seealso::
4353
4354 :ref:`simple_validators` - usage examples for :func:`.validates`
4355
4356 """
4357
4358 def wrap(fn: _Fn) -> _Fn:
4359 fn.__sa_validators__ = names # type: ignore[attr-defined]
4360 fn.__sa_validation_opts__ = { # type: ignore[attr-defined]
4361 "include_removes": include_removes,
4362 "include_backrefs": include_backrefs,
4363 }
4364 return fn
4365
4366 return wrap
4367
4368
4369def _event_on_load(state, ctx):
4370 instrumenting_mapper = state.manager.mapper
4371
4372 if instrumenting_mapper._reconstructor:
4373 instrumenting_mapper._reconstructor(state.obj())
4374
4375
4376def _event_on_init(state, args, kwargs):
4377 """Run init_instance hooks.
4378
4379 This also includes mapper compilation, normally not needed
4380 here but helps with some piecemeal configuration
4381 scenarios (such as in the ORM tutorial).
4382
4383 """
4384
4385 instrumenting_mapper = state.manager.mapper
4386 if instrumenting_mapper:
4387 instrumenting_mapper._check_configure()
4388 if instrumenting_mapper._set_polymorphic_identity:
4389 instrumenting_mapper._set_polymorphic_identity(state)
4390
4391
4392class _ColumnMapping(Dict["ColumnElement[Any]", "MapperProperty[Any]"]):
4393 """Error reporting helper for mapper._columntoproperty."""
4394
4395 __slots__ = ("mapper",)
4396
4397 def __init__(self, mapper):
4398 # TODO: weakref would be a good idea here
4399 self.mapper = mapper
4400
4401 def __missing__(self, column):
4402 prop = self.mapper._props.get(column)
4403 if prop:
4404 raise orm_exc.UnmappedColumnError(
4405 "Column '%s.%s' is not available, due to "
4406 "conflicting property '%s':%r"
4407 % (column.table.name, column.name, column.key, prop)
4408 )
4409 raise orm_exc.UnmappedColumnError(
4410 "No column %s is configured on mapper %s..."
4411 % (column, self.mapper)
4412 )