strategy_options.py 84 KB

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