strategy_options.py 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  1. # Copyright (C) 2005-2017 the SQLAlchemy authors and contributors
  2. # <see AUTHORS file>
  3. #
  4. # This module is part of SQLAlchemy and is released under
  5. # the MIT License: http://www.opensource.org/licenses/mit-license.php
  6. """
  7. """
  8. from .interfaces import MapperOption, PropComparator
  9. from .. import util
  10. from ..sql.base import _generative, Generative
  11. from .. import exc as sa_exc, inspect
  12. from .base import _is_aliased_class, _class_to_mapper
  13. from . import util as orm_util
  14. from .path_registry import PathRegistry, TokenRegistry, \
  15. _WILDCARD_TOKEN, _DEFAULT_TOKEN
  16. class Load(Generative, MapperOption):
  17. """Represents loader options which modify the state of a
  18. :class:`.Query` in order to affect how various mapped attributes are
  19. loaded.
  20. The :class:`.Load` object is in most cases used implicitly behind the
  21. scenes when one makes use of a query option like :func:`.joinedload`,
  22. :func:`.defer`, or similar. However, the :class:`.Load` object
  23. can also be used directly, and in some cases can be useful.
  24. To use :class:`.Load` directly, instantiate it with the target mapped
  25. class as the argument. This style of usage is
  26. useful when dealing with a :class:`.Query` that has multiple entities::
  27. myopt = Load(MyClass).joinedload("widgets")
  28. The above ``myopt`` can now be used with :meth:`.Query.options`, where it
  29. will only take effect for the ``MyClass`` entity::
  30. session.query(MyClass, MyOtherClass).options(myopt)
  31. One case where :class:`.Load` is useful as public API is when specifying
  32. "wildcard" options that only take effect for a certain class::
  33. session.query(Order).options(Load(Order).lazyload('*'))
  34. Above, all relationships on ``Order`` will be lazy-loaded, but other
  35. attributes on those descendant objects will load using their normal
  36. loader strategy.
  37. .. seealso::
  38. :ref:`loading_toplevel`
  39. """
  40. def __init__(self, entity):
  41. insp = inspect(entity)
  42. self.path = insp._path_registry
  43. # note that this .context is shared among all descendant
  44. # Load objects
  45. self.context = {}
  46. self.local_opts = {}
  47. @classmethod
  48. def for_existing_path(cls, path):
  49. load = cls.__new__(cls)
  50. load.path = path
  51. load.context = {}
  52. load.local_opts = {}
  53. return load
  54. def _generate(self):
  55. cloned = super(Load, self)._generate()
  56. cloned.local_opts = {}
  57. return cloned
  58. is_opts_only = False
  59. strategy = None
  60. propagate_to_loaders = False
  61. def process_query(self, query):
  62. self._process(query, True)
  63. def process_query_conditionally(self, query):
  64. self._process(query, False)
  65. def _process(self, query, raiseerr):
  66. current_path = query._current_path
  67. if current_path:
  68. for (token, start_path), loader in self.context.items():
  69. chopped_start_path = self._chop_path(start_path, current_path)
  70. if chopped_start_path is not None:
  71. query._attributes[(token, chopped_start_path)] = loader
  72. else:
  73. query._attributes.update(self.context)
  74. def _generate_path(self, path, attr, wildcard_key, raiseerr=True):
  75. if raiseerr and not path.has_entity:
  76. if isinstance(path, TokenRegistry):
  77. raise sa_exc.ArgumentError(
  78. "Wildcard token cannot be followed by another entity")
  79. else:
  80. raise sa_exc.ArgumentError(
  81. "Attribute '%s' of entity '%s' does not "
  82. "refer to a mapped entity" %
  83. (path.prop.key, path.parent.entity)
  84. )
  85. if isinstance(attr, util.string_types):
  86. default_token = attr.endswith(_DEFAULT_TOKEN)
  87. if attr.endswith(_WILDCARD_TOKEN) or default_token:
  88. if default_token:
  89. self.propagate_to_loaders = False
  90. if wildcard_key:
  91. attr = "%s:%s" % (wildcard_key, attr)
  92. return path.token(attr)
  93. try:
  94. # use getattr on the class to work around
  95. # synonyms, hybrids, etc.
  96. attr = getattr(path.entity.class_, attr)
  97. except AttributeError:
  98. if raiseerr:
  99. raise sa_exc.ArgumentError(
  100. "Can't find property named '%s' on the "
  101. "mapped entity %s in this Query. " % (
  102. attr, path.entity)
  103. )
  104. else:
  105. return None
  106. else:
  107. attr = attr.property
  108. path = path[attr]
  109. else:
  110. prop = attr.property
  111. if not prop.parent.common_parent(path.mapper):
  112. if raiseerr:
  113. raise sa_exc.ArgumentError(
  114. "Attribute '%s' does not "
  115. "link from element '%s'" % (attr, path.entity))
  116. else:
  117. return None
  118. if getattr(attr, '_of_type', None):
  119. ac = attr._of_type
  120. ext_info = inspect(ac)
  121. path_element = ext_info.mapper
  122. existing = path.entity_path[prop].get(
  123. self.context, "path_with_polymorphic")
  124. if not ext_info.is_aliased_class:
  125. ac = orm_util.with_polymorphic(
  126. ext_info.mapper.base_mapper,
  127. ext_info.mapper, aliased=True,
  128. _use_mapper_path=True,
  129. _existing_alias=existing)
  130. path.entity_path[prop].set(
  131. self.context, "path_with_polymorphic", inspect(ac))
  132. path = path[prop][path_element]
  133. else:
  134. path = path[prop]
  135. if path.has_entity:
  136. path = path.entity_path
  137. return path
  138. def __str__(self):
  139. return "Load(strategy=%r)" % (self.strategy, )
  140. def _coerce_strat(self, strategy):
  141. if strategy is not None:
  142. strategy = tuple(sorted(strategy.items()))
  143. return strategy
  144. @_generative
  145. def set_relationship_strategy(
  146. self, attr, strategy, propagate_to_loaders=True):
  147. strategy = self._coerce_strat(strategy)
  148. self.propagate_to_loaders = propagate_to_loaders
  149. # if the path is a wildcard, this will set propagate_to_loaders=False
  150. self.path = self._generate_path(self.path, attr, "relationship")
  151. self.strategy = strategy
  152. if strategy is not None:
  153. self._set_path_strategy()
  154. @_generative
  155. def set_column_strategy(self, attrs, strategy, opts=None, opts_only=False):
  156. strategy = self._coerce_strat(strategy)
  157. for attr in attrs:
  158. path = self._generate_path(self.path, attr, "column")
  159. cloned = self._generate()
  160. cloned.strategy = strategy
  161. cloned.path = path
  162. cloned.propagate_to_loaders = True
  163. if opts:
  164. cloned.local_opts.update(opts)
  165. if opts_only:
  166. cloned.is_opts_only = True
  167. cloned._set_path_strategy()
  168. def _set_for_path(self, context, path, replace=True, merge_opts=False):
  169. if merge_opts or not replace:
  170. existing = path.get(self.context, "loader")
  171. if existing:
  172. if merge_opts:
  173. existing.local_opts.update(self.local_opts)
  174. else:
  175. path.set(context, "loader", self)
  176. else:
  177. existing = path.get(self.context, "loader")
  178. path.set(context, "loader", self)
  179. if existing and existing.is_opts_only:
  180. self.local_opts.update(existing.local_opts)
  181. def _set_path_strategy(self):
  182. if self.path.has_entity:
  183. effective_path = self.path.parent
  184. else:
  185. effective_path = self.path
  186. self._set_for_path(
  187. self.context, effective_path, replace=True,
  188. merge_opts=self.is_opts_only)
  189. def __getstate__(self):
  190. d = self.__dict__.copy()
  191. d["path"] = self.path.serialize()
  192. return d
  193. def __setstate__(self, state):
  194. self.__dict__.update(state)
  195. self.path = PathRegistry.deserialize(self.path)
  196. def _chop_path(self, to_chop, path):
  197. i = -1
  198. for i, (c_token, p_token) in enumerate(zip(to_chop, path.path)):
  199. if isinstance(c_token, util.string_types):
  200. # TODO: this is approximated from the _UnboundLoad
  201. # version and probably has issues, not fully covered.
  202. if i == 0 and c_token.endswith(':' + _DEFAULT_TOKEN):
  203. return to_chop
  204. elif c_token != 'relationship:%s' % (_WILDCARD_TOKEN,) and \
  205. c_token != p_token.key:
  206. return None
  207. if c_token is p_token:
  208. continue
  209. else:
  210. return None
  211. return to_chop[i + 1:]
  212. class _UnboundLoad(Load):
  213. """Represent a loader option that isn't tied to a root entity.
  214. The loader option will produce an entity-linked :class:`.Load`
  215. object when it is passed :meth:`.Query.options`.
  216. This provides compatibility with the traditional system
  217. of freestanding options, e.g. ``joinedload('x.y.z')``.
  218. """
  219. def __init__(self):
  220. self.path = ()
  221. self._to_bind = set()
  222. self.local_opts = {}
  223. _is_chain_link = False
  224. def _set_path_strategy(self):
  225. self._to_bind.add(self)
  226. def _generate_path(self, path, attr, wildcard_key):
  227. if wildcard_key and isinstance(attr, util.string_types) and \
  228. attr in (_WILDCARD_TOKEN, _DEFAULT_TOKEN):
  229. if attr == _DEFAULT_TOKEN:
  230. self.propagate_to_loaders = False
  231. attr = "%s:%s" % (wildcard_key, attr)
  232. return path + (attr, )
  233. def __getstate__(self):
  234. d = self.__dict__.copy()
  235. d['path'] = ret = []
  236. for token in util.to_list(self.path):
  237. if isinstance(token, PropComparator):
  238. ret.append((token._parentmapper.class_, token.key))
  239. else:
  240. ret.append(token)
  241. return d
  242. def __setstate__(self, state):
  243. ret = []
  244. for key in state['path']:
  245. if isinstance(key, tuple):
  246. cls, propkey = key
  247. ret.append(getattr(cls, propkey))
  248. else:
  249. ret.append(key)
  250. state['path'] = tuple(ret)
  251. self.__dict__ = state
  252. def _process(self, query, raiseerr):
  253. for val in self._to_bind:
  254. val._bind_loader(query, query._attributes, raiseerr)
  255. @classmethod
  256. def _from_keys(cls, meth, keys, chained, kw):
  257. opt = _UnboundLoad()
  258. def _split_key(key):
  259. if isinstance(key, util.string_types):
  260. # coerce fooload('*') into "default loader strategy"
  261. if key == _WILDCARD_TOKEN:
  262. return (_DEFAULT_TOKEN, )
  263. # coerce fooload(".*") into "wildcard on default entity"
  264. elif key.startswith("." + _WILDCARD_TOKEN):
  265. key = key[1:]
  266. return key.split(".")
  267. else:
  268. return (key,)
  269. all_tokens = [token for key in keys for token in _split_key(key)]
  270. for token in all_tokens[0:-1]:
  271. if chained:
  272. opt = meth(opt, token, **kw)
  273. else:
  274. opt = opt.defaultload(token)
  275. opt._is_chain_link = True
  276. opt = meth(opt, all_tokens[-1], **kw)
  277. opt._is_chain_link = False
  278. return opt
  279. def _chop_path(self, to_chop, path):
  280. i = -1
  281. for i, (c_token, (p_mapper, p_prop)) in enumerate(
  282. zip(to_chop, path.pairs())):
  283. if isinstance(c_token, util.string_types):
  284. if i == 0 and c_token.endswith(':' + _DEFAULT_TOKEN):
  285. return to_chop
  286. elif c_token != 'relationship:%s' % (
  287. _WILDCARD_TOKEN,) and c_token != p_prop.key:
  288. return None
  289. elif isinstance(c_token, PropComparator):
  290. if c_token.property is not p_prop:
  291. return None
  292. else:
  293. i += 1
  294. return to_chop[i:]
  295. def _bind_loader(self, query, context, raiseerr):
  296. start_path = self.path
  297. # _current_path implies we're in a
  298. # secondary load with an existing path
  299. current_path = query._current_path
  300. if current_path:
  301. start_path = self._chop_path(start_path, current_path)
  302. if not start_path:
  303. return None
  304. token = start_path[0]
  305. if isinstance(token, util.string_types):
  306. entity = self._find_entity_basestring(query, token, raiseerr)
  307. elif isinstance(token, PropComparator):
  308. prop = token.property
  309. entity = self._find_entity_prop_comparator(
  310. query,
  311. prop.key,
  312. token._parententity,
  313. raiseerr)
  314. else:
  315. raise sa_exc.ArgumentError(
  316. "mapper option expects "
  317. "string key or list of attributes")
  318. if not entity:
  319. return
  320. path_element = entity.entity_zero
  321. # transfer our entity-less state into a Load() object
  322. # with a real entity path.
  323. loader = Load(path_element)
  324. loader.context = context
  325. loader.strategy = self.strategy
  326. loader.is_opts_only = self.is_opts_only
  327. path = loader.path
  328. for token in start_path:
  329. loader.path = path = loader._generate_path(
  330. loader.path, token, None, raiseerr)
  331. if path is None:
  332. return
  333. loader.local_opts.update(self.local_opts)
  334. if loader.path.has_entity:
  335. effective_path = loader.path.parent
  336. else:
  337. effective_path = loader.path
  338. # prioritize "first class" options over those
  339. # that were "links in the chain", e.g. "x" and "y" in
  340. # someload("x.y.z") versus someload("x") / someload("x.y")
  341. if effective_path.is_token:
  342. for path in effective_path.generate_for_superclasses():
  343. loader._set_for_path(
  344. context, path,
  345. replace=not self._is_chain_link,
  346. merge_opts=self.is_opts_only)
  347. else:
  348. loader._set_for_path(
  349. context, effective_path,
  350. replace=not self._is_chain_link,
  351. merge_opts=self.is_opts_only)
  352. def _find_entity_prop_comparator(self, query, token, mapper, raiseerr):
  353. if _is_aliased_class(mapper):
  354. searchfor = mapper
  355. else:
  356. searchfor = _class_to_mapper(mapper)
  357. for ent in query._mapper_entities:
  358. if ent.corresponds_to(searchfor):
  359. return ent
  360. else:
  361. if raiseerr:
  362. if not list(query._mapper_entities):
  363. raise sa_exc.ArgumentError(
  364. "Query has only expression-based entities - "
  365. "can't find property named '%s'."
  366. % (token, )
  367. )
  368. else:
  369. raise sa_exc.ArgumentError(
  370. "Can't find property '%s' on any entity "
  371. "specified in this Query. Note the full path "
  372. "from root (%s) to target entity must be specified."
  373. % (token, ",".join(str(x) for
  374. x in query._mapper_entities))
  375. )
  376. else:
  377. return None
  378. def _find_entity_basestring(self, query, token, raiseerr):
  379. if token.endswith(':' + _WILDCARD_TOKEN):
  380. if len(list(query._mapper_entities)) != 1:
  381. if raiseerr:
  382. raise sa_exc.ArgumentError(
  383. "Wildcard loader can only be used with exactly "
  384. "one entity. Use Load(ent) to specify "
  385. "specific entities.")
  386. elif token.endswith(_DEFAULT_TOKEN):
  387. raiseerr = False
  388. for ent in query._mapper_entities:
  389. # return only the first _MapperEntity when searching
  390. # based on string prop name. Ideally object
  391. # attributes are used to specify more exactly.
  392. return ent
  393. else:
  394. if raiseerr:
  395. raise sa_exc.ArgumentError(
  396. "Query has only expression-based entities - "
  397. "can't find property named '%s'."
  398. % (token, )
  399. )
  400. else:
  401. return None
  402. class loader_option(object):
  403. def __init__(self):
  404. pass
  405. def __call__(self, fn):
  406. self.name = name = fn.__name__
  407. self.fn = fn
  408. if hasattr(Load, name):
  409. raise TypeError("Load class already has a %s method." % (name))
  410. setattr(Load, name, fn)
  411. return self
  412. def _add_unbound_fn(self, fn):
  413. self._unbound_fn = fn
  414. fn_doc = self.fn.__doc__
  415. self.fn.__doc__ = """Produce a new :class:`.Load` object with the
  416. :func:`.orm.%(name)s` option applied.
  417. See :func:`.orm.%(name)s` for usage examples.
  418. """ % {"name": self.name}
  419. fn.__doc__ = fn_doc
  420. return self
  421. def _add_unbound_all_fn(self, fn):
  422. self._unbound_all_fn = fn
  423. fn.__doc__ = """Produce a standalone "all" option for :func:`.orm.%(name)s`.
  424. .. deprecated:: 0.9.0
  425. The "_all()" style is replaced by method chaining, e.g.::
  426. session.query(MyClass).options(
  427. %(name)s("someattribute").%(name)s("anotherattribute")
  428. )
  429. """ % {"name": self.name}
  430. return self
  431. @loader_option()
  432. def contains_eager(loadopt, attr, alias=None):
  433. r"""Indicate that the given attribute should be eagerly loaded from
  434. columns stated manually in the query.
  435. This function is part of the :class:`.Load` interface and supports
  436. both method-chained and standalone operation.
  437. The option is used in conjunction with an explicit join that loads
  438. the desired rows, i.e.::
  439. sess.query(Order).\
  440. join(Order.user).\
  441. options(contains_eager(Order.user))
  442. The above query would join from the ``Order`` entity to its related
  443. ``User`` entity, and the returned ``Order`` objects would have the
  444. ``Order.user`` attribute pre-populated.
  445. :func:`contains_eager` also accepts an `alias` argument, which is the
  446. string name of an alias, an :func:`~sqlalchemy.sql.expression.alias`
  447. construct, or an :func:`~sqlalchemy.orm.aliased` construct. Use this when
  448. the eagerly-loaded rows are to come from an aliased table::
  449. user_alias = aliased(User)
  450. sess.query(Order).\
  451. join((user_alias, Order.user)).\
  452. options(contains_eager(Order.user, alias=user_alias))
  453. .. seealso::
  454. :ref:`loading_toplevel`
  455. :ref:`contains_eager`
  456. """
  457. if alias is not None:
  458. if not isinstance(alias, str):
  459. info = inspect(alias)
  460. alias = info.selectable
  461. cloned = loadopt.set_relationship_strategy(
  462. attr,
  463. {"lazy": "joined"},
  464. propagate_to_loaders=False
  465. )
  466. cloned.local_opts['eager_from_alias'] = alias
  467. return cloned
  468. @contains_eager._add_unbound_fn
  469. def contains_eager(*keys, **kw):
  470. return _UnboundLoad()._from_keys(
  471. _UnboundLoad.contains_eager, keys, True, kw)
  472. @loader_option()
  473. def load_only(loadopt, *attrs):
  474. """Indicate that for a particular entity, only the given list
  475. of column-based attribute names should be loaded; all others will be
  476. deferred.
  477. This function is part of the :class:`.Load` interface and supports
  478. both method-chained and standalone operation.
  479. Example - given a class ``User``, load only the ``name`` and ``fullname``
  480. attributes::
  481. session.query(User).options(load_only("name", "fullname"))
  482. Example - given a relationship ``User.addresses -> Address``, specify
  483. subquery loading for the ``User.addresses`` collection, but on each
  484. ``Address`` object load only the ``email_address`` attribute::
  485. session.query(User).options(
  486. subqueryload("addresses").load_only("email_address")
  487. )
  488. For a :class:`.Query` that has multiple entities, the lead entity can be
  489. specifically referred to using the :class:`.Load` constructor::
  490. session.query(User, Address).join(User.addresses).options(
  491. Load(User).load_only("name", "fullname"),
  492. Load(Address).load_only("email_addres")
  493. )
  494. .. versionadded:: 0.9.0
  495. """
  496. cloned = loadopt.set_column_strategy(
  497. attrs,
  498. {"deferred": False, "instrument": True}
  499. )
  500. cloned.set_column_strategy("*",
  501. {"deferred": True, "instrument": True},
  502. {"undefer_pks": True})
  503. return cloned
  504. @load_only._add_unbound_fn
  505. def load_only(*attrs):
  506. return _UnboundLoad().load_only(*attrs)
  507. @loader_option()
  508. def joinedload(loadopt, attr, innerjoin=None):
  509. """Indicate that the given attribute should be loaded using joined
  510. eager loading.
  511. This function is part of the :class:`.Load` interface and supports
  512. both method-chained and standalone operation.
  513. examples::
  514. # joined-load the "orders" collection on "User"
  515. query(User).options(joinedload(User.orders))
  516. # joined-load Order.items and then Item.keywords
  517. query(Order).options(
  518. joinedload(Order.items).joinedload(Item.keywords))
  519. # lazily load Order.items, but when Items are loaded,
  520. # joined-load the keywords collection
  521. query(Order).options(
  522. lazyload(Order.items).joinedload(Item.keywords))
  523. :param innerjoin: if ``True``, indicates that the joined eager load should
  524. use an inner join instead of the default of left outer join::
  525. query(Order).options(joinedload(Order.user, innerjoin=True))
  526. In order to chain multiple eager joins together where some may be
  527. OUTER and others INNER, right-nested joins are used to link them::
  528. query(A).options(
  529. joinedload(A.bs, innerjoin=False).
  530. joinedload(B.cs, innerjoin=True)
  531. )
  532. The above query, linking A.bs via "outer" join and B.cs via "inner" join
  533. would render the joins as "a LEFT OUTER JOIN (b JOIN c)". When using
  534. older versions of SQLite (< 3.7.16), this form of JOIN is translated to
  535. use full subqueries as this syntax is otherwise not directly supported.
  536. The ``innerjoin`` flag can also be stated with the term ``"unnested"``.
  537. This indicates that an INNER JOIN should be used, *unless* the join
  538. is linked to a LEFT OUTER JOIN to the left, in which case it
  539. will render as LEFT OUTER JOIN. For example, supposing ``A.bs``
  540. is an outerjoin::
  541. query(A).options(
  542. joinedload(A.bs).
  543. joinedload(B.cs, innerjoin="unnested")
  544. )
  545. The above join will render as "a LEFT OUTER JOIN b LEFT OUTER JOIN c",
  546. rather than as "a LEFT OUTER JOIN (b JOIN c)".
  547. .. note:: The "unnested" flag does **not** affect the JOIN rendered
  548. from a many-to-many association table, e.g. a table configured
  549. as :paramref:`.relationship.secondary`, to the target table; for
  550. correctness of results, these joins are always INNER and are
  551. therefore right-nested if linked to an OUTER join.
  552. .. versionchanged:: 1.0.0 ``innerjoin=True`` now implies
  553. ``innerjoin="nested"``, whereas in 0.9 it implied
  554. ``innerjoin="unnested"``. In order to achieve the pre-1.0 "unnested"
  555. inner join behavior, use the value ``innerjoin="unnested"``.
  556. See :ref:`migration_3008`.
  557. .. note::
  558. The joins produced by :func:`.orm.joinedload` are **anonymously
  559. aliased**. The criteria by which the join proceeds cannot be
  560. modified, nor can the :class:`.Query` refer to these joins in any way,
  561. including ordering. See :ref:`zen_of_eager_loading` for further
  562. detail.
  563. To produce a specific SQL JOIN which is explicitly available, use
  564. :meth:`.Query.join`. To combine explicit JOINs with eager loading
  565. of collections, use :func:`.orm.contains_eager`; see
  566. :ref:`contains_eager`.
  567. .. seealso::
  568. :ref:`loading_toplevel`
  569. :ref:`joined_eager_loading`
  570. """
  571. loader = loadopt.set_relationship_strategy(attr, {"lazy": "joined"})
  572. if innerjoin is not None:
  573. loader.local_opts['innerjoin'] = innerjoin
  574. return loader
  575. @joinedload._add_unbound_fn
  576. def joinedload(*keys, **kw):
  577. return _UnboundLoad._from_keys(
  578. _UnboundLoad.joinedload, keys, False, kw)
  579. @joinedload._add_unbound_all_fn
  580. def joinedload_all(*keys, **kw):
  581. return _UnboundLoad._from_keys(
  582. _UnboundLoad.joinedload, keys, True, kw)
  583. @loader_option()
  584. def subqueryload(loadopt, attr):
  585. """Indicate that the given attribute should be loaded using
  586. subquery eager loading.
  587. This function is part of the :class:`.Load` interface and supports
  588. both method-chained and standalone operation.
  589. examples::
  590. # subquery-load the "orders" collection on "User"
  591. query(User).options(subqueryload(User.orders))
  592. # subquery-load Order.items and then Item.keywords
  593. query(Order).options(subqueryload(Order.items).subqueryload(Item.keywords))
  594. # lazily load Order.items, but when Items are loaded,
  595. # subquery-load the keywords collection
  596. query(Order).options(lazyload(Order.items).subqueryload(Item.keywords))
  597. .. seealso::
  598. :ref:`loading_toplevel`
  599. :ref:`subquery_eager_loading`
  600. """
  601. return loadopt.set_relationship_strategy(attr, {"lazy": "subquery"})
  602. @subqueryload._add_unbound_fn
  603. def subqueryload(*keys):
  604. return _UnboundLoad._from_keys(_UnboundLoad.subqueryload, keys, False, {})
  605. @subqueryload._add_unbound_all_fn
  606. def subqueryload_all(*keys):
  607. return _UnboundLoad._from_keys(_UnboundLoad.subqueryload, keys, True, {})
  608. @loader_option()
  609. def lazyload(loadopt, attr):
  610. """Indicate that the given attribute should be loaded using "lazy"
  611. loading.
  612. This function is part of the :class:`.Load` interface and supports
  613. both method-chained and standalone operation.
  614. .. seealso::
  615. :ref:`loading_toplevel`
  616. :ref:`lazy_loading`
  617. """
  618. return loadopt.set_relationship_strategy(attr, {"lazy": "select"})
  619. @lazyload._add_unbound_fn
  620. def lazyload(*keys):
  621. return _UnboundLoad._from_keys(_UnboundLoad.lazyload, keys, False, {})
  622. @lazyload._add_unbound_all_fn
  623. def lazyload_all(*keys):
  624. return _UnboundLoad._from_keys(_UnboundLoad.lazyload, keys, True, {})
  625. @loader_option()
  626. def immediateload(loadopt, attr):
  627. """Indicate that the given attribute should be loaded using
  628. an immediate load with a per-attribute SELECT statement.
  629. This function is part of the :class:`.Load` interface and supports
  630. both method-chained and standalone operation.
  631. .. seealso::
  632. :ref:`loading_toplevel`
  633. """
  634. loader = loadopt.set_relationship_strategy(attr, {"lazy": "immediate"})
  635. return loader
  636. @immediateload._add_unbound_fn
  637. def immediateload(*keys):
  638. return _UnboundLoad._from_keys(
  639. _UnboundLoad.immediateload, keys, False, {})
  640. @loader_option()
  641. def noload(loadopt, attr):
  642. """Indicate that the given relationship attribute should remain unloaded.
  643. This function is part of the :class:`.Load` interface and supports
  644. both method-chained and standalone operation.
  645. :func:`.orm.noload` applies to :func:`.relationship` attributes; for
  646. column-based attributes, see :func:`.orm.defer`.
  647. .. seealso::
  648. :ref:`loading_toplevel`
  649. """
  650. return loadopt.set_relationship_strategy(attr, {"lazy": "noload"})
  651. @noload._add_unbound_fn
  652. def noload(*keys):
  653. return _UnboundLoad._from_keys(_UnboundLoad.noload, keys, False, {})
  654. @loader_option()
  655. def raiseload(loadopt, attr, sql_only=False):
  656. """Indicate that the given relationship attribute should disallow lazy loads.
  657. A relationship attribute configured with :func:`.orm.raiseload` will
  658. raise an :exc:`~sqlalchemy.exc.InvalidRequestError` upon access. The
  659. typical way this is useful is when an application is attempting to ensure
  660. that all relationship attributes that are accessed in a particular context
  661. would have been already loaded via eager loading. Instead of having
  662. to read through SQL logs to ensure lazy loads aren't occurring, this
  663. strategy will cause them to raise immediately.
  664. :param sql_only: if True, raise only if the lazy load would emit SQL,
  665. but not if it is only checking the identity map, or determining that
  666. the related value should just be None due to missing keys. When False,
  667. the strategy will raise for all varieties of lazyload.
  668. This function is part of the :class:`.Load` interface and supports
  669. both method-chained and standalone operation.
  670. :func:`.orm.raiseload` applies to :func:`.relationship` attributes only.
  671. .. versionadded:: 1.1
  672. .. seealso::
  673. :ref:`loading_toplevel`
  674. :ref:`prevent_lazy_with_raiseload`
  675. """
  676. return loadopt.set_relationship_strategy(
  677. attr, {"lazy": "raise_on_sql" if sql_only else "raise"})
  678. @raiseload._add_unbound_fn
  679. def raiseload(*keys, **kw):
  680. return _UnboundLoad._from_keys(_UnboundLoad.raiseload, keys, False, kw)
  681. @loader_option()
  682. def defaultload(loadopt, attr):
  683. """Indicate an attribute should load using its default loader style.
  684. This method is used to link to other loader options further into
  685. a chain of attributes without altering the loader style of the links
  686. along the chain. For example, to set joined eager loading for an
  687. element of an element::
  688. session.query(MyClass).options(
  689. defaultload(MyClass.someattribute).
  690. joinedload(MyOtherClass.someotherattribute)
  691. )
  692. :func:`.defaultload` is also useful for setting column-level options
  693. on a related class, namely that of :func:`.defer` and :func:`.undefer`::
  694. session.query(MyClass).options(
  695. defaultload(MyClass.someattribute).
  696. defer("some_column").
  697. undefer("some_other_column")
  698. )
  699. .. seealso::
  700. :ref:`relationship_loader_options`
  701. :ref:`deferred_loading_w_multiple`
  702. """
  703. return loadopt.set_relationship_strategy(
  704. attr,
  705. None
  706. )
  707. @defaultload._add_unbound_fn
  708. def defaultload(*keys):
  709. return _UnboundLoad._from_keys(_UnboundLoad.defaultload, keys, False, {})
  710. @loader_option()
  711. def defer(loadopt, key):
  712. r"""Indicate that the given column-oriented attribute should be deferred, e.g.
  713. not loaded until accessed.
  714. This function is part of the :class:`.Load` interface and supports
  715. both method-chained and standalone operation.
  716. e.g.::
  717. from sqlalchemy.orm import defer
  718. session.query(MyClass).options(
  719. defer("attribute_one"),
  720. defer("attribute_two"))
  721. session.query(MyClass).options(
  722. defer(MyClass.attribute_one),
  723. defer(MyClass.attribute_two))
  724. To specify a deferred load of an attribute on a related class,
  725. the path can be specified one token at a time, specifying the loading
  726. style for each link along the chain. To leave the loading style
  727. for a link unchanged, use :func:`.orm.defaultload`::
  728. session.query(MyClass).options(defaultload("someattr").defer("some_column"))
  729. A :class:`.Load` object that is present on a certain path can have
  730. :meth:`.Load.defer` called multiple times, each will operate on the same
  731. parent entity::
  732. session.query(MyClass).options(
  733. defaultload("someattr").
  734. defer("some_column").
  735. defer("some_other_column").
  736. defer("another_column")
  737. )
  738. :param key: Attribute to be deferred.
  739. :param \*addl_attrs: Deprecated; this option supports the old 0.8 style
  740. of specifying a path as a series of attributes, which is now superseded
  741. by the method-chained style.
  742. .. seealso::
  743. :ref:`deferred`
  744. :func:`.orm.undefer`
  745. """
  746. return loadopt.set_column_strategy(
  747. (key, ),
  748. {"deferred": True, "instrument": True}
  749. )
  750. @defer._add_unbound_fn
  751. def defer(key, *addl_attrs):
  752. return _UnboundLoad._from_keys(
  753. _UnboundLoad.defer, (key, ) + addl_attrs, False, {})
  754. @loader_option()
  755. def undefer(loadopt, key):
  756. r"""Indicate that the given column-oriented attribute should be undeferred,
  757. e.g. specified within the SELECT statement of the entity as a whole.
  758. The column being undeferred is typically set up on the mapping as a
  759. :func:`.deferred` attribute.
  760. This function is part of the :class:`.Load` interface and supports
  761. both method-chained and standalone operation.
  762. Examples::
  763. # undefer two columns
  764. session.query(MyClass).options(undefer("col1"), undefer("col2"))
  765. # undefer all columns specific to a single class using Load + *
  766. session.query(MyClass, MyOtherClass).options(
  767. Load(MyClass).undefer("*"))
  768. :param key: Attribute to be undeferred.
  769. :param \*addl_attrs: Deprecated; this option supports the old 0.8 style
  770. of specifying a path as a series of attributes, which is now superseded
  771. by the method-chained style.
  772. .. seealso::
  773. :ref:`deferred`
  774. :func:`.orm.defer`
  775. :func:`.orm.undefer_group`
  776. """
  777. return loadopt.set_column_strategy(
  778. (key, ),
  779. {"deferred": False, "instrument": True}
  780. )
  781. @undefer._add_unbound_fn
  782. def undefer(key, *addl_attrs):
  783. return _UnboundLoad._from_keys(
  784. _UnboundLoad.undefer, (key, ) + addl_attrs, False, {})
  785. @loader_option()
  786. def undefer_group(loadopt, name):
  787. """Indicate that columns within the given deferred group name should be
  788. undeferred.
  789. The columns being undeferred are set up on the mapping as
  790. :func:`.deferred` attributes and include a "group" name.
  791. E.g::
  792. session.query(MyClass).options(undefer_group("large_attrs"))
  793. To undefer a group of attributes on a related entity, the path can be
  794. spelled out using relationship loader options, such as
  795. :func:`.orm.defaultload`::
  796. session.query(MyClass).options(
  797. defaultload("someattr").undefer_group("large_attrs"))
  798. .. versionchanged:: 0.9.0 :func:`.orm.undefer_group` is now specific to a
  799. particiular entity load path.
  800. .. seealso::
  801. :ref:`deferred`
  802. :func:`.orm.defer`
  803. :func:`.orm.undefer`
  804. """
  805. return loadopt.set_column_strategy(
  806. "*",
  807. None,
  808. {"undefer_group_%s" % name: True},
  809. opts_only=True
  810. )
  811. @undefer_group._add_unbound_fn
  812. def undefer_group(name):
  813. return _UnboundLoad().undefer_group(name)