query.py 125 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487
  1. # orm/query.py
  2. # Copyright (C) 2005-2022 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. """The Query class and support.
  8. Defines the :class:`_query.Query` class, the central
  9. construct used by the ORM to construct database queries.
  10. The :class:`_query.Query` class should not be confused with the
  11. :class:`_expression.Select` class, which defines database
  12. SELECT operations at the SQL (non-ORM) level. ``Query`` differs from
  13. ``Select`` in that it returns ORM-mapped objects and interacts with an
  14. ORM session, whereas the ``Select`` construct interacts directly with the
  15. database to return iterable result sets.
  16. """
  17. import itertools
  18. import operator
  19. import types
  20. from . import exc as orm_exc
  21. from . import interfaces
  22. from . import loading
  23. from . import util as orm_util
  24. from .base import _assertions
  25. from .context import _column_descriptions
  26. from .context import _legacy_determine_last_joined_entity
  27. from .context import _legacy_filter_by_entity_zero
  28. from .context import LABEL_STYLE_LEGACY_ORM
  29. from .context import ORMCompileState
  30. from .context import ORMFromStatementCompileState
  31. from .context import QueryContext
  32. from .interfaces import ORMColumnsClauseRole
  33. from .util import aliased
  34. from .util import AliasedClass
  35. from .util import object_mapper
  36. from .util import with_parent
  37. from .util import with_polymorphic
  38. from .. import exc as sa_exc
  39. from .. import inspect
  40. from .. import inspection
  41. from .. import log
  42. from .. import sql
  43. from .. import util
  44. from ..sql import coercions
  45. from ..sql import elements
  46. from ..sql import expression
  47. from ..sql import roles
  48. from ..sql import Select
  49. from ..sql import util as sql_util
  50. from ..sql import visitors
  51. from ..sql.annotation import SupportsCloneAnnotations
  52. from ..sql.base import _entity_namespace_key
  53. from ..sql.base import _generative
  54. from ..sql.base import Executable
  55. from ..sql.selectable import _MemoizedSelectEntities
  56. from ..sql.selectable import _SelectFromElements
  57. from ..sql.selectable import ForUpdateArg
  58. from ..sql.selectable import GroupedElement
  59. from ..sql.selectable import HasHints
  60. from ..sql.selectable import HasPrefixes
  61. from ..sql.selectable import HasSuffixes
  62. from ..sql.selectable import LABEL_STYLE_TABLENAME_PLUS_COL
  63. from ..sql.selectable import SelectBase
  64. from ..sql.selectable import SelectStatementGrouping
  65. from ..sql.visitors import InternalTraversal
  66. from ..util import collections_abc
  67. __all__ = ["Query", "QueryContext", "aliased"]
  68. @inspection._self_inspects
  69. @log.class_logger
  70. class Query(
  71. _SelectFromElements,
  72. SupportsCloneAnnotations,
  73. HasPrefixes,
  74. HasSuffixes,
  75. HasHints,
  76. Executable,
  77. ):
  78. """ORM-level SQL construction object.
  79. :class:`_query.Query`
  80. is the source of all SELECT statements generated by the
  81. ORM, both those formulated by end-user query operations as well as by
  82. high level internal operations such as related collection loading. It
  83. features a generative interface whereby successive calls return a new
  84. :class:`_query.Query` object, a copy of the former with additional
  85. criteria and options associated with it.
  86. :class:`_query.Query` objects are normally initially generated using the
  87. :meth:`~.Session.query` method of :class:`.Session`, and in
  88. less common cases by instantiating the :class:`_query.Query` directly and
  89. associating with a :class:`.Session` using the
  90. :meth:`_query.Query.with_session`
  91. method.
  92. For a full walk through of :class:`_query.Query` usage, see the
  93. :ref:`ormtutorial_toplevel`.
  94. """
  95. # elements that are in Core and can be cached in the same way
  96. _where_criteria = ()
  97. _having_criteria = ()
  98. _order_by_clauses = ()
  99. _group_by_clauses = ()
  100. _limit_clause = None
  101. _offset_clause = None
  102. _distinct = False
  103. _distinct_on = ()
  104. _for_update_arg = None
  105. _correlate = ()
  106. _auto_correlate = True
  107. _from_obj = ()
  108. _setup_joins = ()
  109. _legacy_setup_joins = ()
  110. _label_style = LABEL_STYLE_LEGACY_ORM
  111. _memoized_select_entities = ()
  112. _compile_options = ORMCompileState.default_compile_options
  113. load_options = QueryContext.default_load_options + {
  114. "_legacy_uniquing": True
  115. }
  116. _params = util.EMPTY_DICT
  117. # local Query builder state, not needed for
  118. # compilation or execution
  119. _aliased_generation = None
  120. _enable_assertions = True
  121. _last_joined_entity = None
  122. _statement = None
  123. # mirrors that of ClauseElement, used to propagate the "orm"
  124. # plugin as well as the "subject" of the plugin, e.g. the mapper
  125. # we are querying against.
  126. _propagate_attrs = util.immutabledict()
  127. def __init__(self, entities, session=None):
  128. """Construct a :class:`_query.Query` directly.
  129. E.g.::
  130. q = Query([User, Address], session=some_session)
  131. The above is equivalent to::
  132. q = some_session.query(User, Address)
  133. :param entities: a sequence of entities and/or SQL expressions.
  134. :param session: a :class:`.Session` with which the
  135. :class:`_query.Query`
  136. will be associated. Optional; a :class:`_query.Query`
  137. can be associated
  138. with a :class:`.Session` generatively via the
  139. :meth:`_query.Query.with_session` method as well.
  140. .. seealso::
  141. :meth:`.Session.query`
  142. :meth:`_query.Query.with_session`
  143. """
  144. self.session = session
  145. self._set_entities(entities)
  146. def _set_propagate_attrs(self, values):
  147. self._propagate_attrs = util.immutabledict(values)
  148. return self
  149. def _set_entities(self, entities):
  150. self._raw_columns = [
  151. coercions.expect(
  152. roles.ColumnsClauseRole,
  153. ent,
  154. apply_propagate_attrs=self,
  155. post_inspect=True,
  156. )
  157. for ent in util.to_list(entities)
  158. ]
  159. def _entity_from_pre_ent_zero(self):
  160. if not self._raw_columns:
  161. return None
  162. ent = self._raw_columns[0]
  163. if "parententity" in ent._annotations:
  164. return ent._annotations["parententity"]
  165. elif isinstance(ent, ORMColumnsClauseRole):
  166. return ent.entity
  167. elif "bundle" in ent._annotations:
  168. return ent._annotations["bundle"]
  169. else:
  170. # label, other SQL expression
  171. for element in visitors.iterate(ent):
  172. if "parententity" in element._annotations:
  173. return element._annotations["parententity"]
  174. else:
  175. return None
  176. def _only_full_mapper_zero(self, methname):
  177. if (
  178. len(self._raw_columns) != 1
  179. or "parententity" not in self._raw_columns[0]._annotations
  180. or not self._raw_columns[0].is_selectable
  181. ):
  182. raise sa_exc.InvalidRequestError(
  183. "%s() can only be used against "
  184. "a single mapped class." % methname
  185. )
  186. return self._raw_columns[0]._annotations["parententity"]
  187. def _set_select_from(self, obj, set_base_alias):
  188. fa = [
  189. coercions.expect(
  190. roles.StrictFromClauseRole,
  191. elem,
  192. allow_select=True,
  193. apply_propagate_attrs=self,
  194. )
  195. for elem in obj
  196. ]
  197. self._compile_options += {"_set_base_alias": set_base_alias}
  198. self._from_obj = tuple(fa)
  199. @_generative
  200. def _set_lazyload_from(self, state):
  201. self.load_options += {"_lazy_loaded_from": state}
  202. def _get_condition(self):
  203. return self._no_criterion_condition(
  204. "get", order_by=False, distinct=False
  205. )
  206. def _get_existing_condition(self):
  207. self._no_criterion_assertion("get", order_by=False, distinct=False)
  208. def _no_criterion_assertion(self, meth, order_by=True, distinct=True):
  209. if not self._enable_assertions:
  210. return
  211. if (
  212. self._where_criteria
  213. or self._statement is not None
  214. or self._from_obj
  215. or self._legacy_setup_joins
  216. or self._limit_clause is not None
  217. or self._offset_clause is not None
  218. or self._group_by_clauses
  219. or (order_by and self._order_by_clauses)
  220. or (distinct and self._distinct)
  221. ):
  222. raise sa_exc.InvalidRequestError(
  223. "Query.%s() being called on a "
  224. "Query with existing criterion. " % meth
  225. )
  226. def _no_criterion_condition(self, meth, order_by=True, distinct=True):
  227. self._no_criterion_assertion(meth, order_by, distinct)
  228. self._from_obj = self._legacy_setup_joins = ()
  229. if self._statement is not None:
  230. self._compile_options += {"_statement": None}
  231. self._where_criteria = ()
  232. self._distinct = False
  233. self._order_by_clauses = self._group_by_clauses = ()
  234. def _no_clauseelement_condition(self, meth):
  235. if not self._enable_assertions:
  236. return
  237. if self._order_by_clauses:
  238. raise sa_exc.InvalidRequestError(
  239. "Query.%s() being called on a "
  240. "Query with existing criterion. " % meth
  241. )
  242. self._no_criterion_condition(meth)
  243. def _no_statement_condition(self, meth):
  244. if not self._enable_assertions:
  245. return
  246. if self._statement is not None:
  247. raise sa_exc.InvalidRequestError(
  248. (
  249. "Query.%s() being called on a Query with an existing full "
  250. "statement - can't apply criterion."
  251. )
  252. % meth
  253. )
  254. def _no_limit_offset(self, meth):
  255. if not self._enable_assertions:
  256. return
  257. if self._limit_clause is not None or self._offset_clause is not None:
  258. raise sa_exc.InvalidRequestError(
  259. "Query.%s() being called on a Query which already has LIMIT "
  260. "or OFFSET applied. Call %s() before limit() or offset() "
  261. "are applied." % (meth, meth)
  262. )
  263. @property
  264. def _has_row_limiting_clause(self):
  265. return (
  266. self._limit_clause is not None or self._offset_clause is not None
  267. )
  268. def _get_options(
  269. self,
  270. populate_existing=None,
  271. version_check=None,
  272. only_load_props=None,
  273. refresh_state=None,
  274. identity_token=None,
  275. ):
  276. load_options = {}
  277. compile_options = {}
  278. if version_check:
  279. load_options["_version_check"] = version_check
  280. if populate_existing:
  281. load_options["_populate_existing"] = populate_existing
  282. if refresh_state:
  283. load_options["_refresh_state"] = refresh_state
  284. compile_options["_for_refresh_state"] = True
  285. if only_load_props:
  286. compile_options["_only_load_props"] = frozenset(only_load_props)
  287. if identity_token:
  288. load_options["_refresh_identity_token"] = identity_token
  289. if load_options:
  290. self.load_options += load_options
  291. if compile_options:
  292. self._compile_options += compile_options
  293. return self
  294. def _clone(self):
  295. return self._generate()
  296. @property
  297. def statement(self):
  298. """The full SELECT statement represented by this Query.
  299. The statement by default will not have disambiguating labels
  300. applied to the construct unless with_labels(True) is called
  301. first.
  302. """
  303. # .statement can return the direct future.Select() construct here, as
  304. # long as we are not using subsequent adaption features that
  305. # are made against raw entities, e.g. from_self(), with_polymorphic(),
  306. # select_entity_from(). If these features are being used, then
  307. # the Select() we return will not have the correct .selected_columns
  308. # collection and will not embed in subsequent queries correctly.
  309. # We could find a way to make this collection "correct", however
  310. # this would not be too different from doing the full compile as
  311. # we are doing in any case, the Select() would still not have the
  312. # proper state for other attributes like whereclause, order_by,
  313. # and these features are all deprecated in any case.
  314. #
  315. # for these reasons, Query is not a Select, it remains an ORM
  316. # object for which __clause_element__() must be called in order for
  317. # it to provide a real expression object.
  318. #
  319. # from there, it starts to look much like Query itself won't be
  320. # passed into the execute process and wont generate its own cache
  321. # key; this will all occur in terms of the ORM-enabled Select.
  322. if (
  323. not self._compile_options._set_base_alias
  324. and not self._compile_options._with_polymorphic_adapt_map
  325. ):
  326. # if we don't have legacy top level aliasing features in use
  327. # then convert to a future select() directly
  328. stmt = self._statement_20(for_statement=True)
  329. else:
  330. stmt = self._compile_state(for_statement=True).statement
  331. if self._params:
  332. stmt = stmt.params(self._params)
  333. return stmt
  334. def _final_statement(self, legacy_query_style=True):
  335. """Return the 'final' SELECT statement for this :class:`.Query`.
  336. This is the Core-only select() that will be rendered by a complete
  337. compilation of this query, and is what .statement used to return
  338. in 1.3.
  339. This method creates a complete compile state so is fairly expensive.
  340. """
  341. q = self._clone()
  342. return q._compile_state(
  343. use_legacy_query_style=legacy_query_style
  344. ).statement
  345. def _statement_20(self, for_statement=False, use_legacy_query_style=True):
  346. # TODO: this event needs to be deprecated, as it currently applies
  347. # only to ORM query and occurs at this spot that is now more
  348. # or less an artificial spot
  349. if self.dispatch.before_compile:
  350. for fn in self.dispatch.before_compile:
  351. new_query = fn(self)
  352. if new_query is not None and new_query is not self:
  353. self = new_query
  354. if not fn._bake_ok:
  355. self._compile_options += {"_bake_ok": False}
  356. compile_options = self._compile_options
  357. compile_options += {
  358. "_for_statement": for_statement,
  359. "_use_legacy_query_style": use_legacy_query_style,
  360. }
  361. if self._statement is not None:
  362. stmt = FromStatement(self._raw_columns, self._statement)
  363. stmt.__dict__.update(
  364. _with_options=self._with_options,
  365. _with_context_options=self._with_context_options,
  366. _compile_options=compile_options,
  367. _execution_options=self._execution_options,
  368. _propagate_attrs=self._propagate_attrs,
  369. )
  370. else:
  371. # Query / select() internal attributes are 99% cross-compatible
  372. stmt = Select._create_raw_select(**self.__dict__)
  373. stmt.__dict__.update(
  374. _label_style=self._label_style,
  375. _compile_options=compile_options,
  376. _propagate_attrs=self._propagate_attrs,
  377. )
  378. stmt.__dict__.pop("session", None)
  379. # ensure the ORM context is used to compile the statement, even
  380. # if it has no ORM entities. This is so ORM-only things like
  381. # _legacy_joins are picked up that wouldn't be picked up by the
  382. # Core statement context
  383. if "compile_state_plugin" not in stmt._propagate_attrs:
  384. stmt._propagate_attrs = stmt._propagate_attrs.union(
  385. {"compile_state_plugin": "orm", "plugin_subject": None}
  386. )
  387. return stmt
  388. def subquery(
  389. self,
  390. name=None,
  391. with_labels=False,
  392. reduce_columns=False,
  393. ):
  394. """Return the full SELECT statement represented by
  395. this :class:`_query.Query`, embedded within an
  396. :class:`_expression.Alias`.
  397. Eager JOIN generation within the query is disabled.
  398. :param name: string name to be assigned as the alias;
  399. this is passed through to :meth:`_expression.FromClause.alias`.
  400. If ``None``, a name will be deterministically generated
  401. at compile time.
  402. :param with_labels: if True, :meth:`.with_labels` will be called
  403. on the :class:`_query.Query` first to apply table-qualified labels
  404. to all columns.
  405. :param reduce_columns: if True,
  406. :meth:`_expression.Select.reduce_columns` will
  407. be called on the resulting :func:`_expression.select` construct,
  408. to remove same-named columns where one also refers to the other
  409. via foreign key or WHERE clause equivalence.
  410. """
  411. q = self.enable_eagerloads(False)
  412. if with_labels:
  413. q = q.set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL)
  414. q = q.statement
  415. if reduce_columns:
  416. q = q.reduce_columns()
  417. return q.alias(name=name)
  418. def cte(self, name=None, recursive=False, nesting=False):
  419. r"""Return the full SELECT statement represented by this
  420. :class:`_query.Query` represented as a common table expression (CTE).
  421. Parameters and usage are the same as those of the
  422. :meth:`_expression.SelectBase.cte` method; see that method for
  423. further details.
  424. Here is the `PostgreSQL WITH
  425. RECURSIVE example
  426. <https://www.postgresql.org/docs/8.4/static/queries-with.html>`_.
  427. Note that, in this example, the ``included_parts`` cte and the
  428. ``incl_alias`` alias of it are Core selectables, which
  429. means the columns are accessed via the ``.c.`` attribute. The
  430. ``parts_alias`` object is an :func:`_orm.aliased` instance of the
  431. ``Part`` entity, so column-mapped attributes are available
  432. directly::
  433. from sqlalchemy.orm import aliased
  434. class Part(Base):
  435. __tablename__ = 'part'
  436. part = Column(String, primary_key=True)
  437. sub_part = Column(String, primary_key=True)
  438. quantity = Column(Integer)
  439. included_parts = session.query(
  440. Part.sub_part,
  441. Part.part,
  442. Part.quantity).\
  443. filter(Part.part=="our part").\
  444. cte(name="included_parts", recursive=True)
  445. incl_alias = aliased(included_parts, name="pr")
  446. parts_alias = aliased(Part, name="p")
  447. included_parts = included_parts.union_all(
  448. session.query(
  449. parts_alias.sub_part,
  450. parts_alias.part,
  451. parts_alias.quantity).\
  452. filter(parts_alias.part==incl_alias.c.sub_part)
  453. )
  454. q = session.query(
  455. included_parts.c.sub_part,
  456. func.sum(included_parts.c.quantity).
  457. label('total_quantity')
  458. ).\
  459. group_by(included_parts.c.sub_part)
  460. .. seealso::
  461. :meth:`_expression.HasCTE.cte`
  462. """
  463. return self.enable_eagerloads(False).statement.cte(
  464. name=name, recursive=recursive, nesting=nesting
  465. )
  466. def label(self, name):
  467. """Return the full SELECT statement represented by this
  468. :class:`_query.Query`, converted
  469. to a scalar subquery with a label of the given name.
  470. Analogous to :meth:`sqlalchemy.sql.expression.SelectBase.label`.
  471. """
  472. return self.enable_eagerloads(False).statement.label(name)
  473. @util.deprecated(
  474. "1.4",
  475. "The :meth:`_query.Query.as_scalar` method is deprecated and will be "
  476. "removed in a future release. Please refer to "
  477. ":meth:`_query.Query.scalar_subquery`.",
  478. )
  479. def as_scalar(self):
  480. """Return the full SELECT statement represented by this
  481. :class:`_query.Query`, converted to a scalar subquery.
  482. """
  483. return self.scalar_subquery()
  484. def scalar_subquery(self):
  485. """Return the full SELECT statement represented by this
  486. :class:`_query.Query`, converted to a scalar subquery.
  487. Analogous to
  488. :meth:`sqlalchemy.sql.expression.SelectBase.scalar_subquery`.
  489. .. versionchanged:: 1.4 The :meth:`_query.Query.scalar_subquery`
  490. method replaces the :meth:`_query.Query.as_scalar` method.
  491. """
  492. return self.enable_eagerloads(False).statement.scalar_subquery()
  493. @property
  494. def selectable(self):
  495. """Return the :class:`_expression.Select` object emitted by this
  496. :class:`_query.Query`.
  497. Used for :func:`_sa.inspect` compatibility, this is equivalent to::
  498. query.enable_eagerloads(False).with_labels().statement
  499. """
  500. return self.__clause_element__()
  501. def __clause_element__(self):
  502. return (
  503. self._with_compile_options(
  504. _enable_eagerloads=False, _render_for_subquery=True
  505. )
  506. .set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL)
  507. .statement
  508. )
  509. @_generative
  510. def only_return_tuples(self, value):
  511. """When set to True, the query results will always be a tuple.
  512. This is specifically for single element queries. The default is False.
  513. .. versionadded:: 1.2.5
  514. .. seealso::
  515. :meth:`_query.Query.is_single_entity`
  516. """
  517. self.load_options += dict(_only_return_tuples=value)
  518. @property
  519. def is_single_entity(self):
  520. """Indicates if this :class:`_query.Query`
  521. returns tuples or single entities.
  522. Returns True if this query returns a single entity for each instance
  523. in its result list, and False if this query returns a tuple of entities
  524. for each result.
  525. .. versionadded:: 1.3.11
  526. .. seealso::
  527. :meth:`_query.Query.only_return_tuples`
  528. """
  529. return (
  530. not self.load_options._only_return_tuples
  531. and len(self._raw_columns) == 1
  532. and "parententity" in self._raw_columns[0]._annotations
  533. and isinstance(
  534. self._raw_columns[0]._annotations["parententity"],
  535. ORMColumnsClauseRole,
  536. )
  537. )
  538. @_generative
  539. def enable_eagerloads(self, value):
  540. """Control whether or not eager joins and subqueries are
  541. rendered.
  542. When set to False, the returned Query will not render
  543. eager joins regardless of :func:`~sqlalchemy.orm.joinedload`,
  544. :func:`~sqlalchemy.orm.subqueryload` options
  545. or mapper-level ``lazy='joined'``/``lazy='subquery'``
  546. configurations.
  547. This is used primarily when nesting the Query's
  548. statement into a subquery or other
  549. selectable, or when using :meth:`_query.Query.yield_per`.
  550. """
  551. self._compile_options += {"_enable_eagerloads": value}
  552. @_generative
  553. def _with_compile_options(self, **opt):
  554. self._compile_options += opt
  555. @util.deprecated_20(
  556. ":meth:`_orm.Query.with_labels` and :meth:`_orm.Query.apply_labels`",
  557. alternative="Use set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL) "
  558. "instead.",
  559. )
  560. def with_labels(self):
  561. return self.set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL)
  562. apply_labels = with_labels
  563. @property
  564. def get_label_style(self):
  565. """
  566. Retrieve the current label style.
  567. .. versionadded:: 1.4
  568. """
  569. return self._label_style
  570. def set_label_style(self, style):
  571. """Apply column labels to the return value of Query.statement.
  572. Indicates that this Query's `statement` accessor should return
  573. a SELECT statement that applies labels to all columns in the
  574. form <tablename>_<columnname>; this is commonly used to
  575. disambiguate columns from multiple tables which have the same
  576. name.
  577. When the `Query` actually issues SQL to load rows, it always
  578. uses column labeling.
  579. .. note:: The :meth:`_query.Query.set_label_style` method *only* applies
  580. the output of :attr:`_query.Query.statement`, and *not* to any of
  581. the result-row invoking systems of :class:`_query.Query` itself,
  582. e.g.
  583. :meth:`_query.Query.first`, :meth:`_query.Query.all`, etc.
  584. To execute
  585. a query using :meth:`_query.Query.set_label_style`, invoke the
  586. :attr:`_query.Query.statement` using :meth:`.Session.execute`::
  587. result = session.execute(
  588. query
  589. .set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL)
  590. .statement
  591. )
  592. .. versionadded:: 1.4
  593. """ # noqa
  594. if self._label_style is not style:
  595. self = self._generate()
  596. self._label_style = style
  597. return self
  598. @_generative
  599. def enable_assertions(self, value):
  600. """Control whether assertions are generated.
  601. When set to False, the returned Query will
  602. not assert its state before certain operations,
  603. including that LIMIT/OFFSET has not been applied
  604. when filter() is called, no criterion exists
  605. when get() is called, and no "from_statement()"
  606. exists when filter()/order_by()/group_by() etc.
  607. is called. This more permissive mode is used by
  608. custom Query subclasses to specify criterion or
  609. other modifiers outside of the usual usage patterns.
  610. Care should be taken to ensure that the usage
  611. pattern is even possible. A statement applied
  612. by from_statement() will override any criterion
  613. set by filter() or order_by(), for example.
  614. """
  615. self._enable_assertions = value
  616. @property
  617. def whereclause(self):
  618. """A readonly attribute which returns the current WHERE criterion for
  619. this Query.
  620. This returned value is a SQL expression construct, or ``None`` if no
  621. criterion has been established.
  622. """
  623. return sql.elements.BooleanClauseList._construct_for_whereclause(
  624. self._where_criteria
  625. )
  626. @_generative
  627. def _with_current_path(self, path):
  628. """indicate that this query applies to objects loaded
  629. within a certain path.
  630. Used by deferred loaders (see strategies.py) which transfer
  631. query options from an originating query to a newly generated
  632. query intended for the deferred load.
  633. """
  634. self._compile_options += {"_current_path": path}
  635. @_generative
  636. @_assertions(_no_clauseelement_condition)
  637. @util.deprecated_20(
  638. ":meth:`_orm.Query.with_polymorphic`",
  639. alternative="Use the orm.with_polymorphic() standalone function",
  640. )
  641. def with_polymorphic(
  642. self, cls_or_mappers, selectable=None, polymorphic_on=None
  643. ):
  644. """Load columns for inheriting classes.
  645. This is a legacy method which is replaced by the
  646. :func:`_orm.with_polymorphic` function.
  647. .. warning:: The :meth:`_orm.Query.with_polymorphic` method does
  648. **not** support 1.4/2.0 style features including
  649. :func:`_orm.with_loader_criteria`. Please migrate code
  650. to use :func:`_orm.with_polymorphic`.
  651. :meth:`_query.Query.with_polymorphic` applies transformations
  652. to the "main" mapped class represented by this :class:`_query.Query`.
  653. The "main" mapped class here means the :class:`_query.Query`
  654. object's first argument is a full class, i.e.
  655. ``session.query(SomeClass)``. These transformations allow additional
  656. tables to be present in the FROM clause so that columns for a
  657. joined-inheritance subclass are available in the query, both for the
  658. purposes of load-time efficiency as well as the ability to use
  659. these columns at query time.
  660. .. seealso::
  661. :ref:`with_polymorphic` - illustrates current patterns
  662. """
  663. entity = _legacy_filter_by_entity_zero(self)
  664. wp = with_polymorphic(
  665. entity,
  666. cls_or_mappers,
  667. selectable=selectable,
  668. polymorphic_on=polymorphic_on,
  669. )
  670. self._compile_options = self._compile_options.add_to_element(
  671. "_with_polymorphic_adapt_map", ((entity, inspect(wp)),)
  672. )
  673. @_generative
  674. def yield_per(self, count):
  675. r"""Yield only ``count`` rows at a time.
  676. The purpose of this method is when fetching very large result sets
  677. (> 10K rows), to batch results in sub-collections and yield them
  678. out partially, so that the Python interpreter doesn't need to declare
  679. very large areas of memory which is both time consuming and leads
  680. to excessive memory use. The performance from fetching hundreds of
  681. thousands of rows can often double when a suitable yield-per setting
  682. (e.g. approximately 1000) is used, even with DBAPIs that buffer
  683. rows (which are most).
  684. As of SQLAlchemy 1.4, the :meth:`_orm.Query.yield_per` method is
  685. equivalent to using the ``yield_per`` execution option at the ORM
  686. level. See the section :ref:`orm_queryguide_yield_per` for further
  687. background on this option.
  688. """
  689. self.load_options += {"_yield_per": count}
  690. @util.deprecated_20(
  691. ":meth:`_orm.Query.get`",
  692. alternative="The method is now available as :meth:`_orm.Session.get`",
  693. becomes_legacy=True,
  694. )
  695. def get(self, ident):
  696. """Return an instance based on the given primary key identifier,
  697. or ``None`` if not found.
  698. E.g.::
  699. my_user = session.query(User).get(5)
  700. some_object = session.query(VersionedFoo).get((5, 10))
  701. some_object = session.query(VersionedFoo).get(
  702. {"id": 5, "version_id": 10})
  703. :meth:`_query.Query.get` is special in that it provides direct
  704. access to the identity map of the owning :class:`.Session`.
  705. If the given primary key identifier is present
  706. in the local identity map, the object is returned
  707. directly from this collection and no SQL is emitted,
  708. unless the object has been marked fully expired.
  709. If not present,
  710. a SELECT is performed in order to locate the object.
  711. :meth:`_query.Query.get` also will perform a check if
  712. the object is present in the identity map and
  713. marked as expired - a SELECT
  714. is emitted to refresh the object as well as to
  715. ensure that the row is still present.
  716. If not, :class:`~sqlalchemy.orm.exc.ObjectDeletedError` is raised.
  717. :meth:`_query.Query.get` is only used to return a single
  718. mapped instance, not multiple instances or
  719. individual column constructs, and strictly
  720. on a single primary key value. The originating
  721. :class:`_query.Query` must be constructed in this way,
  722. i.e. against a single mapped entity,
  723. with no additional filtering criterion. Loading
  724. options via :meth:`_query.Query.options` may be applied
  725. however, and will be used if the object is not
  726. yet locally present.
  727. :param ident: A scalar, tuple, or dictionary representing the
  728. primary key. For a composite (e.g. multiple column) primary key,
  729. a tuple or dictionary should be passed.
  730. For a single-column primary key, the scalar calling form is typically
  731. the most expedient. If the primary key of a row is the value "5",
  732. the call looks like::
  733. my_object = query.get(5)
  734. The tuple form contains primary key values typically in
  735. the order in which they correspond to the mapped
  736. :class:`_schema.Table`
  737. object's primary key columns, or if the
  738. :paramref:`_orm.Mapper.primary_key` configuration parameter were
  739. used, in
  740. the order used for that parameter. For example, if the primary key
  741. of a row is represented by the integer
  742. digits "5, 10" the call would look like::
  743. my_object = query.get((5, 10))
  744. The dictionary form should include as keys the mapped attribute names
  745. corresponding to each element of the primary key. If the mapped class
  746. has the attributes ``id``, ``version_id`` as the attributes which
  747. store the object's primary key value, the call would look like::
  748. my_object = query.get({"id": 5, "version_id": 10})
  749. .. versionadded:: 1.3 the :meth:`_query.Query.get`
  750. method now optionally
  751. accepts a dictionary of attribute names to values in order to
  752. indicate a primary key identifier.
  753. :return: The object instance, or ``None``.
  754. """
  755. self._no_criterion_assertion("get", order_by=False, distinct=False)
  756. # we still implement _get_impl() so that baked query can override
  757. # it
  758. return self._get_impl(ident, loading.load_on_pk_identity)
  759. def _get_impl(self, primary_key_identity, db_load_fn, identity_token=None):
  760. mapper = self._only_full_mapper_zero("get")
  761. return self.session._get_impl(
  762. mapper,
  763. primary_key_identity,
  764. db_load_fn,
  765. populate_existing=self.load_options._populate_existing,
  766. with_for_update=self._for_update_arg,
  767. options=self._with_options,
  768. identity_token=identity_token,
  769. execution_options=self._execution_options,
  770. )
  771. @property
  772. def lazy_loaded_from(self):
  773. """An :class:`.InstanceState` that is using this :class:`_query.Query`
  774. for a lazy load operation.
  775. .. deprecated:: 1.4 This attribute should be viewed via the
  776. :attr:`.ORMExecuteState.lazy_loaded_from` attribute, within
  777. the context of the :meth:`.SessionEvents.do_orm_execute`
  778. event.
  779. .. seealso::
  780. :attr:`.ORMExecuteState.lazy_loaded_from`
  781. """
  782. return self.load_options._lazy_loaded_from
  783. @property
  784. def _current_path(self):
  785. return self._compile_options._current_path
  786. @_generative
  787. def correlate(self, *fromclauses):
  788. """Return a :class:`.Query` construct which will correlate the given
  789. FROM clauses to that of an enclosing :class:`.Query` or
  790. :func:`~.expression.select`.
  791. The method here accepts mapped classes, :func:`.aliased` constructs,
  792. and :func:`.mapper` constructs as arguments, which are resolved into
  793. expression constructs, in addition to appropriate expression
  794. constructs.
  795. The correlation arguments are ultimately passed to
  796. :meth:`_expression.Select.correlate`
  797. after coercion to expression constructs.
  798. The correlation arguments take effect in such cases
  799. as when :meth:`_query.Query.from_self` is used, or when
  800. a subquery as returned by :meth:`_query.Query.subquery` is
  801. embedded in another :func:`_expression.select` construct.
  802. """
  803. self._auto_correlate = False
  804. if fromclauses and fromclauses[0] in {None, False}:
  805. self._correlate = ()
  806. else:
  807. self._correlate = set(self._correlate).union(
  808. coercions.expect(roles.FromClauseRole, f) for f in fromclauses
  809. )
  810. @_generative
  811. def autoflush(self, setting):
  812. """Return a Query with a specific 'autoflush' setting.
  813. As of SQLAlchemy 1.4, the :meth:`_orm.Query.autoflush` method
  814. is equivalent to using the ``autoflush`` execution option at the
  815. ORM level. See the section :ref:`orm_queryguide_autoflush` for
  816. further background on this option.
  817. """
  818. self.load_options += {"_autoflush": setting}
  819. @_generative
  820. def populate_existing(self):
  821. """Return a :class:`_query.Query`
  822. that will expire and refresh all instances
  823. as they are loaded, or reused from the current :class:`.Session`.
  824. As of SQLAlchemy 1.4, the :meth:`_orm.Query.populate_existing` method
  825. is equivalent to using the ``populate_existing`` execution option at
  826. the ORM level. See the section :ref:`orm_queryguide_populate_existing`
  827. for further background on this option.
  828. """
  829. self.load_options += {"_populate_existing": True}
  830. @_generative
  831. def _with_invoke_all_eagers(self, value):
  832. """Set the 'invoke all eagers' flag which causes joined- and
  833. subquery loaders to traverse into already-loaded related objects
  834. and collections.
  835. Default is that of :attr:`_query.Query._invoke_all_eagers`.
  836. """
  837. self.load_options += {"_invoke_all_eagers": value}
  838. @util.deprecated_20(
  839. ":meth:`_orm.Query.with_parent`",
  840. alternative="Use the :func:`_orm.with_parent` standalone construct.",
  841. becomes_legacy=True,
  842. )
  843. @util.preload_module("sqlalchemy.orm.relationships")
  844. def with_parent(self, instance, property=None, from_entity=None): # noqa
  845. """Add filtering criterion that relates the given instance
  846. to a child object or collection, using its attribute state
  847. as well as an established :func:`_orm.relationship()`
  848. configuration.
  849. The method uses the :func:`.with_parent` function to generate
  850. the clause, the result of which is passed to
  851. :meth:`_query.Query.filter`.
  852. Parameters are the same as :func:`.with_parent`, with the exception
  853. that the given property can be None, in which case a search is
  854. performed against this :class:`_query.Query` object's target mapper.
  855. :param instance:
  856. An instance which has some :func:`_orm.relationship`.
  857. :param property:
  858. String property name, or class-bound attribute, which indicates
  859. what relationship from the instance should be used to reconcile the
  860. parent/child relationship.
  861. :param from_entity:
  862. Entity in which to consider as the left side. This defaults to the
  863. "zero" entity of the :class:`_query.Query` itself.
  864. """
  865. relationships = util.preloaded.orm_relationships
  866. if from_entity:
  867. entity_zero = inspect(from_entity)
  868. else:
  869. entity_zero = _legacy_filter_by_entity_zero(self)
  870. if property is None:
  871. # TODO: deprecate, property has to be supplied
  872. mapper = object_mapper(instance)
  873. for prop in mapper.iterate_properties:
  874. if (
  875. isinstance(prop, relationships.RelationshipProperty)
  876. and prop.mapper is entity_zero.mapper
  877. ):
  878. property = prop # noqa
  879. break
  880. else:
  881. raise sa_exc.InvalidRequestError(
  882. "Could not locate a property which relates instances "
  883. "of class '%s' to instances of class '%s'"
  884. % (
  885. entity_zero.mapper.class_.__name__,
  886. instance.__class__.__name__,
  887. )
  888. )
  889. return self.filter(with_parent(instance, property, entity_zero.entity))
  890. @_generative
  891. def add_entity(self, entity, alias=None):
  892. """add a mapped entity to the list of result columns
  893. to be returned."""
  894. if alias is not None:
  895. # TODO: deprecate
  896. entity = aliased(entity, alias)
  897. self._raw_columns = list(self._raw_columns)
  898. self._raw_columns.append(
  899. coercions.expect(
  900. roles.ColumnsClauseRole, entity, apply_propagate_attrs=self
  901. )
  902. )
  903. @_generative
  904. def with_session(self, session):
  905. """Return a :class:`_query.Query` that will use the given
  906. :class:`.Session`.
  907. While the :class:`_query.Query`
  908. object is normally instantiated using the
  909. :meth:`.Session.query` method, it is legal to build the
  910. :class:`_query.Query`
  911. directly without necessarily using a :class:`.Session`. Such a
  912. :class:`_query.Query` object, or any :class:`_query.Query`
  913. already associated
  914. with a different :class:`.Session`, can produce a new
  915. :class:`_query.Query`
  916. object associated with a target session using this method::
  917. from sqlalchemy.orm import Query
  918. query = Query([MyClass]).filter(MyClass.id == 5)
  919. result = query.with_session(my_session).one()
  920. """
  921. self.session = session
  922. @util.deprecated_20(
  923. ":meth:`_query.Query.from_self`",
  924. alternative="The new approach is to use the :func:`.orm.aliased` "
  925. "construct in conjunction with a subquery. See the section "
  926. ":ref:`Selecting from the query itself as a subquery "
  927. "<migration_20_query_from_self>` in the 2.0 migration notes for an "
  928. "example.",
  929. )
  930. def from_self(self, *entities):
  931. r"""return a Query that selects from this Query's
  932. SELECT statement.
  933. :meth:`_query.Query.from_self` essentially turns the SELECT statement
  934. into a SELECT of itself. Given a query such as::
  935. q = session.query(User).filter(User.name.like('e%'))
  936. Given the :meth:`_query.Query.from_self` version::
  937. q = session.query(User).filter(User.name.like('e%')).from_self()
  938. This query renders as:
  939. .. sourcecode:: sql
  940. SELECT anon_1.user_id AS anon_1_user_id,
  941. anon_1.user_name AS anon_1_user_name
  942. FROM (SELECT "user".id AS user_id, "user".name AS user_name
  943. FROM "user"
  944. WHERE "user".name LIKE :name_1) AS anon_1
  945. There are lots of cases where :meth:`_query.Query.from_self`
  946. may be useful.
  947. A simple one is where above, we may want to apply a row LIMIT to
  948. the set of user objects we query against, and then apply additional
  949. joins against that row-limited set::
  950. q = session.query(User).filter(User.name.like('e%')).\
  951. limit(5).from_self().\
  952. join(User.addresses).filter(Address.email.like('q%'))
  953. The above query joins to the ``Address`` entity but only against the
  954. first five results of the ``User`` query:
  955. .. sourcecode:: sql
  956. SELECT anon_1.user_id AS anon_1_user_id,
  957. anon_1.user_name AS anon_1_user_name
  958. FROM (SELECT "user".id AS user_id, "user".name AS user_name
  959. FROM "user"
  960. WHERE "user".name LIKE :name_1
  961. LIMIT :param_1) AS anon_1
  962. JOIN address ON anon_1.user_id = address.user_id
  963. WHERE address.email LIKE :email_1
  964. **Automatic Aliasing**
  965. Another key behavior of :meth:`_query.Query.from_self`
  966. is that it applies
  967. **automatic aliasing** to the entities inside the subquery, when
  968. they are referenced on the outside. Above, if we continue to
  969. refer to the ``User`` entity without any additional aliasing applied
  970. to it, those references will be in terms of the subquery::
  971. q = session.query(User).filter(User.name.like('e%')).\
  972. limit(5).from_self().\
  973. join(User.addresses).filter(Address.email.like('q%')).\
  974. order_by(User.name)
  975. The ORDER BY against ``User.name`` is aliased to be in terms of the
  976. inner subquery:
  977. .. sourcecode:: sql
  978. SELECT anon_1.user_id AS anon_1_user_id,
  979. anon_1.user_name AS anon_1_user_name
  980. FROM (SELECT "user".id AS user_id, "user".name AS user_name
  981. FROM "user"
  982. WHERE "user".name LIKE :name_1
  983. LIMIT :param_1) AS anon_1
  984. JOIN address ON anon_1.user_id = address.user_id
  985. WHERE address.email LIKE :email_1 ORDER BY anon_1.user_name
  986. The automatic aliasing feature only works in a **limited** way,
  987. for simple filters and orderings. More ambitious constructions
  988. such as referring to the entity in joins should prefer to use
  989. explicit subquery objects, typically making use of the
  990. :meth:`_query.Query.subquery`
  991. method to produce an explicit subquery object.
  992. Always test the structure of queries by viewing the SQL to ensure
  993. a particular structure does what's expected!
  994. **Changing the Entities**
  995. :meth:`_query.Query.from_self`
  996. also includes the ability to modify what
  997. columns are being queried. In our example, we want ``User.id``
  998. to be queried by the inner query, so that we can join to the
  999. ``Address`` entity on the outside, but we only wanted the outer
  1000. query to return the ``Address.email`` column::
  1001. q = session.query(User).filter(User.name.like('e%')).\
  1002. limit(5).from_self(Address.email).\
  1003. join(User.addresses).filter(Address.email.like('q%'))
  1004. yielding:
  1005. .. sourcecode:: sql
  1006. SELECT address.email AS address_email
  1007. FROM (SELECT "user".id AS user_id, "user".name AS user_name
  1008. FROM "user"
  1009. WHERE "user".name LIKE :name_1
  1010. LIMIT :param_1) AS anon_1
  1011. JOIN address ON anon_1.user_id = address.user_id
  1012. WHERE address.email LIKE :email_1
  1013. **Looking out for Inner / Outer Columns**
  1014. Keep in mind that when referring to columns that originate from
  1015. inside the subquery, we need to ensure they are present in the
  1016. columns clause of the subquery itself; this is an ordinary aspect of
  1017. SQL. For example, if we wanted to load from a joined entity inside
  1018. the subquery using :func:`.contains_eager`, we need to add those
  1019. columns. Below illustrates a join of ``Address`` to ``User``,
  1020. then a subquery, and then we'd like :func:`.contains_eager` to access
  1021. the ``User`` columns::
  1022. q = session.query(Address).join(Address.user).\
  1023. filter(User.name.like('e%'))
  1024. q = q.add_entity(User).from_self().\
  1025. options(contains_eager(Address.user))
  1026. We use :meth:`_query.Query.add_entity` above **before** we call
  1027. :meth:`_query.Query.from_self`
  1028. so that the ``User`` columns are present
  1029. in the inner subquery, so that they are available to the
  1030. :func:`.contains_eager` modifier we are using on the outside,
  1031. producing:
  1032. .. sourcecode:: sql
  1033. SELECT anon_1.address_id AS anon_1_address_id,
  1034. anon_1.address_email AS anon_1_address_email,
  1035. anon_1.address_user_id AS anon_1_address_user_id,
  1036. anon_1.user_id AS anon_1_user_id,
  1037. anon_1.user_name AS anon_1_user_name
  1038. FROM (
  1039. SELECT address.id AS address_id,
  1040. address.email AS address_email,
  1041. address.user_id AS address_user_id,
  1042. "user".id AS user_id,
  1043. "user".name AS user_name
  1044. FROM address JOIN "user" ON "user".id = address.user_id
  1045. WHERE "user".name LIKE :name_1) AS anon_1
  1046. If we didn't call ``add_entity(User)``, but still asked
  1047. :func:`.contains_eager` to load the ``User`` entity, it would be
  1048. forced to add the table on the outside without the correct
  1049. join criteria - note the ``anon1, "user"`` phrase at
  1050. the end:
  1051. .. sourcecode:: sql
  1052. -- incorrect query
  1053. SELECT anon_1.address_id AS anon_1_address_id,
  1054. anon_1.address_email AS anon_1_address_email,
  1055. anon_1.address_user_id AS anon_1_address_user_id,
  1056. "user".id AS user_id,
  1057. "user".name AS user_name
  1058. FROM (
  1059. SELECT address.id AS address_id,
  1060. address.email AS address_email,
  1061. address.user_id AS address_user_id
  1062. FROM address JOIN "user" ON "user".id = address.user_id
  1063. WHERE "user".name LIKE :name_1) AS anon_1, "user"
  1064. :param \*entities: optional list of entities which will replace
  1065. those being selected.
  1066. """
  1067. return self._from_self(*entities)
  1068. def _from_self(self, *entities):
  1069. fromclause = (
  1070. self.set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL)
  1071. .correlate(None)
  1072. .subquery()
  1073. ._anonymous_fromclause()
  1074. )
  1075. q = self._from_selectable(fromclause)
  1076. if entities:
  1077. q._set_entities(entities)
  1078. return q
  1079. @_generative
  1080. def _set_enable_single_crit(self, val):
  1081. self._compile_options += {"_enable_single_crit": val}
  1082. @_generative
  1083. def _from_selectable(self, fromclause, set_entity_from=True):
  1084. for attr in (
  1085. "_where_criteria",
  1086. "_order_by_clauses",
  1087. "_group_by_clauses",
  1088. "_limit_clause",
  1089. "_offset_clause",
  1090. "_last_joined_entity",
  1091. "_legacy_setup_joins",
  1092. "_memoized_select_entities",
  1093. "_distinct",
  1094. "_distinct_on",
  1095. "_having_criteria",
  1096. "_prefixes",
  1097. "_suffixes",
  1098. ):
  1099. self.__dict__.pop(attr, None)
  1100. self._set_select_from([fromclause], set_entity_from)
  1101. self._compile_options += {
  1102. "_enable_single_crit": False,
  1103. }
  1104. # this enables clause adaptation for non-ORM
  1105. # expressions.
  1106. # legacy. see test/orm/test_froms.py for various
  1107. # "oldstyle" tests that rely on this and the corresponding
  1108. # "newtyle" that do not.
  1109. self._compile_options += {"_orm_only_from_obj_alias": False}
  1110. @util.deprecated(
  1111. "1.4",
  1112. ":meth:`_query.Query.values` "
  1113. "is deprecated and will be removed in a "
  1114. "future release. Please use :meth:`_query.Query.with_entities`",
  1115. )
  1116. def values(self, *columns):
  1117. """Return an iterator yielding result tuples corresponding
  1118. to the given list of columns
  1119. """
  1120. if not columns:
  1121. return iter(())
  1122. q = self._clone().enable_eagerloads(False)
  1123. q._set_entities(columns)
  1124. if not q.load_options._yield_per:
  1125. q.load_options += {"_yield_per": 10}
  1126. return iter(q)
  1127. _values = values
  1128. @util.deprecated(
  1129. "1.4",
  1130. ":meth:`_query.Query.value` "
  1131. "is deprecated and will be removed in a "
  1132. "future release. Please use :meth:`_query.Query.with_entities` "
  1133. "in combination with :meth:`_query.Query.scalar`",
  1134. )
  1135. def value(self, column):
  1136. """Return a scalar result corresponding to the given
  1137. column expression.
  1138. """
  1139. try:
  1140. return next(self.values(column))[0]
  1141. except StopIteration:
  1142. return None
  1143. @_generative
  1144. def with_entities(self, *entities):
  1145. r"""Return a new :class:`_query.Query`
  1146. replacing the SELECT list with the
  1147. given entities.
  1148. e.g.::
  1149. # Users, filtered on some arbitrary criterion
  1150. # and then ordered by related email address
  1151. q = session.query(User).\
  1152. join(User.address).\
  1153. filter(User.name.like('%ed%')).\
  1154. order_by(Address.email)
  1155. # given *only* User.id==5, Address.email, and 'q', what
  1156. # would the *next* User in the result be ?
  1157. subq = q.with_entities(Address.email).\
  1158. order_by(None).\
  1159. filter(User.id==5).\
  1160. subquery()
  1161. q = q.join((subq, subq.c.email < Address.email)).\
  1162. limit(1)
  1163. """
  1164. _MemoizedSelectEntities._generate_for_statement(self)
  1165. self._set_entities(entities)
  1166. @_generative
  1167. def add_columns(self, *column):
  1168. """Add one or more column expressions to the list
  1169. of result columns to be returned."""
  1170. self._raw_columns = list(self._raw_columns)
  1171. self._raw_columns.extend(
  1172. coercions.expect(
  1173. roles.ColumnsClauseRole,
  1174. c,
  1175. apply_propagate_attrs=self,
  1176. post_inspect=True,
  1177. )
  1178. for c in column
  1179. )
  1180. @util.deprecated(
  1181. "1.4",
  1182. ":meth:`_query.Query.add_column` "
  1183. "is deprecated and will be removed in a "
  1184. "future release. Please use :meth:`_query.Query.add_columns`",
  1185. )
  1186. def add_column(self, column):
  1187. """Add a column expression to the list of result columns to be
  1188. returned.
  1189. """
  1190. return self.add_columns(column)
  1191. @_generative
  1192. def options(self, *args):
  1193. """Return a new :class:`_query.Query` object,
  1194. applying the given list of
  1195. mapper options.
  1196. Most supplied options regard changing how column- and
  1197. relationship-mapped attributes are loaded.
  1198. .. seealso::
  1199. :ref:`deferred_options`
  1200. :ref:`relationship_loader_options`
  1201. """
  1202. opts = tuple(util.flatten_iterator(args))
  1203. if self._compile_options._current_path:
  1204. for opt in opts:
  1205. if opt._is_legacy_option:
  1206. opt.process_query_conditionally(self)
  1207. else:
  1208. for opt in opts:
  1209. if opt._is_legacy_option:
  1210. opt.process_query(self)
  1211. self._with_options += opts
  1212. def with_transformation(self, fn):
  1213. """Return a new :class:`_query.Query` object transformed by
  1214. the given function.
  1215. E.g.::
  1216. def filter_something(criterion):
  1217. def transform(q):
  1218. return q.filter(criterion)
  1219. return transform
  1220. q = q.with_transformation(filter_something(x==5))
  1221. This allows ad-hoc recipes to be created for :class:`_query.Query`
  1222. objects. See the example at :ref:`hybrid_transformers`.
  1223. """
  1224. return fn(self)
  1225. def get_execution_options(self):
  1226. """Get the non-SQL options which will take effect during execution.
  1227. .. versionadded:: 1.3
  1228. .. seealso::
  1229. :meth:`_query.Query.execution_options`
  1230. """
  1231. return self._execution_options
  1232. @_generative
  1233. def execution_options(self, **kwargs):
  1234. """Set non-SQL options which take effect during execution.
  1235. Options allowed here include all of those accepted by
  1236. :meth:`_engine.Connection.execution_options`, as well as a series
  1237. of ORM specific options:
  1238. ``populate_existing=True`` - equivalent to using
  1239. :meth:`_orm.Query.populate_existing`
  1240. ``autoflush=True|False`` - equivalent to using
  1241. :meth:`_orm.Query.autoflush`
  1242. ``yield_per=<value>`` - equivalent to using
  1243. :meth:`_orm.Query.yield_per`
  1244. Note that the ``stream_results`` execution option is enabled
  1245. automatically if the :meth:`~sqlalchemy.orm.query.Query.yield_per()`
  1246. method or execution option is used.
  1247. The execution options may also be specified on a per execution basis
  1248. when using :term:`2.0 style` queries via the
  1249. :paramref:`_orm.Session.execution_options` parameter.
  1250. .. versionadded:: 1.4 - added ORM options to
  1251. :meth:`_orm.Query.execution_options`
  1252. .. seealso::
  1253. :ref:`engine_stream_results`
  1254. :meth:`_query.Query.get_execution_options`
  1255. """
  1256. self._execution_options = self._execution_options.union(kwargs)
  1257. @_generative
  1258. def with_for_update(
  1259. self,
  1260. read=False,
  1261. nowait=False,
  1262. of=None,
  1263. skip_locked=False,
  1264. key_share=False,
  1265. ):
  1266. """return a new :class:`_query.Query`
  1267. with the specified options for the
  1268. ``FOR UPDATE`` clause.
  1269. The behavior of this method is identical to that of
  1270. :meth:`_expression.GenerativeSelect.with_for_update`.
  1271. When called with no arguments,
  1272. the resulting ``SELECT`` statement will have a ``FOR UPDATE`` clause
  1273. appended. When additional arguments are specified, backend-specific
  1274. options such as ``FOR UPDATE NOWAIT`` or ``LOCK IN SHARE MODE``
  1275. can take effect.
  1276. E.g.::
  1277. q = sess.query(User).populate_existing().with_for_update(nowait=True, of=User)
  1278. The above query on a PostgreSQL backend will render like::
  1279. SELECT users.id AS users_id FROM users FOR UPDATE OF users NOWAIT
  1280. .. warning::
  1281. Using ``with_for_update`` in the context of eager loading
  1282. relationships is not officially supported or recommended by
  1283. SQLAlchemy and may not work with certain queries on various
  1284. database backends. When ``with_for_update`` is successfully used
  1285. with a query that involves :func:`_orm.joinedload`, SQLAlchemy will
  1286. attempt to emit SQL that locks all involved tables.
  1287. .. note:: It is generally a good idea to combine the use of the
  1288. :meth:`_orm.Query.populate_existing` method when using the
  1289. :meth:`_orm.Query.with_for_update` method. The purpose of
  1290. :meth:`_orm.Query.populate_existing` is to force all the data read
  1291. from the SELECT to be populated into the ORM objects returned,
  1292. even if these objects are already in the :term:`identity map`.
  1293. .. seealso::
  1294. :meth:`_expression.GenerativeSelect.with_for_update`
  1295. - Core level method with
  1296. full argument and behavioral description.
  1297. :meth:`_orm.Query.populate_existing` - overwrites attributes of
  1298. objects already loaded in the identity map.
  1299. """ # noqa: E501
  1300. self._for_update_arg = ForUpdateArg(
  1301. read=read,
  1302. nowait=nowait,
  1303. of=of,
  1304. skip_locked=skip_locked,
  1305. key_share=key_share,
  1306. )
  1307. @_generative
  1308. def params(self, *args, **kwargs):
  1309. r"""Add values for bind parameters which may have been
  1310. specified in filter().
  1311. Parameters may be specified using \**kwargs, or optionally a single
  1312. dictionary as the first positional argument. The reason for both is
  1313. that \**kwargs is convenient, however some parameter dictionaries
  1314. contain unicode keys in which case \**kwargs cannot be used.
  1315. """
  1316. if len(args) == 1:
  1317. kwargs.update(args[0])
  1318. elif len(args) > 0:
  1319. raise sa_exc.ArgumentError(
  1320. "params() takes zero or one positional argument, "
  1321. "which is a dictionary."
  1322. )
  1323. self._params = self._params.union(kwargs)
  1324. def where(self, *criterion):
  1325. """A synonym for :meth:`.Query.filter`.
  1326. .. versionadded:: 1.4
  1327. """
  1328. return self.filter(*criterion)
  1329. @_generative
  1330. @_assertions(_no_statement_condition, _no_limit_offset)
  1331. def filter(self, *criterion):
  1332. r"""Apply the given filtering criterion to a copy
  1333. of this :class:`_query.Query`, using SQL expressions.
  1334. e.g.::
  1335. session.query(MyClass).filter(MyClass.name == 'some name')
  1336. Multiple criteria may be specified as comma separated; the effect
  1337. is that they will be joined together using the :func:`.and_`
  1338. function::
  1339. session.query(MyClass).\
  1340. filter(MyClass.name == 'some name', MyClass.id > 5)
  1341. The criterion is any SQL expression object applicable to the
  1342. WHERE clause of a select. String expressions are coerced
  1343. into SQL expression constructs via the :func:`_expression.text`
  1344. construct.
  1345. .. seealso::
  1346. :meth:`_query.Query.filter_by` - filter on keyword expressions.
  1347. """
  1348. for criterion in list(criterion):
  1349. criterion = coercions.expect(
  1350. roles.WhereHavingRole, criterion, apply_propagate_attrs=self
  1351. )
  1352. # legacy vvvvvvvvvvvvvvvvvvvvvvvvvvv
  1353. if self._aliased_generation:
  1354. criterion = sql_util._deep_annotate(
  1355. criterion, {"aliased_generation": self._aliased_generation}
  1356. )
  1357. # legacy ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  1358. self._where_criteria += (criterion,)
  1359. @util.memoized_property
  1360. def _last_joined_entity(self):
  1361. if self._legacy_setup_joins:
  1362. return _legacy_determine_last_joined_entity(
  1363. self._legacy_setup_joins, self._entity_from_pre_ent_zero()
  1364. )
  1365. else:
  1366. return None
  1367. def _filter_by_zero(self):
  1368. """for the filter_by() method, return the target entity for which
  1369. we will attempt to derive an expression from based on string name.
  1370. """
  1371. if self._legacy_setup_joins:
  1372. _last_joined_entity = self._last_joined_entity
  1373. if _last_joined_entity is not None:
  1374. return _last_joined_entity
  1375. # discussion related to #7239
  1376. # special check determines if we should try to derive attributes
  1377. # for filter_by() from the "from object", i.e., if the user
  1378. # called query.select_from(some selectable).filter_by(some_attr=value).
  1379. # We don't want to do that in the case that methods like
  1380. # from_self(), select_entity_from(), or a set op like union() were
  1381. # called; while these methods also place a
  1382. # selectable in the _from_obj collection, they also set up
  1383. # the _set_base_alias boolean which turns on the whole "adapt the
  1384. # entity to this selectable" thing, meaning the query still continues
  1385. # to construct itself in terms of the lead entity that was passed
  1386. # to query(), e.g. query(User).from_self() is still in terms of User,
  1387. # and not the subquery that from_self() created. This feature of
  1388. # "implicitly adapt all occurrences of entity X to some arbitrary
  1389. # subquery" is the main thing I am trying to do away with in 2.0 as
  1390. # users should now used aliased() for that, but I can't entirely get
  1391. # rid of it due to query.union() and other set ops relying upon it.
  1392. #
  1393. # compare this to the base Select()._filter_by_zero() which can
  1394. # just return self._from_obj[0] if present, because there is no
  1395. # "_set_base_alias" feature.
  1396. #
  1397. # IOW, this conditional essentially detects if
  1398. # "select_from(some_selectable)" has been called, as opposed to
  1399. # "select_entity_from()", "from_self()"
  1400. # or "union() / some_set_op()".
  1401. if self._from_obj and not self._compile_options._set_base_alias:
  1402. return self._from_obj[0]
  1403. return self._raw_columns[0]
  1404. def filter_by(self, **kwargs):
  1405. r"""Apply the given filtering criterion to a copy
  1406. of this :class:`_query.Query`, using keyword expressions.
  1407. e.g.::
  1408. session.query(MyClass).filter_by(name = 'some name')
  1409. Multiple criteria may be specified as comma separated; the effect
  1410. is that they will be joined together using the :func:`.and_`
  1411. function::
  1412. session.query(MyClass).\
  1413. filter_by(name = 'some name', id = 5)
  1414. The keyword expressions are extracted from the primary
  1415. entity of the query, or the last entity that was the
  1416. target of a call to :meth:`_query.Query.join`.
  1417. .. seealso::
  1418. :meth:`_query.Query.filter` - filter on SQL expressions.
  1419. """
  1420. from_entity = self._filter_by_zero()
  1421. if from_entity is None:
  1422. raise sa_exc.InvalidRequestError(
  1423. "Can't use filter_by when the first entity '%s' of a query "
  1424. "is not a mapped class. Please use the filter method instead, "
  1425. "or change the order of the entities in the query"
  1426. % self._query_entity_zero()
  1427. )
  1428. clauses = [
  1429. _entity_namespace_key(from_entity, key) == value
  1430. for key, value in kwargs.items()
  1431. ]
  1432. return self.filter(*clauses)
  1433. @_generative
  1434. @_assertions(_no_statement_condition, _no_limit_offset)
  1435. def order_by(self, *clauses):
  1436. """Apply one or more ORDER BY criteria to the query and return
  1437. the newly resulting :class:`_query.Query`.
  1438. e.g.::
  1439. q = session.query(Entity).order_by(Entity.id, Entity.name)
  1440. All existing ORDER BY criteria may be cancelled by passing
  1441. ``None`` by itself. New ORDER BY criteria may then be added by
  1442. invoking :meth:`_orm.Query.order_by` again, e.g.::
  1443. # will erase all ORDER BY and ORDER BY new_col alone
  1444. q = q.order_by(None).order_by(new_col)
  1445. .. seealso::
  1446. These sections describe ORDER BY in terms of :term:`2.0 style`
  1447. invocation but apply to :class:`_orm.Query` as well:
  1448. :ref:`tutorial_order_by` - in the :ref:`unified_tutorial`
  1449. :ref:`tutorial_order_by_label` - in the :ref:`unified_tutorial`
  1450. """
  1451. if len(clauses) == 1 and (clauses[0] is None or clauses[0] is False):
  1452. self._order_by_clauses = ()
  1453. else:
  1454. criterion = tuple(
  1455. coercions.expect(roles.OrderByRole, clause)
  1456. for clause in clauses
  1457. )
  1458. # legacy vvvvvvvvvvvvvvvvvvvvvvvvvvv
  1459. if self._aliased_generation:
  1460. criterion = tuple(
  1461. [
  1462. sql_util._deep_annotate(
  1463. o, {"aliased_generation": self._aliased_generation}
  1464. )
  1465. for o in criterion
  1466. ]
  1467. )
  1468. # legacy ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  1469. self._order_by_clauses += criterion
  1470. @_generative
  1471. @_assertions(_no_statement_condition, _no_limit_offset)
  1472. def group_by(self, *clauses):
  1473. """Apply one or more GROUP BY criterion to the query and return
  1474. the newly resulting :class:`_query.Query`.
  1475. All existing GROUP BY settings can be suppressed by
  1476. passing ``None`` - this will suppress any GROUP BY configured
  1477. on mappers as well.
  1478. .. seealso::
  1479. These sections describe GROUP BY in terms of :term:`2.0 style`
  1480. invocation but apply to :class:`_orm.Query` as well:
  1481. :ref:`tutorial_group_by_w_aggregates` - in the
  1482. :ref:`unified_tutorial`
  1483. :ref:`tutorial_order_by_label` - in the :ref:`unified_tutorial`
  1484. """
  1485. if len(clauses) == 1 and (clauses[0] is None or clauses[0] is False):
  1486. self._group_by_clauses = ()
  1487. else:
  1488. criterion = tuple(
  1489. coercions.expect(roles.GroupByRole, clause)
  1490. for clause in clauses
  1491. )
  1492. # legacy vvvvvvvvvvvvvvvvvvvvvvvvvvv
  1493. if self._aliased_generation:
  1494. criterion = tuple(
  1495. [
  1496. sql_util._deep_annotate(
  1497. o, {"aliased_generation": self._aliased_generation}
  1498. )
  1499. for o in criterion
  1500. ]
  1501. )
  1502. # legacy ^^^^^^^^^^^^^^^^^^^^^^^^^^
  1503. self._group_by_clauses += criterion
  1504. @_generative
  1505. @_assertions(_no_statement_condition, _no_limit_offset)
  1506. def having(self, criterion):
  1507. r"""Apply a HAVING criterion to the query and return the
  1508. newly resulting :class:`_query.Query`.
  1509. :meth:`_query.Query.having` is used in conjunction with
  1510. :meth:`_query.Query.group_by`.
  1511. HAVING criterion makes it possible to use filters on aggregate
  1512. functions like COUNT, SUM, AVG, MAX, and MIN, eg.::
  1513. q = session.query(User.id).\
  1514. join(User.addresses).\
  1515. group_by(User.id).\
  1516. having(func.count(Address.id) > 2)
  1517. """
  1518. self._having_criteria += (
  1519. coercions.expect(
  1520. roles.WhereHavingRole, criterion, apply_propagate_attrs=self
  1521. ),
  1522. )
  1523. def _set_op(self, expr_fn, *q):
  1524. return self._from_selectable(expr_fn(*([self] + list(q))).subquery())
  1525. def union(self, *q):
  1526. """Produce a UNION of this Query against one or more queries.
  1527. e.g.::
  1528. q1 = sess.query(SomeClass).filter(SomeClass.foo=='bar')
  1529. q2 = sess.query(SomeClass).filter(SomeClass.bar=='foo')
  1530. q3 = q1.union(q2)
  1531. The method accepts multiple Query objects so as to control
  1532. the level of nesting. A series of ``union()`` calls such as::
  1533. x.union(y).union(z).all()
  1534. will nest on each ``union()``, and produces::
  1535. SELECT * FROM (SELECT * FROM (SELECT * FROM X UNION
  1536. SELECT * FROM y) UNION SELECT * FROM Z)
  1537. Whereas::
  1538. x.union(y, z).all()
  1539. produces::
  1540. SELECT * FROM (SELECT * FROM X UNION SELECT * FROM y UNION
  1541. SELECT * FROM Z)
  1542. Note that many database backends do not allow ORDER BY to
  1543. be rendered on a query called within UNION, EXCEPT, etc.
  1544. To disable all ORDER BY clauses including those configured
  1545. on mappers, issue ``query.order_by(None)`` - the resulting
  1546. :class:`_query.Query` object will not render ORDER BY within
  1547. its SELECT statement.
  1548. """
  1549. return self._set_op(expression.union, *q)
  1550. def union_all(self, *q):
  1551. """Produce a UNION ALL of this Query against one or more queries.
  1552. Works the same way as :meth:`~sqlalchemy.orm.query.Query.union`. See
  1553. that method for usage examples.
  1554. """
  1555. return self._set_op(expression.union_all, *q)
  1556. def intersect(self, *q):
  1557. """Produce an INTERSECT of this Query against one or more queries.
  1558. Works the same way as :meth:`~sqlalchemy.orm.query.Query.union`. See
  1559. that method for usage examples.
  1560. """
  1561. return self._set_op(expression.intersect, *q)
  1562. def intersect_all(self, *q):
  1563. """Produce an INTERSECT ALL of this Query against one or more queries.
  1564. Works the same way as :meth:`~sqlalchemy.orm.query.Query.union`. See
  1565. that method for usage examples.
  1566. """
  1567. return self._set_op(expression.intersect_all, *q)
  1568. def except_(self, *q):
  1569. """Produce an EXCEPT of this Query against one or more queries.
  1570. Works the same way as :meth:`~sqlalchemy.orm.query.Query.union`. See
  1571. that method for usage examples.
  1572. """
  1573. return self._set_op(expression.except_, *q)
  1574. def except_all(self, *q):
  1575. """Produce an EXCEPT ALL of this Query against one or more queries.
  1576. Works the same way as :meth:`~sqlalchemy.orm.query.Query.union`. See
  1577. that method for usage examples.
  1578. """
  1579. return self._set_op(expression.except_all, *q)
  1580. def _next_aliased_generation(self):
  1581. if "_aliased_generation_counter" not in self.__dict__:
  1582. self._aliased_generation_counter = 0
  1583. self._aliased_generation_counter += 1
  1584. return self._aliased_generation_counter
  1585. @_generative
  1586. @_assertions(_no_statement_condition, _no_limit_offset)
  1587. def join(self, target, *props, **kwargs):
  1588. r"""Create a SQL JOIN against this :class:`_query.Query`
  1589. object's criterion
  1590. and apply generatively, returning the newly resulting
  1591. :class:`_query.Query`.
  1592. **Simple Relationship Joins**
  1593. Consider a mapping between two classes ``User`` and ``Address``,
  1594. with a relationship ``User.addresses`` representing a collection
  1595. of ``Address`` objects associated with each ``User``. The most
  1596. common usage of :meth:`_query.Query.join`
  1597. is to create a JOIN along this
  1598. relationship, using the ``User.addresses`` attribute as an indicator
  1599. for how this should occur::
  1600. q = session.query(User).join(User.addresses)
  1601. Where above, the call to :meth:`_query.Query.join` along
  1602. ``User.addresses`` will result in SQL approximately equivalent to::
  1603. SELECT user.id, user.name
  1604. FROM user JOIN address ON user.id = address.user_id
  1605. In the above example we refer to ``User.addresses`` as passed to
  1606. :meth:`_query.Query.join` as the "on clause", that is, it indicates
  1607. how the "ON" portion of the JOIN should be constructed.
  1608. To construct a chain of joins, multiple :meth:`_query.Query.join`
  1609. calls may be used. The relationship-bound attribute implies both
  1610. the left and right side of the join at once::
  1611. q = session.query(User).\
  1612. join(User.orders).\
  1613. join(Order.items).\
  1614. join(Item.keywords)
  1615. .. note:: as seen in the above example, **the order in which each
  1616. call to the join() method occurs is important**. Query would not,
  1617. for example, know how to join correctly if we were to specify
  1618. ``User``, then ``Item``, then ``Order``, in our chain of joins; in
  1619. such a case, depending on the arguments passed, it may raise an
  1620. error that it doesn't know how to join, or it may produce invalid
  1621. SQL in which case the database will raise an error. In correct
  1622. practice, the
  1623. :meth:`_query.Query.join` method is invoked in such a way that lines
  1624. up with how we would want the JOIN clauses in SQL to be
  1625. rendered, and each call should represent a clear link from what
  1626. precedes it.
  1627. **Joins to a Target Entity or Selectable**
  1628. A second form of :meth:`_query.Query.join` allows any mapped entity or
  1629. core selectable construct as a target. In this usage,
  1630. :meth:`_query.Query.join` will attempt to create a JOIN along the
  1631. natural foreign key relationship between two entities::
  1632. q = session.query(User).join(Address)
  1633. In the above calling form, :meth:`_query.Query.join` is called upon to
  1634. create the "on clause" automatically for us. This calling form will
  1635. ultimately raise an error if either there are no foreign keys between
  1636. the two entities, or if there are multiple foreign key linkages between
  1637. the target entity and the entity or entities already present on the
  1638. left side such that creating a join requires more information. Note
  1639. that when indicating a join to a target without any ON clause, ORM
  1640. configured relationships are not taken into account.
  1641. **Joins to a Target with an ON Clause**
  1642. The third calling form allows both the target entity as well
  1643. as the ON clause to be passed explicitly. A example that includes
  1644. a SQL expression as the ON clause is as follows::
  1645. q = session.query(User).join(Address, User.id==Address.user_id)
  1646. The above form may also use a relationship-bound attribute as the
  1647. ON clause as well::
  1648. q = session.query(User).join(Address, User.addresses)
  1649. The above syntax can be useful for the case where we wish
  1650. to join to an alias of a particular target entity. If we wanted
  1651. to join to ``Address`` twice, it could be achieved using two
  1652. aliases set up using the :func:`~sqlalchemy.orm.aliased` function::
  1653. a1 = aliased(Address)
  1654. a2 = aliased(Address)
  1655. q = session.query(User).\
  1656. join(a1, User.addresses).\
  1657. join(a2, User.addresses).\
  1658. filter(a1.email_address=='ed@foo.com').\
  1659. filter(a2.email_address=='ed@bar.com')
  1660. The relationship-bound calling form can also specify a target entity
  1661. using the :meth:`_orm.PropComparator.of_type` method; a query
  1662. equivalent to the one above would be::
  1663. a1 = aliased(Address)
  1664. a2 = aliased(Address)
  1665. q = session.query(User).\
  1666. join(User.addresses.of_type(a1)).\
  1667. join(User.addresses.of_type(a2)).\
  1668. filter(a1.email_address == 'ed@foo.com').\
  1669. filter(a2.email_address == 'ed@bar.com')
  1670. **Augmenting Built-in ON Clauses**
  1671. As a substitute for providing a full custom ON condition for an
  1672. existing relationship, the :meth:`_orm.PropComparator.and_` function
  1673. may be applied to a relationship attribute to augment additional
  1674. criteria into the ON clause; the additional criteria will be combined
  1675. with the default criteria using AND::
  1676. q = session.query(User).join(
  1677. User.addresses.and_(Address.email_address != 'foo@bar.com')
  1678. )
  1679. .. versionadded:: 1.4
  1680. **Joining to Tables and Subqueries**
  1681. The target of a join may also be any table or SELECT statement,
  1682. which may be related to a target entity or not. Use the
  1683. appropriate ``.subquery()`` method in order to make a subquery
  1684. out of a query::
  1685. subq = session.query(Address).\
  1686. filter(Address.email_address == 'ed@foo.com').\
  1687. subquery()
  1688. q = session.query(User).join(
  1689. subq, User.id == subq.c.user_id
  1690. )
  1691. Joining to a subquery in terms of a specific relationship and/or
  1692. target entity may be achieved by linking the subquery to the
  1693. entity using :func:`_orm.aliased`::
  1694. subq = session.query(Address).\
  1695. filter(Address.email_address == 'ed@foo.com').\
  1696. subquery()
  1697. address_subq = aliased(Address, subq)
  1698. q = session.query(User).join(
  1699. User.addresses.of_type(address_subq)
  1700. )
  1701. **Controlling what to Join From**
  1702. In cases where the left side of the current state of
  1703. :class:`_query.Query` is not in line with what we want to join from,
  1704. the :meth:`_query.Query.select_from` method may be used::
  1705. q = session.query(Address).select_from(User).\
  1706. join(User.addresses).\
  1707. filter(User.name == 'ed')
  1708. Which will produce SQL similar to::
  1709. SELECT address.* FROM user
  1710. JOIN address ON user.id=address.user_id
  1711. WHERE user.name = :name_1
  1712. **Legacy Features of Query.join()**
  1713. .. deprecated:: 1.4 The following features are deprecated and will
  1714. be removed in SQLAlchemy 2.0.
  1715. The :meth:`_query.Query.join` method currently supports several
  1716. usage patterns and arguments that are considered to be legacy
  1717. as of SQLAlchemy 1.3. A deprecation path will follow
  1718. in the 1.4 series for the following features:
  1719. * Joining on relationship names rather than attributes::
  1720. session.query(User).join("addresses")
  1721. **Why it's legacy**: the string name does not provide enough context
  1722. for :meth:`_query.Query.join` to always know what is desired,
  1723. notably in that there is no indication of what the left side
  1724. of the join should be. This gives rise to flags like
  1725. ``from_joinpoint`` as well as the ability to place several
  1726. join clauses in a single :meth:`_query.Query.join` call
  1727. which don't solve the problem fully while also
  1728. adding new calling styles that are unnecessary and expensive to
  1729. accommodate internally.
  1730. **Modern calling pattern**: Use the actual relationship,
  1731. e.g. ``User.addresses`` in the above case::
  1732. session.query(User).join(User.addresses)
  1733. * Automatic aliasing with the ``aliased=True`` flag::
  1734. session.query(Node).join(Node.children, aliased=True).\
  1735. filter(Node.name == 'some name')
  1736. **Why it's legacy**: the automatic aliasing feature of
  1737. :class:`_query.Query` is intensely complicated, both in its internal
  1738. implementation as well as in its observed behavior, and is almost
  1739. never used. It is difficult to know upon inspection where and when
  1740. its aliasing of a target entity, ``Node`` in the above case, will be
  1741. applied and when it won't, and additionally the feature has to use
  1742. very elaborate heuristics to achieve this implicit behavior.
  1743. **Modern calling pattern**: Use the :func:`_orm.aliased` construct
  1744. explicitly::
  1745. from sqlalchemy.orm import aliased
  1746. n1 = aliased(Node)
  1747. session.query(Node).join(Node.children.of_type(n1)).\
  1748. filter(n1.name == 'some name')
  1749. * Multiple joins in one call::
  1750. session.query(User).join("orders", "items")
  1751. session.query(User).join(User.orders, Order.items)
  1752. session.query(User).join(
  1753. (Order, User.orders),
  1754. (Item, Item.order_id == Order.id)
  1755. )
  1756. session.query(User).join(Order, Item)
  1757. # ... and several more forms actually
  1758. **Why it's legacy**: being able to chain multiple ON clauses in one
  1759. call to :meth:`_query.Query.join` is yet another attempt to solve
  1760. the problem of being able to specify what entity to join from,
  1761. and is the source of a large variety of potential calling patterns
  1762. that are internally expensive and complicated to parse and
  1763. accommodate.
  1764. **Modern calling pattern**: Use relationship-bound attributes
  1765. or SQL-oriented ON clauses within separate calls, so that
  1766. each call to :meth:`_query.Query.join` knows what the left
  1767. side should be::
  1768. session.query(User).join(User.orders).join(
  1769. Item, Item.order_id == Order.id)
  1770. :param \*props: Incoming arguments for :meth:`_query.Query.join`,
  1771. the props collection in modern use should be considered to be a one
  1772. or two argument form, either as a single "target" entity or ORM
  1773. attribute-bound relationship, or as a target entity plus an "on
  1774. clause" which may be a SQL expression or ORM attribute-bound
  1775. relationship.
  1776. :param isouter=False: If True, the join used will be a left outer join,
  1777. just as if the :meth:`_query.Query.outerjoin` method were called.
  1778. :param full=False: render FULL OUTER JOIN; implies ``isouter``.
  1779. .. versionadded:: 1.1
  1780. :param from_joinpoint=False: When using ``aliased=True``, a setting
  1781. of True here will cause the join to be from the most recent
  1782. joined target, rather than starting back from the original
  1783. FROM clauses of the query.
  1784. .. note:: This flag is considered legacy.
  1785. :param aliased=False: If True, indicate that the JOIN target should be
  1786. anonymously aliased. Subsequent calls to :meth:`_query.Query.filter`
  1787. and similar will adapt the incoming criterion to the target
  1788. alias, until :meth:`_query.Query.reset_joinpoint` is called.
  1789. .. note:: This flag is considered legacy.
  1790. .. seealso::
  1791. :ref:`ormtutorial_joins` in the ORM tutorial.
  1792. :ref:`inheritance_toplevel` for details on how
  1793. :meth:`_query.Query.join` is used for inheritance relationships.
  1794. :func:`_orm.join` - a standalone ORM-level join function,
  1795. used internally by :meth:`_query.Query.join`, which in previous
  1796. SQLAlchemy versions was the primary ORM-level joining interface.
  1797. """
  1798. aliased, from_joinpoint, isouter, full = (
  1799. kwargs.pop("aliased", False),
  1800. kwargs.pop("from_joinpoint", False),
  1801. kwargs.pop("isouter", False),
  1802. kwargs.pop("full", False),
  1803. )
  1804. if aliased or from_joinpoint:
  1805. util.warn_deprecated_20(
  1806. "The ``aliased`` and ``from_joinpoint`` keyword arguments "
  1807. "to Query.join() are deprecated and will be removed "
  1808. "in SQLAlchemy 2.0."
  1809. )
  1810. if kwargs:
  1811. raise TypeError(
  1812. "unknown arguments: %s" % ", ".join(sorted(kwargs))
  1813. )
  1814. # legacy vvvvvvvvvvvvvvvvvvvvvvvvvvv
  1815. if not from_joinpoint:
  1816. self._last_joined_entity = None
  1817. self._aliased_generation = None
  1818. # legacy ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  1819. if props:
  1820. onclause, legacy = props[0], props[1:]
  1821. else:
  1822. onclause = legacy = None
  1823. if not legacy and onclause is None and not isinstance(target, tuple):
  1824. # non legacy argument form
  1825. _props = [(target,)]
  1826. elif (
  1827. not legacy
  1828. and isinstance(
  1829. target,
  1830. (
  1831. expression.Selectable,
  1832. type,
  1833. AliasedClass,
  1834. types.FunctionType,
  1835. ),
  1836. )
  1837. and isinstance(
  1838. onclause,
  1839. (
  1840. elements.ColumnElement,
  1841. str,
  1842. interfaces.PropComparator,
  1843. types.FunctionType,
  1844. ),
  1845. )
  1846. ):
  1847. # non legacy argument form
  1848. _props = [(target, onclause)]
  1849. else:
  1850. # legacy forms. more time consuming :)
  1851. _props = []
  1852. _single = []
  1853. for prop in (target,) + props:
  1854. if isinstance(prop, tuple):
  1855. util.warn_deprecated_20(
  1856. "Query.join() will no longer accept tuples as "
  1857. "arguments in SQLAlchemy 2.0."
  1858. )
  1859. if _single:
  1860. _props.extend((_s,) for _s in _single)
  1861. _single = []
  1862. # this checks for an extremely ancient calling form of
  1863. # reversed tuples.
  1864. if isinstance(prop[0], (str, interfaces.PropComparator)):
  1865. prop = (prop[1], prop[0])
  1866. _props.append(prop)
  1867. else:
  1868. _single.append(prop)
  1869. if _single:
  1870. _props.extend((_s,) for _s in _single)
  1871. # legacy vvvvvvvvvvvvvvvvvvvvvvvvvvv
  1872. if aliased:
  1873. self._aliased_generation = self._next_aliased_generation()
  1874. if self._aliased_generation:
  1875. _props = [
  1876. (
  1877. prop[0],
  1878. sql_util._deep_annotate(
  1879. prop[1],
  1880. {"aliased_generation": self._aliased_generation},
  1881. )
  1882. if isinstance(prop[1], expression.ClauseElement)
  1883. else prop[1],
  1884. )
  1885. if len(prop) == 2
  1886. else prop
  1887. for prop in _props
  1888. ]
  1889. # legacy ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  1890. joins_to_add = tuple(
  1891. (
  1892. coercions.expect(
  1893. roles.JoinTargetRole,
  1894. prop[0],
  1895. legacy=True,
  1896. apply_propagate_attrs=self,
  1897. ),
  1898. (
  1899. coercions.expect(roles.OnClauseRole, prop[1], legacy=True)
  1900. # if not isinstance(prop[1], str)
  1901. # else prop[1]
  1902. )
  1903. if len(prop) == 2
  1904. else None,
  1905. None,
  1906. {
  1907. "isouter": isouter,
  1908. "aliased": aliased,
  1909. "from_joinpoint": True if i > 0 else from_joinpoint,
  1910. "full": full,
  1911. "aliased_generation": self._aliased_generation,
  1912. },
  1913. )
  1914. for i, prop in enumerate(_props)
  1915. )
  1916. if len(joins_to_add) > 1:
  1917. util.warn_deprecated_20(
  1918. "Passing a chain of multiple join conditions to Query.join() "
  1919. "is deprecated and will be removed in SQLAlchemy 2.0. "
  1920. "Please use individual join() calls per relationship."
  1921. )
  1922. self._legacy_setup_joins += joins_to_add
  1923. self.__dict__.pop("_last_joined_entity", None)
  1924. def outerjoin(self, target, *props, **kwargs):
  1925. """Create a left outer join against this ``Query`` object's criterion
  1926. and apply generatively, returning the newly resulting ``Query``.
  1927. Usage is the same as the ``join()`` method.
  1928. """
  1929. kwargs["isouter"] = True
  1930. return self.join(target, *props, **kwargs)
  1931. @_generative
  1932. @_assertions(_no_statement_condition)
  1933. def reset_joinpoint(self):
  1934. """Return a new :class:`.Query`, where the "join point" has
  1935. been reset back to the base FROM entities of the query.
  1936. This method is usually used in conjunction with the
  1937. ``aliased=True`` feature of the :meth:`~.Query.join`
  1938. method. See the example in :meth:`~.Query.join` for how
  1939. this is used.
  1940. """
  1941. self._last_joined_entity = None
  1942. self._aliased_generation = None
  1943. @_generative
  1944. @_assertions(_no_clauseelement_condition)
  1945. def select_from(self, *from_obj):
  1946. r"""Set the FROM clause of this :class:`.Query` explicitly.
  1947. :meth:`.Query.select_from` is often used in conjunction with
  1948. :meth:`.Query.join` in order to control which entity is selected
  1949. from on the "left" side of the join.
  1950. The entity or selectable object here effectively replaces the
  1951. "left edge" of any calls to :meth:`~.Query.join`, when no
  1952. joinpoint is otherwise established - usually, the default "join
  1953. point" is the leftmost entity in the :class:`~.Query` object's
  1954. list of entities to be selected.
  1955. A typical example::
  1956. q = session.query(Address).select_from(User).\
  1957. join(User.addresses).\
  1958. filter(User.name == 'ed')
  1959. Which produces SQL equivalent to::
  1960. SELECT address.* FROM user
  1961. JOIN address ON user.id=address.user_id
  1962. WHERE user.name = :name_1
  1963. :param \*from_obj: collection of one or more entities to apply
  1964. to the FROM clause. Entities can be mapped classes,
  1965. :class:`.AliasedClass` objects, :class:`.Mapper` objects
  1966. as well as core :class:`.FromClause` elements like subqueries.
  1967. .. versionchanged:: 0.9
  1968. This method no longer applies the given FROM object
  1969. to be the selectable from which matching entities
  1970. select from; the :meth:`.select_entity_from` method
  1971. now accomplishes this. See that method for a description
  1972. of this behavior.
  1973. .. seealso::
  1974. :meth:`~.Query.join`
  1975. :meth:`.Query.select_entity_from`
  1976. """
  1977. self._set_select_from(from_obj, False)
  1978. @util.deprecated_20(
  1979. ":meth:`_orm.Query.select_entity_from`",
  1980. alternative="Use the :func:`_orm.aliased` construct instead",
  1981. )
  1982. @_generative
  1983. @_assertions(_no_clauseelement_condition)
  1984. def select_entity_from(self, from_obj):
  1985. r"""Set the FROM clause of this :class:`_query.Query` to a
  1986. core selectable, applying it as a replacement FROM clause
  1987. for corresponding mapped entities.
  1988. The :meth:`_query.Query.select_entity_from`
  1989. method supplies an alternative
  1990. approach to the use case of applying an :func:`.aliased` construct
  1991. explicitly throughout a query. Instead of referring to the
  1992. :func:`.aliased` construct explicitly,
  1993. :meth:`_query.Query.select_entity_from` automatically *adapts* all
  1994. occurrences of the entity to the target selectable.
  1995. Given a case for :func:`.aliased` such as selecting ``User``
  1996. objects from a SELECT statement::
  1997. select_stmt = select(User).where(User.id == 7)
  1998. user_alias = aliased(User, select_stmt)
  1999. q = session.query(user_alias).\
  2000. filter(user_alias.name == 'ed')
  2001. Above, we apply the ``user_alias`` object explicitly throughout the
  2002. query. When it's not feasible for ``user_alias`` to be referenced
  2003. explicitly in many places, :meth:`_query.Query.select_entity_from`
  2004. may be
  2005. used at the start of the query to adapt the existing ``User`` entity::
  2006. q = session.query(User).\
  2007. select_entity_from(select_stmt.subquery()).\
  2008. filter(User.name == 'ed')
  2009. Above, the generated SQL will show that the ``User`` entity is
  2010. adapted to our statement, even in the case of the WHERE clause:
  2011. .. sourcecode:: sql
  2012. SELECT anon_1.id AS anon_1_id, anon_1.name AS anon_1_name
  2013. FROM (SELECT "user".id AS id, "user".name AS name
  2014. FROM "user"
  2015. WHERE "user".id = :id_1) AS anon_1
  2016. WHERE anon_1.name = :name_1
  2017. The :meth:`_query.Query.select_entity_from` method is similar to the
  2018. :meth:`_query.Query.select_from` method,
  2019. in that it sets the FROM clause
  2020. of the query. The difference is that it additionally applies
  2021. adaptation to the other parts of the query that refer to the
  2022. primary entity. If above we had used :meth:`_query.Query.select_from`
  2023. instead, the SQL generated would have been:
  2024. .. sourcecode:: sql
  2025. -- uses plain select_from(), not select_entity_from()
  2026. SELECT "user".id AS user_id, "user".name AS user_name
  2027. FROM "user", (SELECT "user".id AS id, "user".name AS name
  2028. FROM "user"
  2029. WHERE "user".id = :id_1) AS anon_1
  2030. WHERE "user".name = :name_1
  2031. To supply textual SQL to the :meth:`_query.Query.select_entity_from`
  2032. method,
  2033. we can make use of the :func:`_expression.text` construct. However,
  2034. the
  2035. :func:`_expression.text`
  2036. construct needs to be aligned with the columns of our
  2037. entity, which is achieved by making use of the
  2038. :meth:`_expression.TextClause.columns` method::
  2039. text_stmt = text("select id, name from user").columns(
  2040. User.id, User.name).subquery()
  2041. q = session.query(User).select_entity_from(text_stmt)
  2042. :meth:`_query.Query.select_entity_from` itself accepts an
  2043. :func:`.aliased`
  2044. object, so that the special options of :func:`.aliased` such as
  2045. :paramref:`.aliased.adapt_on_names` may be used within the
  2046. scope of the :meth:`_query.Query.select_entity_from`
  2047. method's adaptation
  2048. services. Suppose
  2049. a view ``user_view`` also returns rows from ``user``. If
  2050. we reflect this view into a :class:`_schema.Table`, this view has no
  2051. relationship to the :class:`_schema.Table` to which we are mapped,
  2052. however
  2053. we can use name matching to select from it::
  2054. user_view = Table('user_view', metadata,
  2055. autoload_with=engine)
  2056. user_view_alias = aliased(
  2057. User, user_view, adapt_on_names=True)
  2058. q = session.query(User).\
  2059. select_entity_from(user_view_alias).\
  2060. order_by(User.name)
  2061. .. versionchanged:: 1.1.7 The :meth:`_query.Query.select_entity_from`
  2062. method now accepts an :func:`.aliased` object as an alternative
  2063. to a :class:`_expression.FromClause` object.
  2064. :param from_obj: a :class:`_expression.FromClause`
  2065. object that will replace
  2066. the FROM clause of this :class:`_query.Query`.
  2067. It also may be an instance
  2068. of :func:`.aliased`.
  2069. .. seealso::
  2070. :meth:`_query.Query.select_from`
  2071. """
  2072. self._set_select_from([from_obj], True)
  2073. self._compile_options += {"_enable_single_crit": False}
  2074. def __getitem__(self, item):
  2075. return orm_util._getitem(
  2076. self,
  2077. item,
  2078. allow_negative=not self.session or not self.session.future,
  2079. )
  2080. @_generative
  2081. @_assertions(_no_statement_condition)
  2082. def slice(self, start, stop):
  2083. """Computes the "slice" of the :class:`_query.Query` represented by
  2084. the given indices and returns the resulting :class:`_query.Query`.
  2085. The start and stop indices behave like the argument to Python's
  2086. built-in :func:`range` function. This method provides an
  2087. alternative to using ``LIMIT``/``OFFSET`` to get a slice of the
  2088. query.
  2089. For example, ::
  2090. session.query(User).order_by(User.id).slice(1, 3)
  2091. renders as
  2092. .. sourcecode:: sql
  2093. SELECT users.id AS users_id,
  2094. users.name AS users_name
  2095. FROM users ORDER BY users.id
  2096. LIMIT ? OFFSET ?
  2097. (2, 1)
  2098. .. seealso::
  2099. :meth:`_query.Query.limit`
  2100. :meth:`_query.Query.offset`
  2101. """
  2102. self._limit_clause, self._offset_clause = sql_util._make_slice(
  2103. self._limit_clause, self._offset_clause, start, stop
  2104. )
  2105. @_generative
  2106. @_assertions(_no_statement_condition)
  2107. def limit(self, limit):
  2108. """Apply a ``LIMIT`` to the query and return the newly resulting
  2109. ``Query``.
  2110. """
  2111. self._limit_clause = sql_util._offset_or_limit_clause(limit)
  2112. @_generative
  2113. @_assertions(_no_statement_condition)
  2114. def offset(self, offset):
  2115. """Apply an ``OFFSET`` to the query and return the newly resulting
  2116. ``Query``.
  2117. """
  2118. self._offset_clause = sql_util._offset_or_limit_clause(offset)
  2119. @_generative
  2120. @_assertions(_no_statement_condition)
  2121. def distinct(self, *expr):
  2122. r"""Apply a ``DISTINCT`` to the query and return the newly resulting
  2123. ``Query``.
  2124. .. note::
  2125. The ORM-level :meth:`.distinct` call includes logic that will
  2126. automatically add columns from the ORDER BY of the query to the
  2127. columns clause of the SELECT statement, to satisfy the common need
  2128. of the database backend that ORDER BY columns be part of the SELECT
  2129. list when DISTINCT is used. These columns *are not* added to the
  2130. list of columns actually fetched by the :class:`_query.Query`,
  2131. however,
  2132. so would not affect results. The columns are passed through when
  2133. using the :attr:`_query.Query.statement` accessor, however.
  2134. .. deprecated:: 2.0 This logic is deprecated and will be removed
  2135. in SQLAlchemy 2.0. See :ref:`migration_20_query_distinct`
  2136. for a description of this use case in 2.0.
  2137. :param \*expr: optional column expressions. When present,
  2138. the PostgreSQL dialect will render a ``DISTINCT ON (<expressions>)``
  2139. construct.
  2140. .. deprecated:: 1.4 Using \*expr in other dialects is deprecated
  2141. and will raise :class:`_exc.CompileError` in a future version.
  2142. """
  2143. if expr:
  2144. self._distinct = True
  2145. self._distinct_on = self._distinct_on + tuple(
  2146. coercions.expect(roles.ByOfRole, e) for e in expr
  2147. )
  2148. else:
  2149. self._distinct = True
  2150. def all(self):
  2151. """Return the results represented by this :class:`_query.Query`
  2152. as a list.
  2153. This results in an execution of the underlying SQL statement.
  2154. .. warning:: The :class:`_query.Query` object,
  2155. when asked to return either
  2156. a sequence or iterator that consists of full ORM-mapped entities,
  2157. will **deduplicate entries based on primary key**. See the FAQ for
  2158. more details.
  2159. .. seealso::
  2160. :ref:`faq_query_deduplicating`
  2161. """
  2162. return self._iter().all()
  2163. @_generative
  2164. @_assertions(_no_clauseelement_condition)
  2165. def from_statement(self, statement):
  2166. """Execute the given SELECT statement and return results.
  2167. This method bypasses all internal statement compilation, and the
  2168. statement is executed without modification.
  2169. The statement is typically either a :func:`_expression.text`
  2170. or :func:`_expression.select` construct, and should return the set
  2171. of columns
  2172. appropriate to the entity class represented by this
  2173. :class:`_query.Query`.
  2174. .. seealso::
  2175. :ref:`orm_tutorial_literal_sql` - usage examples in the
  2176. ORM tutorial
  2177. """
  2178. statement = coercions.expect(
  2179. roles.SelectStatementRole, statement, apply_propagate_attrs=self
  2180. )
  2181. self._statement = statement
  2182. def first(self):
  2183. """Return the first result of this ``Query`` or
  2184. None if the result doesn't contain any row.
  2185. first() applies a limit of one within the generated SQL, so that
  2186. only one primary entity row is generated on the server side
  2187. (note this may consist of multiple result rows if join-loaded
  2188. collections are present).
  2189. Calling :meth:`_query.Query.first`
  2190. results in an execution of the underlying
  2191. query.
  2192. .. seealso::
  2193. :meth:`_query.Query.one`
  2194. :meth:`_query.Query.one_or_none`
  2195. """
  2196. # replicates limit(1) behavior
  2197. if self._statement is not None:
  2198. return self._iter().first()
  2199. else:
  2200. return self.limit(1)._iter().first()
  2201. def one_or_none(self):
  2202. """Return at most one result or raise an exception.
  2203. Returns ``None`` if the query selects
  2204. no rows. Raises ``sqlalchemy.orm.exc.MultipleResultsFound``
  2205. if multiple object identities are returned, or if multiple
  2206. rows are returned for a query that returns only scalar values
  2207. as opposed to full identity-mapped entities.
  2208. Calling :meth:`_query.Query.one_or_none`
  2209. results in an execution of the
  2210. underlying query.
  2211. .. versionadded:: 1.0.9
  2212. Added :meth:`_query.Query.one_or_none`
  2213. .. seealso::
  2214. :meth:`_query.Query.first`
  2215. :meth:`_query.Query.one`
  2216. """
  2217. return self._iter().one_or_none()
  2218. def one(self):
  2219. """Return exactly one result or raise an exception.
  2220. Raises ``sqlalchemy.orm.exc.NoResultFound`` if the query selects
  2221. no rows. Raises ``sqlalchemy.orm.exc.MultipleResultsFound``
  2222. if multiple object identities are returned, or if multiple
  2223. rows are returned for a query that returns only scalar values
  2224. as opposed to full identity-mapped entities.
  2225. Calling :meth:`.one` results in an execution of the underlying query.
  2226. .. seealso::
  2227. :meth:`_query.Query.first`
  2228. :meth:`_query.Query.one_or_none`
  2229. """
  2230. return self._iter().one()
  2231. def scalar(self):
  2232. """Return the first element of the first result or None
  2233. if no rows present. If multiple rows are returned,
  2234. raises MultipleResultsFound.
  2235. >>> session.query(Item).scalar()
  2236. <Item>
  2237. >>> session.query(Item.id).scalar()
  2238. 1
  2239. >>> session.query(Item.id).filter(Item.id < 0).scalar()
  2240. None
  2241. >>> session.query(Item.id, Item.name).scalar()
  2242. 1
  2243. >>> session.query(func.count(Parent.id)).scalar()
  2244. 20
  2245. This results in an execution of the underlying query.
  2246. """
  2247. # TODO: not sure why we can't use result.scalar() here
  2248. try:
  2249. ret = self.one()
  2250. if not isinstance(ret, collections_abc.Sequence):
  2251. return ret
  2252. return ret[0]
  2253. except orm_exc.NoResultFound:
  2254. return None
  2255. def __iter__(self):
  2256. return self._iter().__iter__()
  2257. def _iter(self):
  2258. # new style execution.
  2259. params = self._params
  2260. statement = self._statement_20()
  2261. result = self.session.execute(
  2262. statement,
  2263. params,
  2264. execution_options={"_sa_orm_load_options": self.load_options},
  2265. )
  2266. # legacy: automatically set scalars, unique
  2267. if result._attributes.get("is_single_entity", False):
  2268. result = result.scalars()
  2269. if (
  2270. result._attributes.get("filtered", False)
  2271. and not self.load_options._yield_per
  2272. ):
  2273. result = result.unique()
  2274. return result
  2275. def __str__(self):
  2276. statement = self._statement_20()
  2277. try:
  2278. bind = (
  2279. self._get_bind_args(statement, self.session.get_bind)
  2280. if self.session
  2281. else None
  2282. )
  2283. except sa_exc.UnboundExecutionError:
  2284. bind = None
  2285. return str(statement.compile(bind))
  2286. def _get_bind_args(self, statement, fn, **kw):
  2287. return fn(clause=statement, **kw)
  2288. @property
  2289. def column_descriptions(self):
  2290. """Return metadata about the columns which would be
  2291. returned by this :class:`_query.Query`.
  2292. Format is a list of dictionaries::
  2293. user_alias = aliased(User, name='user2')
  2294. q = sess.query(User, User.id, user_alias)
  2295. # this expression:
  2296. q.column_descriptions
  2297. # would return:
  2298. [
  2299. {
  2300. 'name':'User',
  2301. 'type':User,
  2302. 'aliased':False,
  2303. 'expr':User,
  2304. 'entity': User
  2305. },
  2306. {
  2307. 'name':'id',
  2308. 'type':Integer(),
  2309. 'aliased':False,
  2310. 'expr':User.id,
  2311. 'entity': User
  2312. },
  2313. {
  2314. 'name':'user2',
  2315. 'type':User,
  2316. 'aliased':True,
  2317. 'expr':user_alias,
  2318. 'entity': user_alias
  2319. }
  2320. ]
  2321. """
  2322. return _column_descriptions(self, legacy=True)
  2323. def instances(self, result_proxy, context=None):
  2324. """Return an ORM result given a :class:`_engine.CursorResult` and
  2325. :class:`.QueryContext`.
  2326. """
  2327. if context is None:
  2328. util.warn_deprecated(
  2329. "Using the Query.instances() method without a context "
  2330. "is deprecated and will be disallowed in a future release. "
  2331. "Please make use of :meth:`_query.Query.from_statement` "
  2332. "for linking ORM results to arbitrary select constructs.",
  2333. version="1.4",
  2334. )
  2335. compile_state = self._compile_state(for_statement=False)
  2336. context = QueryContext(
  2337. compile_state,
  2338. compile_state.statement,
  2339. self._params,
  2340. self.session,
  2341. self.load_options,
  2342. )
  2343. result = loading.instances(result_proxy, context)
  2344. # legacy: automatically set scalars, unique
  2345. if result._attributes.get("is_single_entity", False):
  2346. result = result.scalars()
  2347. if result._attributes.get("filtered", False):
  2348. result = result.unique()
  2349. return result
  2350. @util.deprecated_20(
  2351. ":meth:`_orm.Query.merge_result`",
  2352. alternative="The method is superseded by the "
  2353. ":func:`_orm.merge_frozen_result` function.",
  2354. becomes_legacy=True,
  2355. enable_warnings=False, # warnings occur via loading.merge_result
  2356. )
  2357. def merge_result(self, iterator, load=True):
  2358. """Merge a result into this :class:`_query.Query` object's Session.
  2359. Given an iterator returned by a :class:`_query.Query`
  2360. of the same structure
  2361. as this one, return an identical iterator of results, with all mapped
  2362. instances merged into the session using :meth:`.Session.merge`. This
  2363. is an optimized method which will merge all mapped instances,
  2364. preserving the structure of the result rows and unmapped columns with
  2365. less method overhead than that of calling :meth:`.Session.merge`
  2366. explicitly for each value.
  2367. The structure of the results is determined based on the column list of
  2368. this :class:`_query.Query` - if these do not correspond,
  2369. unchecked errors
  2370. will occur.
  2371. The 'load' argument is the same as that of :meth:`.Session.merge`.
  2372. For an example of how :meth:`_query.Query.merge_result` is used, see
  2373. the source code for the example :ref:`examples_caching`, where
  2374. :meth:`_query.Query.merge_result` is used to efficiently restore state
  2375. from a cache back into a target :class:`.Session`.
  2376. """
  2377. return loading.merge_result(self, iterator, load)
  2378. def exists(self):
  2379. """A convenience method that turns a query into an EXISTS subquery
  2380. of the form EXISTS (SELECT 1 FROM ... WHERE ...).
  2381. e.g.::
  2382. q = session.query(User).filter(User.name == 'fred')
  2383. session.query(q.exists())
  2384. Producing SQL similar to::
  2385. SELECT EXISTS (
  2386. SELECT 1 FROM users WHERE users.name = :name_1
  2387. ) AS anon_1
  2388. The EXISTS construct is usually used in the WHERE clause::
  2389. session.query(User.id).filter(q.exists()).scalar()
  2390. Note that some databases such as SQL Server don't allow an
  2391. EXISTS expression to be present in the columns clause of a
  2392. SELECT. To select a simple boolean value based on the exists
  2393. as a WHERE, use :func:`.literal`::
  2394. from sqlalchemy import literal
  2395. session.query(literal(True)).filter(q.exists()).scalar()
  2396. """
  2397. # .add_columns() for the case that we are a query().select_from(X),
  2398. # so that ".statement" can be produced (#2995) but also without
  2399. # omitting the FROM clause from a query(X) (#2818);
  2400. # .with_only_columns() after we have a core select() so that
  2401. # we get just "SELECT 1" without any entities.
  2402. inner = (
  2403. self.enable_eagerloads(False)
  2404. .add_columns(sql.literal_column("1"))
  2405. .set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL)
  2406. .statement.with_only_columns(1)
  2407. )
  2408. ezero = self._entity_from_pre_ent_zero()
  2409. if ezero is not None:
  2410. inner = inner.select_from(ezero)
  2411. return sql.exists(inner)
  2412. def count(self):
  2413. r"""Return a count of rows this the SQL formed by this :class:`Query`
  2414. would return.
  2415. This generates the SQL for this Query as follows::
  2416. SELECT count(1) AS count_1 FROM (
  2417. SELECT <rest of query follows...>
  2418. ) AS anon_1
  2419. The above SQL returns a single row, which is the aggregate value
  2420. of the count function; the :meth:`_query.Query.count`
  2421. method then returns
  2422. that single integer value.
  2423. .. warning::
  2424. It is important to note that the value returned by
  2425. count() is **not the same as the number of ORM objects that this
  2426. Query would return from a method such as the .all() method**.
  2427. The :class:`_query.Query` object,
  2428. when asked to return full entities,
  2429. will **deduplicate entries based on primary key**, meaning if the
  2430. same primary key value would appear in the results more than once,
  2431. only one object of that primary key would be present. This does
  2432. not apply to a query that is against individual columns.
  2433. .. seealso::
  2434. :ref:`faq_query_deduplicating`
  2435. :ref:`orm_tutorial_query_returning`
  2436. For fine grained control over specific columns to count, to skip the
  2437. usage of a subquery or otherwise control of the FROM clause, or to use
  2438. other aggregate functions, use :attr:`~sqlalchemy.sql.expression.func`
  2439. expressions in conjunction with :meth:`~.Session.query`, i.e.::
  2440. from sqlalchemy import func
  2441. # count User records, without
  2442. # using a subquery.
  2443. session.query(func.count(User.id))
  2444. # return count of user "id" grouped
  2445. # by "name"
  2446. session.query(func.count(User.id)).\
  2447. group_by(User.name)
  2448. from sqlalchemy import distinct
  2449. # count distinct "name" values
  2450. session.query(func.count(distinct(User.name)))
  2451. """
  2452. col = sql.func.count(sql.literal_column("*"))
  2453. return self._from_self(col).enable_eagerloads(False).scalar()
  2454. def delete(self, synchronize_session="evaluate"):
  2455. r"""Perform a DELETE with an arbitrary WHERE clause.
  2456. Deletes rows matched by this query from the database.
  2457. E.g.::
  2458. sess.query(User).filter(User.age == 25).\
  2459. delete(synchronize_session=False)
  2460. sess.query(User).filter(User.age == 25).\
  2461. delete(synchronize_session='evaluate')
  2462. .. warning::
  2463. See the section :ref:`orm_expression_update_delete` for important
  2464. caveats and warnings, including limitations when using bulk UPDATE
  2465. and DELETE with mapper inheritance configurations.
  2466. :param synchronize_session: chooses the strategy to update the
  2467. attributes on objects in the session. See the section
  2468. :ref:`orm_expression_update_delete` for a discussion of these
  2469. strategies.
  2470. :return: the count of rows matched as returned by the database's
  2471. "row count" feature.
  2472. .. seealso::
  2473. :ref:`orm_expression_update_delete`
  2474. """
  2475. bulk_del = BulkDelete(self)
  2476. if self.dispatch.before_compile_delete:
  2477. for fn in self.dispatch.before_compile_delete:
  2478. new_query = fn(bulk_del.query, bulk_del)
  2479. if new_query is not None:
  2480. bulk_del.query = new_query
  2481. self = bulk_del.query
  2482. delete_ = sql.delete(*self._raw_columns)
  2483. delete_._where_criteria = self._where_criteria
  2484. result = self.session.execute(
  2485. delete_,
  2486. self._params,
  2487. execution_options={"synchronize_session": synchronize_session},
  2488. )
  2489. bulk_del.result = result
  2490. self.session.dispatch.after_bulk_delete(bulk_del)
  2491. result.close()
  2492. return result.rowcount
  2493. def update(self, values, synchronize_session="evaluate", update_args=None):
  2494. r"""Perform an UPDATE with an arbitrary WHERE clause.
  2495. Updates rows matched by this query in the database.
  2496. E.g.::
  2497. sess.query(User).filter(User.age == 25).\
  2498. update({User.age: User.age - 10}, synchronize_session=False)
  2499. sess.query(User).filter(User.age == 25).\
  2500. update({"age": User.age - 10}, synchronize_session='evaluate')
  2501. .. warning::
  2502. See the section :ref:`orm_expression_update_delete` for important
  2503. caveats and warnings, including limitations when using arbitrary
  2504. UPDATE and DELETE with mapper inheritance configurations.
  2505. :param values: a dictionary with attributes names, or alternatively
  2506. mapped attributes or SQL expressions, as keys, and literal
  2507. values or sql expressions as values. If :ref:`parameter-ordered
  2508. mode <updates_order_parameters>` is desired, the values can be
  2509. passed as a list of 2-tuples;
  2510. this requires that the
  2511. :paramref:`~sqlalchemy.sql.expression.update.preserve_parameter_order`
  2512. flag is passed to the :paramref:`.Query.update.update_args` dictionary
  2513. as well.
  2514. :param synchronize_session: chooses the strategy to update the
  2515. attributes on objects in the session. See the section
  2516. :ref:`orm_expression_update_delete` for a discussion of these
  2517. strategies.
  2518. :param update_args: Optional dictionary, if present will be passed
  2519. to the underlying :func:`_expression.update`
  2520. construct as the ``**kw`` for
  2521. the object. May be used to pass dialect-specific arguments such
  2522. as ``mysql_limit``, as well as other special arguments such as
  2523. :paramref:`~sqlalchemy.sql.expression.update.preserve_parameter_order`.
  2524. :return: the count of rows matched as returned by the database's
  2525. "row count" feature.
  2526. .. seealso::
  2527. :ref:`orm_expression_update_delete`
  2528. """
  2529. update_args = update_args or {}
  2530. bulk_ud = BulkUpdate(self, values, update_args)
  2531. if self.dispatch.before_compile_update:
  2532. for fn in self.dispatch.before_compile_update:
  2533. new_query = fn(bulk_ud.query, bulk_ud)
  2534. if new_query is not None:
  2535. bulk_ud.query = new_query
  2536. self = bulk_ud.query
  2537. upd = sql.update(*self._raw_columns)
  2538. ppo = update_args.pop("preserve_parameter_order", False)
  2539. if ppo:
  2540. upd = upd.ordered_values(*values)
  2541. else:
  2542. upd = upd.values(values)
  2543. if update_args:
  2544. upd = upd.with_dialect_options(**update_args)
  2545. upd._where_criteria = self._where_criteria
  2546. result = self.session.execute(
  2547. upd,
  2548. self._params,
  2549. execution_options={"synchronize_session": synchronize_session},
  2550. )
  2551. bulk_ud.result = result
  2552. self.session.dispatch.after_bulk_update(bulk_ud)
  2553. result.close()
  2554. return result.rowcount
  2555. def _compile_state(self, for_statement=False, **kw):
  2556. """Create an out-of-compiler ORMCompileState object.
  2557. The ORMCompileState object is normally created directly as a result
  2558. of the SQLCompiler.process() method being handed a Select()
  2559. or FromStatement() object that uses the "orm" plugin. This method
  2560. provides a means of creating this ORMCompileState object directly
  2561. without using the compiler.
  2562. This method is used only for deprecated cases, which include
  2563. the .from_self() method for a Query that has multiple levels
  2564. of .from_self() in use, as well as the instances() method. It is
  2565. also used within the test suite to generate ORMCompileState objects
  2566. for test purposes.
  2567. """
  2568. stmt = self._statement_20(for_statement=for_statement, **kw)
  2569. assert for_statement == stmt._compile_options._for_statement
  2570. # this chooses between ORMFromStatementCompileState and
  2571. # ORMSelectCompileState. We could also base this on
  2572. # query._statement is not None as we have the ORM Query here
  2573. # however this is the more general path.
  2574. compile_state_cls = ORMCompileState._get_plugin_class_for_plugin(
  2575. stmt, "orm"
  2576. )
  2577. return compile_state_cls.create_for_statement(stmt, None)
  2578. def _compile_context(self, for_statement=False):
  2579. compile_state = self._compile_state(for_statement=for_statement)
  2580. context = QueryContext(
  2581. compile_state,
  2582. compile_state.statement,
  2583. self._params,
  2584. self.session,
  2585. self.load_options,
  2586. )
  2587. return context
  2588. class FromStatement(GroupedElement, SelectBase, Executable):
  2589. """Core construct that represents a load of ORM objects from a finished
  2590. select or text construct.
  2591. """
  2592. __visit_name__ = "orm_from_statement"
  2593. _compile_options = ORMFromStatementCompileState.default_compile_options
  2594. _compile_state_factory = ORMFromStatementCompileState.create_for_statement
  2595. _for_update_arg = None
  2596. _traverse_internals = [
  2597. ("_raw_columns", InternalTraversal.dp_clauseelement_list),
  2598. ("element", InternalTraversal.dp_clauseelement),
  2599. ] + Executable._executable_traverse_internals
  2600. _cache_key_traversal = _traverse_internals + [
  2601. ("_compile_options", InternalTraversal.dp_has_cache_key)
  2602. ]
  2603. def __init__(self, entities, element):
  2604. self._raw_columns = [
  2605. coercions.expect(
  2606. roles.ColumnsClauseRole,
  2607. ent,
  2608. apply_propagate_attrs=self,
  2609. post_inspect=True,
  2610. )
  2611. for ent in util.to_list(entities)
  2612. ]
  2613. self.element = element
  2614. def get_label_style(self):
  2615. return self._label_style
  2616. def set_label_style(self, label_style):
  2617. return SelectStatementGrouping(
  2618. self.element.set_label_style(label_style)
  2619. )
  2620. @property
  2621. def _label_style(self):
  2622. return self.element._label_style
  2623. def _compiler_dispatch(self, compiler, **kw):
  2624. """provide a fixed _compiler_dispatch method.
  2625. This is roughly similar to using the sqlalchemy.ext.compiler
  2626. ``@compiles`` extension.
  2627. """
  2628. compile_state = self._compile_state_factory(self, compiler, **kw)
  2629. toplevel = not compiler.stack
  2630. if toplevel:
  2631. compiler.compile_state = compile_state
  2632. return compiler.process(compile_state.statement, **kw)
  2633. def _ensure_disambiguated_names(self):
  2634. return self
  2635. def get_children(self, **kw):
  2636. for elem in itertools.chain.from_iterable(
  2637. element._from_objects for element in self._raw_columns
  2638. ):
  2639. yield elem
  2640. for elem in super(FromStatement, self).get_children(**kw):
  2641. yield elem
  2642. @property
  2643. def _returning(self):
  2644. return self.element._returning if self.element.is_dml else None
  2645. @property
  2646. def _inline(self):
  2647. return self.element._inline if self.element.is_dml else None
  2648. class AliasOption(interfaces.LoaderOption):
  2649. @util.deprecated(
  2650. "1.4",
  2651. "The :class:`.AliasOption` is not necessary "
  2652. "for entities to be matched up to a query that is established "
  2653. "via :meth:`.Query.from_statement` and now does nothing.",
  2654. )
  2655. def __init__(self, alias):
  2656. r"""Return a :class:`.MapperOption` that will indicate to the
  2657. :class:`_query.Query`
  2658. that the main table has been aliased.
  2659. """
  2660. inherit_cache = False
  2661. def process_compile_state(self, compile_state):
  2662. pass
  2663. class BulkUD(object):
  2664. """State used for the orm.Query version of update() / delete().
  2665. This object is now specific to Query only.
  2666. """
  2667. def __init__(self, query):
  2668. self.query = query.enable_eagerloads(False)
  2669. self._validate_query_state()
  2670. self.mapper = self.query._entity_from_pre_ent_zero()
  2671. def _validate_query_state(self):
  2672. for attr, methname, notset, op in (
  2673. ("_limit_clause", "limit()", None, operator.is_),
  2674. ("_offset_clause", "offset()", None, operator.is_),
  2675. ("_order_by_clauses", "order_by()", (), operator.eq),
  2676. ("_group_by_clauses", "group_by()", (), operator.eq),
  2677. ("_distinct", "distinct()", False, operator.is_),
  2678. (
  2679. "_from_obj",
  2680. "join(), outerjoin(), select_from(), or from_self()",
  2681. (),
  2682. operator.eq,
  2683. ),
  2684. (
  2685. "_legacy_setup_joins",
  2686. "join(), outerjoin(), select_from(), or from_self()",
  2687. (),
  2688. operator.eq,
  2689. ),
  2690. ):
  2691. if not op(getattr(self.query, attr), notset):
  2692. raise sa_exc.InvalidRequestError(
  2693. "Can't call Query.update() or Query.delete() "
  2694. "when %s has been called" % (methname,)
  2695. )
  2696. @property
  2697. def session(self):
  2698. return self.query.session
  2699. class BulkUpdate(BulkUD):
  2700. """BulkUD which handles UPDATEs."""
  2701. def __init__(self, query, values, update_kwargs):
  2702. super(BulkUpdate, self).__init__(query)
  2703. self.values = values
  2704. self.update_kwargs = update_kwargs
  2705. class BulkDelete(BulkUD):
  2706. """BulkUD which handles DELETEs."""