strategy_options.py 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031
  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. """
  8. """
  9. from . import util as orm_util
  10. from .attributes import QueryableAttribute
  11. from .base import _class_to_mapper
  12. from .base import _is_aliased_class
  13. from .base import _is_mapped_class
  14. from .base import InspectionAttr
  15. from .interfaces import LoaderOption
  16. from .interfaces import MapperProperty
  17. from .interfaces import PropComparator
  18. from .path_registry import _DEFAULT_TOKEN
  19. from .path_registry import _WILDCARD_TOKEN
  20. from .path_registry import PathRegistry
  21. from .path_registry import TokenRegistry
  22. from .util import _orm_full_deannotate
  23. from .. import exc as sa_exc
  24. from .. import inspect
  25. from .. import util
  26. from ..sql import and_
  27. from ..sql import coercions
  28. from ..sql import roles
  29. from ..sql import traversals
  30. from ..sql import visitors
  31. from ..sql.base import _generative
  32. from ..sql.base import Generative
  33. class Load(Generative, LoaderOption):
  34. """Represents loader options which modify the state of a
  35. :class:`_query.Query` in order to affect how various mapped attributes are
  36. loaded.
  37. The :class:`_orm.Load` object is in most cases used implicitly behind the
  38. scenes when one makes use of a query option like :func:`_orm.joinedload`,
  39. :func:`.defer`, or similar. However, the :class:`_orm.Load` object
  40. can also be used directly, and in some cases can be useful.
  41. To use :class:`_orm.Load` directly, instantiate it with the target mapped
  42. class as the argument. This style of usage is
  43. useful when dealing with a :class:`_query.Query`
  44. that has multiple entities::
  45. myopt = Load(MyClass).joinedload("widgets")
  46. The above ``myopt`` can now be used with :meth:`_query.Query.options`,
  47. where it
  48. will only take effect for the ``MyClass`` entity::
  49. session.query(MyClass, MyOtherClass).options(myopt)
  50. One case where :class:`_orm.Load`
  51. is useful as public API is when specifying
  52. "wildcard" options that only take effect for a certain class::
  53. session.query(Order).options(Load(Order).lazyload('*'))
  54. Above, all relationships on ``Order`` will be lazy-loaded, but other
  55. attributes on those descendant objects will load using their normal
  56. loader strategy.
  57. .. seealso::
  58. :ref:`deferred_options`
  59. :ref:`deferred_loading_w_multiple`
  60. :ref:`relationship_loader_options`
  61. """
  62. _is_strategy_option = True
  63. _cache_key_traversal = [
  64. ("path", visitors.ExtendedInternalTraversal.dp_has_cache_key),
  65. ("strategy", visitors.ExtendedInternalTraversal.dp_plain_obj),
  66. ("_of_type", visitors.ExtendedInternalTraversal.dp_multi),
  67. ("_extra_criteria", visitors.InternalTraversal.dp_clauseelement_list),
  68. (
  69. "_context_cache_key",
  70. visitors.ExtendedInternalTraversal.dp_has_cache_key_tuples,
  71. ),
  72. (
  73. "local_opts",
  74. visitors.ExtendedInternalTraversal.dp_string_multi_dict,
  75. ),
  76. ]
  77. def __init__(self, entity):
  78. insp = inspect(entity)
  79. insp._post_inspect
  80. self.path = insp._path_registry
  81. # note that this .context is shared among all descendant
  82. # Load objects
  83. self.context = util.OrderedDict()
  84. self.local_opts = {}
  85. self.is_class_strategy = False
  86. @classmethod
  87. def for_existing_path(cls, path):
  88. load = cls.__new__(cls)
  89. load.path = path
  90. load.context = {}
  91. load.local_opts = {}
  92. load._of_type = None
  93. load._extra_criteria = ()
  94. return load
  95. def _adapt_cached_option_to_uncached_option(self, context, uncached_opt):
  96. return self._adjust_for_extra_criteria(context)
  97. def _generate_extra_criteria(self, context):
  98. """Apply the current bound parameters in a QueryContext to the
  99. immediate "extra_criteria" stored with this Load object.
  100. Load objects are typically pulled from the cached version of
  101. the statement from a QueryContext. The statement currently being
  102. executed will have new values (and keys) for bound parameters in the
  103. extra criteria which need to be applied by loader strategies when
  104. they handle this criteria for a result set.
  105. """
  106. assert (
  107. self._extra_criteria
  108. ), "this should only be called if _extra_criteria is present"
  109. orig_query = context.compile_state.select_statement
  110. current_query = context.query
  111. # NOTE: while it seems like we should not do the "apply" operation
  112. # here if orig_query is current_query, skipping it in the "optimized"
  113. # case causes the query to be different from a cache key perspective,
  114. # because we are creating a copy of the criteria which is no longer
  115. # the same identity of the _extra_criteria in the loader option
  116. # itself. cache key logic produces a different key for
  117. # (A, copy_of_A) vs. (A, A), because in the latter case it shortens
  118. # the second part of the key to just indicate on identity.
  119. # if orig_query is current_query:
  120. # not cached yet. just do the and_()
  121. # return and_(*self._extra_criteria)
  122. k1 = orig_query._generate_cache_key()
  123. k2 = current_query._generate_cache_key()
  124. return k2._apply_params_to_element(k1, and_(*self._extra_criteria))
  125. def _adjust_for_extra_criteria(self, context):
  126. """Apply the current bound parameters in a QueryContext to all
  127. occurrences "extra_criteria" stored within al this Load object;
  128. copying in place.
  129. """
  130. orig_query = context.compile_state.select_statement
  131. applied = {}
  132. ck = [None, None]
  133. def process(opt):
  134. if not opt._extra_criteria:
  135. return
  136. if ck[0] is None:
  137. ck[:] = (
  138. orig_query._generate_cache_key(),
  139. context.query._generate_cache_key(),
  140. )
  141. k1, k2 = ck
  142. opt._extra_criteria = tuple(
  143. k2._apply_params_to_element(k1, crit)
  144. for crit in opt._extra_criteria
  145. )
  146. return self._deep_clone(applied, process)
  147. def _deep_clone(self, applied, process):
  148. if self in applied:
  149. return applied[self]
  150. cloned = self._generate()
  151. applied[self] = cloned
  152. cloned.strategy = self.strategy
  153. assert cloned.propagate_to_loaders == self.propagate_to_loaders
  154. assert cloned.is_class_strategy == self.is_class_strategy
  155. assert cloned.is_opts_only == self.is_opts_only
  156. if self.context:
  157. cloned.context = util.OrderedDict(
  158. [
  159. (
  160. key,
  161. value._deep_clone(applied, process)
  162. if isinstance(value, Load)
  163. else value,
  164. )
  165. for key, value in self.context.items()
  166. ]
  167. )
  168. cloned.local_opts.update(self.local_opts)
  169. process(cloned)
  170. return cloned
  171. @property
  172. def _context_cache_key(self):
  173. serialized = []
  174. if self.context is None:
  175. return []
  176. for (key, loader_path), obj in self.context.items():
  177. if key != "loader":
  178. continue
  179. serialized.append(loader_path + (obj,))
  180. return serialized
  181. def _generate(self):
  182. cloned = super(Load, self)._generate()
  183. cloned.local_opts = {}
  184. return cloned
  185. is_opts_only = False
  186. is_class_strategy = False
  187. strategy = None
  188. propagate_to_loaders = False
  189. _of_type = None
  190. _extra_criteria = ()
  191. def process_compile_state_replaced_entities(
  192. self, compile_state, mapper_entities
  193. ):
  194. if not compile_state.compile_options._enable_eagerloads:
  195. return
  196. # process is being run here so that the options given are validated
  197. # against what the lead entities were, as well as to accommodate
  198. # for the entities having been replaced with equivalents
  199. self._process(
  200. compile_state,
  201. mapper_entities,
  202. not bool(compile_state.current_path),
  203. )
  204. def process_compile_state(self, compile_state):
  205. if not compile_state.compile_options._enable_eagerloads:
  206. return
  207. self._process(
  208. compile_state,
  209. compile_state._lead_mapper_entities,
  210. not bool(compile_state.current_path)
  211. and not compile_state.compile_options._for_refresh_state,
  212. )
  213. def _process(self, compile_state, mapper_entities, raiseerr):
  214. is_refresh = compile_state.compile_options._for_refresh_state
  215. current_path = compile_state.current_path
  216. if current_path:
  217. for (token, start_path), loader in self.context.items():
  218. if is_refresh and not loader.propagate_to_loaders:
  219. continue
  220. chopped_start_path = self._chop_path(start_path, current_path)
  221. if chopped_start_path is not None:
  222. compile_state.attributes[
  223. (token, chopped_start_path)
  224. ] = loader
  225. else:
  226. compile_state.attributes.update(self.context)
  227. def _generate_path(
  228. self,
  229. path,
  230. attr,
  231. for_strategy,
  232. wildcard_key,
  233. raiseerr=True,
  234. polymorphic_entity_context=None,
  235. ):
  236. existing_of_type = self._of_type
  237. self._of_type = None
  238. if raiseerr and not path.has_entity:
  239. if isinstance(path, TokenRegistry):
  240. raise sa_exc.ArgumentError(
  241. "Wildcard token cannot be followed by another entity"
  242. )
  243. else:
  244. raise sa_exc.ArgumentError(
  245. "Mapped attribute '%s' does not "
  246. "refer to a mapped entity" % (path.prop,)
  247. )
  248. if isinstance(attr, util.string_types):
  249. default_token = attr.endswith(_DEFAULT_TOKEN)
  250. attr_str_name = attr
  251. if attr.endswith(_WILDCARD_TOKEN) or default_token:
  252. if default_token:
  253. self.propagate_to_loaders = False
  254. if wildcard_key:
  255. attr = "%s:%s" % (wildcard_key, attr)
  256. # TODO: AliasedInsp inside the path for of_type is not
  257. # working for a with_polymorphic entity because the
  258. # relationship loaders don't render the with_poly into the
  259. # path. See #4469 which will try to improve this
  260. if existing_of_type and not existing_of_type.is_aliased_class:
  261. path = path.parent[existing_of_type]
  262. path = path.token(attr)
  263. self.path = path
  264. return path
  265. if existing_of_type:
  266. ent = inspect(existing_of_type)
  267. else:
  268. ent = path.entity
  269. util.warn_deprecated_20(
  270. "Using strings to indicate column or "
  271. "relationship paths in loader options is deprecated "
  272. "and will be removed in SQLAlchemy 2.0. Please use "
  273. "the class-bound attribute directly.",
  274. )
  275. try:
  276. # use getattr on the class to work around
  277. # synonyms, hybrids, etc.
  278. attr = getattr(ent.class_, attr)
  279. except AttributeError as err:
  280. if raiseerr:
  281. util.raise_(
  282. sa_exc.ArgumentError(
  283. 'Can\'t find property named "%s" on '
  284. "%s in this Query." % (attr, ent)
  285. ),
  286. replace_context=err,
  287. )
  288. else:
  289. return None
  290. else:
  291. try:
  292. attr = found_property = attr.property
  293. except AttributeError as ae:
  294. if not isinstance(attr, MapperProperty):
  295. util.raise_(
  296. sa_exc.ArgumentError(
  297. 'Expected attribute "%s" on %s to be a '
  298. "mapped attribute; "
  299. "instead got %s object."
  300. % (attr_str_name, ent, type(attr))
  301. ),
  302. replace_context=ae,
  303. )
  304. else:
  305. raise
  306. path = path[attr]
  307. else:
  308. insp = inspect(attr)
  309. if insp.is_mapper or insp.is_aliased_class:
  310. # TODO: this does not appear to be a valid codepath. "attr"
  311. # would never be a mapper. This block is present in 1.2
  312. # as well however does not seem to be accessed in any tests.
  313. if not orm_util._entity_corresponds_to_use_path_impl(
  314. attr.parent, path[-1]
  315. ):
  316. if raiseerr:
  317. raise sa_exc.ArgumentError(
  318. "Attribute '%s' does not "
  319. "link from element '%s'" % (attr, path.entity)
  320. )
  321. else:
  322. return None
  323. elif insp.is_property:
  324. prop = found_property = attr
  325. path = path[prop]
  326. elif insp.is_attribute:
  327. prop = found_property = attr.property
  328. if not orm_util._entity_corresponds_to_use_path_impl(
  329. attr.parent, path[-1]
  330. ):
  331. if raiseerr:
  332. raise sa_exc.ArgumentError(
  333. 'Attribute "%s" does not '
  334. 'link from element "%s".%s'
  335. % (
  336. attr,
  337. path.entity,
  338. (
  339. " Did you mean to use "
  340. "%s.of_type(%s)?"
  341. % (path[-2], attr.class_.__name__)
  342. if len(path) > 1
  343. and path.entity.is_mapper
  344. and attr.parent.is_aliased_class
  345. else ""
  346. ),
  347. )
  348. )
  349. else:
  350. return None
  351. if attr._extra_criteria and not self._extra_criteria:
  352. # in most cases, the process that brings us here will have
  353. # already established _extra_criteria. however if not,
  354. # and it's present on the attribute, then use that.
  355. self._extra_criteria = attr._extra_criteria
  356. if getattr(attr, "_of_type", None):
  357. ac = attr._of_type
  358. ext_info = of_type_info = inspect(ac)
  359. if polymorphic_entity_context is None:
  360. polymorphic_entity_context = self.context
  361. existing = path.entity_path[prop].get(
  362. polymorphic_entity_context, "path_with_polymorphic"
  363. )
  364. if not ext_info.is_aliased_class:
  365. ac = orm_util.with_polymorphic(
  366. ext_info.mapper.base_mapper,
  367. ext_info.mapper,
  368. aliased=True,
  369. _use_mapper_path=True,
  370. _existing_alias=inspect(existing)
  371. if existing is not None
  372. else None,
  373. )
  374. ext_info = inspect(ac)
  375. path.entity_path[prop].set(
  376. polymorphic_entity_context, "path_with_polymorphic", ac
  377. )
  378. path = path[prop][ext_info]
  379. self._of_type = of_type_info
  380. else:
  381. path = path[prop]
  382. if for_strategy is not None:
  383. found_property._get_strategy(for_strategy)
  384. if path.has_entity:
  385. path = path.entity_path
  386. self.path = path
  387. return path
  388. def __str__(self):
  389. return "Load(strategy=%r)" % (self.strategy,)
  390. def _coerce_strat(self, strategy):
  391. if strategy is not None:
  392. strategy = tuple(sorted(strategy.items()))
  393. return strategy
  394. def _apply_to_parent(self, parent, applied, bound):
  395. raise NotImplementedError(
  396. "Only 'unbound' loader options may be used with the "
  397. "Load.options() method"
  398. )
  399. @_generative
  400. def options(self, *opts):
  401. r"""Apply a series of options as sub-options to this
  402. :class:`_orm.Load`
  403. object.
  404. E.g.::
  405. query = session.query(Author)
  406. query = query.options(
  407. joinedload(Author.book).options(
  408. load_only(Book.summary, Book.excerpt),
  409. joinedload(Book.citations).options(
  410. joinedload(Citation.author)
  411. )
  412. )
  413. )
  414. :param \*opts: A series of loader option objects (ultimately
  415. :class:`_orm.Load` objects) which should be applied to the path
  416. specified by this :class:`_orm.Load` object.
  417. .. versionadded:: 1.3.6
  418. .. seealso::
  419. :func:`.defaultload`
  420. :ref:`relationship_loader_options`
  421. :ref:`deferred_loading_w_multiple`
  422. """
  423. apply_cache = {}
  424. bound = not isinstance(self, _UnboundLoad)
  425. if bound:
  426. raise NotImplementedError(
  427. "The options() method is currently only supported "
  428. "for 'unbound' loader options"
  429. )
  430. for opt in opts:
  431. try:
  432. opt._apply_to_parent(self, apply_cache, bound)
  433. except AttributeError as ae:
  434. if not isinstance(opt, Load):
  435. util.raise_(
  436. sa_exc.ArgumentError(
  437. "Loader option %s is not compatible with the "
  438. "Load.options() method." % (opt,)
  439. ),
  440. from_=ae,
  441. )
  442. else:
  443. raise
  444. @_generative
  445. def set_relationship_strategy(
  446. self, attr, strategy, propagate_to_loaders=True
  447. ):
  448. strategy = self._coerce_strat(strategy)
  449. self.propagate_to_loaders = propagate_to_loaders
  450. cloned = self._clone_for_bind_strategy(attr, strategy, "relationship")
  451. self.path = cloned.path
  452. self._of_type = cloned._of_type
  453. self._extra_criteria = cloned._extra_criteria
  454. cloned.is_class_strategy = self.is_class_strategy = False
  455. self.propagate_to_loaders = cloned.propagate_to_loaders
  456. @_generative
  457. def set_column_strategy(self, attrs, strategy, opts=None, opts_only=False):
  458. strategy = self._coerce_strat(strategy)
  459. self.is_class_strategy = False
  460. for attr in attrs:
  461. cloned = self._clone_for_bind_strategy(
  462. attr, strategy, "column", opts_only=opts_only, opts=opts
  463. )
  464. cloned.propagate_to_loaders = True
  465. @_generative
  466. def set_generic_strategy(self, attrs, strategy):
  467. strategy = self._coerce_strat(strategy)
  468. for attr in attrs:
  469. cloned = self._clone_for_bind_strategy(attr, strategy, None)
  470. cloned.propagate_to_loaders = True
  471. @_generative
  472. def set_class_strategy(self, strategy, opts):
  473. strategy = self._coerce_strat(strategy)
  474. cloned = self._clone_for_bind_strategy(None, strategy, None)
  475. cloned.is_class_strategy = True
  476. cloned.propagate_to_loaders = True
  477. cloned.local_opts.update(opts)
  478. def _clone_for_bind_strategy(
  479. self, attr, strategy, wildcard_key, opts_only=False, opts=None
  480. ):
  481. """Create an anonymous clone of the Load/_UnboundLoad that is suitable
  482. to be placed in the context / _to_bind collection of this Load
  483. object. The clone will then lose references to context/_to_bind
  484. in order to not create reference cycles.
  485. """
  486. cloned = self._generate()
  487. cloned._generate_path(self.path, attr, strategy, wildcard_key)
  488. cloned.strategy = strategy
  489. cloned.local_opts = self.local_opts
  490. if opts:
  491. cloned.local_opts.update(opts)
  492. if opts_only:
  493. cloned.is_opts_only = True
  494. if strategy or cloned.is_opts_only:
  495. cloned._set_path_strategy()
  496. return cloned
  497. def _set_for_path(self, context, path, replace=True, merge_opts=False):
  498. if merge_opts or not replace:
  499. existing = path.get(context, "loader")
  500. if existing:
  501. if merge_opts:
  502. existing.local_opts.update(self.local_opts)
  503. existing._extra_criteria += self._extra_criteria
  504. else:
  505. path.set(context, "loader", self)
  506. else:
  507. existing = path.get(context, "loader")
  508. path.set(context, "loader", self)
  509. if existing and existing.is_opts_only:
  510. self.local_opts.update(existing.local_opts)
  511. existing._extra_criteria += self._extra_criteria
  512. def _set_path_strategy(self):
  513. if not self.is_class_strategy and self.path.has_entity:
  514. effective_path = self.path.parent
  515. else:
  516. effective_path = self.path
  517. if effective_path.is_token:
  518. for path in effective_path.generate_for_superclasses():
  519. self._set_for_path(
  520. self.context,
  521. path,
  522. replace=True,
  523. merge_opts=self.is_opts_only,
  524. )
  525. else:
  526. self._set_for_path(
  527. self.context,
  528. effective_path,
  529. replace=True,
  530. merge_opts=self.is_opts_only,
  531. )
  532. # remove cycles; _set_path_strategy is always invoked on an
  533. # anonymous clone of the Load / UnboundLoad object since #5056
  534. self.context = None
  535. def __getstate__(self):
  536. d = self.__dict__.copy()
  537. # can't pickle this right now; warning is raised by strategies
  538. d["_extra_criteria"] = ()
  539. if d["context"] is not None:
  540. d["context"] = PathRegistry.serialize_context_dict(
  541. d["context"], ("loader",)
  542. )
  543. d["path"] = self.path.serialize()
  544. return d
  545. def __setstate__(self, state):
  546. self.__dict__.update(state)
  547. self.path = PathRegistry.deserialize(self.path)
  548. if self.context is not None:
  549. self.context = PathRegistry.deserialize_context_dict(self.context)
  550. def _chop_path(self, to_chop, path):
  551. i = -1
  552. for i, (c_token, p_token) in enumerate(zip(to_chop, path.path)):
  553. if isinstance(c_token, util.string_types):
  554. # TODO: this is approximated from the _UnboundLoad
  555. # version and probably has issues, not fully covered.
  556. if i == 0 and c_token.endswith(":" + _DEFAULT_TOKEN):
  557. return to_chop
  558. elif (
  559. c_token != "relationship:%s" % (_WILDCARD_TOKEN,)
  560. and c_token != p_token.key
  561. ):
  562. return None
  563. if c_token is p_token:
  564. continue
  565. elif (
  566. isinstance(c_token, InspectionAttr)
  567. and c_token.is_mapper
  568. and p_token.is_mapper
  569. and c_token.isa(p_token)
  570. ):
  571. continue
  572. else:
  573. return None
  574. return to_chop[i + 1 :]
  575. class _UnboundLoad(Load):
  576. """Represent a loader option that isn't tied to a root entity.
  577. The loader option will produce an entity-linked :class:`_orm.Load`
  578. object when it is passed :meth:`_query.Query.options`.
  579. This provides compatibility with the traditional system
  580. of freestanding options, e.g. ``joinedload('x.y.z')``.
  581. """
  582. def __init__(self):
  583. self.path = ()
  584. self._to_bind = []
  585. self.local_opts = {}
  586. self._extra_criteria = ()
  587. def _gen_cache_key(self, anon_map, bindparams, _unbound_option_seen=None):
  588. """Inlined gen_cache_key
  589. Original traversal is::
  590. _cache_key_traversal = [
  591. ("path", visitors.ExtendedInternalTraversal.dp_multi_list),
  592. ("strategy", visitors.ExtendedInternalTraversal.dp_plain_obj),
  593. (
  594. "_to_bind",
  595. visitors.ExtendedInternalTraversal.dp_has_cache_key_list,
  596. ),
  597. (
  598. "_extra_criteria",
  599. visitors.InternalTraversal.dp_clauseelement_list),
  600. (
  601. "local_opts",
  602. visitors.ExtendedInternalTraversal.dp_string_multi_dict,
  603. ),
  604. ]
  605. The inlining is so that the "_to_bind" list can be flattened to not
  606. repeat the same UnboundLoad options over and over again.
  607. See #6869
  608. """
  609. idself = id(self)
  610. cls = self.__class__
  611. if idself in anon_map:
  612. return (anon_map[idself], cls)
  613. else:
  614. id_ = anon_map[idself]
  615. vis = traversals._cache_key_traversal_visitor
  616. seen = _unbound_option_seen
  617. if seen is None:
  618. seen = set()
  619. return (
  620. (id_, cls)
  621. + vis.visit_multi_list(
  622. "path", self.path, self, anon_map, bindparams
  623. )
  624. + ("strategy", self.strategy)
  625. + (
  626. (
  627. "_to_bind",
  628. tuple(
  629. elem._gen_cache_key(
  630. anon_map, bindparams, _unbound_option_seen=seen
  631. )
  632. for elem in self._to_bind
  633. if elem not in seen and not seen.add(elem)
  634. ),
  635. )
  636. if self._to_bind
  637. else ()
  638. )
  639. + (
  640. (
  641. "_extra_criteria",
  642. tuple(
  643. elem._gen_cache_key(anon_map, bindparams)
  644. for elem in self._extra_criteria
  645. ),
  646. )
  647. if self._extra_criteria
  648. else ()
  649. )
  650. + (
  651. vis.visit_string_multi_dict(
  652. "local_opts", self.local_opts, self, anon_map, bindparams
  653. )
  654. if self.local_opts
  655. else ()
  656. )
  657. )
  658. _is_chain_link = False
  659. def _set_path_strategy(self):
  660. self._to_bind.append(self)
  661. # remove cycles; _set_path_strategy is always invoked on an
  662. # anonymous clone of the Load / UnboundLoad object since #5056
  663. self._to_bind = None
  664. def _deep_clone(self, applied, process):
  665. if self in applied:
  666. return applied[self]
  667. cloned = self._generate()
  668. applied[self] = cloned
  669. cloned.strategy = self.strategy
  670. assert cloned.propagate_to_loaders == self.propagate_to_loaders
  671. assert cloned.is_class_strategy == self.is_class_strategy
  672. assert cloned.is_opts_only == self.is_opts_only
  673. cloned._to_bind = [
  674. elem._deep_clone(applied, process) for elem in self._to_bind or ()
  675. ]
  676. cloned.local_opts.update(self.local_opts)
  677. process(cloned)
  678. return cloned
  679. def _apply_to_parent(self, parent, applied, bound, to_bind=None):
  680. if self in applied:
  681. return applied[self]
  682. if to_bind is None:
  683. to_bind = self._to_bind
  684. cloned = self._generate()
  685. applied[self] = cloned
  686. cloned.strategy = self.strategy
  687. if self.path:
  688. attr = self.path[-1]
  689. if isinstance(attr, util.string_types) and attr.endswith(
  690. _DEFAULT_TOKEN
  691. ):
  692. attr = attr.split(":")[0] + ":" + _WILDCARD_TOKEN
  693. cloned._generate_path(
  694. parent.path + self.path[0:-1], attr, self.strategy, None
  695. )
  696. # these assertions can go away once the "sub options" API is
  697. # mature
  698. assert cloned.propagate_to_loaders == self.propagate_to_loaders
  699. assert cloned.is_class_strategy == self.is_class_strategy
  700. assert cloned.is_opts_only == self.is_opts_only
  701. uniq = set()
  702. cloned._to_bind = parent._to_bind
  703. cloned._to_bind[:] = [
  704. elem
  705. for elem in cloned._to_bind
  706. if elem not in uniq and not uniq.add(elem)
  707. ] + [
  708. elem._apply_to_parent(parent, applied, bound, to_bind)
  709. for elem in to_bind
  710. if elem not in uniq and not uniq.add(elem)
  711. ]
  712. cloned.local_opts.update(self.local_opts)
  713. return cloned
  714. def _generate_path(self, path, attr, for_strategy, wildcard_key):
  715. if (
  716. wildcard_key
  717. and isinstance(attr, util.string_types)
  718. and attr in (_WILDCARD_TOKEN, _DEFAULT_TOKEN)
  719. ):
  720. if attr == _DEFAULT_TOKEN:
  721. self.propagate_to_loaders = False
  722. attr = "%s:%s" % (wildcard_key, attr)
  723. if path and _is_mapped_class(path[-1]) and not self.is_class_strategy:
  724. path = path[0:-1]
  725. if attr:
  726. path = path + (attr,)
  727. self.path = path
  728. self._extra_criteria = getattr(attr, "_extra_criteria", ())
  729. return path
  730. def __getstate__(self):
  731. d = self.__dict__.copy()
  732. # can't pickle this right now; warning is raised by strategies
  733. d["_extra_criteria"] = ()
  734. d["path"] = self._serialize_path(self.path, filter_aliased_class=True)
  735. return d
  736. def __setstate__(self, state):
  737. ret = []
  738. for key in state["path"]:
  739. if isinstance(key, tuple):
  740. if len(key) == 2:
  741. # support legacy
  742. cls, propkey = key
  743. of_type = None
  744. else:
  745. cls, propkey, of_type = key
  746. prop = getattr(cls, propkey)
  747. if of_type:
  748. prop = prop.of_type(of_type)
  749. ret.append(prop)
  750. else:
  751. ret.append(key)
  752. state["path"] = tuple(ret)
  753. self.__dict__ = state
  754. def _process(self, compile_state, mapper_entities, raiseerr):
  755. dedupes = compile_state.attributes["_unbound_load_dedupes"]
  756. is_refresh = compile_state.compile_options._for_refresh_state
  757. for val in self._to_bind:
  758. if val not in dedupes:
  759. dedupes.add(val)
  760. if is_refresh and not val.propagate_to_loaders:
  761. continue
  762. val._bind_loader(
  763. [ent.entity_zero for ent in mapper_entities],
  764. compile_state.current_path,
  765. compile_state.attributes,
  766. raiseerr,
  767. )
  768. @classmethod
  769. def _from_keys(cls, meth, keys, chained, kw):
  770. opt = _UnboundLoad()
  771. def _split_key(key):
  772. if isinstance(key, util.string_types):
  773. # coerce fooload('*') into "default loader strategy"
  774. if key == _WILDCARD_TOKEN:
  775. return (_DEFAULT_TOKEN,)
  776. # coerce fooload(".*") into "wildcard on default entity"
  777. elif key.startswith("." + _WILDCARD_TOKEN):
  778. util.warn_deprecated(
  779. "The undocumented `.{WILDCARD}` format is deprecated "
  780. "and will be removed in a future version as it is "
  781. "believed to be unused. "
  782. "If you have been using this functionality, please "
  783. "comment on Issue #4390 on the SQLAlchemy project "
  784. "tracker.",
  785. version="1.4",
  786. )
  787. key = key[1:]
  788. return key.split(".")
  789. else:
  790. return (key,)
  791. all_tokens = [token for key in keys for token in _split_key(key)]
  792. for token in all_tokens[0:-1]:
  793. # set _is_chain_link first so that clones of the
  794. # object also inherit this flag
  795. opt._is_chain_link = True
  796. if chained:
  797. opt = meth(opt, token, **kw)
  798. else:
  799. opt = opt.defaultload(token)
  800. opt = meth(opt, all_tokens[-1], **kw)
  801. opt._is_chain_link = False
  802. return opt
  803. def _chop_path(self, to_chop, path):
  804. i = -1
  805. for i, (c_token, (p_entity, p_prop)) in enumerate(
  806. zip(to_chop, path.pairs())
  807. ):
  808. if isinstance(c_token, util.string_types):
  809. if i == 0 and c_token.endswith(":" + _DEFAULT_TOKEN):
  810. return to_chop
  811. elif (
  812. c_token != "relationship:%s" % (_WILDCARD_TOKEN,)
  813. and c_token != p_prop.key
  814. ):
  815. return None
  816. elif isinstance(c_token, PropComparator):
  817. if c_token.property is not p_prop or (
  818. c_token._parententity is not p_entity
  819. and (
  820. not c_token._parententity.is_mapper
  821. or not c_token._parententity.isa(p_entity)
  822. )
  823. ):
  824. return None
  825. else:
  826. i += 1
  827. return to_chop[i:]
  828. def _serialize_path(self, path, filter_aliased_class=False):
  829. ret = []
  830. for token in path:
  831. if isinstance(token, QueryableAttribute):
  832. if (
  833. filter_aliased_class
  834. and token._of_type
  835. and inspect(token._of_type).is_aliased_class
  836. ):
  837. ret.append((token._parentmapper.class_, token.key, None))
  838. else:
  839. ret.append(
  840. (
  841. token._parentmapper.class_,
  842. token.key,
  843. token._of_type.entity if token._of_type else None,
  844. )
  845. )
  846. elif isinstance(token, PropComparator):
  847. ret.append((token._parentmapper.class_, token.key, None))
  848. else:
  849. ret.append(token)
  850. return ret
  851. def _bind_loader(self, entities, current_path, context, raiseerr):
  852. """Convert from an _UnboundLoad() object into a Load() object.
  853. The _UnboundLoad() uses an informal "path" and does not necessarily
  854. refer to a lead entity as it may use string tokens. The Load()
  855. OTOH refers to a complete path. This method reconciles from a
  856. given Query into a Load.
  857. Example::
  858. query = session.query(User).options(
  859. joinedload("orders").joinedload("items"))
  860. The above options will be an _UnboundLoad object along the lines
  861. of (note this is not the exact API of _UnboundLoad)::
  862. _UnboundLoad(
  863. _to_bind=[
  864. _UnboundLoad(["orders"], {"lazy": "joined"}),
  865. _UnboundLoad(["orders", "items"], {"lazy": "joined"}),
  866. ]
  867. )
  868. After this method, we get something more like this (again this is
  869. not exact API)::
  870. Load(
  871. User,
  872. (User, User.orders.property))
  873. Load(
  874. User,
  875. (User, User.orders.property, Order, Order.items.property))
  876. """
  877. start_path = self.path
  878. if self.is_class_strategy and current_path:
  879. start_path += (entities[0],)
  880. # _current_path implies we're in a
  881. # secondary load with an existing path
  882. if current_path:
  883. start_path = self._chop_path(start_path, current_path)
  884. if not start_path:
  885. return None
  886. # look at the first token and try to locate within the Query
  887. # what entity we are referring towards.
  888. token = start_path[0]
  889. if isinstance(token, util.string_types):
  890. entity = self._find_entity_basestring(entities, token, raiseerr)
  891. elif isinstance(token, PropComparator):
  892. prop = token.property
  893. entity = self._find_entity_prop_comparator(
  894. entities, prop, token._parententity, raiseerr
  895. )
  896. elif self.is_class_strategy and _is_mapped_class(token):
  897. entity = inspect(token)
  898. if entity not in entities:
  899. entity = None
  900. else:
  901. raise sa_exc.ArgumentError(
  902. "mapper option expects " "string key or list of attributes"
  903. )
  904. if not entity:
  905. return
  906. path_element = entity
  907. # transfer our entity-less state into a Load() object
  908. # with a real entity path. Start with the lead entity
  909. # we just located, then go through the rest of our path
  910. # tokens and populate into the Load().
  911. loader = Load(path_element)
  912. if context is None:
  913. context = loader.context
  914. loader.strategy = self.strategy
  915. loader.is_opts_only = self.is_opts_only
  916. loader.is_class_strategy = self.is_class_strategy
  917. loader._extra_criteria = self._extra_criteria
  918. path = loader.path
  919. if not loader.is_class_strategy:
  920. for idx, token in enumerate(start_path):
  921. if not loader._generate_path(
  922. loader.path,
  923. token,
  924. self.strategy if idx == len(start_path) - 1 else None,
  925. None,
  926. raiseerr,
  927. polymorphic_entity_context=context,
  928. ):
  929. return
  930. loader.local_opts.update(self.local_opts)
  931. if not loader.is_class_strategy and loader.path.has_entity:
  932. effective_path = loader.path.parent
  933. else:
  934. effective_path = loader.path
  935. # prioritize "first class" options over those
  936. # that were "links in the chain", e.g. "x" and "y" in
  937. # someload("x.y.z") versus someload("x") / someload("x.y")
  938. if effective_path.is_token:
  939. for path in effective_path.generate_for_superclasses():
  940. loader._set_for_path(
  941. context,
  942. path,
  943. replace=not self._is_chain_link,
  944. merge_opts=self.is_opts_only,
  945. )
  946. else:
  947. loader._set_for_path(
  948. context,
  949. effective_path,
  950. replace=not self._is_chain_link,
  951. merge_opts=self.is_opts_only,
  952. )
  953. return loader
  954. def _find_entity_prop_comparator(self, entities, prop, mapper, raiseerr):
  955. if _is_aliased_class(mapper):
  956. searchfor = mapper
  957. else:
  958. searchfor = _class_to_mapper(mapper)
  959. for ent in entities:
  960. if orm_util._entity_corresponds_to(ent, searchfor):
  961. return ent
  962. else:
  963. if raiseerr:
  964. if not list(entities):
  965. raise sa_exc.ArgumentError(
  966. "Query has only expression-based entities, "
  967. 'which do not apply to %s "%s"'
  968. % (util.clsname_as_plain_name(type(prop)), prop)
  969. )
  970. else:
  971. raise sa_exc.ArgumentError(
  972. 'Mapped attribute "%s" does not apply to any of the '
  973. "root entities in this query, e.g. %s. Please "
  974. "specify the full path "
  975. "from one of the root entities to the target "
  976. "attribute. "
  977. % (prop, ", ".join(str(x) for x in entities))
  978. )
  979. else:
  980. return None
  981. def _find_entity_basestring(self, entities, token, raiseerr):
  982. if token.endswith(":" + _WILDCARD_TOKEN):
  983. if len(list(entities)) != 1:
  984. if raiseerr:
  985. raise sa_exc.ArgumentError(
  986. "Can't apply wildcard ('*') or load_only() "
  987. "loader option to multiple entities %s. Specify "
  988. "loader options for each entity individually, such "
  989. "as %s."
  990. % (
  991. ", ".join(str(ent) for ent in entities),
  992. ", ".join(
  993. "Load(%s).some_option('*')" % ent
  994. for ent in entities
  995. ),
  996. )
  997. )
  998. elif token.endswith(_DEFAULT_TOKEN):
  999. raiseerr = False
  1000. for ent in entities:
  1001. # return only the first _MapperEntity when searching
  1002. # based on string prop name. Ideally object
  1003. # attributes are used to specify more exactly.
  1004. return ent
  1005. else:
  1006. if raiseerr:
  1007. raise sa_exc.ArgumentError(
  1008. "Query has only expression-based entities - "
  1009. 'can\'t find property named "%s".' % (token,)
  1010. )
  1011. else:
  1012. return None
  1013. class loader_option(object):
  1014. def __init__(self):
  1015. pass
  1016. def __call__(self, fn):
  1017. self.name = name = fn.__name__
  1018. self.fn = fn
  1019. if hasattr(Load, name):
  1020. raise TypeError("Load class already has a %s method." % (name))
  1021. setattr(Load, name, fn)
  1022. return self
  1023. def _add_unbound_fn(self, fn):
  1024. self._unbound_fn = fn
  1025. fn_doc = self.fn.__doc__
  1026. self.fn.__doc__ = """Produce a new :class:`_orm.Load` object with the
  1027. :func:`_orm.%(name)s` option applied.
  1028. See :func:`_orm.%(name)s` for usage examples.
  1029. """ % {
  1030. "name": self.name
  1031. }
  1032. fn.__doc__ = fn_doc
  1033. return self
  1034. def _add_unbound_all_fn(self, fn):
  1035. fn.__doc__ = """Produce a standalone "all" option for
  1036. :func:`_orm.%(name)s`.
  1037. .. deprecated:: 0.9
  1038. The :func:`_orm.%(name)s_all` function is deprecated, and will be removed
  1039. in a future release. Please use method chaining with
  1040. :func:`_orm.%(name)s` instead, as in::
  1041. session.query(MyClass).options(
  1042. %(name)s("someattribute").%(name)s("anotherattribute")
  1043. )
  1044. """ % {
  1045. "name": self.name
  1046. }
  1047. fn = util.deprecated(
  1048. # This is used by `baked_lazyload_all` was only deprecated in
  1049. # version 1.2 so this must stick around until that is removed
  1050. "0.9",
  1051. "The :func:`.%(name)s_all` function is deprecated, and will be "
  1052. "removed in a future release. Please use method chaining with "
  1053. ":func:`.%(name)s` instead" % {"name": self.name},
  1054. add_deprecation_to_docstring=False,
  1055. )(fn)
  1056. self._unbound_all_fn = fn
  1057. return self
  1058. @loader_option()
  1059. def contains_eager(loadopt, attr, alias=None):
  1060. r"""Indicate that the given attribute should be eagerly loaded from
  1061. columns stated manually in the query.
  1062. This function is part of the :class:`_orm.Load` interface and supports
  1063. both method-chained and standalone operation.
  1064. The option is used in conjunction with an explicit join that loads
  1065. the desired rows, i.e.::
  1066. sess.query(Order).\
  1067. join(Order.user).\
  1068. options(contains_eager(Order.user))
  1069. The above query would join from the ``Order`` entity to its related
  1070. ``User`` entity, and the returned ``Order`` objects would have the
  1071. ``Order.user`` attribute pre-populated.
  1072. It may also be used for customizing the entries in an eagerly loaded
  1073. collection; queries will normally want to use the
  1074. :meth:`_query.Query.populate_existing` method assuming the primary
  1075. collection of parent objects may already have been loaded::
  1076. sess.query(User).\
  1077. join(User.addresses).\
  1078. filter(Address.email_address.like('%@aol.com')).\
  1079. options(contains_eager(User.addresses)).\
  1080. populate_existing()
  1081. See the section :ref:`contains_eager` for complete usage details.
  1082. .. seealso::
  1083. :ref:`loading_toplevel`
  1084. :ref:`contains_eager`
  1085. """
  1086. if alias is not None:
  1087. if not isinstance(alias, str):
  1088. info = inspect(alias)
  1089. alias = info.selectable
  1090. else:
  1091. util.warn_deprecated(
  1092. "Passing a string name for the 'alias' argument to "
  1093. "'contains_eager()` is deprecated, and will not work in a "
  1094. "future release. Please use a sqlalchemy.alias() or "
  1095. "sqlalchemy.orm.aliased() construct.",
  1096. version="1.4",
  1097. )
  1098. elif getattr(attr, "_of_type", None):
  1099. ot = inspect(attr._of_type)
  1100. alias = ot.selectable
  1101. cloned = loadopt.set_relationship_strategy(
  1102. attr, {"lazy": "joined"}, propagate_to_loaders=False
  1103. )
  1104. cloned.local_opts["eager_from_alias"] = alias
  1105. return cloned
  1106. @contains_eager._add_unbound_fn
  1107. def contains_eager(*keys, **kw):
  1108. return _UnboundLoad()._from_keys(
  1109. _UnboundLoad.contains_eager, keys, True, kw
  1110. )
  1111. @loader_option()
  1112. def load_only(loadopt, *attrs):
  1113. """Indicate that for a particular entity, only the given list
  1114. of column-based attribute names should be loaded; all others will be
  1115. deferred.
  1116. This function is part of the :class:`_orm.Load` interface and supports
  1117. both method-chained and standalone operation.
  1118. Example - given a class ``User``, load only the ``name`` and ``fullname``
  1119. attributes::
  1120. session.query(User).options(load_only(User.name, User.fullname))
  1121. Example - given a relationship ``User.addresses -> Address``, specify
  1122. subquery loading for the ``User.addresses`` collection, but on each
  1123. ``Address`` object load only the ``email_address`` attribute::
  1124. session.query(User).options(
  1125. subqueryload(User.addresses).load_only(Address.email_address)
  1126. )
  1127. For a :class:`_query.Query` that has multiple entities,
  1128. the lead entity can be
  1129. specifically referred to using the :class:`_orm.Load` constructor::
  1130. session.query(User, Address).join(User.addresses).options(
  1131. Load(User).load_only(User.name, User.fullname),
  1132. Load(Address).load_only(Address.email_address)
  1133. )
  1134. .. note:: This method will still load a :class:`_schema.Column` even
  1135. if the column property is defined with ``deferred=True``
  1136. for the :func:`.column_property` function.
  1137. .. versionadded:: 0.9.0
  1138. """
  1139. cloned = loadopt.set_column_strategy(
  1140. attrs, {"deferred": False, "instrument": True}
  1141. )
  1142. cloned.set_column_strategy(
  1143. "*", {"deferred": True, "instrument": True}, {"undefer_pks": True}
  1144. )
  1145. return cloned
  1146. @load_only._add_unbound_fn
  1147. def load_only(*attrs):
  1148. return _UnboundLoad().load_only(*attrs)
  1149. @loader_option()
  1150. def joinedload(loadopt, attr, innerjoin=None):
  1151. """Indicate that the given attribute should be loaded using joined
  1152. eager loading.
  1153. This function is part of the :class:`_orm.Load` interface and supports
  1154. both method-chained and standalone operation.
  1155. examples::
  1156. # joined-load the "orders" collection on "User"
  1157. query(User).options(joinedload(User.orders))
  1158. # joined-load Order.items and then Item.keywords
  1159. query(Order).options(
  1160. joinedload(Order.items).joinedload(Item.keywords))
  1161. # lazily load Order.items, but when Items are loaded,
  1162. # joined-load the keywords collection
  1163. query(Order).options(
  1164. lazyload(Order.items).joinedload(Item.keywords))
  1165. :param innerjoin: if ``True``, indicates that the joined eager load should
  1166. use an inner join instead of the default of left outer join::
  1167. query(Order).options(joinedload(Order.user, innerjoin=True))
  1168. In order to chain multiple eager joins together where some may be
  1169. OUTER and others INNER, right-nested joins are used to link them::
  1170. query(A).options(
  1171. joinedload(A.bs, innerjoin=False).
  1172. joinedload(B.cs, innerjoin=True)
  1173. )
  1174. The above query, linking A.bs via "outer" join and B.cs via "inner" join
  1175. would render the joins as "a LEFT OUTER JOIN (b JOIN c)". When using
  1176. older versions of SQLite (< 3.7.16), this form of JOIN is translated to
  1177. use full subqueries as this syntax is otherwise not directly supported.
  1178. The ``innerjoin`` flag can also be stated with the term ``"unnested"``.
  1179. This indicates that an INNER JOIN should be used, *unless* the join
  1180. is linked to a LEFT OUTER JOIN to the left, in which case it
  1181. will render as LEFT OUTER JOIN. For example, supposing ``A.bs``
  1182. is an outerjoin::
  1183. query(A).options(
  1184. joinedload(A.bs).
  1185. joinedload(B.cs, innerjoin="unnested")
  1186. )
  1187. The above join will render as "a LEFT OUTER JOIN b LEFT OUTER JOIN c",
  1188. rather than as "a LEFT OUTER JOIN (b JOIN c)".
  1189. .. note:: The "unnested" flag does **not** affect the JOIN rendered
  1190. from a many-to-many association table, e.g. a table configured
  1191. as :paramref:`_orm.relationship.secondary`, to the target table; for
  1192. correctness of results, these joins are always INNER and are
  1193. therefore right-nested if linked to an OUTER join.
  1194. .. versionchanged:: 1.0.0 ``innerjoin=True`` now implies
  1195. ``innerjoin="nested"``, whereas in 0.9 it implied
  1196. ``innerjoin="unnested"``. In order to achieve the pre-1.0 "unnested"
  1197. inner join behavior, use the value ``innerjoin="unnested"``.
  1198. See :ref:`migration_3008`.
  1199. .. note::
  1200. The joins produced by :func:`_orm.joinedload` are **anonymously
  1201. aliased**. The criteria by which the join proceeds cannot be
  1202. modified, nor can the :class:`_query.Query`
  1203. refer to these joins in any way,
  1204. including ordering. See :ref:`zen_of_eager_loading` for further
  1205. detail.
  1206. To produce a specific SQL JOIN which is explicitly available, use
  1207. :meth:`_query.Query.join`.
  1208. To combine explicit JOINs with eager loading
  1209. of collections, use :func:`_orm.contains_eager`; see
  1210. :ref:`contains_eager`.
  1211. .. seealso::
  1212. :ref:`loading_toplevel`
  1213. :ref:`joined_eager_loading`
  1214. """
  1215. loader = loadopt.set_relationship_strategy(attr, {"lazy": "joined"})
  1216. if innerjoin is not None:
  1217. loader.local_opts["innerjoin"] = innerjoin
  1218. return loader
  1219. @joinedload._add_unbound_fn
  1220. def joinedload(*keys, **kw):
  1221. return _UnboundLoad._from_keys(_UnboundLoad.joinedload, keys, False, kw)
  1222. @loader_option()
  1223. def subqueryload(loadopt, attr):
  1224. """Indicate that the given attribute should be loaded using
  1225. subquery eager loading.
  1226. This function is part of the :class:`_orm.Load` interface and supports
  1227. both method-chained and standalone operation.
  1228. examples::
  1229. # subquery-load the "orders" collection on "User"
  1230. query(User).options(subqueryload(User.orders))
  1231. # subquery-load Order.items and then Item.keywords
  1232. query(Order).options(
  1233. subqueryload(Order.items).subqueryload(Item.keywords))
  1234. # lazily load Order.items, but when Items are loaded,
  1235. # subquery-load the keywords collection
  1236. query(Order).options(
  1237. lazyload(Order.items).subqueryload(Item.keywords))
  1238. .. seealso::
  1239. :ref:`loading_toplevel`
  1240. :ref:`subquery_eager_loading`
  1241. """
  1242. return loadopt.set_relationship_strategy(attr, {"lazy": "subquery"})
  1243. @subqueryload._add_unbound_fn
  1244. def subqueryload(*keys):
  1245. return _UnboundLoad._from_keys(_UnboundLoad.subqueryload, keys, False, {})
  1246. @loader_option()
  1247. def selectinload(loadopt, attr):
  1248. """Indicate that the given attribute should be loaded using
  1249. SELECT IN eager loading.
  1250. This function is part of the :class:`_orm.Load` interface and supports
  1251. both method-chained and standalone operation.
  1252. examples::
  1253. # selectin-load the "orders" collection on "User"
  1254. query(User).options(selectinload(User.orders))
  1255. # selectin-load Order.items and then Item.keywords
  1256. query(Order).options(
  1257. selectinload(Order.items).selectinload(Item.keywords))
  1258. # lazily load Order.items, but when Items are loaded,
  1259. # selectin-load the keywords collection
  1260. query(Order).options(
  1261. lazyload(Order.items).selectinload(Item.keywords))
  1262. .. versionadded:: 1.2
  1263. .. seealso::
  1264. :ref:`loading_toplevel`
  1265. :ref:`selectin_eager_loading`
  1266. """
  1267. return loadopt.set_relationship_strategy(attr, {"lazy": "selectin"})
  1268. @selectinload._add_unbound_fn
  1269. def selectinload(*keys):
  1270. return _UnboundLoad._from_keys(_UnboundLoad.selectinload, keys, False, {})
  1271. @loader_option()
  1272. def lazyload(loadopt, attr):
  1273. """Indicate that the given attribute should be loaded using "lazy"
  1274. loading.
  1275. This function is part of the :class:`_orm.Load` interface and supports
  1276. both method-chained and standalone operation.
  1277. .. seealso::
  1278. :ref:`loading_toplevel`
  1279. :ref:`lazy_loading`
  1280. """
  1281. return loadopt.set_relationship_strategy(attr, {"lazy": "select"})
  1282. @lazyload._add_unbound_fn
  1283. def lazyload(*keys):
  1284. return _UnboundLoad._from_keys(_UnboundLoad.lazyload, keys, False, {})
  1285. @loader_option()
  1286. def immediateload(loadopt, attr):
  1287. """Indicate that the given attribute should be loaded using
  1288. an immediate load with a per-attribute SELECT statement.
  1289. The load is achieved using the "lazyloader" strategy and does not
  1290. fire off any additional eager loaders.
  1291. The :func:`.immediateload` option is superseded in general
  1292. by the :func:`.selectinload` option, which performs the same task
  1293. more efficiently by emitting a SELECT for all loaded objects.
  1294. This function is part of the :class:`_orm.Load` interface and supports
  1295. both method-chained and standalone operation.
  1296. .. seealso::
  1297. :ref:`loading_toplevel`
  1298. :ref:`selectin_eager_loading`
  1299. """
  1300. loader = loadopt.set_relationship_strategy(attr, {"lazy": "immediate"})
  1301. return loader
  1302. @immediateload._add_unbound_fn
  1303. def immediateload(*keys):
  1304. return _UnboundLoad._from_keys(_UnboundLoad.immediateload, keys, False, {})
  1305. @loader_option()
  1306. def noload(loadopt, attr):
  1307. """Indicate that the given relationship attribute should remain unloaded.
  1308. The relationship attribute will return ``None`` when accessed without
  1309. producing any loading effect.
  1310. This function is part of the :class:`_orm.Load` interface and supports
  1311. both method-chained and standalone operation.
  1312. :func:`_orm.noload` applies to :func:`_orm.relationship` attributes; for
  1313. column-based attributes, see :func:`_orm.defer`.
  1314. .. note:: Setting this loading strategy as the default strategy
  1315. for a relationship using the :paramref:`.orm.relationship.lazy`
  1316. parameter may cause issues with flushes, such if a delete operation
  1317. needs to load related objects and instead ``None`` was returned.
  1318. .. seealso::
  1319. :ref:`loading_toplevel`
  1320. """
  1321. return loadopt.set_relationship_strategy(attr, {"lazy": "noload"})
  1322. @noload._add_unbound_fn
  1323. def noload(*keys):
  1324. return _UnboundLoad._from_keys(_UnboundLoad.noload, keys, False, {})
  1325. @loader_option()
  1326. def raiseload(loadopt, attr, sql_only=False):
  1327. """Indicate that the given attribute should raise an error if accessed.
  1328. A relationship attribute configured with :func:`_orm.raiseload` will
  1329. raise an :exc:`~sqlalchemy.exc.InvalidRequestError` upon access. The
  1330. typical way this is useful is when an application is attempting to ensure
  1331. that all relationship attributes that are accessed in a particular context
  1332. would have been already loaded via eager loading. Instead of having
  1333. to read through SQL logs to ensure lazy loads aren't occurring, this
  1334. strategy will cause them to raise immediately.
  1335. :func:`_orm.raiseload` applies to :func:`_orm.relationship`
  1336. attributes only.
  1337. In order to apply raise-on-SQL behavior to a column-based attribute,
  1338. use the :paramref:`.orm.defer.raiseload` parameter on the :func:`.defer`
  1339. loader option.
  1340. :param sql_only: if True, raise only if the lazy load would emit SQL, but
  1341. not if it is only checking the identity map, or determining that the
  1342. related value should just be None due to missing keys. When False, the
  1343. strategy will raise for all varieties of relationship loading.
  1344. This function is part of the :class:`_orm.Load` interface and supports
  1345. both method-chained and standalone operation.
  1346. .. versionadded:: 1.1
  1347. .. seealso::
  1348. :ref:`loading_toplevel`
  1349. :ref:`prevent_lazy_with_raiseload`
  1350. :ref:`deferred_raiseload`
  1351. """
  1352. return loadopt.set_relationship_strategy(
  1353. attr, {"lazy": "raise_on_sql" if sql_only else "raise"}
  1354. )
  1355. @raiseload._add_unbound_fn
  1356. def raiseload(*keys, **kw):
  1357. return _UnboundLoad._from_keys(_UnboundLoad.raiseload, keys, False, kw)
  1358. @loader_option()
  1359. def defaultload(loadopt, attr):
  1360. """Indicate an attribute should load using its default loader style.
  1361. This method is used to link to other loader options further into
  1362. a chain of attributes without altering the loader style of the links
  1363. along the chain. For example, to set joined eager loading for an
  1364. element of an element::
  1365. session.query(MyClass).options(
  1366. defaultload(MyClass.someattribute).
  1367. joinedload(MyOtherClass.someotherattribute)
  1368. )
  1369. :func:`.defaultload` is also useful for setting column-level options
  1370. on a related class, namely that of :func:`.defer` and :func:`.undefer`::
  1371. session.query(MyClass).options(
  1372. defaultload(MyClass.someattribute).
  1373. defer("some_column").
  1374. undefer("some_other_column")
  1375. )
  1376. .. seealso::
  1377. :meth:`_orm.Load.options` - allows for complex hierarchical
  1378. loader option structures with less verbosity than with individual
  1379. :func:`.defaultload` directives.
  1380. :ref:`relationship_loader_options`
  1381. :ref:`deferred_loading_w_multiple`
  1382. """
  1383. return loadopt.set_relationship_strategy(attr, None)
  1384. @defaultload._add_unbound_fn
  1385. def defaultload(*keys):
  1386. return _UnboundLoad._from_keys(_UnboundLoad.defaultload, keys, False, {})
  1387. @loader_option()
  1388. def defer(loadopt, key, raiseload=False):
  1389. r"""Indicate that the given column-oriented attribute should be deferred,
  1390. e.g. not loaded until accessed.
  1391. This function is part of the :class:`_orm.Load` interface and supports
  1392. both method-chained and standalone operation.
  1393. e.g.::
  1394. from sqlalchemy.orm import defer
  1395. session.query(MyClass).options(
  1396. defer("attribute_one"),
  1397. defer("attribute_two"))
  1398. session.query(MyClass).options(
  1399. defer(MyClass.attribute_one),
  1400. defer(MyClass.attribute_two))
  1401. To specify a deferred load of an attribute on a related class,
  1402. the path can be specified one token at a time, specifying the loading
  1403. style for each link along the chain. To leave the loading style
  1404. for a link unchanged, use :func:`_orm.defaultload`::
  1405. session.query(MyClass).options(defaultload("someattr").defer("some_column"))
  1406. A :class:`_orm.Load` object that is present on a certain path can have
  1407. :meth:`_orm.Load.defer` called multiple times,
  1408. each will operate on the same
  1409. parent entity::
  1410. session.query(MyClass).options(
  1411. defaultload("someattr").
  1412. defer("some_column").
  1413. defer("some_other_column").
  1414. defer("another_column")
  1415. )
  1416. :param key: Attribute to be deferred.
  1417. :param raiseload: raise :class:`.InvalidRequestError` if the column
  1418. value is to be loaded from emitting SQL. Used to prevent unwanted
  1419. SQL from being emitted.
  1420. .. versionadded:: 1.4
  1421. .. seealso::
  1422. :ref:`deferred_raiseload`
  1423. :param \*addl_attrs: This option supports the old 0.8 style
  1424. of specifying a path as a series of attributes, which is now superseded
  1425. by the method-chained style.
  1426. .. deprecated:: 0.9 The \*addl_attrs on :func:`_orm.defer` is
  1427. deprecated and will be removed in a future release. Please
  1428. use method chaining in conjunction with defaultload() to
  1429. indicate a path.
  1430. .. seealso::
  1431. :ref:`deferred`
  1432. :func:`_orm.undefer`
  1433. """
  1434. strategy = {"deferred": True, "instrument": True}
  1435. if raiseload:
  1436. strategy["raiseload"] = True
  1437. return loadopt.set_column_strategy((key,), strategy)
  1438. @defer._add_unbound_fn
  1439. def defer(key, *addl_attrs, **kw):
  1440. if addl_attrs:
  1441. util.warn_deprecated(
  1442. "The *addl_attrs on orm.defer is deprecated. Please use "
  1443. "method chaining in conjunction with defaultload() to "
  1444. "indicate a path.",
  1445. version="1.3",
  1446. )
  1447. return _UnboundLoad._from_keys(
  1448. _UnboundLoad.defer, (key,) + addl_attrs, False, kw
  1449. )
  1450. @loader_option()
  1451. def undefer(loadopt, key):
  1452. r"""Indicate that the given column-oriented attribute should be undeferred,
  1453. e.g. specified within the SELECT statement of the entity as a whole.
  1454. The column being undeferred is typically set up on the mapping as a
  1455. :func:`.deferred` attribute.
  1456. This function is part of the :class:`_orm.Load` interface and supports
  1457. both method-chained and standalone operation.
  1458. Examples::
  1459. # undefer two columns
  1460. session.query(MyClass).options(undefer("col1"), undefer("col2"))
  1461. # undefer all columns specific to a single class using Load + *
  1462. session.query(MyClass, MyOtherClass).options(
  1463. Load(MyClass).undefer("*"))
  1464. # undefer a column on a related object
  1465. session.query(MyClass).options(
  1466. defaultload(MyClass.items).undefer('text'))
  1467. :param key: Attribute to be undeferred.
  1468. :param \*addl_attrs: This option supports the old 0.8 style
  1469. of specifying a path as a series of attributes, which is now superseded
  1470. by the method-chained style.
  1471. .. deprecated:: 0.9 The \*addl_attrs on :func:`_orm.undefer` is
  1472. deprecated and will be removed in a future release. Please
  1473. use method chaining in conjunction with defaultload() to
  1474. indicate a path.
  1475. .. seealso::
  1476. :ref:`deferred`
  1477. :func:`_orm.defer`
  1478. :func:`_orm.undefer_group`
  1479. """
  1480. return loadopt.set_column_strategy(
  1481. (key,), {"deferred": False, "instrument": True}
  1482. )
  1483. @undefer._add_unbound_fn
  1484. def undefer(key, *addl_attrs):
  1485. if addl_attrs:
  1486. util.warn_deprecated(
  1487. "The *addl_attrs on orm.undefer is deprecated. Please use "
  1488. "method chaining in conjunction with defaultload() to "
  1489. "indicate a path.",
  1490. version="1.3",
  1491. )
  1492. return _UnboundLoad._from_keys(
  1493. _UnboundLoad.undefer, (key,) + addl_attrs, False, {}
  1494. )
  1495. @loader_option()
  1496. def undefer_group(loadopt, name):
  1497. """Indicate that columns within the given deferred group name should be
  1498. undeferred.
  1499. The columns being undeferred are set up on the mapping as
  1500. :func:`.deferred` attributes and include a "group" name.
  1501. E.g::
  1502. session.query(MyClass).options(undefer_group("large_attrs"))
  1503. To undefer a group of attributes on a related entity, the path can be
  1504. spelled out using relationship loader options, such as
  1505. :func:`_orm.defaultload`::
  1506. session.query(MyClass).options(
  1507. defaultload("someattr").undefer_group("large_attrs"))
  1508. .. versionchanged:: 0.9.0 :func:`_orm.undefer_group` is now specific to a
  1509. particular entity load path.
  1510. .. seealso::
  1511. :ref:`deferred`
  1512. :func:`_orm.defer`
  1513. :func:`_orm.undefer`
  1514. """
  1515. return loadopt.set_column_strategy(
  1516. "*", None, {"undefer_group_%s" % name: True}, opts_only=True
  1517. )
  1518. @undefer_group._add_unbound_fn
  1519. def undefer_group(name):
  1520. return _UnboundLoad().undefer_group(name)
  1521. @loader_option()
  1522. def with_expression(loadopt, key, expression):
  1523. r"""Apply an ad-hoc SQL expression to a "deferred expression" attribute.
  1524. This option is used in conjunction with the :func:`_orm.query_expression`
  1525. mapper-level construct that indicates an attribute which should be the
  1526. target of an ad-hoc SQL expression.
  1527. E.g.::
  1528. sess.query(SomeClass).options(
  1529. with_expression(SomeClass.x_y_expr, SomeClass.x + SomeClass.y)
  1530. )
  1531. .. versionadded:: 1.2
  1532. :param key: Attribute to be populated.
  1533. :param expr: SQL expression to be applied to the attribute.
  1534. .. versionchanged:: 1.4 Loader options such as
  1535. :func:`_orm.with_expression`
  1536. take effect only at the **outermost** query used, and should not be used
  1537. within subqueries or inner elements of a UNION. See the change notes at
  1538. :ref:`change_8879` for background on how to correctly add arbitrary
  1539. columns to subqueries.
  1540. .. note:: the target attribute is populated only if the target object
  1541. is **not currently loaded** in the current :class:`_orm.Session`
  1542. unless the :meth:`_query.Query.populate_existing` method is used.
  1543. Please refer to :ref:`mapper_querytime_expression` for complete
  1544. usage details.
  1545. .. seealso::
  1546. :ref:`mapper_querytime_expression`
  1547. """
  1548. expression = coercions.expect(
  1549. roles.LabeledColumnExprRole, _orm_full_deannotate(expression)
  1550. )
  1551. return loadopt.set_column_strategy(
  1552. (key,), {"query_expression": True}, opts={"expression": expression}
  1553. )
  1554. @with_expression._add_unbound_fn
  1555. def with_expression(key, expression):
  1556. return _UnboundLoad._from_keys(
  1557. _UnboundLoad.with_expression, (key,), False, {"expression": expression}
  1558. )
  1559. @loader_option()
  1560. def selectin_polymorphic(loadopt, classes):
  1561. """Indicate an eager load should take place for all attributes
  1562. specific to a subclass.
  1563. This uses an additional SELECT with IN against all matched primary
  1564. key values, and is the per-query analogue to the ``"selectin"``
  1565. setting on the :paramref:`.mapper.polymorphic_load` parameter.
  1566. .. versionadded:: 1.2
  1567. .. seealso::
  1568. :ref:`polymorphic_selectin`
  1569. """
  1570. loadopt.set_class_strategy(
  1571. {"selectinload_polymorphic": True},
  1572. opts={
  1573. "entities": tuple(
  1574. sorted((inspect(cls) for cls in classes), key=id)
  1575. )
  1576. },
  1577. )
  1578. return loadopt
  1579. @selectin_polymorphic._add_unbound_fn
  1580. def selectin_polymorphic(base_cls, classes):
  1581. ul = _UnboundLoad()
  1582. ul.is_class_strategy = True
  1583. ul.path = (inspect(base_cls),)
  1584. ul.selectin_polymorphic(classes)
  1585. return ul