properties.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. # orm/properties.py
  2. # Copyright (C) 2005-2024 the SQLAlchemy authors and contributors
  3. # <see AUTHORS file>
  4. #
  5. # This module is part of SQLAlchemy and is released under
  6. # the MIT License: https://www.opensource.org/licenses/mit-license.php
  7. """MapperProperty implementations.
  8. This is a private module which defines the behavior of individual ORM-
  9. mapped attributes.
  10. """
  11. from __future__ import absolute_import
  12. from . import attributes
  13. from .descriptor_props import CompositeProperty
  14. from .descriptor_props import ConcreteInheritedProperty
  15. from .descriptor_props import SynonymProperty
  16. from .interfaces import PropComparator
  17. from .interfaces import StrategizedProperty
  18. from .relationships import RelationshipProperty
  19. from .. import log
  20. from .. import util
  21. from ..sql import coercions
  22. from ..sql import roles
  23. __all__ = [
  24. "ColumnProperty",
  25. "CompositeProperty",
  26. "ConcreteInheritedProperty",
  27. "RelationshipProperty",
  28. "SynonymProperty",
  29. ]
  30. @log.class_logger
  31. class ColumnProperty(StrategizedProperty):
  32. """Describes an object attribute that corresponds to a table column.
  33. Public constructor is the :func:`_orm.column_property` function.
  34. """
  35. strategy_wildcard_key = "column"
  36. inherit_cache = True
  37. _links_to_entity = False
  38. __slots__ = (
  39. "columns",
  40. "group",
  41. "deferred",
  42. "instrument",
  43. "comparator_factory",
  44. "descriptor",
  45. "active_history",
  46. "expire_on_flush",
  47. "info",
  48. "doc",
  49. "strategy_key",
  50. "_creation_order",
  51. "_is_polymorphic_discriminator",
  52. "_mapped_by_synonym",
  53. "_deferred_column_loader",
  54. "_raise_column_loader",
  55. "_renders_in_subqueries",
  56. "raiseload",
  57. )
  58. def __init__(self, *columns, **kwargs):
  59. r"""Provide a column-level property for use with a mapping.
  60. Column-based properties can normally be applied to the mapper's
  61. ``properties`` dictionary using the :class:`_schema.Column`
  62. element directly.
  63. Use this function when the given column is not directly present within
  64. the mapper's selectable; examples include SQL expressions, functions,
  65. and scalar SELECT queries.
  66. The :func:`_orm.column_property` function returns an instance of
  67. :class:`.ColumnProperty`.
  68. Columns that aren't present in the mapper's selectable won't be
  69. persisted by the mapper and are effectively "read-only" attributes.
  70. :param \*cols:
  71. list of Column objects to be mapped.
  72. :param active_history=False:
  73. When ``True``, indicates that the "previous" value for a
  74. scalar attribute should be loaded when replaced, if not
  75. already loaded. Normally, history tracking logic for
  76. simple non-primary-key scalar values only needs to be
  77. aware of the "new" value in order to perform a flush. This
  78. flag is available for applications that make use of
  79. :func:`.attributes.get_history` or :meth:`.Session.is_modified`
  80. which also need to know
  81. the "previous" value of the attribute.
  82. :param comparator_factory: a class which extends
  83. :class:`.ColumnProperty.Comparator` which provides custom SQL
  84. clause generation for comparison operations.
  85. :param group:
  86. a group name for this property when marked as deferred.
  87. :param deferred:
  88. when True, the column property is "deferred", meaning that
  89. it does not load immediately, and is instead loaded when the
  90. attribute is first accessed on an instance. See also
  91. :func:`~sqlalchemy.orm.deferred`.
  92. :param doc:
  93. optional string that will be applied as the doc on the
  94. class-bound descriptor.
  95. :param expire_on_flush=True:
  96. Disable expiry on flush. A column_property() which refers
  97. to a SQL expression (and not a single table-bound column)
  98. is considered to be a "read only" property; populating it
  99. has no effect on the state of data, and it can only return
  100. database state. For this reason a column_property()'s value
  101. is expired whenever the parent object is involved in a
  102. flush, that is, has any kind of "dirty" state within a flush.
  103. Setting this parameter to ``False`` will have the effect of
  104. leaving any existing value present after the flush proceeds.
  105. Note however that the :class:`.Session` with default expiration
  106. settings still expires
  107. all attributes after a :meth:`.Session.commit` call, however.
  108. :param info: Optional data dictionary which will be populated into the
  109. :attr:`.MapperProperty.info` attribute of this object.
  110. :param raiseload: if True, indicates the column should raise an error
  111. when undeferred, rather than loading the value. This can be
  112. altered at query time by using the :func:`.deferred` option with
  113. raiseload=False.
  114. .. versionadded:: 1.4
  115. .. seealso::
  116. :ref:`deferred_raiseload`
  117. .. seealso::
  118. :ref:`column_property_options` - to map columns while including
  119. mapping options
  120. :ref:`mapper_column_property_sql_expressions` - to map SQL
  121. expressions
  122. """
  123. super(ColumnProperty, self).__init__()
  124. self.columns = [
  125. coercions.expect(roles.LabeledColumnExprRole, c) for c in columns
  126. ]
  127. self.group = kwargs.pop("group", None)
  128. self.deferred = kwargs.pop("deferred", False)
  129. self.raiseload = kwargs.pop("raiseload", False)
  130. self.instrument = kwargs.pop("_instrument", True)
  131. self.comparator_factory = kwargs.pop(
  132. "comparator_factory", self.__class__.Comparator
  133. )
  134. self.descriptor = kwargs.pop("descriptor", None)
  135. self.active_history = kwargs.pop("active_history", False)
  136. self.expire_on_flush = kwargs.pop("expire_on_flush", True)
  137. if "info" in kwargs:
  138. self.info = kwargs.pop("info")
  139. if "doc" in kwargs:
  140. self.doc = kwargs.pop("doc")
  141. else:
  142. for col in reversed(self.columns):
  143. doc = getattr(col, "doc", None)
  144. if doc is not None:
  145. self.doc = doc
  146. break
  147. else:
  148. self.doc = None
  149. if kwargs:
  150. raise TypeError(
  151. "%s received unexpected keyword argument(s): %s"
  152. % (self.__class__.__name__, ", ".join(sorted(kwargs.keys())))
  153. )
  154. util.set_creation_order(self)
  155. self.strategy_key = (
  156. ("deferred", self.deferred),
  157. ("instrument", self.instrument),
  158. )
  159. if self.raiseload:
  160. self.strategy_key += (("raiseload", True),)
  161. def _memoized_attr__renders_in_subqueries(self):
  162. if ("query_expression", True) in self.strategy_key:
  163. return self.strategy._have_default_expression
  164. return ("deferred", True) not in self.strategy_key or (
  165. self not in self.parent._readonly_props
  166. )
  167. @util.preload_module("sqlalchemy.orm.state", "sqlalchemy.orm.strategies")
  168. def _memoized_attr__deferred_column_loader(self):
  169. state = util.preloaded.orm_state
  170. strategies = util.preloaded.orm_strategies
  171. return state.InstanceState._instance_level_callable_processor(
  172. self.parent.class_manager,
  173. strategies.LoadDeferredColumns(self.key),
  174. self.key,
  175. )
  176. @util.preload_module("sqlalchemy.orm.state", "sqlalchemy.orm.strategies")
  177. def _memoized_attr__raise_column_loader(self):
  178. state = util.preloaded.orm_state
  179. strategies = util.preloaded.orm_strategies
  180. return state.InstanceState._instance_level_callable_processor(
  181. self.parent.class_manager,
  182. strategies.LoadDeferredColumns(self.key, True),
  183. self.key,
  184. )
  185. def __clause_element__(self):
  186. """Allow the ColumnProperty to work in expression before it is turned
  187. into an instrumented attribute.
  188. """
  189. return self.expression
  190. @property
  191. def expression(self):
  192. """Return the primary column or expression for this ColumnProperty.
  193. E.g.::
  194. class File(Base):
  195. # ...
  196. name = Column(String(64))
  197. extension = Column(String(8))
  198. filename = column_property(name + '.' + extension)
  199. path = column_property('C:/' + filename.expression)
  200. .. seealso::
  201. :ref:`mapper_column_property_sql_expressions_composed`
  202. """
  203. return self.columns[0]
  204. def instrument_class(self, mapper):
  205. if not self.instrument:
  206. return
  207. attributes.register_descriptor(
  208. mapper.class_,
  209. self.key,
  210. comparator=self.comparator_factory(self, mapper),
  211. parententity=mapper,
  212. doc=self.doc,
  213. )
  214. def do_init(self):
  215. super(ColumnProperty, self).do_init()
  216. if len(self.columns) > 1 and set(self.parent.primary_key).issuperset(
  217. self.columns
  218. ):
  219. util.warn(
  220. (
  221. "On mapper %s, primary key column '%s' is being combined "
  222. "with distinct primary key column '%s' in attribute '%s'. "
  223. "Use explicit properties to give each column its own "
  224. "mapped attribute name."
  225. )
  226. % (self.parent, self.columns[1], self.columns[0], self.key)
  227. )
  228. def copy(self):
  229. return ColumnProperty(
  230. deferred=self.deferred,
  231. group=self.group,
  232. active_history=self.active_history,
  233. *self.columns
  234. )
  235. def _getcommitted(
  236. self, state, dict_, column, passive=attributes.PASSIVE_OFF
  237. ):
  238. return state.get_impl(self.key).get_committed_value(
  239. state, dict_, passive=passive
  240. )
  241. def merge(
  242. self,
  243. session,
  244. source_state,
  245. source_dict,
  246. dest_state,
  247. dest_dict,
  248. load,
  249. _recursive,
  250. _resolve_conflict_map,
  251. ):
  252. if not self.instrument:
  253. return
  254. elif self.key in source_dict:
  255. value = source_dict[self.key]
  256. if not load:
  257. dest_dict[self.key] = value
  258. else:
  259. impl = dest_state.get_impl(self.key)
  260. impl.set(dest_state, dest_dict, value, None)
  261. elif dest_state.has_identity and self.key not in dest_dict:
  262. dest_state._expire_attributes(
  263. dest_dict, [self.key], no_loader=True
  264. )
  265. class Comparator(util.MemoizedSlots, PropComparator):
  266. """Produce boolean, comparison, and other operators for
  267. :class:`.ColumnProperty` attributes.
  268. See the documentation for :class:`.PropComparator` for a brief
  269. overview.
  270. .. seealso::
  271. :class:`.PropComparator`
  272. :class:`.ColumnOperators`
  273. :ref:`types_operators`
  274. :attr:`.TypeEngine.comparator_factory`
  275. """
  276. __slots__ = "__clause_element__", "info", "expressions"
  277. def _orm_annotate_column(self, column):
  278. """annotate and possibly adapt a column to be returned
  279. as the mapped-attribute exposed version of the column.
  280. The column in this context needs to act as much like the
  281. column in an ORM mapped context as possible, so includes
  282. annotations to give hints to various ORM functions as to
  283. the source entity of this column. It also adapts it
  284. to the mapper's with_polymorphic selectable if one is
  285. present.
  286. """
  287. pe = self._parententity
  288. annotations = {
  289. "entity_namespace": pe,
  290. "parententity": pe,
  291. "parentmapper": pe,
  292. "proxy_key": self.prop.key,
  293. }
  294. col = column
  295. # for a mapper with polymorphic_on and an adapter, return
  296. # the column against the polymorphic selectable.
  297. # see also orm.util._orm_downgrade_polymorphic_columns
  298. # for the reverse operation.
  299. if self._parentmapper._polymorphic_adapter:
  300. mapper_local_col = col
  301. col = self._parentmapper._polymorphic_adapter.traverse(col)
  302. # this is a clue to the ORM Query etc. that this column
  303. # was adapted to the mapper's polymorphic_adapter. the
  304. # ORM uses this hint to know which column its adapting.
  305. annotations["adapt_column"] = mapper_local_col
  306. return col._annotate(annotations)._set_propagate_attrs(
  307. {"compile_state_plugin": "orm", "plugin_subject": pe}
  308. )
  309. def _memoized_method___clause_element__(self):
  310. if self.adapter:
  311. return self.adapter(self.prop.columns[0], self.prop.key)
  312. else:
  313. return self._orm_annotate_column(self.prop.columns[0])
  314. def _memoized_attr_info(self):
  315. """The .info dictionary for this attribute."""
  316. ce = self.__clause_element__()
  317. try:
  318. return ce.info
  319. except AttributeError:
  320. return self.prop.info
  321. def _memoized_attr_expressions(self):
  322. """The full sequence of columns referenced by this
  323. attribute, adjusted for any aliasing in progress.
  324. .. versionadded:: 1.3.17
  325. """
  326. if self.adapter:
  327. return [
  328. self.adapter(col, self.prop.key)
  329. for col in self.prop.columns
  330. ]
  331. else:
  332. return [
  333. self._orm_annotate_column(col) for col in self.prop.columns
  334. ]
  335. def _fallback_getattr(self, key):
  336. """proxy attribute access down to the mapped column.
  337. this allows user-defined comparison methods to be accessed.
  338. """
  339. return getattr(self.__clause_element__(), key)
  340. def operate(self, op, *other, **kwargs):
  341. return op(self.__clause_element__(), *other, **kwargs)
  342. def reverse_operate(self, op, other, **kwargs):
  343. col = self.__clause_element__()
  344. return op(col._bind_param(op, other), col, **kwargs)
  345. def __str__(self):
  346. return str(self.parent.class_.__name__) + "." + self.key