You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3594 lines
133KB

  1. # orm/mapper.py
  2. # Copyright (C) 2005-2021 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: http://www.opensource.org/licenses/mit-license.php
  7. """Logic to map Python classes to and from selectables.
  8. Defines the :class:`~sqlalchemy.orm.mapper.Mapper` class, the central
  9. configurational unit which associates a class with a database table.
  10. This is a semi-private module; the main configurational API of the ORM is
  11. available in :class:`~sqlalchemy.orm.`.
  12. """
  13. from __future__ import absolute_import
  14. from collections import deque
  15. from itertools import chain
  16. import sys
  17. import weakref
  18. from . import attributes
  19. from . import exc as orm_exc
  20. from . import instrumentation
  21. from . import loading
  22. from . import properties
  23. from . import util as orm_util
  24. from .base import _class_to_mapper
  25. from .base import _state_mapper
  26. from .base import class_mapper
  27. from .base import state_str
  28. from .interfaces import _MappedAttribute
  29. from .interfaces import EXT_SKIP
  30. from .interfaces import InspectionAttr
  31. from .interfaces import MapperProperty
  32. from .interfaces import ORMEntityColumnsClauseRole
  33. from .interfaces import ORMFromClauseRole
  34. from .path_registry import PathRegistry
  35. from .. import event
  36. from .. import exc as sa_exc
  37. from .. import inspection
  38. from .. import log
  39. from .. import schema
  40. from .. import sql
  41. from .. import util
  42. from ..sql import base as sql_base
  43. from ..sql import coercions
  44. from ..sql import expression
  45. from ..sql import operators
  46. from ..sql import roles
  47. from ..sql import util as sql_util
  48. from ..sql import visitors
  49. from ..sql.selectable import LABEL_STYLE_TABLENAME_PLUS_COL
  50. from ..util import HasMemoized
  51. _mapper_registries = weakref.WeakKeyDictionary()
  52. _legacy_registry = None
  53. def _all_registries():
  54. with _CONFIGURE_MUTEX:
  55. return set(_mapper_registries)
  56. def _unconfigured_mappers():
  57. for reg in _all_registries():
  58. for mapper in reg._mappers_to_configure():
  59. yield mapper
  60. _already_compiling = False
  61. # a constant returned by _get_attr_by_column to indicate
  62. # this mapper is not handling an attribute for a particular
  63. # column
  64. NO_ATTRIBUTE = util.symbol("NO_ATTRIBUTE")
  65. # lock used to synchronize the "mapper configure" step
  66. _CONFIGURE_MUTEX = util.threading.RLock()
  67. @inspection._self_inspects
  68. @log.class_logger
  69. class Mapper(
  70. ORMFromClauseRole,
  71. ORMEntityColumnsClauseRole,
  72. sql_base.MemoizedHasCacheKey,
  73. InspectionAttr,
  74. ):
  75. """Define the correlation of class attributes to database table
  76. columns.
  77. The :class:`_orm.Mapper` object is instantiated using the
  78. :func:`~sqlalchemy.orm.mapper` function. For information
  79. about instantiating new :class:`_orm.Mapper` objects, see
  80. that function's documentation.
  81. When :func:`.mapper` is used
  82. explicitly to link a user defined class with table
  83. metadata, this is referred to as *classical mapping*.
  84. Modern SQLAlchemy usage tends to favor the
  85. :mod:`sqlalchemy.ext.declarative` extension for class
  86. configuration, which
  87. makes usage of :func:`.mapper` behind the scenes.
  88. Given a particular class known to be mapped by the ORM,
  89. the :class:`_orm.Mapper` which maintains it can be acquired
  90. using the :func:`_sa.inspect` function::
  91. from sqlalchemy import inspect
  92. mapper = inspect(MyClass)
  93. A class which was mapped by the :mod:`sqlalchemy.ext.declarative`
  94. extension will also have its mapper available via the ``__mapper__``
  95. attribute.
  96. """
  97. _dispose_called = False
  98. _ready_for_configure = False
  99. @util.deprecated_params(
  100. non_primary=(
  101. "1.3",
  102. "The :paramref:`.mapper.non_primary` parameter is deprecated, "
  103. "and will be removed in a future release. The functionality "
  104. "of non primary mappers is now better suited using the "
  105. ":class:`.AliasedClass` construct, which can also be used "
  106. "as the target of a :func:`_orm.relationship` in 1.3.",
  107. ),
  108. )
  109. def __init__(
  110. self,
  111. class_,
  112. local_table=None,
  113. properties=None,
  114. primary_key=None,
  115. non_primary=False,
  116. inherits=None,
  117. inherit_condition=None,
  118. inherit_foreign_keys=None,
  119. always_refresh=False,
  120. version_id_col=None,
  121. version_id_generator=None,
  122. polymorphic_on=None,
  123. _polymorphic_map=None,
  124. polymorphic_identity=None,
  125. concrete=False,
  126. with_polymorphic=None,
  127. polymorphic_load=None,
  128. allow_partial_pks=True,
  129. batch=True,
  130. column_prefix=None,
  131. include_properties=None,
  132. exclude_properties=None,
  133. passive_updates=True,
  134. passive_deletes=False,
  135. confirm_deleted_rows=True,
  136. eager_defaults=False,
  137. legacy_is_orphan=False,
  138. _compiled_cache_size=100,
  139. ):
  140. r"""Direct constructor for a new :class:`_orm.Mapper` object.
  141. The :func:`_orm.mapper` function is normally invoked through the
  142. use of the :class:`_orm.registry` object through either the
  143. :ref:`Declarative <orm_declarative_mapping>` or
  144. :ref:`Imperative <orm_imperative_mapping>` mapping styles.
  145. .. versionchanged:: 1.4 The :func:`_orm.mapper` function should not
  146. be called directly for classical mapping; for a classical mapping
  147. configuration, use the :meth:`_orm.registry.map_imperatively`
  148. method. The :func:`_orm.mapper` function may become private in a
  149. future release.
  150. Parameters documented below may be passed to either the
  151. :meth:`_orm.registry.map_imperatively` method, or may be passed in the
  152. ``__mapper_args__`` declarative class attribute described at
  153. :ref:`orm_declarative_mapper_options`.
  154. :param class\_: The class to be mapped. When using Declarative,
  155. this argument is automatically passed as the declared class
  156. itself.
  157. :param local_table: The :class:`_schema.Table` or other selectable
  158. to which the class is mapped. May be ``None`` if
  159. this mapper inherits from another mapper using single-table
  160. inheritance. When using Declarative, this argument is
  161. automatically passed by the extension, based on what
  162. is configured via the ``__table__`` argument or via the
  163. :class:`_schema.Table`
  164. produced as a result of the ``__tablename__``
  165. and :class:`_schema.Column` arguments present.
  166. :param always_refresh: If True, all query operations for this mapped
  167. class will overwrite all data within object instances that already
  168. exist within the session, erasing any in-memory changes with
  169. whatever information was loaded from the database. Usage of this
  170. flag is highly discouraged; as an alternative, see the method
  171. :meth:`_query.Query.populate_existing`.
  172. :param allow_partial_pks: Defaults to True. Indicates that a
  173. composite primary key with some NULL values should be considered as
  174. possibly existing within the database. This affects whether a
  175. mapper will assign an incoming row to an existing identity, as well
  176. as if :meth:`.Session.merge` will check the database first for a
  177. particular primary key value. A "partial primary key" can occur if
  178. one has mapped to an OUTER JOIN, for example.
  179. :param batch: Defaults to ``True``, indicating that save operations
  180. of multiple entities can be batched together for efficiency.
  181. Setting to False indicates
  182. that an instance will be fully saved before saving the next
  183. instance. This is used in the extremely rare case that a
  184. :class:`.MapperEvents` listener requires being called
  185. in between individual row persistence operations.
  186. :param column_prefix: A string which will be prepended
  187. to the mapped attribute name when :class:`_schema.Column`
  188. objects are automatically assigned as attributes to the
  189. mapped class. Does not affect explicitly specified
  190. column-based properties.
  191. See the section :ref:`column_prefix` for an example.
  192. :param concrete: If True, indicates this mapper should use concrete
  193. table inheritance with its parent mapper.
  194. See the section :ref:`concrete_inheritance` for an example.
  195. :param confirm_deleted_rows: defaults to True; when a DELETE occurs
  196. of one more rows based on specific primary keys, a warning is
  197. emitted when the number of rows matched does not equal the number
  198. of rows expected. This parameter may be set to False to handle the
  199. case where database ON DELETE CASCADE rules may be deleting some of
  200. those rows automatically. The warning may be changed to an
  201. exception in a future release.
  202. .. versionadded:: 0.9.4 - added
  203. :paramref:`.mapper.confirm_deleted_rows` as well as conditional
  204. matched row checking on delete.
  205. :param eager_defaults: if True, the ORM will immediately fetch the
  206. value of server-generated default values after an INSERT or UPDATE,
  207. rather than leaving them as expired to be fetched on next access.
  208. This can be used for event schemes where the server-generated values
  209. are needed immediately before the flush completes. By default,
  210. this scheme will emit an individual ``SELECT`` statement per row
  211. inserted or updated, which note can add significant performance
  212. overhead. However, if the
  213. target database supports :term:`RETURNING`, the default values will
  214. be returned inline with the INSERT or UPDATE statement, which can
  215. greatly enhance performance for an application that needs frequent
  216. access to just-generated server defaults.
  217. .. seealso::
  218. :ref:`orm_server_defaults`
  219. .. versionchanged:: 0.9.0 The ``eager_defaults`` option can now
  220. make use of :term:`RETURNING` for backends which support it.
  221. :param exclude_properties: A list or set of string column names to
  222. be excluded from mapping.
  223. See :ref:`include_exclude_cols` for an example.
  224. :param include_properties: An inclusive list or set of string column
  225. names to map.
  226. See :ref:`include_exclude_cols` for an example.
  227. :param inherits: A mapped class or the corresponding
  228. :class:`_orm.Mapper`
  229. of one indicating a superclass to which this :class:`_orm.Mapper`
  230. should *inherit* from. The mapped class here must be a subclass
  231. of the other mapper's class. When using Declarative, this argument
  232. is passed automatically as a result of the natural class
  233. hierarchy of the declared classes.
  234. .. seealso::
  235. :ref:`inheritance_toplevel`
  236. :param inherit_condition: For joined table inheritance, a SQL
  237. expression which will
  238. define how the two tables are joined; defaults to a natural join
  239. between the two tables.
  240. :param inherit_foreign_keys: When ``inherit_condition`` is used and
  241. the columns present are missing a :class:`_schema.ForeignKey`
  242. configuration, this parameter can be used to specify which columns
  243. are "foreign". In most cases can be left as ``None``.
  244. :param legacy_is_orphan: Boolean, defaults to ``False``.
  245. When ``True``, specifies that "legacy" orphan consideration
  246. is to be applied to objects mapped by this mapper, which means
  247. that a pending (that is, not persistent) object is auto-expunged
  248. from an owning :class:`.Session` only when it is de-associated
  249. from *all* parents that specify a ``delete-orphan`` cascade towards
  250. this mapper. The new default behavior is that the object is
  251. auto-expunged when it is de-associated with *any* of its parents
  252. that specify ``delete-orphan`` cascade. This behavior is more
  253. consistent with that of a persistent object, and allows behavior to
  254. be consistent in more scenarios independently of whether or not an
  255. orphan object has been flushed yet or not.
  256. See the change note and example at :ref:`legacy_is_orphan_addition`
  257. for more detail on this change.
  258. :param non_primary: Specify that this :class:`_orm.Mapper`
  259. is in addition
  260. to the "primary" mapper, that is, the one used for persistence.
  261. The :class:`_orm.Mapper` created here may be used for ad-hoc
  262. mapping of the class to an alternate selectable, for loading
  263. only.
  264. .. seealso::
  265. :ref:`relationship_aliased_class` - the new pattern that removes
  266. the need for the :paramref:`_orm.Mapper.non_primary` flag.
  267. :param passive_deletes: Indicates DELETE behavior of foreign key
  268. columns when a joined-table inheritance entity is being deleted.
  269. Defaults to ``False`` for a base mapper; for an inheriting mapper,
  270. defaults to ``False`` unless the value is set to ``True``
  271. on the superclass mapper.
  272. When ``True``, it is assumed that ON DELETE CASCADE is configured
  273. on the foreign key relationships that link this mapper's table
  274. to its superclass table, so that when the unit of work attempts
  275. to delete the entity, it need only emit a DELETE statement for the
  276. superclass table, and not this table.
  277. When ``False``, a DELETE statement is emitted for this mapper's
  278. table individually. If the primary key attributes local to this
  279. table are unloaded, then a SELECT must be emitted in order to
  280. validate these attributes; note that the primary key columns
  281. of a joined-table subclass are not part of the "primary key" of
  282. the object as a whole.
  283. Note that a value of ``True`` is **always** forced onto the
  284. subclass mappers; that is, it's not possible for a superclass
  285. to specify passive_deletes without this taking effect for
  286. all subclass mappers.
  287. .. versionadded:: 1.1
  288. .. seealso::
  289. :ref:`passive_deletes` - description of similar feature as
  290. used with :func:`_orm.relationship`
  291. :paramref:`.mapper.passive_updates` - supporting ON UPDATE
  292. CASCADE for joined-table inheritance mappers
  293. :param passive_updates: Indicates UPDATE behavior of foreign key
  294. columns when a primary key column changes on a joined-table
  295. inheritance mapping. Defaults to ``True``.
  296. When True, it is assumed that ON UPDATE CASCADE is configured on
  297. the foreign key in the database, and that the database will handle
  298. propagation of an UPDATE from a source column to dependent columns
  299. on joined-table rows.
  300. When False, it is assumed that the database does not enforce
  301. referential integrity and will not be issuing its own CASCADE
  302. operation for an update. The unit of work process will
  303. emit an UPDATE statement for the dependent columns during a
  304. primary key change.
  305. .. seealso::
  306. :ref:`passive_updates` - description of a similar feature as
  307. used with :func:`_orm.relationship`
  308. :paramref:`.mapper.passive_deletes` - supporting ON DELETE
  309. CASCADE for joined-table inheritance mappers
  310. :param polymorphic_load: Specifies "polymorphic loading" behavior
  311. for a subclass in an inheritance hierarchy (joined and single
  312. table inheritance only). Valid values are:
  313. * "'inline'" - specifies this class should be part of the
  314. "with_polymorphic" mappers, e.g. its columns will be included
  315. in a SELECT query against the base.
  316. * "'selectin'" - specifies that when instances of this class
  317. are loaded, an additional SELECT will be emitted to retrieve
  318. the columns specific to this subclass. The SELECT uses
  319. IN to fetch multiple subclasses at once.
  320. .. versionadded:: 1.2
  321. .. seealso::
  322. :ref:`with_polymorphic_mapper_config`
  323. :ref:`polymorphic_selectin`
  324. :param polymorphic_on: Specifies the column, attribute, or
  325. SQL expression used to determine the target class for an
  326. incoming row, when inheriting classes are present.
  327. This value is commonly a :class:`_schema.Column` object that's
  328. present in the mapped :class:`_schema.Table`::
  329. class Employee(Base):
  330. __tablename__ = 'employee'
  331. id = Column(Integer, primary_key=True)
  332. discriminator = Column(String(50))
  333. __mapper_args__ = {
  334. "polymorphic_on":discriminator,
  335. "polymorphic_identity":"employee"
  336. }
  337. It may also be specified
  338. as a SQL expression, as in this example where we
  339. use the :func:`.case` construct to provide a conditional
  340. approach::
  341. class Employee(Base):
  342. __tablename__ = 'employee'
  343. id = Column(Integer, primary_key=True)
  344. discriminator = Column(String(50))
  345. __mapper_args__ = {
  346. "polymorphic_on":case([
  347. (discriminator == "EN", "engineer"),
  348. (discriminator == "MA", "manager"),
  349. ], else_="employee"),
  350. "polymorphic_identity":"employee"
  351. }
  352. It may also refer to any attribute
  353. configured with :func:`.column_property`, or to the
  354. string name of one::
  355. class Employee(Base):
  356. __tablename__ = 'employee'
  357. id = Column(Integer, primary_key=True)
  358. discriminator = Column(String(50))
  359. employee_type = column_property(
  360. case([
  361. (discriminator == "EN", "engineer"),
  362. (discriminator == "MA", "manager"),
  363. ], else_="employee")
  364. )
  365. __mapper_args__ = {
  366. "polymorphic_on":employee_type,
  367. "polymorphic_identity":"employee"
  368. }
  369. When setting ``polymorphic_on`` to reference an
  370. attribute or expression that's not present in the
  371. locally mapped :class:`_schema.Table`, yet the value
  372. of the discriminator should be persisted to the database,
  373. the value of the
  374. discriminator is not automatically set on new
  375. instances; this must be handled by the user,
  376. either through manual means or via event listeners.
  377. A typical approach to establishing such a listener
  378. looks like::
  379. from sqlalchemy import event
  380. from sqlalchemy.orm import object_mapper
  381. @event.listens_for(Employee, "init", propagate=True)
  382. def set_identity(instance, *arg, **kw):
  383. mapper = object_mapper(instance)
  384. instance.discriminator = mapper.polymorphic_identity
  385. Where above, we assign the value of ``polymorphic_identity``
  386. for the mapped class to the ``discriminator`` attribute,
  387. thus persisting the value to the ``discriminator`` column
  388. in the database.
  389. .. warning::
  390. Currently, **only one discriminator column may be set**, typically
  391. on the base-most class in the hierarchy. "Cascading" polymorphic
  392. columns are not yet supported.
  393. .. seealso::
  394. :ref:`inheritance_toplevel`
  395. :param polymorphic_identity: Specifies the value which
  396. identifies this particular class as returned by the
  397. column expression referred to by the ``polymorphic_on``
  398. setting. As rows are received, the value corresponding
  399. to the ``polymorphic_on`` column expression is compared
  400. to this value, indicating which subclass should
  401. be used for the newly reconstructed object.
  402. :param properties: A dictionary mapping the string names of object
  403. attributes to :class:`.MapperProperty` instances, which define the
  404. persistence behavior of that attribute. Note that
  405. :class:`_schema.Column`
  406. objects present in
  407. the mapped :class:`_schema.Table` are automatically placed into
  408. ``ColumnProperty`` instances upon mapping, unless overridden.
  409. When using Declarative, this argument is passed automatically,
  410. based on all those :class:`.MapperProperty` instances declared
  411. in the declared class body.
  412. :param primary_key: A list of :class:`_schema.Column`
  413. objects which define
  414. the primary key to be used against this mapper's selectable unit.
  415. This is normally simply the primary key of the ``local_table``, but
  416. can be overridden here.
  417. :param version_id_col: A :class:`_schema.Column`
  418. that will be used to keep a running version id of rows
  419. in the table. This is used to detect concurrent updates or
  420. the presence of stale data in a flush. The methodology is to
  421. detect if an UPDATE statement does not match the last known
  422. version id, a
  423. :class:`~sqlalchemy.orm.exc.StaleDataError` exception is
  424. thrown.
  425. By default, the column must be of :class:`.Integer` type,
  426. unless ``version_id_generator`` specifies an alternative version
  427. generator.
  428. .. seealso::
  429. :ref:`mapper_version_counter` - discussion of version counting
  430. and rationale.
  431. :param version_id_generator: Define how new version ids should
  432. be generated. Defaults to ``None``, which indicates that
  433. a simple integer counting scheme be employed. To provide a custom
  434. versioning scheme, provide a callable function of the form::
  435. def generate_version(version):
  436. return next_version
  437. Alternatively, server-side versioning functions such as triggers,
  438. or programmatic versioning schemes outside of the version id
  439. generator may be used, by specifying the value ``False``.
  440. Please see :ref:`server_side_version_counter` for a discussion
  441. of important points when using this option.
  442. .. versionadded:: 0.9.0 ``version_id_generator`` supports
  443. server-side version number generation.
  444. .. seealso::
  445. :ref:`custom_version_counter`
  446. :ref:`server_side_version_counter`
  447. :param with_polymorphic: A tuple in the form ``(<classes>,
  448. <selectable>)`` indicating the default style of "polymorphic"
  449. loading, that is, which tables are queried at once. <classes> is
  450. any single or list of mappers and/or classes indicating the
  451. inherited classes that should be loaded at once. The special value
  452. ``'*'`` may be used to indicate all descending classes should be
  453. loaded immediately. The second tuple argument <selectable>
  454. indicates a selectable that will be used to query for multiple
  455. classes.
  456. .. seealso::
  457. :ref:`with_polymorphic` - discussion of polymorphic querying
  458. techniques.
  459. """
  460. self.class_ = util.assert_arg_type(class_, type, "class_")
  461. self._sort_key = "%s.%s" % (
  462. self.class_.__module__,
  463. self.class_.__name__,
  464. )
  465. self.class_manager = None
  466. self._primary_key_argument = util.to_list(primary_key)
  467. self.non_primary = non_primary
  468. self.always_refresh = always_refresh
  469. if isinstance(version_id_col, MapperProperty):
  470. self.version_id_prop = version_id_col
  471. self.version_id_col = None
  472. else:
  473. self.version_id_col = version_id_col
  474. if version_id_generator is False:
  475. self.version_id_generator = False
  476. elif version_id_generator is None:
  477. self.version_id_generator = lambda x: (x or 0) + 1
  478. else:
  479. self.version_id_generator = version_id_generator
  480. self.concrete = concrete
  481. self.single = False
  482. self.inherits = inherits
  483. if local_table is not None:
  484. self.local_table = coercions.expect(
  485. roles.StrictFromClauseRole, local_table
  486. )
  487. else:
  488. self.local_table = None
  489. self.inherit_condition = inherit_condition
  490. self.inherit_foreign_keys = inherit_foreign_keys
  491. self._init_properties = properties or {}
  492. self._delete_orphans = []
  493. self.batch = batch
  494. self.eager_defaults = eager_defaults
  495. self.column_prefix = column_prefix
  496. self.polymorphic_on = (
  497. coercions.expect(
  498. roles.ColumnArgumentOrKeyRole,
  499. polymorphic_on,
  500. argname="polymorphic_on",
  501. )
  502. if polymorphic_on is not None
  503. else None
  504. )
  505. self._dependency_processors = []
  506. self.validators = util.EMPTY_DICT
  507. self.passive_updates = passive_updates
  508. self.passive_deletes = passive_deletes
  509. self.legacy_is_orphan = legacy_is_orphan
  510. self._clause_adapter = None
  511. self._requires_row_aliasing = False
  512. self._inherits_equated_pairs = None
  513. self._memoized_values = {}
  514. self._compiled_cache_size = _compiled_cache_size
  515. self._reconstructor = None
  516. self.allow_partial_pks = allow_partial_pks
  517. if self.inherits and not self.concrete:
  518. self.confirm_deleted_rows = False
  519. else:
  520. self.confirm_deleted_rows = confirm_deleted_rows
  521. self._set_with_polymorphic(with_polymorphic)
  522. self.polymorphic_load = polymorphic_load
  523. # our 'polymorphic identity', a string name that when located in a
  524. # result set row indicates this Mapper should be used to construct
  525. # the object instance for that row.
  526. self.polymorphic_identity = polymorphic_identity
  527. # a dictionary of 'polymorphic identity' names, associating those
  528. # names with Mappers that will be used to construct object instances
  529. # upon a select operation.
  530. if _polymorphic_map is None:
  531. self.polymorphic_map = {}
  532. else:
  533. self.polymorphic_map = _polymorphic_map
  534. if include_properties is not None:
  535. self.include_properties = util.to_set(include_properties)
  536. else:
  537. self.include_properties = None
  538. if exclude_properties:
  539. self.exclude_properties = util.to_set(exclude_properties)
  540. else:
  541. self.exclude_properties = None
  542. # prevent this mapper from being constructed
  543. # while a configure_mappers() is occurring (and defer a
  544. # configure_mappers() until construction succeeds)
  545. with _CONFIGURE_MUTEX:
  546. self.dispatch._events._new_mapper_instance(class_, self)
  547. self._configure_inheritance()
  548. self._configure_class_instrumentation()
  549. self._configure_properties()
  550. self._configure_polymorphic_setter()
  551. self._configure_pks()
  552. self.registry._flag_new_mapper(self)
  553. self._log("constructed")
  554. self._expire_memoizations()
  555. # major attributes initialized at the classlevel so that
  556. # they can be Sphinx-documented.
  557. is_mapper = True
  558. """Part of the inspection API."""
  559. represents_outer_join = False
  560. @property
  561. def mapper(self):
  562. """Part of the inspection API.
  563. Returns self.
  564. """
  565. return self
  566. def _gen_cache_key(self, anon_map, bindparams):
  567. return (self,)
  568. @property
  569. def entity(self):
  570. r"""Part of the inspection API.
  571. Returns self.class\_.
  572. """
  573. return self.class_
  574. local_table = None
  575. """The :class:`_expression.Selectable` which this :class:`_orm.Mapper`
  576. manages.
  577. Typically is an instance of :class:`_schema.Table` or
  578. :class:`_expression.Alias`.
  579. May also be ``None``.
  580. The "local" table is the
  581. selectable that the :class:`_orm.Mapper` is directly responsible for
  582. managing from an attribute access and flush perspective. For
  583. non-inheriting mappers, the local table is the same as the
  584. "mapped" table. For joined-table inheritance mappers, local_table
  585. will be the particular sub-table of the overall "join" which
  586. this :class:`_orm.Mapper` represents. If this mapper is a
  587. single-table inheriting mapper, local_table will be ``None``.
  588. .. seealso::
  589. :attr:`_orm.Mapper.persist_selectable`.
  590. """
  591. persist_selectable = None
  592. """The :class:`_expression.Selectable` to which this :class:`_orm.Mapper`
  593. is mapped.
  594. Typically an instance of :class:`_schema.Table`,
  595. :class:`_expression.Join`, or :class:`_expression.Alias`.
  596. The :attr:`_orm.Mapper.persist_selectable` is separate from
  597. :attr:`_orm.Mapper.selectable` in that the former represents columns
  598. that are mapped on this class or its superclasses, whereas the
  599. latter may be a "polymorphic" selectable that contains additional columns
  600. which are in fact mapped on subclasses only.
  601. "persist selectable" is the "thing the mapper writes to" and
  602. "selectable" is the "thing the mapper selects from".
  603. :attr:`_orm.Mapper.persist_selectable` is also separate from
  604. :attr:`_orm.Mapper.local_table`, which represents the set of columns that
  605. are locally mapped on this class directly.
  606. .. seealso::
  607. :attr:`_orm.Mapper.selectable`.
  608. :attr:`_orm.Mapper.local_table`.
  609. """
  610. inherits = None
  611. """References the :class:`_orm.Mapper` which this :class:`_orm.Mapper`
  612. inherits from, if any.
  613. This is a *read only* attribute determined during mapper construction.
  614. Behavior is undefined if directly modified.
  615. """
  616. configured = False
  617. """Represent ``True`` if this :class:`_orm.Mapper` has been configured.
  618. This is a *read only* attribute determined during mapper construction.
  619. Behavior is undefined if directly modified.
  620. .. seealso::
  621. :func:`.configure_mappers`.
  622. """
  623. concrete = None
  624. """Represent ``True`` if this :class:`_orm.Mapper` is a concrete
  625. inheritance mapper.
  626. This is a *read only* attribute determined during mapper construction.
  627. Behavior is undefined if directly modified.
  628. """
  629. tables = None
  630. """An iterable containing the collection of :class:`_schema.Table` objects
  631. which this :class:`_orm.Mapper` is aware of.
  632. If the mapper is mapped to a :class:`_expression.Join`, or an
  633. :class:`_expression.Alias`
  634. representing a :class:`_expression.Select`, the individual
  635. :class:`_schema.Table`
  636. objects that comprise the full construct will be represented here.
  637. This is a *read only* attribute determined during mapper construction.
  638. Behavior is undefined if directly modified.
  639. """
  640. primary_key = None
  641. """An iterable containing the collection of :class:`_schema.Column`
  642. objects
  643. which comprise the 'primary key' of the mapped table, from the
  644. perspective of this :class:`_orm.Mapper`.
  645. This list is against the selectable in
  646. :attr:`_orm.Mapper.persist_selectable`.
  647. In the case of inheriting mappers, some columns may be managed by a
  648. superclass mapper. For example, in the case of a
  649. :class:`_expression.Join`, the
  650. primary key is determined by all of the primary key columns across all
  651. tables referenced by the :class:`_expression.Join`.
  652. The list is also not necessarily the same as the primary key column
  653. collection associated with the underlying tables; the :class:`_orm.Mapper`
  654. features a ``primary_key`` argument that can override what the
  655. :class:`_orm.Mapper` considers as primary key columns.
  656. This is a *read only* attribute determined during mapper construction.
  657. Behavior is undefined if directly modified.
  658. """
  659. class_ = None
  660. """The Python class which this :class:`_orm.Mapper` maps.
  661. This is a *read only* attribute determined during mapper construction.
  662. Behavior is undefined if directly modified.
  663. """
  664. class_manager = None
  665. """The :class:`.ClassManager` which maintains event listeners
  666. and class-bound descriptors for this :class:`_orm.Mapper`.
  667. This is a *read only* attribute determined during mapper construction.
  668. Behavior is undefined if directly modified.
  669. """
  670. single = None
  671. """Represent ``True`` if this :class:`_orm.Mapper` is a single table
  672. inheritance mapper.
  673. :attr:`_orm.Mapper.local_table` will be ``None`` if this flag is set.
  674. This is a *read only* attribute determined during mapper construction.
  675. Behavior is undefined if directly modified.
  676. """
  677. non_primary = None
  678. """Represent ``True`` if this :class:`_orm.Mapper` is a "non-primary"
  679. mapper, e.g. a mapper that is used only to select rows but not for
  680. persistence management.
  681. This is a *read only* attribute determined during mapper construction.
  682. Behavior is undefined if directly modified.
  683. """
  684. polymorphic_on = None
  685. """The :class:`_schema.Column` or SQL expression specified as the
  686. ``polymorphic_on`` argument
  687. for this :class:`_orm.Mapper`, within an inheritance scenario.
  688. This attribute is normally a :class:`_schema.Column` instance but
  689. may also be an expression, such as one derived from
  690. :func:`.cast`.
  691. This is a *read only* attribute determined during mapper construction.
  692. Behavior is undefined if directly modified.
  693. """
  694. polymorphic_map = None
  695. """A mapping of "polymorphic identity" identifiers mapped to
  696. :class:`_orm.Mapper` instances, within an inheritance scenario.
  697. The identifiers can be of any type which is comparable to the
  698. type of column represented by :attr:`_orm.Mapper.polymorphic_on`.
  699. An inheritance chain of mappers will all reference the same
  700. polymorphic map object. The object is used to correlate incoming
  701. result rows to target mappers.
  702. This is a *read only* attribute determined during mapper construction.
  703. Behavior is undefined if directly modified.
  704. """
  705. polymorphic_identity = None
  706. """Represent an identifier which is matched against the
  707. :attr:`_orm.Mapper.polymorphic_on` column during result row loading.
  708. Used only with inheritance, this object can be of any type which is
  709. comparable to the type of column represented by
  710. :attr:`_orm.Mapper.polymorphic_on`.
  711. This is a *read only* attribute determined during mapper construction.
  712. Behavior is undefined if directly modified.
  713. """
  714. base_mapper = None
  715. """The base-most :class:`_orm.Mapper` in an inheritance chain.
  716. In a non-inheriting scenario, this attribute will always be this
  717. :class:`_orm.Mapper`. In an inheritance scenario, it references
  718. the :class:`_orm.Mapper` which is parent to all other :class:`_orm.Mapper`
  719. objects in the inheritance chain.
  720. This is a *read only* attribute determined during mapper construction.
  721. Behavior is undefined if directly modified.
  722. """
  723. columns = None
  724. """A collection of :class:`_schema.Column` or other scalar expression
  725. objects maintained by this :class:`_orm.Mapper`.
  726. The collection behaves the same as that of the ``c`` attribute on
  727. any :class:`_schema.Table` object,
  728. except that only those columns included in
  729. this mapping are present, and are keyed based on the attribute name
  730. defined in the mapping, not necessarily the ``key`` attribute of the
  731. :class:`_schema.Column` itself. Additionally, scalar expressions mapped
  732. by :func:`.column_property` are also present here.
  733. This is a *read only* attribute determined during mapper construction.
  734. Behavior is undefined if directly modified.
  735. """
  736. validators = None
  737. """An immutable dictionary of attributes which have been decorated
  738. using the :func:`_orm.validates` decorator.
  739. The dictionary contains string attribute names as keys
  740. mapped to the actual validation method.
  741. """
  742. c = None
  743. """A synonym for :attr:`_orm.Mapper.columns`."""
  744. @property
  745. @util.deprecated("1.3", "Use .persist_selectable")
  746. def mapped_table(self):
  747. return self.persist_selectable
  748. @util.memoized_property
  749. def _path_registry(self):
  750. return PathRegistry.per_mapper(self)
  751. def _configure_inheritance(self):
  752. """Configure settings related to inheriting and/or inherited mappers
  753. being present."""
  754. # a set of all mappers which inherit from this one.
  755. self._inheriting_mappers = util.WeakSequence()
  756. if self.inherits:
  757. if isinstance(self.inherits, type):
  758. self.inherits = class_mapper(self.inherits, configure=False)
  759. if not issubclass(self.class_, self.inherits.class_):
  760. raise sa_exc.ArgumentError(
  761. "Class '%s' does not inherit from '%s'"
  762. % (self.class_.__name__, self.inherits.class_.__name__)
  763. )
  764. self.dispatch._update(self.inherits.dispatch)
  765. if self.non_primary != self.inherits.non_primary:
  766. np = not self.non_primary and "primary" or "non-primary"
  767. raise sa_exc.ArgumentError(
  768. "Inheritance of %s mapper for class '%s' is "
  769. "only allowed from a %s mapper"
  770. % (np, self.class_.__name__, np)
  771. )
  772. # inherit_condition is optional.
  773. if self.local_table is None:
  774. self.local_table = self.inherits.local_table
  775. self.persist_selectable = self.inherits.persist_selectable
  776. self.single = True
  777. elif self.local_table is not self.inherits.local_table:
  778. if self.concrete:
  779. self.persist_selectable = self.local_table
  780. for mapper in self.iterate_to_root():
  781. if mapper.polymorphic_on is not None:
  782. mapper._requires_row_aliasing = True
  783. else:
  784. if self.inherit_condition is None:
  785. # figure out inherit condition from our table to the
  786. # immediate table of the inherited mapper, not its
  787. # full table which could pull in other stuff we don't
  788. # want (allows test/inheritance.InheritTest4 to pass)
  789. self.inherit_condition = sql_util.join_condition(
  790. self.inherits.local_table, self.local_table
  791. )
  792. self.persist_selectable = sql.join(
  793. self.inherits.persist_selectable,
  794. self.local_table,
  795. self.inherit_condition,
  796. )
  797. fks = util.to_set(self.inherit_foreign_keys)
  798. self._inherits_equated_pairs = sql_util.criterion_as_pairs(
  799. self.persist_selectable.onclause,
  800. consider_as_foreign_keys=fks,
  801. )
  802. else:
  803. self.persist_selectable = self.local_table
  804. if self.polymorphic_identity is not None and not self.concrete:
  805. self._identity_class = self.inherits._identity_class
  806. else:
  807. self._identity_class = self.class_
  808. if self.version_id_col is None:
  809. self.version_id_col = self.inherits.version_id_col
  810. self.version_id_generator = self.inherits.version_id_generator
  811. elif (
  812. self.inherits.version_id_col is not None
  813. and self.version_id_col is not self.inherits.version_id_col
  814. ):
  815. util.warn(
  816. "Inheriting version_id_col '%s' does not match inherited "
  817. "version_id_col '%s' and will not automatically populate "
  818. "the inherited versioning column. "
  819. "version_id_col should only be specified on "
  820. "the base-most mapper that includes versioning."
  821. % (
  822. self.version_id_col.description,
  823. self.inherits.version_id_col.description,
  824. )
  825. )
  826. self.polymorphic_map = self.inherits.polymorphic_map
  827. self.batch = self.inherits.batch
  828. self.inherits._inheriting_mappers.append(self)
  829. self.base_mapper = self.inherits.base_mapper
  830. self.passive_updates = self.inherits.passive_updates
  831. self.passive_deletes = (
  832. self.inherits.passive_deletes or self.passive_deletes
  833. )
  834. self._all_tables = self.inherits._all_tables
  835. if self.polymorphic_identity is not None:
  836. if self.polymorphic_identity in self.polymorphic_map:
  837. util.warn(
  838. "Reassigning polymorphic association for identity %r "
  839. "from %r to %r: Check for duplicate use of %r as "
  840. "value for polymorphic_identity."
  841. % (
  842. self.polymorphic_identity,
  843. self.polymorphic_map[self.polymorphic_identity],
  844. self,
  845. self.polymorphic_identity,
  846. )
  847. )
  848. self.polymorphic_map[self.polymorphic_identity] = self
  849. if self.polymorphic_load and self.concrete:
  850. raise sa_exc.ArgumentError(
  851. "polymorphic_load is not currently supported "
  852. "with concrete table inheritance"
  853. )
  854. if self.polymorphic_load == "inline":
  855. self.inherits._add_with_polymorphic_subclass(self)
  856. elif self.polymorphic_load == "selectin":
  857. pass
  858. elif self.polymorphic_load is not None:
  859. raise sa_exc.ArgumentError(
  860. "unknown argument for polymorphic_load: %r"
  861. % self.polymorphic_load
  862. )
  863. else:
  864. self._all_tables = set()
  865. self.base_mapper = self
  866. self.persist_selectable = self.local_table
  867. if self.polymorphic_identity is not None:
  868. self.polymorphic_map[self.polymorphic_identity] = self
  869. self._identity_class = self.class_
  870. if self.persist_selectable is None:
  871. raise sa_exc.ArgumentError(
  872. "Mapper '%s' does not have a persist_selectable specified."
  873. % self
  874. )
  875. def _set_with_polymorphic(self, with_polymorphic):
  876. if with_polymorphic == "*":
  877. self.with_polymorphic = ("*", None)
  878. elif isinstance(with_polymorphic, (tuple, list)):
  879. if isinstance(
  880. with_polymorphic[0], util.string_types + (tuple, list)
  881. ):
  882. self.with_polymorphic = with_polymorphic
  883. else:
  884. self.with_polymorphic = (with_polymorphic, None)
  885. elif with_polymorphic is not None:
  886. raise sa_exc.ArgumentError("Invalid setting for with_polymorphic")
  887. else:
  888. self.with_polymorphic = None
  889. if self.with_polymorphic and self.with_polymorphic[1] is not None:
  890. self.with_polymorphic = (
  891. self.with_polymorphic[0],
  892. coercions.expect(
  893. roles.StrictFromClauseRole,
  894. self.with_polymorphic[1],
  895. allow_select=True,
  896. ),
  897. )
  898. if self.configured:
  899. self._expire_memoizations()
  900. def _add_with_polymorphic_subclass(self, mapper):
  901. subcl = mapper.class_
  902. if self.with_polymorphic is None:
  903. self._set_with_polymorphic((subcl,))
  904. elif self.with_polymorphic[0] != "*":
  905. self._set_with_polymorphic(
  906. (self.with_polymorphic[0] + (subcl,), self.with_polymorphic[1])
  907. )
  908. def _set_concrete_base(self, mapper):
  909. """Set the given :class:`_orm.Mapper` as the 'inherits' for this
  910. :class:`_orm.Mapper`, assuming this :class:`_orm.Mapper` is concrete
  911. and does not already have an inherits."""
  912. assert self.concrete
  913. assert not self.inherits
  914. assert isinstance(mapper, Mapper)
  915. self.inherits = mapper
  916. self.inherits.polymorphic_map.update(self.polymorphic_map)
  917. self.polymorphic_map = self.inherits.polymorphic_map
  918. for mapper in self.iterate_to_root():
  919. if mapper.polymorphic_on is not None:
  920. mapper._requires_row_aliasing = True
  921. self.batch = self.inherits.batch
  922. for mp in self.self_and_descendants:
  923. mp.base_mapper = self.inherits.base_mapper
  924. self.inherits._inheriting_mappers.append(self)
  925. self.passive_updates = self.inherits.passive_updates
  926. self._all_tables = self.inherits._all_tables
  927. for key, prop in mapper._props.items():
  928. if key not in self._props and not self._should_exclude(
  929. key, key, local=False, column=None
  930. ):
  931. self._adapt_inherited_property(key, prop, False)
  932. def _set_polymorphic_on(self, polymorphic_on):
  933. self.polymorphic_on = polymorphic_on
  934. self._configure_polymorphic_setter(True)
  935. def _configure_class_instrumentation(self):
  936. """If this mapper is to be a primary mapper (i.e. the
  937. non_primary flag is not set), associate this Mapper with the
  938. given class and entity name.
  939. Subsequent calls to ``class_mapper()`` for the ``class_`` / ``entity``
  940. name combination will return this mapper. Also decorate the
  941. `__init__` method on the mapped class to include optional
  942. auto-session attachment logic.
  943. """
  944. # we expect that declarative has applied the class manager
  945. # already and set up a registry. if this is None,
  946. # we will emit a deprecation warning below when we also see that
  947. # it has no registry.
  948. manager = attributes.manager_of_class(self.class_)
  949. if self.non_primary:
  950. if not manager or not manager.is_mapped:
  951. raise sa_exc.InvalidRequestError(
  952. "Class %s has no primary mapper configured. Configure "
  953. "a primary mapper first before setting up a non primary "
  954. "Mapper." % self.class_
  955. )
  956. self.class_manager = manager
  957. self.registry = manager.registry
  958. self._identity_class = manager.mapper._identity_class
  959. manager.registry._add_non_primary_mapper(self)
  960. return
  961. if manager is not None:
  962. assert manager.class_ is self.class_
  963. if manager.is_mapped:
  964. raise sa_exc.ArgumentError(
  965. "Class '%s' already has a primary mapper defined. "
  966. % self.class_
  967. )
  968. # else:
  969. # a ClassManager may already exist as
  970. # ClassManager.instrument_attribute() creates
  971. # new managers for each subclass if they don't yet exist.
  972. self.dispatch.instrument_class(self, self.class_)
  973. # this invokes the class_instrument event and sets up
  974. # the __init__ method. documented behavior is that this must
  975. # occur after the instrument_class event above.
  976. # yes two events with the same two words reversed and different APIs.
  977. # :(
  978. manager = instrumentation.register_class(
  979. self.class_,
  980. mapper=self,
  981. expired_attribute_loader=util.partial(
  982. loading.load_scalar_attributes, self
  983. ),
  984. # finalize flag means instrument the __init__ method
  985. # and call the class_instrument event
  986. finalize=True,
  987. )
  988. if not manager.registry:
  989. util.warn_deprecated_20(
  990. "Calling the mapper() function directly outside of a "
  991. "declarative registry is deprecated."
  992. " Please use the sqlalchemy.orm.registry.map_imperatively() "
  993. "function for a classical mapping."
  994. )
  995. assert _legacy_registry is not None
  996. _legacy_registry._add_manager(manager)
  997. self.class_manager = manager
  998. self.registry = manager.registry
  999. # The remaining members can be added by any mapper,
  1000. # e_name None or not.
  1001. if manager.mapper is None:
  1002. return
  1003. event.listen(manager, "init", _event_on_init, raw=True)
  1004. for key, method in util.iterate_attributes(self.class_):
  1005. if key == "__init__" and hasattr(method, "_sa_original_init"):
  1006. method = method._sa_original_init
  1007. if hasattr(method, "__func__"):
  1008. method = method.__func__
  1009. if callable(method):
  1010. if hasattr(method, "__sa_reconstructor__"):
  1011. self._reconstructor = method
  1012. event.listen(manager, "load", _event_on_load, raw=True)
  1013. elif hasattr(method, "__sa_validators__"):
  1014. validation_opts = method.__sa_validation_opts__
  1015. for name in method.__sa_validators__:
  1016. if name in self.validators:
  1017. raise sa_exc.InvalidRequestError(
  1018. "A validation function for mapped "
  1019. "attribute %r on mapper %s already exists."
  1020. % (name, self)
  1021. )
  1022. self.validators = self.validators.union(
  1023. {name: (method, validation_opts)}
  1024. )
  1025. def _set_dispose_flags(self):
  1026. self.configured = True
  1027. self._ready_for_configure = True
  1028. self._dispose_called = True
  1029. self.__dict__.pop("_configure_failed", None)
  1030. def _configure_pks(self):
  1031. self.tables = sql_util.find_tables(self.persist_selectable)
  1032. self._pks_by_table = {}
  1033. self._cols_by_table = {}
  1034. all_cols = util.column_set(
  1035. chain(*[col.proxy_set for col in self._columntoproperty])
  1036. )
  1037. pk_cols = util.column_set(c for c in all_cols if c.primary_key)
  1038. # identify primary key columns which are also mapped by this mapper.
  1039. tables = set(self.tables + [self.persist_selectable])
  1040. self._all_tables.update(tables)
  1041. for t in tables:
  1042. if t.primary_key and pk_cols.issuperset(t.primary_key):
  1043. # ordering is important since it determines the ordering of
  1044. # mapper.primary_key (and therefore query.get())
  1045. self._pks_by_table[t] = util.ordered_column_set(
  1046. t.primary_key
  1047. ).intersection(pk_cols)
  1048. self._cols_by_table[t] = util.ordered_column_set(t.c).intersection(
  1049. all_cols
  1050. )
  1051. # if explicit PK argument sent, add those columns to the
  1052. # primary key mappings
  1053. if self._primary_key_argument:
  1054. for k in self._primary_key_argument:
  1055. if k.table not in self._pks_by_table:
  1056. self._pks_by_table[k.table] = util.OrderedSet()
  1057. self._pks_by_table[k.table].add(k)
  1058. # otherwise, see that we got a full PK for the mapped table
  1059. elif (
  1060. self.persist_selectable not in self._pks_by_table
  1061. or len(self._pks_by_table[self.persist_selectable]) == 0
  1062. ):
  1063. raise sa_exc.ArgumentError(
  1064. "Mapper %s could not assemble any primary "
  1065. "key columns for mapped table '%s'"
  1066. % (self, self.persist_selectable.description)
  1067. )
  1068. elif self.local_table not in self._pks_by_table and isinstance(
  1069. self.local_table, schema.Table
  1070. ):
  1071. util.warn(
  1072. "Could not assemble any primary "
  1073. "keys for locally mapped table '%s' - "
  1074. "no rows will be persisted in this Table."
  1075. % self.local_table.description
  1076. )
  1077. if (
  1078. self.inherits
  1079. and not self.concrete
  1080. and not self._primary_key_argument
  1081. ):
  1082. # if inheriting, the "primary key" for this mapper is
  1083. # that of the inheriting (unless concrete or explicit)
  1084. self.primary_key = self.inherits.primary_key
  1085. else:
  1086. # determine primary key from argument or persist_selectable pks -
  1087. # reduce to the minimal set of columns
  1088. if self._primary_key_argument:
  1089. primary_key = sql_util.reduce_columns(
  1090. [
  1091. self.persist_selectable.corresponding_column(c)
  1092. for c in self._primary_key_argument
  1093. ],
  1094. ignore_nonexistent_tables=True,
  1095. )
  1096. else:
  1097. primary_key = sql_util.reduce_columns(
  1098. self._pks_by_table[self.persist_selectable],
  1099. ignore_nonexistent_tables=True,
  1100. )
  1101. if len(primary_key) == 0:
  1102. raise sa_exc.ArgumentError(
  1103. "Mapper %s could not assemble any primary "
  1104. "key columns for mapped table '%s'"
  1105. % (self, self.persist_selectable.description)
  1106. )
  1107. self.primary_key = tuple(primary_key)
  1108. self._log("Identified primary key columns: %s", primary_key)
  1109. # determine cols that aren't expressed within our tables; mark these
  1110. # as "read only" properties which are refreshed upon INSERT/UPDATE
  1111. self._readonly_props = set(
  1112. self._columntoproperty[col]
  1113. for col in self._columntoproperty
  1114. if self._columntoproperty[col] not in self._identity_key_props
  1115. and (
  1116. not hasattr(col, "table")
  1117. or col.table not in self._cols_by_table
  1118. )
  1119. )
  1120. def _configure_properties(self):
  1121. # Column and other ClauseElement objects which are mapped
  1122. # TODO: technically this should be a DedupeColumnCollection
  1123. # however DCC needs changes and more tests to fully cover
  1124. # storing columns under a separate key name
  1125. self.columns = self.c = sql_base.ColumnCollection()
  1126. # object attribute names mapped to MapperProperty objects
  1127. self._props = util.OrderedDict()
  1128. # table columns mapped to lists of MapperProperty objects
  1129. # using a list allows a single column to be defined as
  1130. # populating multiple object attributes
  1131. self._columntoproperty = _ColumnMapping(self)
  1132. # load custom properties
  1133. if self._init_properties:
  1134. for key, prop in self._init_properties.items():
  1135. self._configure_property(key, prop, False)
  1136. # pull properties from the inherited mapper if any.
  1137. if self.inherits:
  1138. for key, prop in self.inherits._props.items():
  1139. if key not in self._props and not self._should_exclude(
  1140. key, key, local=False, column=None
  1141. ):
  1142. self._adapt_inherited_property(key, prop, False)
  1143. # create properties for each column in the mapped table,
  1144. # for those columns which don't already map to a property
  1145. for column in self.persist_selectable.columns:
  1146. if column in self._columntoproperty:
  1147. continue
  1148. column_key = (self.column_prefix or "") + column.key
  1149. if self._should_exclude(
  1150. column.key,
  1151. column_key,
  1152. local=self.local_table.c.contains_column(column),
  1153. column=column,
  1154. ):
  1155. continue
  1156. # adjust the "key" used for this column to that
  1157. # of the inheriting mapper
  1158. for mapper in self.iterate_to_root():
  1159. if column in mapper._columntoproperty:
  1160. column_key = mapper._columntoproperty[column].key
  1161. self._configure_property(
  1162. column_key, column, init=False, setparent=True
  1163. )
  1164. def _configure_polymorphic_setter(self, init=False):
  1165. """Configure an attribute on the mapper representing the
  1166. 'polymorphic_on' column, if applicable, and not
  1167. already generated by _configure_properties (which is typical).
  1168. Also create a setter function which will assign this
  1169. attribute to the value of the 'polymorphic_identity'
  1170. upon instance construction, also if applicable. This
  1171. routine will run when an instance is created.
  1172. """
  1173. setter = False
  1174. if self.polymorphic_on is not None:
  1175. setter = True
  1176. if isinstance(self.polymorphic_on, util.string_types):
  1177. # polymorphic_on specified as a string - link
  1178. # it to mapped ColumnProperty
  1179. try:
  1180. self.polymorphic_on = self._props[self.polymorphic_on]
  1181. except KeyError as err:
  1182. util.raise_(
  1183. sa_exc.ArgumentError(
  1184. "Can't determine polymorphic_on "
  1185. "value '%s' - no attribute is "
  1186. "mapped to this name." % self.polymorphic_on
  1187. ),
  1188. replace_context=err,
  1189. )
  1190. if self.polymorphic_on in self._columntoproperty:
  1191. # polymorphic_on is a column that is already mapped
  1192. # to a ColumnProperty
  1193. prop = self._columntoproperty[self.polymorphic_on]
  1194. elif isinstance(self.polymorphic_on, MapperProperty):
  1195. # polymorphic_on is directly a MapperProperty,
  1196. # ensure it's a ColumnProperty
  1197. if not isinstance(
  1198. self.polymorphic_on, properties.ColumnProperty
  1199. ):
  1200. raise sa_exc.ArgumentError(
  1201. "Only direct column-mapped "
  1202. "property or SQL expression "
  1203. "can be passed for polymorphic_on"
  1204. )
  1205. prop = self.polymorphic_on
  1206. else:
  1207. # polymorphic_on is a Column or SQL expression and
  1208. # doesn't appear to be mapped. this means it can be 1.
  1209. # only present in the with_polymorphic selectable or
  1210. # 2. a totally standalone SQL expression which we'd
  1211. # hope is compatible with this mapper's persist_selectable
  1212. col = self.persist_selectable.corresponding_column(
  1213. self.polymorphic_on
  1214. )
  1215. if col is None:
  1216. # polymorphic_on doesn't derive from any
  1217. # column/expression isn't present in the mapped
  1218. # table. we will make a "hidden" ColumnProperty
  1219. # for it. Just check that if it's directly a
  1220. # schema.Column and we have with_polymorphic, it's
  1221. # likely a user error if the schema.Column isn't
  1222. # represented somehow in either persist_selectable or
  1223. # with_polymorphic. Otherwise as of 0.7.4 we
  1224. # just go with it and assume the user wants it
  1225. # that way (i.e. a CASE statement)
  1226. setter = False
  1227. instrument = False
  1228. col = self.polymorphic_on
  1229. if isinstance(col, schema.Column) and (
  1230. self.with_polymorphic is None
  1231. or self.with_polymorphic[1].corresponding_column(col)
  1232. is None
  1233. ):
  1234. raise sa_exc.InvalidRequestError(
  1235. "Could not map polymorphic_on column "
  1236. "'%s' to the mapped table - polymorphic "
  1237. "loads will not function properly"
  1238. % col.description
  1239. )
  1240. else:
  1241. # column/expression that polymorphic_on derives from
  1242. # is present in our mapped table
  1243. # and is probably mapped, but polymorphic_on itself
  1244. # is not. This happens when
  1245. # the polymorphic_on is only directly present in the
  1246. # with_polymorphic selectable, as when use
  1247. # polymorphic_union.
  1248. # we'll make a separate ColumnProperty for it.
  1249. instrument = True
  1250. key = getattr(col, "key", None)
  1251. if key:
  1252. if self._should_exclude(col.key, col.key, False, col):
  1253. raise sa_exc.InvalidRequestError(
  1254. "Cannot exclude or override the "
  1255. "discriminator column %r" % col.key
  1256. )
  1257. else:
  1258. self.polymorphic_on = col = col.label("_sa_polymorphic_on")
  1259. key = col.key
  1260. prop = properties.ColumnProperty(col, _instrument=instrument)
  1261. self._configure_property(key, prop, init=init, setparent=True)
  1262. # the actual polymorphic_on should be the first public-facing
  1263. # column in the property
  1264. self.polymorphic_on = prop.columns[0]
  1265. polymorphic_key = prop.key
  1266. else:
  1267. # no polymorphic_on was set.
  1268. # check inheriting mappers for one.
  1269. for mapper in self.iterate_to_root():
  1270. # determine if polymorphic_on of the parent
  1271. # should be propagated here. If the col
  1272. # is present in our mapped table, or if our mapped
  1273. # table is the same as the parent (i.e. single table
  1274. # inheritance), we can use it
  1275. if mapper.polymorphic_on is not None:
  1276. if self.persist_selectable is mapper.persist_selectable:
  1277. self.polymorphic_on = mapper.polymorphic_on
  1278. else:
  1279. self.polymorphic_on = (
  1280. self.persist_selectable
  1281. ).corresponding_column(mapper.polymorphic_on)
  1282. # we can use the parent mapper's _set_polymorphic_identity
  1283. # directly; it ensures the polymorphic_identity of the
  1284. # instance's mapper is used so is portable to subclasses.
  1285. if self.polymorphic_on is not None:
  1286. self._set_polymorphic_identity = (
  1287. mapper._set_polymorphic_identity
  1288. )
  1289. self._validate_polymorphic_identity = (
  1290. mapper._validate_polymorphic_identity
  1291. )
  1292. else:
  1293. self._set_polymorphic_identity = None
  1294. return
  1295. if setter:
  1296. def _set_polymorphic_identity(state):
  1297. dict_ = state.dict
  1298. state.get_impl(polymorphic_key).set(
  1299. state,
  1300. dict_,
  1301. state.manager.mapper.polymorphic_identity,
  1302. None,
  1303. )
  1304. def _validate_polymorphic_identity(mapper, state, dict_):
  1305. if (
  1306. polymorphic_key in dict_
  1307. and dict_[polymorphic_key]
  1308. not in mapper._acceptable_polymorphic_identities
  1309. ):
  1310. util.warn_limited(
  1311. "Flushing object %s with "
  1312. "incompatible polymorphic identity %r; the "
  1313. "object may not refresh and/or load correctly",
  1314. (state_str(state), dict_[polymorphic_key]),
  1315. )
  1316. self._set_polymorphic_identity = _set_polymorphic_identity
  1317. self._validate_polymorphic_identity = (
  1318. _validate_polymorphic_identity
  1319. )
  1320. else:
  1321. self._set_polymorphic_identity = None
  1322. _validate_polymorphic_identity = None
  1323. @HasMemoized.memoized_attribute
  1324. def _version_id_prop(self):
  1325. if self.version_id_col is not None:
  1326. return self._columntoproperty[self.version_id_col]
  1327. else:
  1328. return None
  1329. @HasMemoized.memoized_attribute
  1330. def _acceptable_polymorphic_identities(self):
  1331. identities = set()
  1332. stack = deque([self])
  1333. while stack:
  1334. item = stack.popleft()
  1335. if item.persist_selectable is self.persist_selectable:
  1336. identities.add(item.polymorphic_identity)
  1337. stack.extend(item._inheriting_mappers)
  1338. return identities
  1339. @HasMemoized.memoized_attribute
  1340. def _prop_set(self):
  1341. return frozenset(self._props.values())
  1342. @util.preload_module("sqlalchemy.orm.descriptor_props")
  1343. def _adapt_inherited_property(self, key, prop, init):
  1344. descriptor_props = util.preloaded.orm_descriptor_props
  1345. if not self.concrete:
  1346. self._configure_property(key, prop, init=False, setparent=False)
  1347. elif key not in self._props:
  1348. # determine if the class implements this attribute; if not,
  1349. # or if it is implemented by the attribute that is handling the
  1350. # given superclass-mapped property, then we need to report that we
  1351. # can't use this at the instance level since we are a concrete
  1352. # mapper and we don't map this. don't trip user-defined
  1353. # descriptors that might have side effects when invoked.
  1354. implementing_attribute = self.class_manager._get_class_attr_mro(
  1355. key, prop
  1356. )
  1357. if implementing_attribute is prop or (
  1358. isinstance(
  1359. implementing_attribute, attributes.InstrumentedAttribute
  1360. )
  1361. and implementing_attribute._parententity is prop.parent
  1362. ):
  1363. self._configure_property(
  1364. key,
  1365. descriptor_props.ConcreteInheritedProperty(),
  1366. init=init,
  1367. setparent=True,
  1368. )
  1369. @util.preload_module("sqlalchemy.orm.descriptor_props")
  1370. def _configure_property(self, key, prop, init=True, setparent=True):
  1371. descriptor_props = util.preloaded.orm_descriptor_props
  1372. self._log("_configure_property(%s, %s)", key, prop.__class__.__name__)
  1373. if not isinstance(prop, MapperProperty):
  1374. prop = self._property_from_column(key, prop)
  1375. if isinstance(prop, properties.ColumnProperty):
  1376. col = self.persist_selectable.corresponding_column(prop.columns[0])
  1377. # if the column is not present in the mapped table,
  1378. # test if a column has been added after the fact to the
  1379. # parent table (or their parent, etc.) [ticket:1570]
  1380. if col is None and self.inherits:
  1381. path = [self]
  1382. for m in self.inherits.iterate_to_root():
  1383. col = m.local_table.corresponding_column(prop.columns[0])
  1384. if col is not None:
  1385. for m2 in path:
  1386. m2.persist_selectable._refresh_for_new_column(col)
  1387. col = self.persist_selectable.corresponding_column(
  1388. prop.columns[0]
  1389. )
  1390. break
  1391. path.append(m)
  1392. # subquery expression, column not present in the mapped
  1393. # selectable.
  1394. if col is None:
  1395. col = prop.columns[0]
  1396. # column is coming in after _readonly_props was
  1397. # initialized; check for 'readonly'
  1398. if hasattr(self, "_readonly_props") and (
  1399. not hasattr(col, "table")
  1400. or col.table not in self._cols_by_table
  1401. ):
  1402. self._readonly_props.add(prop)
  1403. else:
  1404. # if column is coming in after _cols_by_table was
  1405. # initialized, ensure the col is in the right set
  1406. if (
  1407. hasattr(self, "_cols_by_table")
  1408. and col.table in self._cols_by_table
  1409. and col not in self._cols_by_table[col.table]
  1410. ):
  1411. self._cols_by_table[col.table].add(col)
  1412. # if this properties.ColumnProperty represents the "polymorphic
  1413. # discriminator" column, mark it. We'll need this when rendering
  1414. # columns in SELECT statements.
  1415. if not hasattr(prop, "_is_polymorphic_discriminator"):
  1416. prop._is_polymorphic_discriminator = (
  1417. col is self.polymorphic_on
  1418. or prop.columns[0] is self.polymorphic_on
  1419. )
  1420. if isinstance(col, expression.Label):
  1421. # new in 1.4, get column property against expressions
  1422. # to be addressable in subqueries
  1423. col.key = col._key_label = key
  1424. self.columns.add(col, key)
  1425. for col in prop.columns + prop._orig_columns:
  1426. for col in col.proxy_set:
  1427. self._columntoproperty[col] = prop
  1428. prop.key = key
  1429. if setparent:
  1430. prop.set_parent(self, init)
  1431. if key in self._props and getattr(
  1432. self._props[key], "_mapped_by_synonym", False
  1433. ):
  1434. syn = self._props[key]._mapped_by_synonym
  1435. raise sa_exc.ArgumentError(
  1436. "Can't call map_column=True for synonym %r=%r, "
  1437. "a ColumnProperty already exists keyed to the name "
  1438. "%r for column %r" % (syn, key, key, syn)
  1439. )
  1440. if (
  1441. key in self._props
  1442. and not isinstance(prop, properties.ColumnProperty)
  1443. and not isinstance(
  1444. self._props[key],
  1445. (
  1446. properties.ColumnProperty,
  1447. descriptor_props.ConcreteInheritedProperty,
  1448. ),
  1449. )
  1450. ):
  1451. util.warn(
  1452. "Property %s on %s being replaced with new "
  1453. "property %s; the old property will be discarded"
  1454. % (self._props[key], self, prop)
  1455. )
  1456. oldprop = self._props[key]
  1457. self._path_registry.pop(oldprop, None)
  1458. self._props[key] = prop
  1459. if not self.non_primary:
  1460. prop.instrument_class(self)
  1461. for mapper in self._inheriting_mappers:
  1462. mapper._adapt_inherited_property(key, prop, init)
  1463. if init:
  1464. prop.init()
  1465. prop.post_instrument_class(self)
  1466. if self.configured:
  1467. self._expire_memoizations()
  1468. @util.preload_module("sqlalchemy.orm.descriptor_props")
  1469. def _property_from_column(self, key, prop):
  1470. """generate/update a :class:`.ColumnProperty` given a
  1471. :class:`_schema.Column` object."""
  1472. descriptor_props = util.preloaded.orm_descriptor_props
  1473. # we were passed a Column or a list of Columns;
  1474. # generate a properties.ColumnProperty
  1475. columns = util.to_list(prop)
  1476. column = columns[0]
  1477. assert isinstance(column, expression.ColumnElement)
  1478. prop = self._props.get(key, None)
  1479. if isinstance(prop, properties.ColumnProperty):
  1480. if (
  1481. (
  1482. not self._inherits_equated_pairs
  1483. or (prop.columns[0], column)
  1484. not in self._inherits_equated_pairs
  1485. )
  1486. and not prop.columns[0].shares_lineage(column)
  1487. and prop.columns[0] is not self.version_id_col
  1488. and column is not self.version_id_col
  1489. ):
  1490. warn_only = prop.parent is not self
  1491. msg = (
  1492. "Implicitly combining column %s with column "
  1493. "%s under attribute '%s'. Please configure one "
  1494. "or more attributes for these same-named columns "
  1495. "explicitly." % (prop.columns[-1], column, key)
  1496. )
  1497. if warn_only:
  1498. util.warn(msg)
  1499. else:
  1500. raise sa_exc.InvalidRequestError(msg)
  1501. # existing properties.ColumnProperty from an inheriting
  1502. # mapper. make a copy and append our column to it
  1503. prop = prop.copy()
  1504. prop.columns.insert(0, column)
  1505. self._log(
  1506. "inserting column to existing list "
  1507. "in properties.ColumnProperty %s" % (key)
  1508. )
  1509. return prop
  1510. elif prop is None or isinstance(
  1511. prop, descriptor_props.ConcreteInheritedProperty
  1512. ):
  1513. mapped_column = []
  1514. for c in columns:
  1515. mc = self.persist_selectable.corresponding_column(c)
  1516. if mc is None:
  1517. mc = self.local_table.corresponding_column(c)
  1518. if mc is not None:
  1519. # if the column is in the local table but not the
  1520. # mapped table, this corresponds to adding a
  1521. # column after the fact to the local table.
  1522. # [ticket:1523]
  1523. self.persist_selectable._refresh_for_new_column(mc)
  1524. mc = self.persist_selectable.corresponding_column(c)
  1525. if mc is None:
  1526. raise sa_exc.ArgumentError(
  1527. "When configuring property '%s' on %s, "
  1528. "column '%s' is not represented in the mapper's "
  1529. "table. Use the `column_property()` function to "
  1530. "force this column to be mapped as a read-only "
  1531. "attribute." % (key, self, c)
  1532. )
  1533. mapped_column.append(mc)
  1534. return properties.ColumnProperty(*mapped_column)
  1535. else:
  1536. raise sa_exc.ArgumentError(
  1537. "WARNING: when configuring property '%s' on %s, "
  1538. "column '%s' conflicts with property '%r'. "
  1539. "To resolve this, map the column to the class under a "
  1540. "different name in the 'properties' dictionary. Or, "
  1541. "to remove all awareness of the column entirely "
  1542. "(including its availability as a foreign key), "
  1543. "use the 'include_properties' or 'exclude_properties' "
  1544. "mapper arguments to control specifically which table "
  1545. "columns get mapped." % (key, self, column.key, prop)
  1546. )
  1547. def _check_configure(self):
  1548. if self.registry._new_mappers:
  1549. _configure_registries({self.registry}, cascade=True)
  1550. def _post_configure_properties(self):
  1551. """Call the ``init()`` method on all ``MapperProperties``
  1552. attached to this mapper.
  1553. This is a deferred configuration step which is intended
  1554. to execute once all mappers have been constructed.
  1555. """
  1556. self._log("_post_configure_properties() started")
  1557. l = [(key, prop) for key, prop in self._props.items()]
  1558. for key, prop in l:
  1559. self._log("initialize prop %s", key)
  1560. if prop.parent is self and not prop._configure_started:
  1561. prop.init()
  1562. if prop._configure_finished:
  1563. prop.post_instrument_class(self)
  1564. self._log("_post_configure_properties() complete")
  1565. self.configured = True
  1566. def add_properties(self, dict_of_properties):
  1567. """Add the given dictionary of properties to this mapper,
  1568. using `add_property`.
  1569. """
  1570. for key, value in dict_of_properties.items():
  1571. self.add_property(key, value)
  1572. def add_property(self, key, prop):
  1573. """Add an individual MapperProperty to this mapper.
  1574. If the mapper has not been configured yet, just adds the
  1575. property to the initial properties dictionary sent to the
  1576. constructor. If this Mapper has already been configured, then
  1577. the given MapperProperty is configured immediately.
  1578. """
  1579. self._init_properties[key] = prop
  1580. self._configure_property(key, prop, init=self.configured)
  1581. def _expire_memoizations(self):
  1582. for mapper in self.iterate_to_root():
  1583. mapper._reset_memoizations()
  1584. @property
  1585. def _log_desc(self):
  1586. return (
  1587. "("
  1588. + self.class_.__name__
  1589. + "|"
  1590. + (
  1591. self.local_table is not None
  1592. and self.local_table.description
  1593. or str(self.local_table)
  1594. )
  1595. + (self.non_primary and "|non-primary" or "")
  1596. + ")"
  1597. )
  1598. def _log(self, msg, *args):
  1599. self.logger.info("%s " + msg, *((self._log_desc,) + args))
  1600. def _log_debug(self, msg, *args):
  1601. self.logger.debug("%s " + msg, *((self._log_desc,) + args))
  1602. def __repr__(self):
  1603. return "<Mapper at 0x%x; %s>" % (id(self), self.class_.__name__)
  1604. def __str__(self):
  1605. return "mapped class %s%s->%s" % (
  1606. self.class_.__name__,
  1607. self.non_primary and " (non-primary)" or "",
  1608. self.local_table.description
  1609. if self.local_table is not None
  1610. else self.persist_selectable.description,
  1611. )
  1612. def _is_orphan(self, state):
  1613. orphan_possible = False
  1614. for mapper in self.iterate_to_root():
  1615. for (key, cls) in mapper._delete_orphans:
  1616. orphan_possible = True
  1617. has_parent = attributes.manager_of_class(cls).has_parent(
  1618. state, key, optimistic=state.has_identity
  1619. )
  1620. if self.legacy_is_orphan and has_parent:
  1621. return False
  1622. elif not self.legacy_is_orphan and not has_parent:
  1623. return True
  1624. if self.legacy_is_orphan:
  1625. return orphan_possible
  1626. else:
  1627. return False
  1628. def has_property(self, key):
  1629. return key in self._props
  1630. def get_property(self, key, _configure_mappers=True):
  1631. """return a MapperProperty associated with the given key."""
  1632. if _configure_mappers:
  1633. self._check_configure()
  1634. try:
  1635. return self._props[key]
  1636. except KeyError as err:
  1637. util.raise_(
  1638. sa_exc.InvalidRequestError(
  1639. "Mapper '%s' has no property '%s'" % (self, key)
  1640. ),
  1641. replace_context=err,
  1642. )
  1643. def get_property_by_column(self, column):
  1644. """Given a :class:`_schema.Column` object, return the
  1645. :class:`.MapperProperty` which maps this column."""
  1646. return self._columntoproperty[column]
  1647. @property
  1648. def iterate_properties(self):
  1649. """return an iterator of all MapperProperty objects."""
  1650. self._check_configure()
  1651. return iter(self._props.values())
  1652. def _mappers_from_spec(self, spec, selectable):
  1653. """given a with_polymorphic() argument, return the set of mappers it
  1654. represents.
  1655. Trims the list of mappers to just those represented within the given
  1656. selectable, if present. This helps some more legacy-ish mappings.
  1657. """
  1658. if spec == "*":
  1659. mappers = list(self.self_and_descendants)
  1660. elif spec:
  1661. mappers = set()
  1662. for m in util.to_list(spec):
  1663. m = _class_to_mapper(m)
  1664. if not m.isa(self):
  1665. raise sa_exc.InvalidRequestError(
  1666. "%r does not inherit from %r" % (m, self)
  1667. )
  1668. if selectable is None:
  1669. mappers.update(m.iterate_to_root())
  1670. else:
  1671. mappers.add(m)
  1672. mappers = [m for m in self.self_and_descendants if m in mappers]
  1673. else:
  1674. mappers = []
  1675. if selectable is not None:
  1676. tables = set(
  1677. sql_util.find_tables(selectable, include_aliases=True)
  1678. )
  1679. mappers = [m for m in mappers if m.local_table in tables]
  1680. return mappers
  1681. def _selectable_from_mappers(self, mappers, innerjoin):
  1682. """given a list of mappers (assumed to be within this mapper's
  1683. inheritance hierarchy), construct an outerjoin amongst those mapper's
  1684. mapped tables.
  1685. """
  1686. from_obj = self.persist_selectable
  1687. for m in mappers:
  1688. if m is self:
  1689. continue
  1690. if m.concrete:
  1691. raise sa_exc.InvalidRequestError(
  1692. "'with_polymorphic()' requires 'selectable' argument "
  1693. "when concrete-inheriting mappers are used."
  1694. )
  1695. elif not m.single:
  1696. if innerjoin:
  1697. from_obj = from_obj.join(
  1698. m.local_table, m.inherit_condition
  1699. )
  1700. else:
  1701. from_obj = from_obj.outerjoin(
  1702. m.local_table, m.inherit_condition
  1703. )
  1704. return from_obj
  1705. @HasMemoized.memoized_attribute
  1706. def _single_table_criterion(self):
  1707. if self.single and self.inherits and self.polymorphic_on is not None:
  1708. return self.polymorphic_on._annotate({"parentmapper": self}).in_(
  1709. m.polymorphic_identity for m in self.self_and_descendants
  1710. )
  1711. else:
  1712. return None
  1713. @HasMemoized.memoized_attribute
  1714. def _with_polymorphic_mappers(self):
  1715. self._check_configure()
  1716. if not self.with_polymorphic:
  1717. return []
  1718. return self._mappers_from_spec(*self.with_polymorphic)
  1719. @HasMemoized.memoized_attribute
  1720. def _post_inspect(self):
  1721. """This hook is invoked by attribute inspection.
  1722. E.g. when Query calls:
  1723. coercions.expect(roles.ColumnsClauseRole, ent, keep_inspect=True)
  1724. This allows the inspection process run a configure mappers hook.
  1725. """
  1726. self._check_configure()
  1727. @HasMemoized.memoized_attribute
  1728. def _with_polymorphic_selectable(self):
  1729. if not self.with_polymorphic:
  1730. return self.persist_selectable
  1731. spec, selectable = self.with_polymorphic
  1732. if selectable is not None:
  1733. return selectable
  1734. else:
  1735. return self._selectable_from_mappers(
  1736. self._mappers_from_spec(spec, selectable), False
  1737. )
  1738. with_polymorphic_mappers = _with_polymorphic_mappers
  1739. """The list of :class:`_orm.Mapper` objects included in the
  1740. default "polymorphic" query.
  1741. """
  1742. @HasMemoized.memoized_attribute
  1743. def _insert_cols_evaluating_none(self):
  1744. return dict(
  1745. (
  1746. table,
  1747. frozenset(
  1748. col for col in columns if col.type.should_evaluate_none
  1749. ),
  1750. )
  1751. for table, columns in self._cols_by_table.items()
  1752. )
  1753. @HasMemoized.memoized_attribute
  1754. def _insert_cols_as_none(self):
  1755. return dict(
  1756. (
  1757. table,
  1758. frozenset(
  1759. col.key
  1760. for col in columns
  1761. if not col.primary_key
  1762. and not col.server_default
  1763. and not col.default
  1764. and not col.type.should_evaluate_none
  1765. ),
  1766. )
  1767. for table, columns in self._cols_by_table.items()
  1768. )
  1769. @HasMemoized.memoized_attribute
  1770. def _propkey_to_col(self):
  1771. return dict(
  1772. (
  1773. table,
  1774. dict(
  1775. (self._columntoproperty[col].key, col) for col in columns
  1776. ),
  1777. )
  1778. for table, columns in self._cols_by_table.items()
  1779. )
  1780. @HasMemoized.memoized_attribute
  1781. def _pk_keys_by_table(self):
  1782. return dict(
  1783. (table, frozenset([col.key for col in pks]))
  1784. for table, pks in self._pks_by_table.items()
  1785. )
  1786. @HasMemoized.memoized_attribute
  1787. def _pk_attr_keys_by_table(self):
  1788. return dict(
  1789. (
  1790. table,
  1791. frozenset([self._columntoproperty[col].key for col in pks]),
  1792. )
  1793. for table, pks in self._pks_by_table.items()
  1794. )
  1795. @HasMemoized.memoized_attribute
  1796. def _server_default_cols(self):
  1797. return dict(
  1798. (
  1799. table,
  1800. frozenset(
  1801. [
  1802. col.key
  1803. for col in columns
  1804. if col.server_default is not None
  1805. ]
  1806. ),
  1807. )
  1808. for table, columns in self._cols_by_table.items()
  1809. )
  1810. @HasMemoized.memoized_attribute
  1811. def _server_default_plus_onupdate_propkeys(self):
  1812. result = set()
  1813. for table, columns in self._cols_by_table.items():
  1814. for col in columns:
  1815. if (
  1816. col.server_default is not None
  1817. or col.server_onupdate is not None
  1818. ) and col in self._columntoproperty:
  1819. result.add(self._columntoproperty[col].key)
  1820. return result
  1821. @HasMemoized.memoized_attribute
  1822. def _server_onupdate_default_cols(self):
  1823. return dict(
  1824. (
  1825. table,
  1826. frozenset(
  1827. [
  1828. col.key
  1829. for col in columns
  1830. if col.server_onupdate is not None
  1831. ]
  1832. ),
  1833. )
  1834. for table, columns in self._cols_by_table.items()
  1835. )
  1836. @HasMemoized.memoized_instancemethod
  1837. def __clause_element__(self):
  1838. annotations = {
  1839. "entity_namespace": self,
  1840. "parententity": self,
  1841. "parentmapper": self,
  1842. }
  1843. if self.persist_selectable is not self.local_table:
  1844. # joined table inheritance, with polymorphic selectable,
  1845. # etc.
  1846. annotations["dml_table"] = self.local_table._annotate(
  1847. {
  1848. "entity_namespace": self,
  1849. "parententity": self,
  1850. "parentmapper": self,
  1851. }
  1852. )._set_propagate_attrs(
  1853. {"compile_state_plugin": "orm", "plugin_subject": self}
  1854. )
  1855. return self.selectable._annotate(annotations)._set_propagate_attrs(
  1856. {"compile_state_plugin": "orm", "plugin_subject": self}
  1857. )
  1858. @util.memoized_property
  1859. def select_identity_token(self):
  1860. return (
  1861. expression.null()
  1862. ._annotate(
  1863. {
  1864. "entity_namespace": self,
  1865. "parententity": self,
  1866. "parentmapper": self,
  1867. "identity_token": True,
  1868. }
  1869. )
  1870. ._set_propagate_attrs(
  1871. {"compile_state_plugin": "orm", "plugin_subject": self}
  1872. )
  1873. )
  1874. @property
  1875. def selectable(self):
  1876. """The :class:`_schema.FromClause` construct this
  1877. :class:`_orm.Mapper` selects from by default.
  1878. Normally, this is equivalent to :attr:`.persist_selectable`, unless
  1879. the ``with_polymorphic`` feature is in use, in which case the
  1880. full "polymorphic" selectable is returned.
  1881. """
  1882. return self._with_polymorphic_selectable
  1883. def _with_polymorphic_args(
  1884. self, spec=None, selectable=False, innerjoin=False
  1885. ):
  1886. if selectable not in (None, False):
  1887. selectable = coercions.expect(
  1888. roles.StrictFromClauseRole, selectable, allow_select=True
  1889. )
  1890. if self.with_polymorphic:
  1891. if not spec:
  1892. spec = self.with_polymorphic[0]
  1893. if selectable is False:
  1894. selectable = self.with_polymorphic[1]
  1895. elif selectable is False:
  1896. selectable = None
  1897. mappers = self._mappers_from_spec(spec, selectable)
  1898. if selectable is not None:
  1899. return mappers, selectable
  1900. else:
  1901. return mappers, self._selectable_from_mappers(mappers, innerjoin)
  1902. @HasMemoized.memoized_attribute
  1903. def _polymorphic_properties(self):
  1904. return list(
  1905. self._iterate_polymorphic_properties(
  1906. self._with_polymorphic_mappers
  1907. )
  1908. )
  1909. @property
  1910. def _all_column_expressions(self):
  1911. poly_properties = self._polymorphic_properties
  1912. adapter = self._polymorphic_adapter
  1913. return [
  1914. adapter.columns[prop.columns[0]] if adapter else prop.columns[0]
  1915. for prop in poly_properties
  1916. if isinstance(prop, properties.ColumnProperty)
  1917. and prop._renders_in_subqueries
  1918. ]
  1919. def _columns_plus_keys(self, polymorphic_mappers=()):
  1920. if polymorphic_mappers:
  1921. poly_properties = self._iterate_polymorphic_properties(
  1922. polymorphic_mappers
  1923. )
  1924. else:
  1925. poly_properties = self._polymorphic_properties
  1926. return [
  1927. (prop.key, prop.columns[0])
  1928. for prop in poly_properties
  1929. if isinstance(prop, properties.ColumnProperty)
  1930. ]
  1931. @HasMemoized.memoized_attribute
  1932. def _polymorphic_adapter(self):
  1933. if self.with_polymorphic:
  1934. return sql_util.ColumnAdapter(
  1935. self.selectable, equivalents=self._equivalent_columns
  1936. )
  1937. else:
  1938. return None
  1939. def _iterate_polymorphic_properties(self, mappers=None):
  1940. """Return an iterator of MapperProperty objects which will render into
  1941. a SELECT."""
  1942. if mappers is None:
  1943. mappers = self._with_polymorphic_mappers
  1944. if not mappers:
  1945. for c in self.iterate_properties:
  1946. yield c
  1947. else:
  1948. # in the polymorphic case, filter out discriminator columns
  1949. # from other mappers, as these are sometimes dependent on that
  1950. # mapper's polymorphic selectable (which we don't want rendered)
  1951. for c in util.unique_list(
  1952. chain(
  1953. *[
  1954. list(mapper.iterate_properties)
  1955. for mapper in [self] + mappers
  1956. ]
  1957. )
  1958. ):
  1959. if getattr(c, "_is_polymorphic_discriminator", False) and (
  1960. self.polymorphic_on is None
  1961. or c.columns[0] is not self.polymorphic_on
  1962. ):
  1963. continue
  1964. yield c
  1965. @HasMemoized.memoized_attribute
  1966. def attrs(self):
  1967. """A namespace of all :class:`.MapperProperty` objects
  1968. associated this mapper.
  1969. This is an object that provides each property based on
  1970. its key name. For instance, the mapper for a
  1971. ``User`` class which has ``User.name`` attribute would
  1972. provide ``mapper.attrs.name``, which would be the
  1973. :class:`.ColumnProperty` representing the ``name``
  1974. column. The namespace object can also be iterated,
  1975. which would yield each :class:`.MapperProperty`.
  1976. :class:`_orm.Mapper` has several pre-filtered views
  1977. of this attribute which limit the types of properties
  1978. returned, including :attr:`.synonyms`, :attr:`.column_attrs`,
  1979. :attr:`.relationships`, and :attr:`.composites`.
  1980. .. warning::
  1981. The :attr:`_orm.Mapper.attrs` accessor namespace is an
  1982. instance of :class:`.OrderedProperties`. This is
  1983. a dictionary-like object which includes a small number of
  1984. named methods such as :meth:`.OrderedProperties.items`
  1985. and :meth:`.OrderedProperties.values`. When
  1986. accessing attributes dynamically, favor using the dict-access
  1987. scheme, e.g. ``mapper.attrs[somename]`` over
  1988. ``getattr(mapper.attrs, somename)`` to avoid name collisions.
  1989. .. seealso::
  1990. :attr:`_orm.Mapper.all_orm_descriptors`
  1991. """
  1992. self._check_configure()
  1993. return util.ImmutableProperties(self._props)
  1994. @HasMemoized.memoized_attribute
  1995. def all_orm_descriptors(self):
  1996. """A namespace of all :class:`.InspectionAttr` attributes associated
  1997. with the mapped class.
  1998. These attributes are in all cases Python :term:`descriptors`
  1999. associated with the mapped class or its superclasses.
  2000. This namespace includes attributes that are mapped to the class
  2001. as well as attributes declared by extension modules.
  2002. It includes any Python descriptor type that inherits from
  2003. :class:`.InspectionAttr`. This includes
  2004. :class:`.QueryableAttribute`, as well as extension types such as
  2005. :class:`.hybrid_property`, :class:`.hybrid_method` and
  2006. :class:`.AssociationProxy`.
  2007. To distinguish between mapped attributes and extension attributes,
  2008. the attribute :attr:`.InspectionAttr.extension_type` will refer
  2009. to a constant that distinguishes between different extension types.
  2010. The sorting of the attributes is based on the following rules:
  2011. 1. Iterate through the class and its superclasses in order from
  2012. subclass to superclass (i.e. iterate through ``cls.__mro__``)
  2013. 2. For each class, yield the attributes in the order in which they
  2014. appear in ``__dict__``, with the exception of those in step
  2015. 3 below. In Python 3.6 and above this ordering will be the
  2016. same as that of the class' construction, with the exception
  2017. of attributes that were added after the fact by the application
  2018. or the mapper.
  2019. 3. If a certain attribute key is also in the superclass ``__dict__``,
  2020. then it's included in the iteration for that class, and not the
  2021. class in which it first appeared.
  2022. The above process produces an ordering that is deterministic in terms
  2023. of the order in which attributes were assigned to the class.
  2024. .. versionchanged:: 1.3.19 ensured deterministic ordering for
  2025. :meth:`_orm.Mapper.all_orm_descriptors`.
  2026. When dealing with a :class:`.QueryableAttribute`, the
  2027. :attr:`.QueryableAttribute.property` attribute refers to the
  2028. :class:`.MapperProperty` property, which is what you get when
  2029. referring to the collection of mapped properties via
  2030. :attr:`_orm.Mapper.attrs`.
  2031. .. warning::
  2032. The :attr:`_orm.Mapper.all_orm_descriptors`
  2033. accessor namespace is an
  2034. instance of :class:`.OrderedProperties`. This is
  2035. a dictionary-like object which includes a small number of
  2036. named methods such as :meth:`.OrderedProperties.items`
  2037. and :meth:`.OrderedProperties.values`. When
  2038. accessing attributes dynamically, favor using the dict-access
  2039. scheme, e.g. ``mapper.all_orm_descriptors[somename]`` over
  2040. ``getattr(mapper.all_orm_descriptors, somename)`` to avoid name
  2041. collisions.
  2042. .. seealso::
  2043. :attr:`_orm.Mapper.attrs`
  2044. """
  2045. return util.ImmutableProperties(
  2046. dict(self.class_manager._all_sqla_attributes())
  2047. )
  2048. @HasMemoized.memoized_attribute
  2049. @util.preload_module("sqlalchemy.orm.descriptor_props")
  2050. def synonyms(self):
  2051. """Return a namespace of all :class:`.SynonymProperty`
  2052. properties maintained by this :class:`_orm.Mapper`.
  2053. .. seealso::
  2054. :attr:`_orm.Mapper.attrs` - namespace of all
  2055. :class:`.MapperProperty`
  2056. objects.
  2057. """
  2058. descriptor_props = util.preloaded.orm_descriptor_props
  2059. return self._filter_properties(descriptor_props.SynonymProperty)
  2060. @property
  2061. def entity_namespace(self):
  2062. return self.class_
  2063. @HasMemoized.memoized_attribute
  2064. def column_attrs(self):
  2065. """Return a namespace of all :class:`.ColumnProperty`
  2066. properties maintained by this :class:`_orm.Mapper`.
  2067. .. seealso::
  2068. :attr:`_orm.Mapper.attrs` - namespace of all
  2069. :class:`.MapperProperty`
  2070. objects.
  2071. """
  2072. return self._filter_properties(properties.ColumnProperty)
  2073. @util.preload_module("sqlalchemy.orm.relationships")
  2074. @HasMemoized.memoized_attribute
  2075. def relationships(self):
  2076. """A namespace of all :class:`.RelationshipProperty` properties
  2077. maintained by this :class:`_orm.Mapper`.
  2078. .. warning::
  2079. the :attr:`_orm.Mapper.relationships` accessor namespace is an
  2080. instance of :class:`.OrderedProperties`. This is
  2081. a dictionary-like object which includes a small number of
  2082. named methods such as :meth:`.OrderedProperties.items`
  2083. and :meth:`.OrderedProperties.values`. When
  2084. accessing attributes dynamically, favor using the dict-access
  2085. scheme, e.g. ``mapper.relationships[somename]`` over
  2086. ``getattr(mapper.relationships, somename)`` to avoid name
  2087. collisions.
  2088. .. seealso::
  2089. :attr:`_orm.Mapper.attrs` - namespace of all
  2090. :class:`.MapperProperty`
  2091. objects.
  2092. """
  2093. return self._filter_properties(
  2094. util.preloaded.orm_relationships.RelationshipProperty
  2095. )
  2096. @HasMemoized.memoized_attribute
  2097. @util.preload_module("sqlalchemy.orm.descriptor_props")
  2098. def composites(self):
  2099. """Return a namespace of all :class:`.CompositeProperty`
  2100. properties maintained by this :class:`_orm.Mapper`.
  2101. .. seealso::
  2102. :attr:`_orm.Mapper.attrs` - namespace of all
  2103. :class:`.MapperProperty`
  2104. objects.
  2105. """
  2106. return self._filter_properties(
  2107. util.preloaded.orm_descriptor_props.CompositeProperty
  2108. )
  2109. def _filter_properties(self, type_):
  2110. self._check_configure()
  2111. return util.ImmutableProperties(
  2112. util.OrderedDict(
  2113. (k, v) for k, v in self._props.items() if isinstance(v, type_)
  2114. )
  2115. )
  2116. @HasMemoized.memoized_attribute
  2117. def _get_clause(self):
  2118. """create a "get clause" based on the primary key. this is used
  2119. by query.get() and many-to-one lazyloads to load this item
  2120. by primary key.
  2121. """
  2122. params = [
  2123. (
  2124. primary_key,
  2125. sql.bindparam("pk_%d" % idx, type_=primary_key.type),
  2126. )
  2127. for idx, primary_key in enumerate(self.primary_key, 1)
  2128. ]
  2129. return (
  2130. sql.and_(*[k == v for (k, v) in params]),
  2131. util.column_dict(params),
  2132. )
  2133. @HasMemoized.memoized_attribute
  2134. def _equivalent_columns(self):
  2135. """Create a map of all equivalent columns, based on
  2136. the determination of column pairs that are equated to
  2137. one another based on inherit condition. This is designed
  2138. to work with the queries that util.polymorphic_union
  2139. comes up with, which often don't include the columns from
  2140. the base table directly (including the subclass table columns
  2141. only).
  2142. The resulting structure is a dictionary of columns mapped
  2143. to lists of equivalent columns, e.g.::
  2144. {
  2145. tablea.col1:
  2146. {tableb.col1, tablec.col1},
  2147. tablea.col2:
  2148. {tabled.col2}
  2149. }
  2150. """
  2151. result = util.column_dict()
  2152. def visit_binary(binary):
  2153. if binary.operator == operators.eq:
  2154. if binary.left in result:
  2155. result[binary.left].add(binary.right)
  2156. else:
  2157. result[binary.left] = util.column_set((binary.right,))
  2158. if binary.right in result:
  2159. result[binary.right].add(binary.left)
  2160. else:
  2161. result[binary.right] = util.column_set((binary.left,))
  2162. for mapper in self.base_mapper.self_and_descendants:
  2163. if mapper.inherit_condition is not None:
  2164. visitors.traverse(
  2165. mapper.inherit_condition, {}, {"binary": visit_binary}
  2166. )
  2167. return result
  2168. def _is_userland_descriptor(self, assigned_name, obj):
  2169. if isinstance(
  2170. obj,
  2171. (
  2172. _MappedAttribute,
  2173. instrumentation.ClassManager,
  2174. expression.ColumnElement,
  2175. ),
  2176. ):
  2177. return False
  2178. else:
  2179. return assigned_name not in self._dataclass_fields
  2180. @HasMemoized.memoized_attribute
  2181. def _dataclass_fields(self):
  2182. return [f.name for f in util.dataclass_fields(self.class_)]
  2183. def _should_exclude(self, name, assigned_name, local, column):
  2184. """determine whether a particular property should be implicitly
  2185. present on the class.
  2186. This occurs when properties are propagated from an inherited class, or
  2187. are applied from the columns present in the mapped table.
  2188. """
  2189. # check for class-bound attributes and/or descriptors,
  2190. # either local or from an inherited class
  2191. # ignore dataclass field default values
  2192. if local:
  2193. if self.class_.__dict__.get(
  2194. assigned_name, None
  2195. ) is not None and self._is_userland_descriptor(
  2196. assigned_name, self.class_.__dict__[assigned_name]
  2197. ):
  2198. return True
  2199. else:
  2200. attr = self.class_manager._get_class_attr_mro(assigned_name, None)
  2201. if attr is not None and self._is_userland_descriptor(
  2202. assigned_name, attr
  2203. ):
  2204. return True
  2205. if (
  2206. self.include_properties is not None
  2207. and name not in self.include_properties
  2208. and (column is None or column not in self.include_properties)
  2209. ):
  2210. self._log("not including property %s" % (name))
  2211. return True
  2212. if self.exclude_properties is not None and (
  2213. name in self.exclude_properties
  2214. or (column is not None and column in self.exclude_properties)
  2215. ):
  2216. self._log("excluding property %s" % (name))
  2217. return True
  2218. return False
  2219. def common_parent(self, other):
  2220. """Return true if the given mapper shares a
  2221. common inherited parent as this mapper."""
  2222. return self.base_mapper is other.base_mapper
  2223. def is_sibling(self, other):
  2224. """return true if the other mapper is an inheriting sibling to this
  2225. one. common parent but different branch
  2226. """
  2227. return (
  2228. self.base_mapper is other.base_mapper
  2229. and not self.isa(other)
  2230. and not other.isa(self)
  2231. )
  2232. def _canload(self, state, allow_subtypes):
  2233. s = self.primary_mapper()
  2234. if self.polymorphic_on is not None or allow_subtypes:
  2235. return _state_mapper(state).isa(s)
  2236. else:
  2237. return _state_mapper(state) is s
  2238. def isa(self, other):
  2239. """Return True if the this mapper inherits from the given mapper."""
  2240. m = self
  2241. while m and m is not other:
  2242. m = m.inherits
  2243. return bool(m)
  2244. def iterate_to_root(self):
  2245. m = self
  2246. while m:
  2247. yield m
  2248. m = m.inherits
  2249. @HasMemoized.memoized_attribute
  2250. def self_and_descendants(self):
  2251. """The collection including this mapper and all descendant mappers.
  2252. This includes not just the immediately inheriting mappers but
  2253. all their inheriting mappers as well.
  2254. """
  2255. descendants = []
  2256. stack = deque([self])
  2257. while stack:
  2258. item = stack.popleft()
  2259. descendants.append(item)
  2260. stack.extend(item._inheriting_mappers)
  2261. return util.WeakSequence(descendants)
  2262. def polymorphic_iterator(self):
  2263. """Iterate through the collection including this mapper and
  2264. all descendant mappers.
  2265. This includes not just the immediately inheriting mappers but
  2266. all their inheriting mappers as well.
  2267. To iterate through an entire hierarchy, use
  2268. ``mapper.base_mapper.polymorphic_iterator()``.
  2269. """
  2270. return iter(self.self_and_descendants)
  2271. def primary_mapper(self):
  2272. """Return the primary mapper corresponding to this mapper's class key
  2273. (class)."""
  2274. return self.class_manager.mapper
  2275. @property
  2276. def primary_base_mapper(self):
  2277. return self.class_manager.mapper.base_mapper
  2278. def _result_has_identity_key(self, result, adapter=None):
  2279. pk_cols = self.primary_key
  2280. if adapter:
  2281. pk_cols = [adapter.columns[c] for c in pk_cols]
  2282. rk = result.keys()
  2283. for col in pk_cols:
  2284. if col not in rk:
  2285. return False
  2286. else:
  2287. return True
  2288. def identity_key_from_row(self, row, identity_token=None, adapter=None):
  2289. """Return an identity-map key for use in storing/retrieving an
  2290. item from the identity map.
  2291. :param row: A :class:`.Row` instance. The columns which are
  2292. mapped by this :class:`_orm.Mapper` should be locatable in the row,
  2293. preferably via the :class:`_schema.Column`
  2294. object directly (as is the case
  2295. when a :func:`_expression.select` construct is executed), or
  2296. via string names of the form ``<tablename>_<colname>``.
  2297. """
  2298. pk_cols = self.primary_key
  2299. if adapter:
  2300. pk_cols = [adapter.columns[c] for c in pk_cols]
  2301. return (
  2302. self._identity_class,
  2303. tuple(row[column] for column in pk_cols),
  2304. identity_token,
  2305. )
  2306. def identity_key_from_primary_key(self, primary_key, identity_token=None):
  2307. """Return an identity-map key for use in storing/retrieving an
  2308. item from an identity map.
  2309. :param primary_key: A list of values indicating the identifier.
  2310. """
  2311. return self._identity_class, tuple(primary_key), identity_token
  2312. def identity_key_from_instance(self, instance):
  2313. """Return the identity key for the given instance, based on
  2314. its primary key attributes.
  2315. If the instance's state is expired, calling this method
  2316. will result in a database check to see if the object has been deleted.
  2317. If the row no longer exists,
  2318. :class:`~sqlalchemy.orm.exc.ObjectDeletedError` is raised.
  2319. This value is typically also found on the instance state under the
  2320. attribute name `key`.
  2321. """
  2322. state = attributes.instance_state(instance)
  2323. return self._identity_key_from_state(state, attributes.PASSIVE_OFF)
  2324. def _identity_key_from_state(
  2325. self, state, passive=attributes.PASSIVE_RETURN_NO_VALUE
  2326. ):
  2327. dict_ = state.dict
  2328. manager = state.manager
  2329. return (
  2330. self._identity_class,
  2331. tuple(
  2332. [
  2333. manager[prop.key].impl.get(state, dict_, passive)
  2334. for prop in self._identity_key_props
  2335. ]
  2336. ),
  2337. state.identity_token,
  2338. )
  2339. def primary_key_from_instance(self, instance):
  2340. """Return the list of primary key values for the given
  2341. instance.
  2342. If the instance's state is expired, calling this method
  2343. will result in a database check to see if the object has been deleted.
  2344. If the row no longer exists,
  2345. :class:`~sqlalchemy.orm.exc.ObjectDeletedError` is raised.
  2346. """
  2347. state = attributes.instance_state(instance)
  2348. identity_key = self._identity_key_from_state(
  2349. state, attributes.PASSIVE_OFF
  2350. )
  2351. return identity_key[1]
  2352. @HasMemoized.memoized_attribute
  2353. def _persistent_sortkey_fn(self):
  2354. key_fns = [col.type.sort_key_function for col in self.primary_key]
  2355. if set(key_fns).difference([None]):
  2356. def key(state):
  2357. return tuple(
  2358. key_fn(val) if key_fn is not None else val
  2359. for key_fn, val in zip(key_fns, state.key[1])
  2360. )
  2361. else:
  2362. def key(state):
  2363. return state.key[1]
  2364. return key
  2365. @HasMemoized.memoized_attribute
  2366. def _identity_key_props(self):
  2367. return [self._columntoproperty[col] for col in self.primary_key]
  2368. @HasMemoized.memoized_attribute
  2369. def _all_pk_cols(self):
  2370. collection = set()
  2371. for table in self.tables:
  2372. collection.update(self._pks_by_table[table])
  2373. return collection
  2374. @HasMemoized.memoized_attribute
  2375. def _should_undefer_in_wildcard(self):
  2376. cols = set(self.primary_key)
  2377. if self.polymorphic_on is not None:
  2378. cols.add(self.polymorphic_on)
  2379. return cols
  2380. @HasMemoized.memoized_attribute
  2381. def _primary_key_propkeys(self):
  2382. return {self._columntoproperty[col].key for col in self._all_pk_cols}
  2383. def _get_state_attr_by_column(
  2384. self, state, dict_, column, passive=attributes.PASSIVE_RETURN_NO_VALUE
  2385. ):
  2386. prop = self._columntoproperty[column]
  2387. return state.manager[prop.key].impl.get(state, dict_, passive=passive)
  2388. def _set_committed_state_attr_by_column(self, state, dict_, column, value):
  2389. prop = self._columntoproperty[column]
  2390. state.manager[prop.key].impl.set_committed_value(state, dict_, value)
  2391. def _set_state_attr_by_column(self, state, dict_, column, value):
  2392. prop = self._columntoproperty[column]
  2393. state.manager[prop.key].impl.set(state, dict_, value, None)
  2394. def _get_committed_attr_by_column(self, obj, column):
  2395. state = attributes.instance_state(obj)
  2396. dict_ = attributes.instance_dict(obj)
  2397. return self._get_committed_state_attr_by_column(
  2398. state, dict_, column, passive=attributes.PASSIVE_OFF
  2399. )
  2400. def _get_committed_state_attr_by_column(
  2401. self, state, dict_, column, passive=attributes.PASSIVE_RETURN_NO_VALUE
  2402. ):
  2403. prop = self._columntoproperty[column]
  2404. return state.manager[prop.key].impl.get_committed_value(
  2405. state, dict_, passive=passive
  2406. )
  2407. def _optimized_get_statement(self, state, attribute_names):
  2408. """assemble a WHERE clause which retrieves a given state by primary
  2409. key, using a minimized set of tables.
  2410. Applies to a joined-table inheritance mapper where the
  2411. requested attribute names are only present on joined tables,
  2412. not the base table. The WHERE clause attempts to include
  2413. only those tables to minimize joins.
  2414. """
  2415. props = self._props
  2416. col_attribute_names = set(attribute_names).intersection(
  2417. state.mapper.column_attrs.keys()
  2418. )
  2419. tables = set(
  2420. chain(
  2421. *[
  2422. sql_util.find_tables(c, check_columns=True)
  2423. for key in col_attribute_names
  2424. for c in props[key].columns
  2425. ]
  2426. )
  2427. )
  2428. if self.base_mapper.local_table in tables:
  2429. return None
  2430. def visit_binary(binary):
  2431. leftcol = binary.left
  2432. rightcol = binary.right
  2433. if leftcol is None or rightcol is None:
  2434. return
  2435. if leftcol.table not in tables:
  2436. leftval = self._get_committed_state_attr_by_column(
  2437. state,
  2438. state.dict,
  2439. leftcol,
  2440. passive=attributes.PASSIVE_NO_INITIALIZE,
  2441. )
  2442. if leftval in orm_util._none_set:
  2443. raise _OptGetColumnsNotAvailable()
  2444. binary.left = sql.bindparam(
  2445. None, leftval, type_=binary.right.type
  2446. )
  2447. elif rightcol.table not in tables:
  2448. rightval = self._get_committed_state_attr_by_column(
  2449. state,
  2450. state.dict,
  2451. rightcol,
  2452. passive=attributes.PASSIVE_NO_INITIALIZE,
  2453. )
  2454. if rightval in orm_util._none_set:
  2455. raise _OptGetColumnsNotAvailable()
  2456. binary.right = sql.bindparam(
  2457. None, rightval, type_=binary.right.type
  2458. )
  2459. allconds = []
  2460. try:
  2461. start = False
  2462. for mapper in reversed(list(self.iterate_to_root())):
  2463. if mapper.local_table in tables:
  2464. start = True
  2465. elif not isinstance(
  2466. mapper.local_table, expression.TableClause
  2467. ):
  2468. return None
  2469. if start and not mapper.single:
  2470. allconds.append(
  2471. visitors.cloned_traverse(
  2472. mapper.inherit_condition,
  2473. {},
  2474. {"binary": visit_binary},
  2475. )
  2476. )
  2477. except _OptGetColumnsNotAvailable:
  2478. return None
  2479. cond = sql.and_(*allconds)
  2480. cols = []
  2481. for key in col_attribute_names:
  2482. cols.extend(props[key].columns)
  2483. return (
  2484. sql.select(*cols)
  2485. .where(cond)
  2486. .set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL)
  2487. )
  2488. def _iterate_to_target_viawpoly(self, mapper):
  2489. if self.isa(mapper):
  2490. prev = self
  2491. for m in self.iterate_to_root():
  2492. yield m
  2493. if m is not prev and prev not in m._with_polymorphic_mappers:
  2494. break
  2495. prev = m
  2496. if m is mapper:
  2497. break
  2498. def _should_selectin_load(self, enabled_via_opt, polymorphic_from):
  2499. if not enabled_via_opt:
  2500. # common case, takes place for all polymorphic loads
  2501. mapper = polymorphic_from
  2502. for m in self._iterate_to_target_viawpoly(mapper):
  2503. if m.polymorphic_load == "selectin":
  2504. return m
  2505. else:
  2506. # uncommon case, selectin load options were used
  2507. enabled_via_opt = set(enabled_via_opt)
  2508. enabled_via_opt_mappers = {e.mapper: e for e in enabled_via_opt}
  2509. for entity in enabled_via_opt.union([polymorphic_from]):
  2510. mapper = entity.mapper
  2511. for m in self._iterate_to_target_viawpoly(mapper):
  2512. if (
  2513. m.polymorphic_load == "selectin"
  2514. or m in enabled_via_opt_mappers
  2515. ):
  2516. return enabled_via_opt_mappers.get(m, m)
  2517. return None
  2518. @util.preload_module(
  2519. "sqlalchemy.ext.baked", "sqlalchemy.orm.strategy_options"
  2520. )
  2521. def _subclass_load_via_in(self, entity):
  2522. """Assemble a BakedQuery that can load the columns local to
  2523. this subclass as a SELECT with IN.
  2524. """
  2525. strategy_options = util.preloaded.orm_strategy_options
  2526. baked = util.preloaded.ext_baked
  2527. assert self.inherits
  2528. polymorphic_prop = self._columntoproperty[self.polymorphic_on]
  2529. keep_props = set([polymorphic_prop] + self._identity_key_props)
  2530. disable_opt = strategy_options.Load(entity)
  2531. enable_opt = strategy_options.Load(entity)
  2532. for prop in self.attrs:
  2533. if prop.parent is self or prop in keep_props:
  2534. # "enable" options, to turn on the properties that we want to
  2535. # load by default (subject to options from the query)
  2536. enable_opt.set_generic_strategy(
  2537. (prop.key,), dict(prop.strategy_key)
  2538. )
  2539. else:
  2540. # "disable" options, to turn off the properties from the
  2541. # superclass that we *don't* want to load, applied after
  2542. # the options from the query to override them
  2543. disable_opt.set_generic_strategy(
  2544. (prop.key,), {"do_nothing": True}
  2545. )
  2546. primary_key = [
  2547. sql_util._deep_annotate(pk, {"_orm_adapt": True})
  2548. for pk in self.primary_key
  2549. ]
  2550. if len(primary_key) > 1:
  2551. in_expr = sql.tuple_(*primary_key)
  2552. else:
  2553. in_expr = primary_key[0]
  2554. if entity.is_aliased_class:
  2555. assert entity.mapper is self
  2556. q = baked.BakedQuery(
  2557. self._compiled_cache,
  2558. lambda session: session.query(entity).select_entity_from(
  2559. entity.selectable
  2560. ),
  2561. (self,),
  2562. )
  2563. q.spoil()
  2564. else:
  2565. q = baked.BakedQuery(
  2566. self._compiled_cache,
  2567. lambda session: session.query(self),
  2568. (self,),
  2569. )
  2570. q += lambda q: q.filter(
  2571. in_expr.in_(sql.bindparam("primary_keys", expanding=True))
  2572. ).order_by(*primary_key)
  2573. return q, enable_opt, disable_opt
  2574. @HasMemoized.memoized_attribute
  2575. def _subclass_load_via_in_mapper(self):
  2576. return self._subclass_load_via_in(self)
  2577. def cascade_iterator(self, type_, state, halt_on=None):
  2578. r"""Iterate each element and its mapper in an object graph,
  2579. for all relationships that meet the given cascade rule.
  2580. :param type\_:
  2581. The name of the cascade rule (i.e. ``"save-update"``, ``"delete"``,
  2582. etc.).
  2583. .. note:: the ``"all"`` cascade is not accepted here. For a generic
  2584. object traversal function, see :ref:`faq_walk_objects`.
  2585. :param state:
  2586. The lead InstanceState. child items will be processed per
  2587. the relationships defined for this object's mapper.
  2588. :return: the method yields individual object instances.
  2589. .. seealso::
  2590. :ref:`unitofwork_cascades`
  2591. :ref:`faq_walk_objects` - illustrates a generic function to
  2592. traverse all objects without relying on cascades.
  2593. """
  2594. visited_states = set()
  2595. prp, mpp = object(), object()
  2596. assert state.mapper.isa(self)
  2597. visitables = deque(
  2598. [(deque(state.mapper._props.values()), prp, state, state.dict)]
  2599. )
  2600. while visitables:
  2601. iterator, item_type, parent_state, parent_dict = visitables[-1]
  2602. if not iterator:
  2603. visitables.pop()
  2604. continue
  2605. if item_type is prp:
  2606. prop = iterator.popleft()
  2607. if type_ not in prop.cascade:
  2608. continue
  2609. queue = deque(
  2610. prop.cascade_iterator(
  2611. type_,
  2612. parent_state,
  2613. parent_dict,
  2614. visited_states,
  2615. halt_on,
  2616. )
  2617. )
  2618. if queue:
  2619. visitables.append((queue, mpp, None, None))
  2620. elif item_type is mpp:
  2621. (
  2622. instance,
  2623. instance_mapper,
  2624. corresponding_state,
  2625. corresponding_dict,
  2626. ) = iterator.popleft()
  2627. yield (
  2628. instance,
  2629. instance_mapper,
  2630. corresponding_state,
  2631. corresponding_dict,
  2632. )
  2633. visitables.append(
  2634. (
  2635. deque(instance_mapper._props.values()),
  2636. prp,
  2637. corresponding_state,
  2638. corresponding_dict,
  2639. )
  2640. )
  2641. @HasMemoized.memoized_attribute
  2642. def _compiled_cache(self):
  2643. return util.LRUCache(self._compiled_cache_size)
  2644. @HasMemoized.memoized_attribute
  2645. def _sorted_tables(self):
  2646. table_to_mapper = {}
  2647. for mapper in self.base_mapper.self_and_descendants:
  2648. for t in mapper.tables:
  2649. table_to_mapper.setdefault(t, mapper)
  2650. extra_dependencies = []
  2651. for table, mapper in table_to_mapper.items():
  2652. super_ = mapper.inherits
  2653. if super_:
  2654. extra_dependencies.extend(
  2655. [(super_table, table) for super_table in super_.tables]
  2656. )
  2657. def skip(fk):
  2658. # attempt to skip dependencies that are not
  2659. # significant to the inheritance chain
  2660. # for two tables that are related by inheritance.
  2661. # while that dependency may be important, it's technically
  2662. # not what we mean to sort on here.
  2663. parent = table_to_mapper.get(fk.parent.table)
  2664. dep = table_to_mapper.get(fk.column.table)
  2665. if (
  2666. parent is not None
  2667. and dep is not None
  2668. and dep is not parent
  2669. and dep.inherit_condition is not None
  2670. ):
  2671. cols = set(sql_util._find_columns(dep.inherit_condition))
  2672. if parent.inherit_condition is not None:
  2673. cols = cols.union(
  2674. sql_util._find_columns(parent.inherit_condition)
  2675. )
  2676. return fk.parent not in cols and fk.column not in cols
  2677. else:
  2678. return fk.parent not in cols
  2679. return False
  2680. sorted_ = sql_util.sort_tables(
  2681. table_to_mapper,
  2682. skip_fn=skip,
  2683. extra_dependencies=extra_dependencies,
  2684. )
  2685. ret = util.OrderedDict()
  2686. for t in sorted_:
  2687. ret[t] = table_to_mapper[t]
  2688. return ret
  2689. def _memo(self, key, callable_):
  2690. if key in self._memoized_values:
  2691. return self._memoized_values[key]
  2692. else:
  2693. self._memoized_values[key] = value = callable_()
  2694. return value
  2695. @util.memoized_property
  2696. def _table_to_equated(self):
  2697. """memoized map of tables to collections of columns to be
  2698. synchronized upwards to the base mapper."""
  2699. result = util.defaultdict(list)
  2700. for table in self._sorted_tables:
  2701. cols = set(table.c)
  2702. for m in self.iterate_to_root():
  2703. if m._inherits_equated_pairs and cols.intersection(
  2704. util.reduce(
  2705. set.union,
  2706. [l.proxy_set for l, r in m._inherits_equated_pairs],
  2707. )
  2708. ):
  2709. result[table].append((m, m._inherits_equated_pairs))
  2710. return result
  2711. class _OptGetColumnsNotAvailable(Exception):
  2712. pass
  2713. def configure_mappers():
  2714. """Initialize the inter-mapper relationships of all mappers that
  2715. have been constructed thus far across all :class:`_orm.registry`
  2716. collections.
  2717. The configure step is used to reconcile and initialize the
  2718. :func:`_orm.relationship` linkages between mapped classes, as well as to
  2719. invoke configuration events such as the
  2720. :meth:`_orm.MapperEvents.before_configured` and
  2721. :meth:`_orm.MapperEvents.after_configured`, which may be used by ORM
  2722. extensions or user-defined extension hooks.
  2723. Mapper configuration is normally invoked automatically, the first time
  2724. mappings from a particular :class:`_orm.registry` are used, as well as
  2725. whenever mappings are used and additional not-yet-configured mappers have
  2726. been constructed. The automatic configuration process however is local only
  2727. to the :class:`_orm.registry` involving the target mapper and any related
  2728. :class:`_orm.registry` objects which it may depend on; this is
  2729. equivalent to invoking the :meth:`_orm.registry.configure` method
  2730. on a particular :class:`_orm.registry`.
  2731. By contrast, the :func:`_orm.configure_mappers` function will invoke the
  2732. configuration process on all :class:`_orm.registry` objects that
  2733. exist in memory, and may be useful for scenarios where many individual
  2734. :class:`_orm.registry` objects that are nonetheless interrelated are
  2735. in use.
  2736. .. versionchanged:: 1.4
  2737. As of SQLAlchemy 1.4.0b2, this function works on a
  2738. per-:class:`_orm.registry` basis, locating all :class:`_orm.registry`
  2739. objects present and invoking the :meth:`_orm.registry.configure` method
  2740. on each. The :meth:`_orm.registry.configure` method may be preferred to
  2741. limit the configuration of mappers to those local to a particular
  2742. :class:`_orm.registry` and/or declarative base class.
  2743. Points at which automatic configuration is invoked include when a mapped
  2744. class is instantiated into an instance, as well as when ORM queries
  2745. are emitted using :meth:`.Session.query` or :meth:`_orm.Session.execute`
  2746. with an ORM-enabled statement.
  2747. The mapper configure process, whether invoked by
  2748. :func:`_orm.configure_mappers` or from :meth:`_orm.registry.configure`,
  2749. provides several event hooks that can be used to augment the mapper
  2750. configuration step. These hooks include:
  2751. * :meth:`.MapperEvents.before_configured` - called once before
  2752. :func:`.configure_mappers` or :meth:`_orm.registry.configure` does any
  2753. work; this can be used to establish additional options, properties, or
  2754. related mappings before the operation proceeds.
  2755. * :meth:`.MapperEvents.mapper_configured` - called as each individual
  2756. :class:`_orm.Mapper` is configured within the process; will include all
  2757. mapper state except for backrefs set up by other mappers that are still
  2758. to be configured.
  2759. * :meth:`.MapperEvents.after_configured` - called once after
  2760. :func:`.configure_mappers` or :meth:`_orm.registry.configure` is
  2761. complete; at this stage, all :class:`_orm.Mapper` objects that fall
  2762. within the scope of the configuration operation will be fully configured.
  2763. Note that the calling application may still have other mappings that
  2764. haven't been produced yet, such as if they are in modules as yet
  2765. unimported, and may also have mappings that are still to be configured,
  2766. if they are in other :class:`_orm.registry` collections not part of the
  2767. current scope of configuration.
  2768. """
  2769. _configure_registries(_all_registries(), cascade=True)
  2770. def _configure_registries(registries, cascade):
  2771. for reg in registries:
  2772. if reg._new_mappers:
  2773. break
  2774. else:
  2775. return
  2776. with _CONFIGURE_MUTEX:
  2777. global _already_compiling
  2778. if _already_compiling:
  2779. return
  2780. _already_compiling = True
  2781. try:
  2782. # double-check inside mutex
  2783. for reg in registries:
  2784. if reg._new_mappers:
  2785. break
  2786. else:
  2787. return
  2788. Mapper.dispatch._for_class(Mapper).before_configured()
  2789. # initialize properties on all mappers
  2790. # note that _mapper_registry is unordered, which
  2791. # may randomly conceal/reveal issues related to
  2792. # the order of mapper compilation
  2793. _do_configure_registries(registries, cascade)
  2794. finally:
  2795. _already_compiling = False
  2796. Mapper.dispatch._for_class(Mapper).after_configured()
  2797. @util.preload_module("sqlalchemy.orm.decl_api")
  2798. def _do_configure_registries(registries, cascade):
  2799. registry = util.preloaded.orm_decl_api.registry
  2800. orig = set(registries)
  2801. for reg in registry._recurse_with_dependencies(registries):
  2802. has_skip = False
  2803. for mapper in reg._mappers_to_configure():
  2804. run_configure = None
  2805. for fn in mapper.dispatch.before_mapper_configured:
  2806. run_configure = fn(mapper, mapper.class_)
  2807. if run_configure is EXT_SKIP:
  2808. has_skip = True
  2809. break
  2810. if run_configure is EXT_SKIP:
  2811. continue
  2812. if getattr(mapper, "_configure_failed", False):
  2813. e = sa_exc.InvalidRequestError(
  2814. "One or more mappers failed to initialize - "
  2815. "can't proceed with initialization of other "
  2816. "mappers. Triggering mapper: '%s'. "
  2817. "Original exception was: %s"
  2818. % (mapper, mapper._configure_failed)
  2819. )
  2820. e._configure_failed = mapper._configure_failed
  2821. raise e
  2822. if not mapper.configured:
  2823. try:
  2824. mapper._post_configure_properties()
  2825. mapper._expire_memoizations()
  2826. mapper.dispatch.mapper_configured(mapper, mapper.class_)
  2827. except Exception:
  2828. exc = sys.exc_info()[1]
  2829. if not hasattr(exc, "_configure_failed"):
  2830. mapper._configure_failed = exc
  2831. raise
  2832. if not has_skip:
  2833. reg._new_mappers = False
  2834. if not cascade and reg._dependencies.difference(orig):
  2835. raise sa_exc.InvalidRequestError(
  2836. "configure was called with cascade=False but "
  2837. "additional registries remain"
  2838. )
  2839. @util.preload_module("sqlalchemy.orm.decl_api")
  2840. def _dispose_registries(registries, cascade):
  2841. registry = util.preloaded.orm_decl_api.registry
  2842. orig = set(registries)
  2843. for reg in registry._recurse_with_dependents(registries):
  2844. if not cascade and reg._dependents.difference(orig):
  2845. raise sa_exc.InvalidRequestError(
  2846. "Registry has dependent registries that are not disposed; "
  2847. "pass cascade=True to clear these also"
  2848. )
  2849. while reg._managers:
  2850. try:
  2851. manager, _ = reg._managers.popitem()
  2852. except KeyError:
  2853. # guard against race between while and popitem
  2854. pass
  2855. else:
  2856. reg._dispose_manager_and_mapper(manager)
  2857. reg._non_primary_mappers.clear()
  2858. reg._dependents.clear()
  2859. for dep in reg._dependencies:
  2860. dep._dependents.discard(reg)
  2861. reg._dependencies.clear()
  2862. # this wasn't done in the 1.3 clear_mappers() and in fact it
  2863. # was a bug, as it could cause configure_mappers() to invoke
  2864. # the "before_configured" event even though mappers had all been
  2865. # disposed.
  2866. reg._new_mappers = False
  2867. def reconstructor(fn):
  2868. """Decorate a method as the 'reconstructor' hook.
  2869. Designates a method as the "reconstructor", an ``__init__``-like
  2870. method that will be called by the ORM after the instance has been
  2871. loaded from the database or otherwise reconstituted.
  2872. The reconstructor will be invoked with no arguments. Scalar
  2873. (non-collection) database-mapped attributes of the instance will
  2874. be available for use within the function. Eagerly-loaded
  2875. collections are generally not yet available and will usually only
  2876. contain the first element. ORM state changes made to objects at
  2877. this stage will not be recorded for the next flush() operation, so
  2878. the activity within a reconstructor should be conservative.
  2879. .. seealso::
  2880. :ref:`mapping_constructors`
  2881. :meth:`.InstanceEvents.load`
  2882. """
  2883. fn.__sa_reconstructor__ = True
  2884. return fn
  2885. def validates(*names, **kw):
  2886. r"""Decorate a method as a 'validator' for one or more named properties.
  2887. Designates a method as a validator, a method which receives the
  2888. name of the attribute as well as a value to be assigned, or in the
  2889. case of a collection, the value to be added to the collection.
  2890. The function can then raise validation exceptions to halt the
  2891. process from continuing (where Python's built-in ``ValueError``
  2892. and ``AssertionError`` exceptions are reasonable choices), or can
  2893. modify or replace the value before proceeding. The function should
  2894. otherwise return the given value.
  2895. Note that a validator for a collection **cannot** issue a load of that
  2896. collection within the validation routine - this usage raises
  2897. an assertion to avoid recursion overflows. This is a reentrant
  2898. condition which is not supported.
  2899. :param \*names: list of attribute names to be validated.
  2900. :param include_removes: if True, "remove" events will be
  2901. sent as well - the validation function must accept an additional
  2902. argument "is_remove" which will be a boolean.
  2903. :param include_backrefs: defaults to ``True``; if ``False``, the
  2904. validation function will not emit if the originator is an attribute
  2905. event related via a backref. This can be used for bi-directional
  2906. :func:`.validates` usage where only one validator should emit per
  2907. attribute operation.
  2908. .. versionadded:: 0.9.0
  2909. .. seealso::
  2910. :ref:`simple_validators` - usage examples for :func:`.validates`
  2911. """
  2912. include_removes = kw.pop("include_removes", False)
  2913. include_backrefs = kw.pop("include_backrefs", True)
  2914. def wrap(fn):
  2915. fn.__sa_validators__ = names
  2916. fn.__sa_validation_opts__ = {
  2917. "include_removes": include_removes,
  2918. "include_backrefs": include_backrefs,
  2919. }
  2920. return fn
  2921. return wrap
  2922. def _event_on_load(state, ctx):
  2923. instrumenting_mapper = state.manager.mapper
  2924. if instrumenting_mapper._reconstructor:
  2925. instrumenting_mapper._reconstructor(state.obj())
  2926. def _event_on_init(state, args, kwargs):
  2927. """Run init_instance hooks.
  2928. This also includes mapper compilation, normally not needed
  2929. here but helps with some piecemeal configuration
  2930. scenarios (such as in the ORM tutorial).
  2931. """
  2932. instrumenting_mapper = state.manager.mapper
  2933. if instrumenting_mapper:
  2934. instrumenting_mapper._check_configure()
  2935. if instrumenting_mapper._set_polymorphic_identity:
  2936. instrumenting_mapper._set_polymorphic_identity(state)
  2937. class _ColumnMapping(dict):
  2938. """Error reporting helper for mapper._columntoproperty."""
  2939. __slots__ = ("mapper",)
  2940. def __init__(self, mapper):
  2941. self.mapper = mapper
  2942. def __missing__(self, column):
  2943. prop = self.mapper._props.get(column)
  2944. if prop:
  2945. raise orm_exc.UnmappedColumnError(
  2946. "Column '%s.%s' is not available, due to "
  2947. "conflicting property '%s':%r"
  2948. % (column.table.name, column.name, column.key, prop)
  2949. )
  2950. raise orm_exc.UnmappedColumnError(
  2951. "No column %s is configured on mapper %s..."
  2952. % (column, self.mapper)
  2953. )