strategy_options.py 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530
  1. # orm/strategy_options.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. # mypy: allow-untyped-defs, allow-untyped-calls
  8. """
  9. """
  10. from __future__ import annotations
  11. import typing
  12. from typing import Any
  13. from typing import Callable
  14. from typing import cast
  15. from typing import Dict
  16. from typing import Iterable
  17. from typing import Optional
  18. from typing import overload
  19. from typing import Sequence
  20. from typing import Tuple
  21. from typing import Type
  22. from typing import TypeVar
  23. from typing import Union
  24. from . import util as orm_util
  25. from ._typing import insp_is_aliased_class
  26. from ._typing import insp_is_attribute
  27. from ._typing import insp_is_mapper
  28. from ._typing import insp_is_mapper_property
  29. from .attributes import QueryableAttribute
  30. from .base import InspectionAttr
  31. from .interfaces import LoaderOption
  32. from .path_registry import _DEFAULT_TOKEN
  33. from .path_registry import _StrPathToken
  34. from .path_registry import _WILDCARD_TOKEN
  35. from .path_registry import AbstractEntityRegistry
  36. from .path_registry import path_is_property
  37. from .path_registry import PathRegistry
  38. from .path_registry import TokenRegistry
  39. from .util import _orm_full_deannotate
  40. from .util import AliasedInsp
  41. from .. import exc as sa_exc
  42. from .. import inspect
  43. from .. import util
  44. from ..sql import and_
  45. from ..sql import cache_key
  46. from ..sql import coercions
  47. from ..sql import roles
  48. from ..sql import traversals
  49. from ..sql import visitors
  50. from ..sql.base import _generative
  51. from ..util.typing import Final
  52. from ..util.typing import Literal
  53. from ..util.typing import Self
  54. _RELATIONSHIP_TOKEN: Final[Literal["relationship"]] = "relationship"
  55. _COLUMN_TOKEN: Final[Literal["column"]] = "column"
  56. _FN = TypeVar("_FN", bound="Callable[..., Any]")
  57. if typing.TYPE_CHECKING:
  58. from ._typing import _EntityType
  59. from ._typing import _InternalEntityType
  60. from .context import _MapperEntity
  61. from .context import ORMCompileState
  62. from .context import QueryContext
  63. from .interfaces import _StrategyKey
  64. from .interfaces import MapperProperty
  65. from .interfaces import ORMOption
  66. from .mapper import Mapper
  67. from .path_registry import _PathRepresentation
  68. from ..sql._typing import _ColumnExpressionArgument
  69. from ..sql._typing import _FromClauseArgument
  70. from ..sql.cache_key import _CacheKeyTraversalType
  71. from ..sql.cache_key import CacheKey
  72. _AttrType = Union[Literal["*"], "QueryableAttribute[Any]"]
  73. _WildcardKeyType = Literal["relationship", "column"]
  74. _StrategySpec = Dict[str, Any]
  75. _OptsType = Dict[str, Any]
  76. _AttrGroupType = Tuple[_AttrType, ...]
  77. class _AbstractLoad(traversals.GenerativeOnTraversal, LoaderOption):
  78. __slots__ = ("propagate_to_loaders",)
  79. _is_strategy_option = True
  80. propagate_to_loaders: bool
  81. def contains_eager(
  82. self,
  83. attr: _AttrType,
  84. alias: Optional[_FromClauseArgument] = None,
  85. _is_chain: bool = False,
  86. ) -> Self:
  87. r"""Indicate that the given attribute should be eagerly loaded from
  88. columns stated manually in the query.
  89. This function is part of the :class:`_orm.Load` interface and supports
  90. both method-chained and standalone operation.
  91. The option is used in conjunction with an explicit join that loads
  92. the desired rows, i.e.::
  93. sess.query(Order).\
  94. join(Order.user).\
  95. options(contains_eager(Order.user))
  96. The above query would join from the ``Order`` entity to its related
  97. ``User`` entity, and the returned ``Order`` objects would have the
  98. ``Order.user`` attribute pre-populated.
  99. It may also be used for customizing the entries in an eagerly loaded
  100. collection; queries will normally want to use the
  101. :ref:`orm_queryguide_populate_existing` execution option assuming the
  102. primary collection of parent objects may already have been loaded::
  103. sess.query(User).\
  104. join(User.addresses).\
  105. filter(Address.email_address.like('%@aol.com')).\
  106. options(contains_eager(User.addresses)).\
  107. populate_existing()
  108. See the section :ref:`contains_eager` for complete usage details.
  109. .. seealso::
  110. :ref:`loading_toplevel`
  111. :ref:`contains_eager`
  112. """
  113. if alias is not None:
  114. if not isinstance(alias, str):
  115. coerced_alias = coercions.expect(roles.FromClauseRole, alias)
  116. else:
  117. util.warn_deprecated(
  118. "Passing a string name for the 'alias' argument to "
  119. "'contains_eager()` is deprecated, and will not work in a "
  120. "future release. Please use a sqlalchemy.alias() or "
  121. "sqlalchemy.orm.aliased() construct.",
  122. version="1.4",
  123. )
  124. coerced_alias = alias
  125. elif getattr(attr, "_of_type", None):
  126. assert isinstance(attr, QueryableAttribute)
  127. ot: Optional[_InternalEntityType[Any]] = inspect(attr._of_type)
  128. assert ot is not None
  129. coerced_alias = ot.selectable
  130. else:
  131. coerced_alias = None
  132. cloned = self._set_relationship_strategy(
  133. attr,
  134. {"lazy": "joined"},
  135. propagate_to_loaders=False,
  136. opts={"eager_from_alias": coerced_alias},
  137. _reconcile_to_other=True if _is_chain else None,
  138. )
  139. return cloned
  140. def load_only(self, *attrs: _AttrType, raiseload: bool = False) -> Self:
  141. r"""Indicate that for a particular entity, only the given list
  142. of column-based attribute names should be loaded; all others will be
  143. deferred.
  144. This function is part of the :class:`_orm.Load` interface and supports
  145. both method-chained and standalone operation.
  146. Example - given a class ``User``, load only the ``name`` and
  147. ``fullname`` attributes::
  148. session.query(User).options(load_only(User.name, User.fullname))
  149. Example - given a relationship ``User.addresses -> Address``, specify
  150. subquery loading for the ``User.addresses`` collection, but on each
  151. ``Address`` object load only the ``email_address`` attribute::
  152. session.query(User).options(
  153. subqueryload(User.addresses).load_only(Address.email_address)
  154. )
  155. For a statement that has multiple entities,
  156. the lead entity can be
  157. specifically referred to using the :class:`_orm.Load` constructor::
  158. stmt = select(User, Address).join(User.addresses).options(
  159. Load(User).load_only(User.name, User.fullname),
  160. Load(Address).load_only(Address.email_address)
  161. )
  162. :param \*attrs: Attributes to be loaded, all others will be deferred.
  163. :param raiseload: raise :class:`.InvalidRequestError` rather than
  164. lazy loading a value when a deferred attribute is accessed. Used
  165. to prevent unwanted SQL from being emitted.
  166. .. versionadded:: 2.0
  167. .. seealso::
  168. :ref:`orm_queryguide_column_deferral` - in the
  169. :ref:`queryguide_toplevel`
  170. :param \*attrs: Attributes to be loaded, all others will be deferred.
  171. :param raiseload: raise :class:`.InvalidRequestError` rather than
  172. lazy loading a value when a deferred attribute is accessed. Used
  173. to prevent unwanted SQL from being emitted.
  174. .. versionadded:: 2.0
  175. """
  176. cloned = self._set_column_strategy(
  177. attrs,
  178. {"deferred": False, "instrument": True},
  179. )
  180. wildcard_strategy = {"deferred": True, "instrument": True}
  181. if raiseload:
  182. wildcard_strategy["raiseload"] = True
  183. cloned = cloned._set_column_strategy(
  184. ("*",),
  185. wildcard_strategy,
  186. )
  187. return cloned
  188. def joinedload(
  189. self,
  190. attr: _AttrType,
  191. innerjoin: Optional[bool] = None,
  192. ) -> Self:
  193. """Indicate that the given attribute should be loaded using joined
  194. eager loading.
  195. This function is part of the :class:`_orm.Load` interface and supports
  196. both method-chained and standalone operation.
  197. examples::
  198. # joined-load the "orders" collection on "User"
  199. query(User).options(joinedload(User.orders))
  200. # joined-load Order.items and then Item.keywords
  201. query(Order).options(
  202. joinedload(Order.items).joinedload(Item.keywords))
  203. # lazily load Order.items, but when Items are loaded,
  204. # joined-load the keywords collection
  205. query(Order).options(
  206. lazyload(Order.items).joinedload(Item.keywords))
  207. :param innerjoin: if ``True``, indicates that the joined eager load
  208. should use an inner join instead of the default of left outer join::
  209. query(Order).options(joinedload(Order.user, innerjoin=True))
  210. In order to chain multiple eager joins together where some may be
  211. OUTER and others INNER, right-nested joins are used to link them::
  212. query(A).options(
  213. joinedload(A.bs, innerjoin=False).
  214. joinedload(B.cs, innerjoin=True)
  215. )
  216. The above query, linking A.bs via "outer" join and B.cs via "inner"
  217. join would render the joins as "a LEFT OUTER JOIN (b JOIN c)". When
  218. using older versions of SQLite (< 3.7.16), this form of JOIN is
  219. translated to use full subqueries as this syntax is otherwise not
  220. directly supported.
  221. The ``innerjoin`` flag can also be stated with the term ``"unnested"``.
  222. This indicates that an INNER JOIN should be used, *unless* the join
  223. is linked to a LEFT OUTER JOIN to the left, in which case it
  224. will render as LEFT OUTER JOIN. For example, supposing ``A.bs``
  225. is an outerjoin::
  226. query(A).options(
  227. joinedload(A.bs).
  228. joinedload(B.cs, innerjoin="unnested")
  229. )
  230. The above join will render as "a LEFT OUTER JOIN b LEFT OUTER JOIN c",
  231. rather than as "a LEFT OUTER JOIN (b JOIN c)".
  232. .. note:: The "unnested" flag does **not** affect the JOIN rendered
  233. from a many-to-many association table, e.g. a table configured as
  234. :paramref:`_orm.relationship.secondary`, to the target table; for
  235. correctness of results, these joins are always INNER and are
  236. therefore right-nested if linked to an OUTER join.
  237. .. note::
  238. The joins produced by :func:`_orm.joinedload` are **anonymously
  239. aliased**. The criteria by which the join proceeds cannot be
  240. modified, nor can the ORM-enabled :class:`_sql.Select` or legacy
  241. :class:`_query.Query` refer to these joins in any way, including
  242. ordering. See :ref:`zen_of_eager_loading` for further detail.
  243. To produce a specific SQL JOIN which is explicitly available, use
  244. :meth:`_sql.Select.join` and :meth:`_query.Query.join`. To combine
  245. explicit JOINs with eager loading of collections, use
  246. :func:`_orm.contains_eager`; see :ref:`contains_eager`.
  247. .. seealso::
  248. :ref:`loading_toplevel`
  249. :ref:`joined_eager_loading`
  250. """
  251. loader = self._set_relationship_strategy(
  252. attr,
  253. {"lazy": "joined"},
  254. opts={"innerjoin": innerjoin}
  255. if innerjoin is not None
  256. else util.EMPTY_DICT,
  257. )
  258. return loader
  259. def subqueryload(self, attr: _AttrType) -> Self:
  260. """Indicate that the given attribute should be loaded using
  261. subquery eager loading.
  262. This function is part of the :class:`_orm.Load` interface and supports
  263. both method-chained and standalone operation.
  264. examples::
  265. # subquery-load the "orders" collection on "User"
  266. query(User).options(subqueryload(User.orders))
  267. # subquery-load Order.items and then Item.keywords
  268. query(Order).options(
  269. subqueryload(Order.items).subqueryload(Item.keywords))
  270. # lazily load Order.items, but when Items are loaded,
  271. # subquery-load the keywords collection
  272. query(Order).options(
  273. lazyload(Order.items).subqueryload(Item.keywords))
  274. .. seealso::
  275. :ref:`loading_toplevel`
  276. :ref:`subquery_eager_loading`
  277. """
  278. return self._set_relationship_strategy(attr, {"lazy": "subquery"})
  279. def selectinload(
  280. self,
  281. attr: _AttrType,
  282. recursion_depth: Optional[int] = None,
  283. ) -> Self:
  284. """Indicate that the given attribute should be loaded using
  285. SELECT IN eager loading.
  286. This function is part of the :class:`_orm.Load` interface and supports
  287. both method-chained and standalone operation.
  288. examples::
  289. # selectin-load the "orders" collection on "User"
  290. query(User).options(selectinload(User.orders))
  291. # selectin-load Order.items and then Item.keywords
  292. query(Order).options(
  293. selectinload(Order.items).selectinload(Item.keywords))
  294. # lazily load Order.items, but when Items are loaded,
  295. # selectin-load the keywords collection
  296. query(Order).options(
  297. lazyload(Order.items).selectinload(Item.keywords))
  298. :param recursion_depth: optional int; when set to a positive integer
  299. in conjunction with a self-referential relationship,
  300. indicates "selectin" loading will continue that many levels deep
  301. automatically until no items are found.
  302. .. note:: The :paramref:`_orm.selectinload.recursion_depth` option
  303. currently supports only self-referential relationships. There
  304. is not yet an option to automatically traverse recursive structures
  305. with more than one relationship involved.
  306. Additionally, the :paramref:`_orm.selectinload.recursion_depth`
  307. parameter is new and experimental and should be treated as "alpha"
  308. status for the 2.0 series.
  309. .. versionadded:: 2.0 added
  310. :paramref:`_orm.selectinload.recursion_depth`
  311. .. seealso::
  312. :ref:`loading_toplevel`
  313. :ref:`selectin_eager_loading`
  314. """
  315. return self._set_relationship_strategy(
  316. attr,
  317. {"lazy": "selectin"},
  318. opts={"recursion_depth": recursion_depth},
  319. )
  320. def lazyload(self, attr: _AttrType) -> Self:
  321. """Indicate that the given attribute should be loaded using "lazy"
  322. loading.
  323. This function is part of the :class:`_orm.Load` interface and supports
  324. both method-chained and standalone operation.
  325. .. seealso::
  326. :ref:`loading_toplevel`
  327. :ref:`lazy_loading`
  328. """
  329. return self._set_relationship_strategy(attr, {"lazy": "select"})
  330. def immediateload(
  331. self,
  332. attr: _AttrType,
  333. recursion_depth: Optional[int] = None,
  334. ) -> Self:
  335. """Indicate that the given attribute should be loaded using
  336. an immediate load with a per-attribute SELECT statement.
  337. The load is achieved using the "lazyloader" strategy and does not
  338. fire off any additional eager loaders.
  339. The :func:`.immediateload` option is superseded in general
  340. by the :func:`.selectinload` option, which performs the same task
  341. more efficiently by emitting a SELECT for all loaded objects.
  342. This function is part of the :class:`_orm.Load` interface and supports
  343. both method-chained and standalone operation.
  344. :param recursion_depth: optional int; when set to a positive integer
  345. in conjunction with a self-referential relationship,
  346. indicates "selectin" loading will continue that many levels deep
  347. automatically until no items are found.
  348. .. note:: The :paramref:`_orm.immediateload.recursion_depth` option
  349. currently supports only self-referential relationships. There
  350. is not yet an option to automatically traverse recursive structures
  351. with more than one relationship involved.
  352. .. warning:: This parameter is new and experimental and should be
  353. treated as "alpha" status
  354. .. versionadded:: 2.0 added
  355. :paramref:`_orm.immediateload.recursion_depth`
  356. .. seealso::
  357. :ref:`loading_toplevel`
  358. :ref:`selectin_eager_loading`
  359. """
  360. loader = self._set_relationship_strategy(
  361. attr,
  362. {"lazy": "immediate"},
  363. opts={"recursion_depth": recursion_depth},
  364. )
  365. return loader
  366. def noload(self, attr: _AttrType) -> Self:
  367. """Indicate that the given relationship attribute should remain
  368. unloaded.
  369. The relationship attribute will return ``None`` when accessed without
  370. producing any loading effect.
  371. This function is part of the :class:`_orm.Load` interface and supports
  372. both method-chained and standalone operation.
  373. :func:`_orm.noload` applies to :func:`_orm.relationship` attributes
  374. only.
  375. .. note:: Setting this loading strategy as the default strategy
  376. for a relationship using the :paramref:`.orm.relationship.lazy`
  377. parameter may cause issues with flushes, such if a delete operation
  378. needs to load related objects and instead ``None`` was returned.
  379. .. seealso::
  380. :ref:`loading_toplevel`
  381. """
  382. return self._set_relationship_strategy(attr, {"lazy": "noload"})
  383. def raiseload(self, attr: _AttrType, sql_only: bool = False) -> Self:
  384. """Indicate that the given attribute should raise an error if accessed.
  385. A relationship attribute configured with :func:`_orm.raiseload` will
  386. raise an :exc:`~sqlalchemy.exc.InvalidRequestError` upon access. The
  387. typical way this is useful is when an application is attempting to
  388. ensure that all relationship attributes that are accessed in a
  389. particular context would have been already loaded via eager loading.
  390. Instead of having to read through SQL logs to ensure lazy loads aren't
  391. occurring, this strategy will cause them to raise immediately.
  392. :func:`_orm.raiseload` applies to :func:`_orm.relationship` attributes
  393. only. In order to apply raise-on-SQL behavior to a column-based
  394. attribute, use the :paramref:`.orm.defer.raiseload` parameter on the
  395. :func:`.defer` loader option.
  396. :param sql_only: if True, raise only if the lazy load would emit SQL,
  397. but not if it is only checking the identity map, or determining that
  398. the related value should just be None due to missing keys. When False,
  399. the strategy will raise for all varieties of relationship loading.
  400. This function is part of the :class:`_orm.Load` interface and supports
  401. both method-chained and standalone operation.
  402. .. seealso::
  403. :ref:`loading_toplevel`
  404. :ref:`prevent_lazy_with_raiseload`
  405. :ref:`orm_queryguide_deferred_raiseload`
  406. """
  407. return self._set_relationship_strategy(
  408. attr, {"lazy": "raise_on_sql" if sql_only else "raise"}
  409. )
  410. def defaultload(self, attr: _AttrType) -> Self:
  411. """Indicate an attribute should load using its predefined loader style.
  412. The behavior of this loading option is to not change the current
  413. loading style of the attribute, meaning that the previously configured
  414. one is used or, if no previous style was selected, the default
  415. loading will be used.
  416. This method is used to link to other loader options further into
  417. a chain of attributes without altering the loader style of the links
  418. along the chain. For example, to set joined eager loading for an
  419. element of an element::
  420. session.query(MyClass).options(
  421. defaultload(MyClass.someattribute).
  422. joinedload(MyOtherClass.someotherattribute)
  423. )
  424. :func:`.defaultload` is also useful for setting column-level options on
  425. a related class, namely that of :func:`.defer` and :func:`.undefer`::
  426. session.query(MyClass).options(
  427. defaultload(MyClass.someattribute).
  428. defer("some_column").
  429. undefer("some_other_column")
  430. )
  431. .. seealso::
  432. :ref:`orm_queryguide_relationship_sub_options`
  433. :meth:`_orm.Load.options`
  434. """
  435. return self._set_relationship_strategy(attr, None)
  436. def defer(self, key: _AttrType, raiseload: bool = False) -> Self:
  437. r"""Indicate that the given column-oriented attribute should be
  438. deferred, e.g. not loaded until accessed.
  439. This function is part of the :class:`_orm.Load` interface and supports
  440. both method-chained and standalone operation.
  441. e.g.::
  442. from sqlalchemy.orm import defer
  443. session.query(MyClass).options(
  444. defer(MyClass.attribute_one),
  445. defer(MyClass.attribute_two)
  446. )
  447. To specify a deferred load of an attribute on a related class,
  448. the path can be specified one token at a time, specifying the loading
  449. style for each link along the chain. To leave the loading style
  450. for a link unchanged, use :func:`_orm.defaultload`::
  451. session.query(MyClass).options(
  452. defaultload(MyClass.someattr).defer(RelatedClass.some_column)
  453. )
  454. Multiple deferral options related to a relationship can be bundled
  455. at once using :meth:`_orm.Load.options`::
  456. session.query(MyClass).options(
  457. defaultload(MyClass.someattr).options(
  458. defer(RelatedClass.some_column),
  459. defer(RelatedClass.some_other_column),
  460. defer(RelatedClass.another_column)
  461. )
  462. )
  463. :param key: Attribute to be deferred.
  464. :param raiseload: raise :class:`.InvalidRequestError` rather than
  465. lazy loading a value when the deferred attribute is accessed. Used
  466. to prevent unwanted SQL from being emitted.
  467. .. versionadded:: 1.4
  468. .. seealso::
  469. :ref:`orm_queryguide_column_deferral` - in the
  470. :ref:`queryguide_toplevel`
  471. :func:`_orm.load_only`
  472. :func:`_orm.undefer`
  473. """
  474. strategy = {"deferred": True, "instrument": True}
  475. if raiseload:
  476. strategy["raiseload"] = True
  477. return self._set_column_strategy((key,), strategy)
  478. def undefer(self, key: _AttrType) -> Self:
  479. r"""Indicate that the given column-oriented attribute should be
  480. undeferred, e.g. specified within the SELECT statement of the entity
  481. as a whole.
  482. The column being undeferred is typically set up on the mapping as a
  483. :func:`.deferred` attribute.
  484. This function is part of the :class:`_orm.Load` interface and supports
  485. both method-chained and standalone operation.
  486. Examples::
  487. # undefer two columns
  488. session.query(MyClass).options(
  489. undefer(MyClass.col1), undefer(MyClass.col2)
  490. )
  491. # undefer all columns specific to a single class using Load + *
  492. session.query(MyClass, MyOtherClass).options(
  493. Load(MyClass).undefer("*"))
  494. # undefer a column on a related object
  495. session.query(MyClass).options(
  496. defaultload(MyClass.items).undefer(MyClass.text))
  497. :param key: Attribute to be undeferred.
  498. .. seealso::
  499. :ref:`orm_queryguide_column_deferral` - in the
  500. :ref:`queryguide_toplevel`
  501. :func:`_orm.defer`
  502. :func:`_orm.undefer_group`
  503. """
  504. return self._set_column_strategy(
  505. (key,), {"deferred": False, "instrument": True}
  506. )
  507. def undefer_group(self, name: str) -> Self:
  508. """Indicate that columns within the given deferred group name should be
  509. undeferred.
  510. The columns being undeferred are set up on the mapping as
  511. :func:`.deferred` attributes and include a "group" name.
  512. E.g::
  513. session.query(MyClass).options(undefer_group("large_attrs"))
  514. To undefer a group of attributes on a related entity, the path can be
  515. spelled out using relationship loader options, such as
  516. :func:`_orm.defaultload`::
  517. session.query(MyClass).options(
  518. defaultload("someattr").undefer_group("large_attrs"))
  519. .. seealso::
  520. :ref:`orm_queryguide_column_deferral` - in the
  521. :ref:`queryguide_toplevel`
  522. :func:`_orm.defer`
  523. :func:`_orm.undefer`
  524. """
  525. return self._set_column_strategy(
  526. (_WILDCARD_TOKEN,), None, {f"undefer_group_{name}": True}
  527. )
  528. def with_expression(
  529. self,
  530. key: _AttrType,
  531. expression: _ColumnExpressionArgument[Any],
  532. ) -> Self:
  533. r"""Apply an ad-hoc SQL expression to a "deferred expression"
  534. attribute.
  535. This option is used in conjunction with the
  536. :func:`_orm.query_expression` mapper-level construct that indicates an
  537. attribute which should be the target of an ad-hoc SQL expression.
  538. E.g.::
  539. stmt = select(SomeClass).options(
  540. with_expression(SomeClass.x_y_expr, SomeClass.x + SomeClass.y)
  541. )
  542. .. versionadded:: 1.2
  543. :param key: Attribute to be populated
  544. :param expr: SQL expression to be applied to the attribute.
  545. .. seealso::
  546. :ref:`orm_queryguide_with_expression` - background and usage
  547. examples
  548. """
  549. expression = _orm_full_deannotate(
  550. coercions.expect(roles.LabeledColumnExprRole, expression)
  551. )
  552. return self._set_column_strategy(
  553. (key,), {"query_expression": True}, extra_criteria=(expression,)
  554. )
  555. def selectin_polymorphic(self, classes: Iterable[Type[Any]]) -> Self:
  556. """Indicate an eager load should take place for all attributes
  557. specific to a subclass.
  558. This uses an additional SELECT with IN against all matched primary
  559. key values, and is the per-query analogue to the ``"selectin"``
  560. setting on the :paramref:`.mapper.polymorphic_load` parameter.
  561. .. versionadded:: 1.2
  562. .. seealso::
  563. :ref:`polymorphic_selectin`
  564. """
  565. self = self._set_class_strategy(
  566. {"selectinload_polymorphic": True},
  567. opts={
  568. "entities": tuple(
  569. sorted((inspect(cls) for cls in classes), key=id)
  570. )
  571. },
  572. )
  573. return self
  574. @overload
  575. def _coerce_strat(self, strategy: _StrategySpec) -> _StrategyKey:
  576. ...
  577. @overload
  578. def _coerce_strat(self, strategy: Literal[None]) -> None:
  579. ...
  580. def _coerce_strat(
  581. self, strategy: Optional[_StrategySpec]
  582. ) -> Optional[_StrategyKey]:
  583. if strategy is not None:
  584. strategy_key = tuple(sorted(strategy.items()))
  585. else:
  586. strategy_key = None
  587. return strategy_key
  588. @_generative
  589. def _set_relationship_strategy(
  590. self,
  591. attr: _AttrType,
  592. strategy: Optional[_StrategySpec],
  593. propagate_to_loaders: bool = True,
  594. opts: Optional[_OptsType] = None,
  595. _reconcile_to_other: Optional[bool] = None,
  596. ) -> Self:
  597. strategy_key = self._coerce_strat(strategy)
  598. self._clone_for_bind_strategy(
  599. (attr,),
  600. strategy_key,
  601. _RELATIONSHIP_TOKEN,
  602. opts=opts,
  603. propagate_to_loaders=propagate_to_loaders,
  604. reconcile_to_other=_reconcile_to_other,
  605. )
  606. return self
  607. @_generative
  608. def _set_column_strategy(
  609. self,
  610. attrs: Tuple[_AttrType, ...],
  611. strategy: Optional[_StrategySpec],
  612. opts: Optional[_OptsType] = None,
  613. extra_criteria: Optional[Tuple[Any, ...]] = None,
  614. ) -> Self:
  615. strategy_key = self._coerce_strat(strategy)
  616. self._clone_for_bind_strategy(
  617. attrs,
  618. strategy_key,
  619. _COLUMN_TOKEN,
  620. opts=opts,
  621. attr_group=attrs,
  622. extra_criteria=extra_criteria,
  623. )
  624. return self
  625. @_generative
  626. def _set_generic_strategy(
  627. self,
  628. attrs: Tuple[_AttrType, ...],
  629. strategy: _StrategySpec,
  630. _reconcile_to_other: Optional[bool] = None,
  631. ) -> Self:
  632. strategy_key = self._coerce_strat(strategy)
  633. self._clone_for_bind_strategy(
  634. attrs,
  635. strategy_key,
  636. None,
  637. propagate_to_loaders=True,
  638. reconcile_to_other=_reconcile_to_other,
  639. )
  640. return self
  641. @_generative
  642. def _set_class_strategy(
  643. self, strategy: _StrategySpec, opts: _OptsType
  644. ) -> Self:
  645. strategy_key = self._coerce_strat(strategy)
  646. self._clone_for_bind_strategy(None, strategy_key, None, opts=opts)
  647. return self
  648. def _apply_to_parent(self, parent: Load) -> None:
  649. """apply this :class:`_orm._AbstractLoad` object as a sub-option o
  650. a :class:`_orm.Load` object.
  651. Implementation is provided by subclasses.
  652. """
  653. raise NotImplementedError()
  654. def options(self, *opts: _AbstractLoad) -> Self:
  655. r"""Apply a series of options as sub-options to this
  656. :class:`_orm._AbstractLoad` object.
  657. Implementation is provided by subclasses.
  658. """
  659. raise NotImplementedError()
  660. def _clone_for_bind_strategy(
  661. self,
  662. attrs: Optional[Tuple[_AttrType, ...]],
  663. strategy: Optional[_StrategyKey],
  664. wildcard_key: Optional[_WildcardKeyType],
  665. opts: Optional[_OptsType] = None,
  666. attr_group: Optional[_AttrGroupType] = None,
  667. propagate_to_loaders: bool = True,
  668. reconcile_to_other: Optional[bool] = None,
  669. extra_criteria: Optional[Tuple[Any, ...]] = None,
  670. ) -> Self:
  671. raise NotImplementedError()
  672. def process_compile_state_replaced_entities(
  673. self,
  674. compile_state: ORMCompileState,
  675. mapper_entities: Sequence[_MapperEntity],
  676. ) -> None:
  677. if not compile_state.compile_options._enable_eagerloads:
  678. return
  679. # process is being run here so that the options given are validated
  680. # against what the lead entities were, as well as to accommodate
  681. # for the entities having been replaced with equivalents
  682. self._process(
  683. compile_state,
  684. mapper_entities,
  685. not bool(compile_state.current_path),
  686. )
  687. def process_compile_state(self, compile_state: ORMCompileState) -> None:
  688. if not compile_state.compile_options._enable_eagerloads:
  689. return
  690. self._process(
  691. compile_state,
  692. compile_state._lead_mapper_entities,
  693. not bool(compile_state.current_path)
  694. and not compile_state.compile_options._for_refresh_state,
  695. )
  696. def _process(
  697. self,
  698. compile_state: ORMCompileState,
  699. mapper_entities: Sequence[_MapperEntity],
  700. raiseerr: bool,
  701. ) -> None:
  702. """implemented by subclasses"""
  703. raise NotImplementedError()
  704. @classmethod
  705. def _chop_path(
  706. cls,
  707. to_chop: _PathRepresentation,
  708. path: PathRegistry,
  709. debug: bool = False,
  710. ) -> Optional[_PathRepresentation]:
  711. i = -1
  712. for i, (c_token, p_token) in enumerate(
  713. zip(to_chop, path.natural_path)
  714. ):
  715. if isinstance(c_token, str):
  716. if i == 0 and (
  717. c_token.endswith(f":{_DEFAULT_TOKEN}")
  718. or c_token.endswith(f":{_WILDCARD_TOKEN}")
  719. ):
  720. return to_chop
  721. elif (
  722. c_token != f"{_RELATIONSHIP_TOKEN}:{_WILDCARD_TOKEN}"
  723. and c_token != p_token.key # type: ignore
  724. ):
  725. return None
  726. if c_token is p_token:
  727. continue
  728. elif (
  729. isinstance(c_token, InspectionAttr)
  730. and insp_is_mapper(c_token)
  731. and insp_is_mapper(p_token)
  732. and c_token.isa(p_token)
  733. ):
  734. continue
  735. else:
  736. return None
  737. return to_chop[i + 1 :]
  738. class Load(_AbstractLoad):
  739. """Represents loader options which modify the state of a
  740. ORM-enabled :class:`_sql.Select` or a legacy :class:`_query.Query` in
  741. order to affect how various mapped attributes are loaded.
  742. The :class:`_orm.Load` object is in most cases used implicitly behind the
  743. scenes when one makes use of a query option like :func:`_orm.joinedload`,
  744. :func:`_orm.defer`, or similar. It typically is not instantiated directly
  745. except for in some very specific cases.
  746. .. seealso::
  747. :ref:`orm_queryguide_relationship_per_entity_wildcard` - illustrates an
  748. example where direct use of :class:`_orm.Load` may be useful
  749. """
  750. __slots__ = (
  751. "path",
  752. "context",
  753. "additional_source_entities",
  754. )
  755. _traverse_internals = [
  756. ("path", visitors.ExtendedInternalTraversal.dp_has_cache_key),
  757. (
  758. "context",
  759. visitors.InternalTraversal.dp_has_cache_key_list,
  760. ),
  761. ("propagate_to_loaders", visitors.InternalTraversal.dp_boolean),
  762. (
  763. "additional_source_entities",
  764. visitors.InternalTraversal.dp_has_cache_key_list,
  765. ),
  766. ]
  767. _cache_key_traversal = None
  768. path: PathRegistry
  769. context: Tuple[_LoadElement, ...]
  770. additional_source_entities: Tuple[_InternalEntityType[Any], ...]
  771. def __init__(self, entity: _EntityType[Any]):
  772. insp = cast("Union[Mapper[Any], AliasedInsp[Any]]", inspect(entity))
  773. insp._post_inspect
  774. self.path = insp._path_registry
  775. self.context = ()
  776. self.propagate_to_loaders = False
  777. self.additional_source_entities = ()
  778. def __str__(self) -> str:
  779. return f"Load({self.path[0]})"
  780. @classmethod
  781. def _construct_for_existing_path(
  782. cls, path: AbstractEntityRegistry
  783. ) -> Load:
  784. load = cls.__new__(cls)
  785. load.path = path
  786. load.context = ()
  787. load.propagate_to_loaders = False
  788. load.additional_source_entities = ()
  789. return load
  790. def _adapt_cached_option_to_uncached_option(
  791. self, context: QueryContext, uncached_opt: ORMOption
  792. ) -> ORMOption:
  793. return self._adjust_for_extra_criteria(context)
  794. def _prepend_path(self, path: PathRegistry) -> Load:
  795. cloned = self._clone()
  796. cloned.context = tuple(
  797. element._prepend_path(path) for element in self.context
  798. )
  799. return cloned
  800. def _adjust_for_extra_criteria(self, context: QueryContext) -> Load:
  801. """Apply the current bound parameters in a QueryContext to all
  802. occurrences "extra_criteria" stored within this ``Load`` object,
  803. returning a new instance of this ``Load`` object.
  804. """
  805. orig_query = context.compile_state.select_statement
  806. orig_cache_key: Optional[CacheKey] = None
  807. replacement_cache_key: Optional[CacheKey] = None
  808. found_crit = False
  809. def process(opt: _LoadElement) -> _LoadElement:
  810. nonlocal orig_cache_key, replacement_cache_key, found_crit
  811. found_crit = True
  812. if orig_cache_key is None or replacement_cache_key is None:
  813. orig_cache_key = orig_query._generate_cache_key()
  814. replacement_cache_key = context.query._generate_cache_key()
  815. assert orig_cache_key is not None
  816. assert replacement_cache_key is not None
  817. opt._extra_criteria = tuple(
  818. replacement_cache_key._apply_params_to_element(
  819. orig_cache_key, crit
  820. )
  821. for crit in opt._extra_criteria
  822. )
  823. return opt
  824. # avoid generating cache keys for the queries if we don't
  825. # actually have any extra_criteria options, which is the
  826. # common case
  827. new_context = tuple(
  828. process(value._clone()) if value._extra_criteria else value
  829. for value in self.context
  830. )
  831. if found_crit:
  832. cloned = self._clone()
  833. cloned.context = new_context
  834. return cloned
  835. else:
  836. return self
  837. def _reconcile_query_entities_with_us(self, mapper_entities, raiseerr):
  838. """called at process time to allow adjustment of the root
  839. entity inside of _LoadElement objects.
  840. """
  841. path = self.path
  842. ezero = None
  843. for ent in mapper_entities:
  844. ezero = ent.entity_zero
  845. if ezero and orm_util._entity_corresponds_to(
  846. # technically this can be a token also, but this is
  847. # safe to pass to _entity_corresponds_to()
  848. ezero,
  849. cast("_InternalEntityType[Any]", path[0]),
  850. ):
  851. return ezero
  852. return None
  853. def _process(
  854. self,
  855. compile_state: ORMCompileState,
  856. mapper_entities: Sequence[_MapperEntity],
  857. raiseerr: bool,
  858. ) -> None:
  859. reconciled_lead_entity = self._reconcile_query_entities_with_us(
  860. mapper_entities, raiseerr
  861. )
  862. for loader in self.context:
  863. loader.process_compile_state(
  864. self,
  865. compile_state,
  866. mapper_entities,
  867. reconciled_lead_entity,
  868. raiseerr,
  869. )
  870. def _apply_to_parent(self, parent: Load) -> None:
  871. """apply this :class:`_orm.Load` object as a sub-option of another
  872. :class:`_orm.Load` object.
  873. This method is used by the :meth:`_orm.Load.options` method.
  874. """
  875. cloned = self._generate()
  876. assert cloned.propagate_to_loaders == self.propagate_to_loaders
  877. if not any(
  878. orm_util._entity_corresponds_to_use_path_impl(
  879. elem, cloned.path.odd_element(0)
  880. )
  881. for elem in (parent.path.odd_element(-1),)
  882. + parent.additional_source_entities
  883. ):
  884. if len(cloned.path) > 1:
  885. attrname = cloned.path[1]
  886. parent_entity = cloned.path[0]
  887. else:
  888. attrname = cloned.path[0]
  889. parent_entity = cloned.path[0]
  890. _raise_for_does_not_link(parent.path, attrname, parent_entity)
  891. cloned.path = PathRegistry.coerce(parent.path[0:-1] + cloned.path[:])
  892. if self.context:
  893. cloned.context = tuple(
  894. value._prepend_path_from(parent) for value in self.context
  895. )
  896. if cloned.context:
  897. parent.context += cloned.context
  898. parent.additional_source_entities += (
  899. cloned.additional_source_entities
  900. )
  901. @_generative
  902. def options(self, *opts: _AbstractLoad) -> Self:
  903. r"""Apply a series of options as sub-options to this
  904. :class:`_orm.Load`
  905. object.
  906. E.g.::
  907. query = session.query(Author)
  908. query = query.options(
  909. joinedload(Author.book).options(
  910. load_only(Book.summary, Book.excerpt),
  911. joinedload(Book.citations).options(
  912. joinedload(Citation.author)
  913. )
  914. )
  915. )
  916. :param \*opts: A series of loader option objects (ultimately
  917. :class:`_orm.Load` objects) which should be applied to the path
  918. specified by this :class:`_orm.Load` object.
  919. .. versionadded:: 1.3.6
  920. .. seealso::
  921. :func:`.defaultload`
  922. :ref:`orm_queryguide_relationship_sub_options`
  923. """
  924. for opt in opts:
  925. try:
  926. opt._apply_to_parent(self)
  927. except AttributeError as ae:
  928. if not isinstance(opt, _AbstractLoad):
  929. raise sa_exc.ArgumentError(
  930. f"Loader option {opt} is not compatible with the "
  931. "Load.options() method."
  932. ) from ae
  933. else:
  934. raise
  935. return self
  936. def _clone_for_bind_strategy(
  937. self,
  938. attrs: Optional[Tuple[_AttrType, ...]],
  939. strategy: Optional[_StrategyKey],
  940. wildcard_key: Optional[_WildcardKeyType],
  941. opts: Optional[_OptsType] = None,
  942. attr_group: Optional[_AttrGroupType] = None,
  943. propagate_to_loaders: bool = True,
  944. reconcile_to_other: Optional[bool] = None,
  945. extra_criteria: Optional[Tuple[Any, ...]] = None,
  946. ) -> Self:
  947. # for individual strategy that needs to propagate, set the whole
  948. # Load container to also propagate, so that it shows up in
  949. # InstanceState.load_options
  950. if propagate_to_loaders:
  951. self.propagate_to_loaders = True
  952. if self.path.is_token:
  953. raise sa_exc.ArgumentError(
  954. "Wildcard token cannot be followed by another entity"
  955. )
  956. elif path_is_property(self.path):
  957. # re-use the lookup which will raise a nicely formatted
  958. # LoaderStrategyException
  959. if strategy:
  960. self.path.prop._strategy_lookup(self.path.prop, strategy[0])
  961. else:
  962. raise sa_exc.ArgumentError(
  963. f"Mapped attribute '{self.path.prop}' does not "
  964. "refer to a mapped entity"
  965. )
  966. if attrs is None:
  967. load_element = _ClassStrategyLoad.create(
  968. self.path,
  969. None,
  970. strategy,
  971. wildcard_key,
  972. opts,
  973. propagate_to_loaders,
  974. attr_group=attr_group,
  975. reconcile_to_other=reconcile_to_other,
  976. extra_criteria=extra_criteria,
  977. )
  978. if load_element:
  979. self.context += (load_element,)
  980. assert opts is not None
  981. self.additional_source_entities += cast(
  982. "Tuple[_InternalEntityType[Any]]", opts["entities"]
  983. )
  984. else:
  985. for attr in attrs:
  986. if isinstance(attr, str):
  987. load_element = _TokenStrategyLoad.create(
  988. self.path,
  989. attr,
  990. strategy,
  991. wildcard_key,
  992. opts,
  993. propagate_to_loaders,
  994. attr_group=attr_group,
  995. reconcile_to_other=reconcile_to_other,
  996. extra_criteria=extra_criteria,
  997. )
  998. else:
  999. load_element = _AttributeStrategyLoad.create(
  1000. self.path,
  1001. attr,
  1002. strategy,
  1003. wildcard_key,
  1004. opts,
  1005. propagate_to_loaders,
  1006. attr_group=attr_group,
  1007. reconcile_to_other=reconcile_to_other,
  1008. extra_criteria=extra_criteria,
  1009. )
  1010. if load_element:
  1011. # for relationship options, update self.path on this Load
  1012. # object with the latest path.
  1013. if wildcard_key is _RELATIONSHIP_TOKEN:
  1014. self.path = load_element.path
  1015. self.context += (load_element,)
  1016. # this seems to be effective for selectinloader,
  1017. # giving the extra match to one more level deep.
  1018. # but does not work for immediateloader, which still
  1019. # must add additional options at load time
  1020. if load_element.local_opts.get("recursion_depth", False):
  1021. r1 = load_element._recurse()
  1022. self.context += (r1,)
  1023. return self
  1024. def __getstate__(self):
  1025. d = self._shallow_to_dict()
  1026. d["path"] = self.path.serialize()
  1027. return d
  1028. def __setstate__(self, state):
  1029. state["path"] = PathRegistry.deserialize(state["path"])
  1030. self._shallow_from_dict(state)
  1031. class _WildcardLoad(_AbstractLoad):
  1032. """represent a standalone '*' load operation"""
  1033. __slots__ = ("strategy", "path", "local_opts")
  1034. _traverse_internals = [
  1035. ("strategy", visitors.ExtendedInternalTraversal.dp_plain_obj),
  1036. ("path", visitors.ExtendedInternalTraversal.dp_plain_obj),
  1037. (
  1038. "local_opts",
  1039. visitors.ExtendedInternalTraversal.dp_string_multi_dict,
  1040. ),
  1041. ]
  1042. cache_key_traversal: _CacheKeyTraversalType = None
  1043. strategy: Optional[Tuple[Any, ...]]
  1044. local_opts: _OptsType
  1045. path: Union[Tuple[()], Tuple[str]]
  1046. propagate_to_loaders = False
  1047. def __init__(self) -> None:
  1048. self.path = ()
  1049. self.strategy = None
  1050. self.local_opts = util.EMPTY_DICT
  1051. def _clone_for_bind_strategy(
  1052. self,
  1053. attrs,
  1054. strategy,
  1055. wildcard_key,
  1056. opts=None,
  1057. attr_group=None,
  1058. propagate_to_loaders=True,
  1059. reconcile_to_other=None,
  1060. extra_criteria=None,
  1061. ):
  1062. assert attrs is not None
  1063. attr = attrs[0]
  1064. assert (
  1065. wildcard_key
  1066. and isinstance(attr, str)
  1067. and attr in (_WILDCARD_TOKEN, _DEFAULT_TOKEN)
  1068. )
  1069. attr = f"{wildcard_key}:{attr}"
  1070. self.strategy = strategy
  1071. self.path = (attr,)
  1072. if opts:
  1073. self.local_opts = util.immutabledict(opts)
  1074. assert extra_criteria is None
  1075. def options(self, *opts: _AbstractLoad) -> Self:
  1076. raise NotImplementedError("Star option does not support sub-options")
  1077. def _apply_to_parent(self, parent: Load) -> None:
  1078. """apply this :class:`_orm._WildcardLoad` object as a sub-option of
  1079. a :class:`_orm.Load` object.
  1080. This method is used by the :meth:`_orm.Load.options` method. Note
  1081. that :class:`_orm.WildcardLoad` itself can't have sub-options, but
  1082. it may be used as the sub-option of a :class:`_orm.Load` object.
  1083. """
  1084. assert self.path
  1085. attr = self.path[0]
  1086. if attr.endswith(_DEFAULT_TOKEN):
  1087. attr = f"{attr.split(':')[0]}:{_WILDCARD_TOKEN}"
  1088. effective_path = cast(AbstractEntityRegistry, parent.path).token(attr)
  1089. assert effective_path.is_token
  1090. loader = _TokenStrategyLoad.create(
  1091. effective_path,
  1092. None,
  1093. self.strategy,
  1094. None,
  1095. self.local_opts,
  1096. self.propagate_to_loaders,
  1097. )
  1098. parent.context += (loader,)
  1099. def _process(self, compile_state, mapper_entities, raiseerr):
  1100. is_refresh = compile_state.compile_options._for_refresh_state
  1101. if is_refresh and not self.propagate_to_loaders:
  1102. return
  1103. entities = [ent.entity_zero for ent in mapper_entities]
  1104. current_path = compile_state.current_path
  1105. start_path: _PathRepresentation = self.path
  1106. if current_path:
  1107. # TODO: no cases in test suite where we actually get
  1108. # None back here
  1109. new_path = self._chop_path(start_path, current_path)
  1110. if new_path is None:
  1111. return
  1112. # chop_path does not actually "chop" a wildcard token path,
  1113. # just returns it
  1114. assert new_path == start_path
  1115. # start_path is a single-token tuple
  1116. assert start_path and len(start_path) == 1
  1117. token = start_path[0]
  1118. assert isinstance(token, str)
  1119. entity = self._find_entity_basestring(entities, token, raiseerr)
  1120. if not entity:
  1121. return
  1122. path_element = entity
  1123. # transfer our entity-less state into a Load() object
  1124. # with a real entity path. Start with the lead entity
  1125. # we just located, then go through the rest of our path
  1126. # tokens and populate into the Load().
  1127. assert isinstance(token, str)
  1128. loader = _TokenStrategyLoad.create(
  1129. path_element._path_registry,
  1130. token,
  1131. self.strategy,
  1132. None,
  1133. self.local_opts,
  1134. self.propagate_to_loaders,
  1135. raiseerr=raiseerr,
  1136. )
  1137. if not loader:
  1138. return
  1139. assert loader.path.is_token
  1140. # don't pass a reconciled lead entity here
  1141. loader.process_compile_state(
  1142. self, compile_state, mapper_entities, None, raiseerr
  1143. )
  1144. return loader
  1145. def _find_entity_basestring(
  1146. self,
  1147. entities: Iterable[_InternalEntityType[Any]],
  1148. token: str,
  1149. raiseerr: bool,
  1150. ) -> Optional[_InternalEntityType[Any]]:
  1151. if token.endswith(f":{_WILDCARD_TOKEN}"):
  1152. if len(list(entities)) != 1:
  1153. if raiseerr:
  1154. raise sa_exc.ArgumentError(
  1155. "Can't apply wildcard ('*') or load_only() "
  1156. f"loader option to multiple entities "
  1157. f"{', '.join(str(ent) for ent in entities)}. Specify "
  1158. "loader options for each entity individually, such as "
  1159. f"""{
  1160. ", ".join(
  1161. f"Load({ent}).some_option('*')"
  1162. for ent in entities
  1163. )
  1164. }."""
  1165. )
  1166. elif token.endswith(_DEFAULT_TOKEN):
  1167. raiseerr = False
  1168. for ent in entities:
  1169. # return only the first _MapperEntity when searching
  1170. # based on string prop name. Ideally object
  1171. # attributes are used to specify more exactly.
  1172. return ent
  1173. else:
  1174. if raiseerr:
  1175. raise sa_exc.ArgumentError(
  1176. "Query has only expression-based entities - "
  1177. f'can\'t find property named "{token}".'
  1178. )
  1179. else:
  1180. return None
  1181. def __getstate__(self) -> Dict[str, Any]:
  1182. d = self._shallow_to_dict()
  1183. return d
  1184. def __setstate__(self, state: Dict[str, Any]) -> None:
  1185. self._shallow_from_dict(state)
  1186. class _LoadElement(
  1187. cache_key.HasCacheKey, traversals.HasShallowCopy, visitors.Traversible
  1188. ):
  1189. """represents strategy information to select for a LoaderStrategy
  1190. and pass options to it.
  1191. :class:`._LoadElement` objects provide the inner datastructure
  1192. stored by a :class:`_orm.Load` object and are also the object passed
  1193. to methods like :meth:`.LoaderStrategy.setup_query`.
  1194. .. versionadded:: 2.0
  1195. """
  1196. __slots__ = (
  1197. "path",
  1198. "strategy",
  1199. "propagate_to_loaders",
  1200. "local_opts",
  1201. "_extra_criteria",
  1202. "_reconcile_to_other",
  1203. )
  1204. __visit_name__ = "load_element"
  1205. _traverse_internals = [
  1206. ("path", visitors.ExtendedInternalTraversal.dp_has_cache_key),
  1207. ("strategy", visitors.ExtendedInternalTraversal.dp_plain_obj),
  1208. (
  1209. "local_opts",
  1210. visitors.ExtendedInternalTraversal.dp_string_multi_dict,
  1211. ),
  1212. ("_extra_criteria", visitors.InternalTraversal.dp_clauseelement_list),
  1213. ("propagate_to_loaders", visitors.InternalTraversal.dp_plain_obj),
  1214. ("_reconcile_to_other", visitors.InternalTraversal.dp_plain_obj),
  1215. ]
  1216. _cache_key_traversal = None
  1217. _extra_criteria: Tuple[Any, ...]
  1218. _reconcile_to_other: Optional[bool]
  1219. strategy: Optional[_StrategyKey]
  1220. path: PathRegistry
  1221. propagate_to_loaders: bool
  1222. local_opts: util.immutabledict[str, Any]
  1223. is_token_strategy: bool
  1224. is_class_strategy: bool
  1225. def __hash__(self) -> int:
  1226. return id(self)
  1227. def __eq__(self, other):
  1228. return traversals.compare(self, other)
  1229. @property
  1230. def is_opts_only(self) -> bool:
  1231. return bool(self.local_opts and self.strategy is None)
  1232. def _clone(self, **kw: Any) -> _LoadElement:
  1233. cls = self.__class__
  1234. s = cls.__new__(cls)
  1235. self._shallow_copy_to(s)
  1236. return s
  1237. def _update_opts(self, **kw: Any) -> _LoadElement:
  1238. new = self._clone()
  1239. new.local_opts = new.local_opts.union(kw)
  1240. return new
  1241. def __getstate__(self) -> Dict[str, Any]:
  1242. d = self._shallow_to_dict()
  1243. d["path"] = self.path.serialize()
  1244. return d
  1245. def __setstate__(self, state: Dict[str, Any]) -> None:
  1246. state["path"] = PathRegistry.deserialize(state["path"])
  1247. self._shallow_from_dict(state)
  1248. def _raise_for_no_match(self, parent_loader, mapper_entities):
  1249. path = parent_loader.path
  1250. found_entities = False
  1251. for ent in mapper_entities:
  1252. ezero = ent.entity_zero
  1253. if ezero:
  1254. found_entities = True
  1255. break
  1256. if not found_entities:
  1257. raise sa_exc.ArgumentError(
  1258. "Query has only expression-based entities; "
  1259. f"attribute loader options for {path[0]} can't "
  1260. "be applied here."
  1261. )
  1262. else:
  1263. raise sa_exc.ArgumentError(
  1264. f"Mapped class {path[0]} does not apply to any of the "
  1265. f"root entities in this query, e.g. "
  1266. f"""{
  1267. ", ".join(
  1268. str(x.entity_zero)
  1269. for x in mapper_entities if x.entity_zero
  1270. )}. Please """
  1271. "specify the full path "
  1272. "from one of the root entities to the target "
  1273. "attribute. "
  1274. )
  1275. def _adjust_effective_path_for_current_path(
  1276. self, effective_path: PathRegistry, current_path: PathRegistry
  1277. ) -> Optional[PathRegistry]:
  1278. """receives the 'current_path' entry from an :class:`.ORMCompileState`
  1279. instance, which is set during lazy loads and secondary loader strategy
  1280. loads, and adjusts the given path to be relative to the
  1281. current_path.
  1282. E.g. given a loader path and current path::
  1283. lp: User -> orders -> Order -> items -> Item -> keywords -> Keyword
  1284. cp: User -> orders -> Order -> items
  1285. The adjusted path would be::
  1286. Item -> keywords -> Keyword
  1287. """
  1288. chopped_start_path = Load._chop_path(
  1289. effective_path.natural_path, current_path
  1290. )
  1291. if not chopped_start_path:
  1292. return None
  1293. tokens_removed_from_start_path = len(effective_path) - len(
  1294. chopped_start_path
  1295. )
  1296. loader_lead_path_element = self.path[tokens_removed_from_start_path]
  1297. effective_path = PathRegistry.coerce(
  1298. (loader_lead_path_element,) + chopped_start_path[1:]
  1299. )
  1300. return effective_path
  1301. def _init_path(
  1302. self, path, attr, wildcard_key, attr_group, raiseerr, extra_criteria
  1303. ):
  1304. """Apply ORM attributes and/or wildcard to an existing path, producing
  1305. a new path.
  1306. This method is used within the :meth:`.create` method to initialize
  1307. a :class:`._LoadElement` object.
  1308. """
  1309. raise NotImplementedError()
  1310. def _prepare_for_compile_state(
  1311. self,
  1312. parent_loader,
  1313. compile_state,
  1314. mapper_entities,
  1315. reconciled_lead_entity,
  1316. raiseerr,
  1317. ):
  1318. """implemented by subclasses."""
  1319. raise NotImplementedError()
  1320. def process_compile_state(
  1321. self,
  1322. parent_loader,
  1323. compile_state,
  1324. mapper_entities,
  1325. reconciled_lead_entity,
  1326. raiseerr,
  1327. ):
  1328. """populate ORMCompileState.attributes with loader state for this
  1329. _LoadElement.
  1330. """
  1331. keys = self._prepare_for_compile_state(
  1332. parent_loader,
  1333. compile_state,
  1334. mapper_entities,
  1335. reconciled_lead_entity,
  1336. raiseerr,
  1337. )
  1338. for key in keys:
  1339. if key in compile_state.attributes:
  1340. compile_state.attributes[key] = _LoadElement._reconcile(
  1341. self, compile_state.attributes[key]
  1342. )
  1343. else:
  1344. compile_state.attributes[key] = self
  1345. @classmethod
  1346. def create(
  1347. cls,
  1348. path: PathRegistry,
  1349. attr: Union[_AttrType, _StrPathToken, None],
  1350. strategy: Optional[_StrategyKey],
  1351. wildcard_key: Optional[_WildcardKeyType],
  1352. local_opts: Optional[_OptsType],
  1353. propagate_to_loaders: bool,
  1354. raiseerr: bool = True,
  1355. attr_group: Optional[_AttrGroupType] = None,
  1356. reconcile_to_other: Optional[bool] = None,
  1357. extra_criteria: Optional[Tuple[Any, ...]] = None,
  1358. ) -> _LoadElement:
  1359. """Create a new :class:`._LoadElement` object."""
  1360. opt = cls.__new__(cls)
  1361. opt.path = path
  1362. opt.strategy = strategy
  1363. opt.propagate_to_loaders = propagate_to_loaders
  1364. opt.local_opts = (
  1365. util.immutabledict(local_opts) if local_opts else util.EMPTY_DICT
  1366. )
  1367. opt._extra_criteria = ()
  1368. if reconcile_to_other is not None:
  1369. opt._reconcile_to_other = reconcile_to_other
  1370. elif strategy is None and not local_opts:
  1371. opt._reconcile_to_other = True
  1372. else:
  1373. opt._reconcile_to_other = None
  1374. path = opt._init_path(
  1375. path, attr, wildcard_key, attr_group, raiseerr, extra_criteria
  1376. )
  1377. if not path:
  1378. return None # type: ignore
  1379. assert opt.is_token_strategy == path.is_token
  1380. opt.path = path
  1381. return opt
  1382. def __init__(self) -> None:
  1383. raise NotImplementedError()
  1384. def _recurse(self) -> _LoadElement:
  1385. cloned = self._clone()
  1386. cloned.path = PathRegistry.coerce(self.path[:] + self.path[-2:])
  1387. return cloned
  1388. def _prepend_path_from(self, parent: Load) -> _LoadElement:
  1389. """adjust the path of this :class:`._LoadElement` to be
  1390. a subpath of that of the given parent :class:`_orm.Load` object's
  1391. path.
  1392. This is used by the :meth:`_orm.Load._apply_to_parent` method,
  1393. which is in turn part of the :meth:`_orm.Load.options` method.
  1394. """
  1395. if not any(
  1396. orm_util._entity_corresponds_to_use_path_impl(
  1397. elem,
  1398. self.path.odd_element(0),
  1399. )
  1400. for elem in (parent.path.odd_element(-1),)
  1401. + parent.additional_source_entities
  1402. ):
  1403. raise sa_exc.ArgumentError(
  1404. f'Attribute "{self.path[1]}" does not link '
  1405. f'from element "{parent.path[-1]}".'
  1406. )
  1407. return self._prepend_path(parent.path)
  1408. def _prepend_path(self, path: PathRegistry) -> _LoadElement:
  1409. cloned = self._clone()
  1410. assert cloned.strategy == self.strategy
  1411. assert cloned.local_opts == self.local_opts
  1412. assert cloned.is_class_strategy == self.is_class_strategy
  1413. cloned.path = PathRegistry.coerce(path[0:-1] + cloned.path[:])
  1414. return cloned
  1415. @staticmethod
  1416. def _reconcile(
  1417. replacement: _LoadElement, existing: _LoadElement
  1418. ) -> _LoadElement:
  1419. """define behavior for when two Load objects are to be put into
  1420. the context.attributes under the same key.
  1421. :param replacement: ``_LoadElement`` that seeks to replace the
  1422. existing one
  1423. :param existing: ``_LoadElement`` that is already present.
  1424. """
  1425. # mapper inheritance loading requires fine-grained "block other
  1426. # options" / "allow these options to be overridden" behaviors
  1427. # see test_poly_loading.py
  1428. if replacement._reconcile_to_other:
  1429. return existing
  1430. elif replacement._reconcile_to_other is False:
  1431. return replacement
  1432. elif existing._reconcile_to_other:
  1433. return replacement
  1434. elif existing._reconcile_to_other is False:
  1435. return existing
  1436. if existing is replacement:
  1437. return replacement
  1438. elif (
  1439. existing.strategy == replacement.strategy
  1440. and existing.local_opts == replacement.local_opts
  1441. ):
  1442. return replacement
  1443. elif replacement.is_opts_only:
  1444. existing = existing._clone()
  1445. existing.local_opts = existing.local_opts.union(
  1446. replacement.local_opts
  1447. )
  1448. existing._extra_criteria += replacement._extra_criteria
  1449. return existing
  1450. elif existing.is_opts_only:
  1451. replacement = replacement._clone()
  1452. replacement.local_opts = replacement.local_opts.union(
  1453. existing.local_opts
  1454. )
  1455. replacement._extra_criteria += existing._extra_criteria
  1456. return replacement
  1457. elif replacement.path.is_token:
  1458. # use 'last one wins' logic for wildcard options. this is also
  1459. # kind of inconsistent vs. options that are specific paths which
  1460. # will raise as below
  1461. return replacement
  1462. raise sa_exc.InvalidRequestError(
  1463. f"Loader strategies for {replacement.path} conflict"
  1464. )
  1465. class _AttributeStrategyLoad(_LoadElement):
  1466. """Loader strategies against specific relationship or column paths.
  1467. e.g.::
  1468. joinedload(User.addresses)
  1469. defer(Order.name)
  1470. selectinload(User.orders).lazyload(Order.items)
  1471. """
  1472. __slots__ = ("_of_type", "_path_with_polymorphic_path")
  1473. __visit_name__ = "attribute_strategy_load_element"
  1474. _traverse_internals = _LoadElement._traverse_internals + [
  1475. ("_of_type", visitors.ExtendedInternalTraversal.dp_multi),
  1476. (
  1477. "_path_with_polymorphic_path",
  1478. visitors.ExtendedInternalTraversal.dp_has_cache_key,
  1479. ),
  1480. ]
  1481. _of_type: Union[Mapper[Any], AliasedInsp[Any], None]
  1482. _path_with_polymorphic_path: Optional[PathRegistry]
  1483. is_class_strategy = False
  1484. is_token_strategy = False
  1485. def _init_path(
  1486. self, path, attr, wildcard_key, attr_group, raiseerr, extra_criteria
  1487. ):
  1488. assert attr is not None
  1489. self._of_type = None
  1490. self._path_with_polymorphic_path = None
  1491. insp, _, prop = _parse_attr_argument(attr)
  1492. if insp.is_property:
  1493. # direct property can be sent from internal strategy logic
  1494. # that sets up specific loaders, such as
  1495. # emit_lazyload->_lazyload_reverse
  1496. # prop = found_property = attr
  1497. prop = attr
  1498. path = path[prop]
  1499. if path.has_entity:
  1500. path = path.entity_path
  1501. return path
  1502. elif not insp.is_attribute:
  1503. # should not reach here;
  1504. assert False
  1505. # here we assume we have user-passed InstrumentedAttribute
  1506. if not orm_util._entity_corresponds_to_use_path_impl(
  1507. path[-1], attr.parent
  1508. ):
  1509. if raiseerr:
  1510. if attr_group and attr is not attr_group[0]:
  1511. raise sa_exc.ArgumentError(
  1512. "Can't apply wildcard ('*') or load_only() "
  1513. "loader option to multiple entities in the "
  1514. "same option. Use separate options per entity."
  1515. )
  1516. else:
  1517. _raise_for_does_not_link(path, str(attr), attr.parent)
  1518. else:
  1519. return None
  1520. # note the essential logic of this attribute was very different in
  1521. # 1.4, where there were caching failures in e.g.
  1522. # test_relationship_criteria.py::RelationshipCriteriaTest::
  1523. # test_selectinload_nested_criteria[True] if an existing
  1524. # "_extra_criteria" on a Load object were replaced with that coming
  1525. # from an attribute. This appears to have been an artifact of how
  1526. # _UnboundLoad / Load interacted together, which was opaque and
  1527. # poorly defined.
  1528. if extra_criteria:
  1529. assert not attr._extra_criteria
  1530. self._extra_criteria = extra_criteria
  1531. else:
  1532. self._extra_criteria = attr._extra_criteria
  1533. if getattr(attr, "_of_type", None):
  1534. ac = attr._of_type
  1535. ext_info = inspect(ac)
  1536. self._of_type = ext_info
  1537. self._path_with_polymorphic_path = path.entity_path[prop]
  1538. path = path[prop][ext_info]
  1539. else:
  1540. path = path[prop]
  1541. if path.has_entity:
  1542. path = path.entity_path
  1543. return path
  1544. def _generate_extra_criteria(self, context):
  1545. """Apply the current bound parameters in a QueryContext to the
  1546. immediate "extra_criteria" stored with this Load object.
  1547. Load objects are typically pulled from the cached version of
  1548. the statement from a QueryContext. The statement currently being
  1549. executed will have new values (and keys) for bound parameters in the
  1550. extra criteria which need to be applied by loader strategies when
  1551. they handle this criteria for a result set.
  1552. """
  1553. assert (
  1554. self._extra_criteria
  1555. ), "this should only be called if _extra_criteria is present"
  1556. orig_query = context.compile_state.select_statement
  1557. current_query = context.query
  1558. # NOTE: while it seems like we should not do the "apply" operation
  1559. # here if orig_query is current_query, skipping it in the "optimized"
  1560. # case causes the query to be different from a cache key perspective,
  1561. # because we are creating a copy of the criteria which is no longer
  1562. # the same identity of the _extra_criteria in the loader option
  1563. # itself. cache key logic produces a different key for
  1564. # (A, copy_of_A) vs. (A, A), because in the latter case it shortens
  1565. # the second part of the key to just indicate on identity.
  1566. # if orig_query is current_query:
  1567. # not cached yet. just do the and_()
  1568. # return and_(*self._extra_criteria)
  1569. k1 = orig_query._generate_cache_key()
  1570. k2 = current_query._generate_cache_key()
  1571. return k2._apply_params_to_element(k1, and_(*self._extra_criteria))
  1572. def _set_of_type_info(self, context, current_path):
  1573. assert self._path_with_polymorphic_path
  1574. pwpi = self._of_type
  1575. assert pwpi
  1576. if not pwpi.is_aliased_class:
  1577. pwpi = inspect(
  1578. orm_util.AliasedInsp._with_polymorphic_factory(
  1579. pwpi.mapper.base_mapper,
  1580. (pwpi.mapper,),
  1581. aliased=True,
  1582. _use_mapper_path=True,
  1583. )
  1584. )
  1585. start_path = self._path_with_polymorphic_path
  1586. if current_path:
  1587. new_path = self._adjust_effective_path_for_current_path(
  1588. start_path, current_path
  1589. )
  1590. if new_path is None:
  1591. return
  1592. start_path = new_path
  1593. key = ("path_with_polymorphic", start_path.natural_path)
  1594. if key in context:
  1595. existing_aliased_insp = context[key]
  1596. this_aliased_insp = pwpi
  1597. new_aliased_insp = existing_aliased_insp._merge_with(
  1598. this_aliased_insp
  1599. )
  1600. context[key] = new_aliased_insp
  1601. else:
  1602. context[key] = pwpi
  1603. def _prepare_for_compile_state(
  1604. self,
  1605. parent_loader,
  1606. compile_state,
  1607. mapper_entities,
  1608. reconciled_lead_entity,
  1609. raiseerr,
  1610. ):
  1611. # _AttributeStrategyLoad
  1612. current_path = compile_state.current_path
  1613. is_refresh = compile_state.compile_options._for_refresh_state
  1614. assert not self.path.is_token
  1615. if is_refresh and not self.propagate_to_loaders:
  1616. return []
  1617. if self._of_type:
  1618. # apply additional with_polymorphic alias that may have been
  1619. # generated. this has to happen even if this is a defaultload
  1620. self._set_of_type_info(compile_state.attributes, current_path)
  1621. # omit setting loader attributes for a "defaultload" type of option
  1622. if not self.strategy and not self.local_opts:
  1623. return []
  1624. if raiseerr and not reconciled_lead_entity:
  1625. self._raise_for_no_match(parent_loader, mapper_entities)
  1626. if self.path.has_entity:
  1627. effective_path = self.path.parent
  1628. else:
  1629. effective_path = self.path
  1630. if current_path:
  1631. assert effective_path is not None
  1632. effective_path = self._adjust_effective_path_for_current_path(
  1633. effective_path, current_path
  1634. )
  1635. if effective_path is None:
  1636. return []
  1637. return [("loader", cast(PathRegistry, effective_path).natural_path)]
  1638. def __getstate__(self):
  1639. d = super().__getstate__()
  1640. # can't pickle this. See
  1641. # test_pickled.py -> test_lazyload_extra_criteria_not_supported
  1642. # where we should be emitting a warning for the usual case where this
  1643. # would be non-None
  1644. d["_extra_criteria"] = ()
  1645. if self._path_with_polymorphic_path:
  1646. d[
  1647. "_path_with_polymorphic_path"
  1648. ] = self._path_with_polymorphic_path.serialize()
  1649. if self._of_type:
  1650. if self._of_type.is_aliased_class:
  1651. d["_of_type"] = None
  1652. elif self._of_type.is_mapper:
  1653. d["_of_type"] = self._of_type.class_
  1654. else:
  1655. assert False, "unexpected object for _of_type"
  1656. return d
  1657. def __setstate__(self, state):
  1658. super().__setstate__(state)
  1659. if state.get("_path_with_polymorphic_path", None):
  1660. self._path_with_polymorphic_path = PathRegistry.deserialize(
  1661. state["_path_with_polymorphic_path"]
  1662. )
  1663. else:
  1664. self._path_with_polymorphic_path = None
  1665. if state.get("_of_type", None):
  1666. self._of_type = inspect(state["_of_type"])
  1667. else:
  1668. self._of_type = None
  1669. class _TokenStrategyLoad(_LoadElement):
  1670. """Loader strategies against wildcard attributes
  1671. e.g.::
  1672. raiseload('*')
  1673. Load(User).lazyload('*')
  1674. defer('*')
  1675. load_only(User.name, User.email) # will create a defer('*')
  1676. joinedload(User.addresses).raiseload('*')
  1677. """
  1678. __visit_name__ = "token_strategy_load_element"
  1679. inherit_cache = True
  1680. is_class_strategy = False
  1681. is_token_strategy = True
  1682. def _init_path(
  1683. self, path, attr, wildcard_key, attr_group, raiseerr, extra_criteria
  1684. ):
  1685. # assert isinstance(attr, str) or attr is None
  1686. if attr is not None:
  1687. default_token = attr.endswith(_DEFAULT_TOKEN)
  1688. if attr.endswith(_WILDCARD_TOKEN) or default_token:
  1689. if wildcard_key:
  1690. attr = f"{wildcard_key}:{attr}"
  1691. path = path.token(attr)
  1692. return path
  1693. else:
  1694. raise sa_exc.ArgumentError(
  1695. "Strings are not accepted for attribute names in loader "
  1696. "options; please use class-bound attributes directly."
  1697. )
  1698. return path
  1699. def _prepare_for_compile_state(
  1700. self,
  1701. parent_loader,
  1702. compile_state,
  1703. mapper_entities,
  1704. reconciled_lead_entity,
  1705. raiseerr,
  1706. ):
  1707. # _TokenStrategyLoad
  1708. current_path = compile_state.current_path
  1709. is_refresh = compile_state.compile_options._for_refresh_state
  1710. assert self.path.is_token
  1711. if is_refresh and not self.propagate_to_loaders:
  1712. return []
  1713. # omit setting attributes for a "defaultload" type of option
  1714. if not self.strategy and not self.local_opts:
  1715. return []
  1716. effective_path = self.path
  1717. if reconciled_lead_entity:
  1718. effective_path = PathRegistry.coerce(
  1719. (reconciled_lead_entity,) + effective_path.path[1:]
  1720. )
  1721. if current_path:
  1722. new_effective_path = self._adjust_effective_path_for_current_path(
  1723. effective_path, current_path
  1724. )
  1725. if new_effective_path is None:
  1726. return []
  1727. effective_path = new_effective_path
  1728. # for a wildcard token, expand out the path we set
  1729. # to encompass everything from the query entity on
  1730. # forward. not clear if this is necessary when current_path
  1731. # is set.
  1732. return [
  1733. ("loader", natural_path)
  1734. for natural_path in (
  1735. cast(
  1736. TokenRegistry, effective_path
  1737. )._generate_natural_for_superclasses()
  1738. )
  1739. ]
  1740. class _ClassStrategyLoad(_LoadElement):
  1741. """Loader strategies that deals with a class as a target, not
  1742. an attribute path
  1743. e.g.::
  1744. q = s.query(Person).options(
  1745. selectin_polymorphic(Person, [Engineer, Manager])
  1746. )
  1747. """
  1748. inherit_cache = True
  1749. is_class_strategy = True
  1750. is_token_strategy = False
  1751. __visit_name__ = "class_strategy_load_element"
  1752. def _init_path(
  1753. self, path, attr, wildcard_key, attr_group, raiseerr, extra_criteria
  1754. ):
  1755. return path
  1756. def _prepare_for_compile_state(
  1757. self,
  1758. parent_loader,
  1759. compile_state,
  1760. mapper_entities,
  1761. reconciled_lead_entity,
  1762. raiseerr,
  1763. ):
  1764. # _ClassStrategyLoad
  1765. current_path = compile_state.current_path
  1766. is_refresh = compile_state.compile_options._for_refresh_state
  1767. if is_refresh and not self.propagate_to_loaders:
  1768. return []
  1769. # omit setting attributes for a "defaultload" type of option
  1770. if not self.strategy and not self.local_opts:
  1771. return []
  1772. effective_path = self.path
  1773. if current_path:
  1774. new_effective_path = self._adjust_effective_path_for_current_path(
  1775. effective_path, current_path
  1776. )
  1777. if new_effective_path is None:
  1778. return []
  1779. effective_path = new_effective_path
  1780. return [("loader", effective_path.natural_path)]
  1781. def _generate_from_keys(
  1782. meth: Callable[..., _AbstractLoad],
  1783. keys: Tuple[_AttrType, ...],
  1784. chained: bool,
  1785. kw: Any,
  1786. ) -> _AbstractLoad:
  1787. lead_element: Optional[_AbstractLoad] = None
  1788. attr: Any
  1789. for is_default, _keys in (True, keys[0:-1]), (False, keys[-1:]):
  1790. for attr in _keys:
  1791. if isinstance(attr, str):
  1792. if attr.startswith("." + _WILDCARD_TOKEN):
  1793. util.warn_deprecated(
  1794. "The undocumented `.{WILDCARD}` format is "
  1795. "deprecated "
  1796. "and will be removed in a future version as "
  1797. "it is "
  1798. "believed to be unused. "
  1799. "If you have been using this functionality, "
  1800. "please "
  1801. "comment on Issue #4390 on the SQLAlchemy project "
  1802. "tracker.",
  1803. version="1.4",
  1804. )
  1805. attr = attr[1:]
  1806. if attr == _WILDCARD_TOKEN:
  1807. if is_default:
  1808. raise sa_exc.ArgumentError(
  1809. "Wildcard token cannot be followed by "
  1810. "another entity",
  1811. )
  1812. if lead_element is None:
  1813. lead_element = _WildcardLoad()
  1814. lead_element = meth(lead_element, _DEFAULT_TOKEN, **kw)
  1815. else:
  1816. raise sa_exc.ArgumentError(
  1817. "Strings are not accepted for attribute names in "
  1818. "loader options; please use class-bound "
  1819. "attributes directly.",
  1820. )
  1821. else:
  1822. if lead_element is None:
  1823. _, lead_entity, _ = _parse_attr_argument(attr)
  1824. lead_element = Load(lead_entity)
  1825. if is_default:
  1826. if not chained:
  1827. lead_element = lead_element.defaultload(attr)
  1828. else:
  1829. lead_element = meth(
  1830. lead_element, attr, _is_chain=True, **kw
  1831. )
  1832. else:
  1833. lead_element = meth(lead_element, attr, **kw)
  1834. assert lead_element
  1835. return lead_element
  1836. def _parse_attr_argument(
  1837. attr: _AttrType,
  1838. ) -> Tuple[InspectionAttr, _InternalEntityType[Any], MapperProperty[Any]]:
  1839. """parse an attribute or wildcard argument to produce an
  1840. :class:`._AbstractLoad` instance.
  1841. This is used by the standalone loader strategy functions like
  1842. ``joinedload()``, ``defer()``, etc. to produce :class:`_orm.Load` or
  1843. :class:`._WildcardLoad` objects.
  1844. """
  1845. try:
  1846. # TODO: need to figure out this None thing being returned by
  1847. # inspect(), it should not have None as an option in most cases
  1848. # if at all
  1849. insp: InspectionAttr = inspect(attr) # type: ignore
  1850. except sa_exc.NoInspectionAvailable as err:
  1851. raise sa_exc.ArgumentError(
  1852. "expected ORM mapped attribute for loader strategy argument"
  1853. ) from err
  1854. lead_entity: _InternalEntityType[Any]
  1855. if insp_is_mapper_property(insp):
  1856. lead_entity = insp.parent
  1857. prop = insp
  1858. elif insp_is_attribute(insp):
  1859. lead_entity = insp.parent
  1860. prop = insp.prop
  1861. else:
  1862. raise sa_exc.ArgumentError(
  1863. "expected ORM mapped attribute for loader strategy argument"
  1864. )
  1865. return insp, lead_entity, prop
  1866. def loader_unbound_fn(fn: _FN) -> _FN:
  1867. """decorator that applies docstrings between standalone loader functions
  1868. and the loader methods on :class:`._AbstractLoad`.
  1869. """
  1870. bound_fn = getattr(_AbstractLoad, fn.__name__)
  1871. fn_doc = bound_fn.__doc__
  1872. bound_fn.__doc__ = f"""Produce a new :class:`_orm.Load` object with the
  1873. :func:`_orm.{fn.__name__}` option applied.
  1874. See :func:`_orm.{fn.__name__}` for usage examples.
  1875. """
  1876. fn.__doc__ = fn_doc
  1877. return fn
  1878. # standalone functions follow. docstrings are filled in
  1879. # by the ``@loader_unbound_fn`` decorator.
  1880. @loader_unbound_fn
  1881. def contains_eager(*keys: _AttrType, **kw: Any) -> _AbstractLoad:
  1882. return _generate_from_keys(Load.contains_eager, keys, True, kw)
  1883. @loader_unbound_fn
  1884. def load_only(*attrs: _AttrType, raiseload: bool = False) -> _AbstractLoad:
  1885. # TODO: attrs against different classes. we likely have to
  1886. # add some extra state to Load of some kind
  1887. _, lead_element, _ = _parse_attr_argument(attrs[0])
  1888. return Load(lead_element).load_only(*attrs, raiseload=raiseload)
  1889. @loader_unbound_fn
  1890. def joinedload(*keys: _AttrType, **kw: Any) -> _AbstractLoad:
  1891. return _generate_from_keys(Load.joinedload, keys, False, kw)
  1892. @loader_unbound_fn
  1893. def subqueryload(*keys: _AttrType) -> _AbstractLoad:
  1894. return _generate_from_keys(Load.subqueryload, keys, False, {})
  1895. @loader_unbound_fn
  1896. def selectinload(
  1897. *keys: _AttrType, recursion_depth: Optional[int] = None
  1898. ) -> _AbstractLoad:
  1899. return _generate_from_keys(
  1900. Load.selectinload, keys, False, {"recursion_depth": recursion_depth}
  1901. )
  1902. @loader_unbound_fn
  1903. def lazyload(*keys: _AttrType) -> _AbstractLoad:
  1904. return _generate_from_keys(Load.lazyload, keys, False, {})
  1905. @loader_unbound_fn
  1906. def immediateload(
  1907. *keys: _AttrType, recursion_depth: Optional[int] = None
  1908. ) -> _AbstractLoad:
  1909. return _generate_from_keys(
  1910. Load.immediateload, keys, False, {"recursion_depth": recursion_depth}
  1911. )
  1912. @loader_unbound_fn
  1913. def noload(*keys: _AttrType) -> _AbstractLoad:
  1914. return _generate_from_keys(Load.noload, keys, False, {})
  1915. @loader_unbound_fn
  1916. def raiseload(*keys: _AttrType, **kw: Any) -> _AbstractLoad:
  1917. return _generate_from_keys(Load.raiseload, keys, False, kw)
  1918. @loader_unbound_fn
  1919. def defaultload(*keys: _AttrType) -> _AbstractLoad:
  1920. return _generate_from_keys(Load.defaultload, keys, False, {})
  1921. @loader_unbound_fn
  1922. def defer(
  1923. key: _AttrType, *addl_attrs: _AttrType, raiseload: bool = False
  1924. ) -> _AbstractLoad:
  1925. if addl_attrs:
  1926. util.warn_deprecated(
  1927. "The *addl_attrs on orm.defer is deprecated. Please use "
  1928. "method chaining in conjunction with defaultload() to "
  1929. "indicate a path.",
  1930. version="1.3",
  1931. )
  1932. if raiseload:
  1933. kw = {"raiseload": raiseload}
  1934. else:
  1935. kw = {}
  1936. return _generate_from_keys(Load.defer, (key,) + addl_attrs, False, kw)
  1937. @loader_unbound_fn
  1938. def undefer(key: _AttrType, *addl_attrs: _AttrType) -> _AbstractLoad:
  1939. if addl_attrs:
  1940. util.warn_deprecated(
  1941. "The *addl_attrs on orm.undefer is deprecated. Please use "
  1942. "method chaining in conjunction with defaultload() to "
  1943. "indicate a path.",
  1944. version="1.3",
  1945. )
  1946. return _generate_from_keys(Load.undefer, (key,) + addl_attrs, False, {})
  1947. @loader_unbound_fn
  1948. def undefer_group(name: str) -> _AbstractLoad:
  1949. element = _WildcardLoad()
  1950. return element.undefer_group(name)
  1951. @loader_unbound_fn
  1952. def with_expression(
  1953. key: _AttrType, expression: _ColumnExpressionArgument[Any]
  1954. ) -> _AbstractLoad:
  1955. return _generate_from_keys(
  1956. Load.with_expression, (key,), False, {"expression": expression}
  1957. )
  1958. @loader_unbound_fn
  1959. def selectin_polymorphic(
  1960. base_cls: _EntityType[Any], classes: Iterable[Type[Any]]
  1961. ) -> _AbstractLoad:
  1962. ul = Load(base_cls)
  1963. return ul.selectin_polymorphic(classes)
  1964. def _raise_for_does_not_link(path, attrname, parent_entity):
  1965. if len(path) > 1:
  1966. path_is_of_type = path[-1].entity is not path[-2].mapper.class_
  1967. if insp_is_aliased_class(parent_entity):
  1968. parent_entity_str = str(parent_entity)
  1969. else:
  1970. parent_entity_str = parent_entity.class_.__name__
  1971. raise sa_exc.ArgumentError(
  1972. f'ORM mapped entity or attribute "{attrname}" does not '
  1973. f'link from relationship "{path[-2]}%s".%s'
  1974. % (
  1975. f".of_type({path[-1]})" if path_is_of_type else "",
  1976. (
  1977. " Did you mean to use "
  1978. f'"{path[-2]}'
  1979. f'.of_type({parent_entity_str})" or "loadopt.options('
  1980. f"selectin_polymorphic({path[-2].mapper.class_.__name__}, "
  1981. f'[{parent_entity_str}]), ...)" ?'
  1982. if not path_is_of_type
  1983. and not path[-1].is_aliased_class
  1984. and orm_util._entity_corresponds_to(
  1985. path.entity, inspect(parent_entity).mapper
  1986. )
  1987. else ""
  1988. ),
  1989. )
  1990. )
  1991. else:
  1992. raise sa_exc.ArgumentError(
  1993. f'ORM mapped attribute "{attrname}" does not '
  1994. f'link mapped class "{path[-1]}"'
  1995. )