mapper.py 167 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416
  1. # orm/mapper.py
  2. # Copyright (C) 2005-2024 the SQLAlchemy authors and contributors
  3. # <see AUTHORS file>
  4. #
  5. # This module is part of SQLAlchemy and is released under
  6. # the MIT License: https://www.opensource.org/licenses/mit-license.php
  7. # mypy: allow-untyped-defs, allow-untyped-calls
  8. """Logic to map Python classes to and from selectables.
  9. Defines the :class:`~sqlalchemy.orm.mapper.Mapper` class, the central
  10. configurational unit which associates a class with a database table.
  11. This is a semi-private module; the main configurational API of the ORM is
  12. available in :class:`~sqlalchemy.orm.`.
  13. """
  14. from __future__ import annotations
  15. from collections import deque
  16. from functools import reduce
  17. from itertools import chain
  18. import sys
  19. import threading
  20. from typing import Any
  21. from typing import Callable
  22. from typing import cast
  23. from typing import Collection
  24. from typing import Deque
  25. from typing import Dict
  26. from typing import FrozenSet
  27. from typing import Generic
  28. from typing import Iterable
  29. from typing import Iterator
  30. from typing import List
  31. from typing import Mapping
  32. from typing import Optional
  33. from typing import Sequence
  34. from typing import Set
  35. from typing import Tuple
  36. from typing import Type
  37. from typing import TYPE_CHECKING
  38. from typing import TypeVar
  39. from typing import Union
  40. import weakref
  41. from . import attributes
  42. from . import exc as orm_exc
  43. from . import instrumentation
  44. from . import loading
  45. from . import properties
  46. from . import util as orm_util
  47. from ._typing import _O
  48. from .base import _class_to_mapper
  49. from .base import _parse_mapper_argument
  50. from .base import _state_mapper
  51. from .base import PassiveFlag
  52. from .base import state_str
  53. from .interfaces import _MappedAttribute
  54. from .interfaces import EXT_SKIP
  55. from .interfaces import InspectionAttr
  56. from .interfaces import MapperProperty
  57. from .interfaces import ORMEntityColumnsClauseRole
  58. from .interfaces import ORMFromClauseRole
  59. from .interfaces import StrategizedProperty
  60. from .path_registry import PathRegistry
  61. from .. import event
  62. from .. import exc as sa_exc
  63. from .. import inspection
  64. from .. import log
  65. from .. import schema
  66. from .. import sql
  67. from .. import util
  68. from ..event import dispatcher
  69. from ..event import EventTarget
  70. from ..sql import base as sql_base
  71. from ..sql import coercions
  72. from ..sql import expression
  73. from ..sql import operators
  74. from ..sql import roles
  75. from ..sql import TableClause
  76. from ..sql import util as sql_util
  77. from ..sql import visitors
  78. from ..sql.cache_key import MemoizedHasCacheKey
  79. from ..sql.elements import KeyedColumnElement
  80. from ..sql.schema import Column
  81. from ..sql.schema import Table
  82. from ..sql.selectable import LABEL_STYLE_TABLENAME_PLUS_COL
  83. from ..util import HasMemoized
  84. from ..util import HasMemoized_ro_memoized_attribute
  85. from ..util.typing import Literal
  86. if TYPE_CHECKING:
  87. from ._typing import _IdentityKeyType
  88. from ._typing import _InstanceDict
  89. from ._typing import _ORMColumnExprArgument
  90. from ._typing import _RegistryType
  91. from .decl_api import registry
  92. from .dependency import DependencyProcessor
  93. from .descriptor_props import CompositeProperty
  94. from .descriptor_props import SynonymProperty
  95. from .events import MapperEvents
  96. from .instrumentation import ClassManager
  97. from .path_registry import CachingEntityRegistry
  98. from .properties import ColumnProperty
  99. from .relationships import RelationshipProperty
  100. from .state import InstanceState
  101. from .util import ORMAdapter
  102. from ..engine import Row
  103. from ..engine import RowMapping
  104. from ..sql._typing import _ColumnExpressionArgument
  105. from ..sql._typing import _EquivalentColumnMap
  106. from ..sql.base import ReadOnlyColumnCollection
  107. from ..sql.elements import ColumnClause
  108. from ..sql.elements import ColumnElement
  109. from ..sql.selectable import FromClause
  110. from ..util import OrderedSet
  111. _T = TypeVar("_T", bound=Any)
  112. _MP = TypeVar("_MP", bound="MapperProperty[Any]")
  113. _Fn = TypeVar("_Fn", bound="Callable[..., Any]")
  114. _WithPolymorphicArg = Union[
  115. Literal["*"],
  116. Tuple[
  117. Union[Literal["*"], Sequence[Union["Mapper[Any]", Type[Any]]]],
  118. Optional["FromClause"],
  119. ],
  120. Sequence[Union["Mapper[Any]", Type[Any]]],
  121. ]
  122. _mapper_registries: weakref.WeakKeyDictionary[
  123. _RegistryType, bool
  124. ] = weakref.WeakKeyDictionary()
  125. def _all_registries() -> Set[registry]:
  126. with _CONFIGURE_MUTEX:
  127. return set(_mapper_registries)
  128. def _unconfigured_mappers() -> Iterator[Mapper[Any]]:
  129. for reg in _all_registries():
  130. yield from reg._mappers_to_configure()
  131. _already_compiling = False
  132. # a constant returned by _get_attr_by_column to indicate
  133. # this mapper is not handling an attribute for a particular
  134. # column
  135. NO_ATTRIBUTE = util.symbol("NO_ATTRIBUTE")
  136. # lock used to synchronize the "mapper configure" step
  137. _CONFIGURE_MUTEX = threading.RLock()
  138. @inspection._self_inspects
  139. @log.class_logger
  140. class Mapper(
  141. ORMFromClauseRole,
  142. ORMEntityColumnsClauseRole[_O],
  143. MemoizedHasCacheKey,
  144. InspectionAttr,
  145. log.Identified,
  146. inspection.Inspectable["Mapper[_O]"],
  147. EventTarget,
  148. Generic[_O],
  149. ):
  150. """Defines an association between a Python class and a database table or
  151. other relational structure, so that ORM operations against the class may
  152. proceed.
  153. The :class:`_orm.Mapper` object is instantiated using mapping methods
  154. present on the :class:`_orm.registry` object. For information
  155. about instantiating new :class:`_orm.Mapper` objects, see
  156. :ref:`orm_mapping_classes_toplevel`.
  157. """
  158. dispatch: dispatcher[Mapper[_O]]
  159. _dispose_called = False
  160. _configure_failed: Any = False
  161. _ready_for_configure = False
  162. @util.deprecated_params(
  163. non_primary=(
  164. "1.3",
  165. "The :paramref:`.mapper.non_primary` parameter is deprecated, "
  166. "and will be removed in a future release. The functionality "
  167. "of non primary mappers is now better suited using the "
  168. ":class:`.AliasedClass` construct, which can also be used "
  169. "as the target of a :func:`_orm.relationship` in 1.3.",
  170. ),
  171. )
  172. def __init__(
  173. self,
  174. class_: Type[_O],
  175. local_table: Optional[FromClause] = None,
  176. properties: Optional[Mapping[str, MapperProperty[Any]]] = None,
  177. primary_key: Optional[Iterable[_ORMColumnExprArgument[Any]]] = None,
  178. non_primary: bool = False,
  179. inherits: Optional[Union[Mapper[Any], Type[Any]]] = None,
  180. inherit_condition: Optional[_ColumnExpressionArgument[bool]] = None,
  181. inherit_foreign_keys: Optional[
  182. Sequence[_ORMColumnExprArgument[Any]]
  183. ] = None,
  184. always_refresh: bool = False,
  185. version_id_col: Optional[_ORMColumnExprArgument[Any]] = None,
  186. version_id_generator: Optional[
  187. Union[Literal[False], Callable[[Any], Any]]
  188. ] = None,
  189. polymorphic_on: Optional[
  190. Union[_ORMColumnExprArgument[Any], str, MapperProperty[Any]]
  191. ] = None,
  192. _polymorphic_map: Optional[Dict[Any, Mapper[Any]]] = None,
  193. polymorphic_identity: Optional[Any] = None,
  194. concrete: bool = False,
  195. with_polymorphic: Optional[_WithPolymorphicArg] = None,
  196. polymorphic_abstract: bool = False,
  197. polymorphic_load: Optional[Literal["selectin", "inline"]] = None,
  198. allow_partial_pks: bool = True,
  199. batch: bool = True,
  200. column_prefix: Optional[str] = None,
  201. include_properties: Optional[Sequence[str]] = None,
  202. exclude_properties: Optional[Sequence[str]] = None,
  203. passive_updates: bool = True,
  204. passive_deletes: bool = False,
  205. confirm_deleted_rows: bool = True,
  206. eager_defaults: Literal[True, False, "auto"] = "auto",
  207. legacy_is_orphan: bool = False,
  208. _compiled_cache_size: int = 100,
  209. ):
  210. r"""Direct constructor for a new :class:`_orm.Mapper` object.
  211. The :class:`_orm.Mapper` constructor is not called directly, and
  212. is normally invoked through the
  213. use of the :class:`_orm.registry` object through either the
  214. :ref:`Declarative <orm_declarative_mapping>` or
  215. :ref:`Imperative <orm_imperative_mapping>` mapping styles.
  216. .. versionchanged:: 2.0 The public facing ``mapper()`` function is
  217. removed; for a classical mapping configuration, use the
  218. :meth:`_orm.registry.map_imperatively` method.
  219. Parameters documented below may be passed to either the
  220. :meth:`_orm.registry.map_imperatively` method, or may be passed in the
  221. ``__mapper_args__`` declarative class attribute described at
  222. :ref:`orm_declarative_mapper_options`.
  223. :param class\_: The class to be mapped. When using Declarative,
  224. this argument is automatically passed as the declared class
  225. itself.
  226. :param local_table: The :class:`_schema.Table` or other
  227. :class:`_sql.FromClause` (i.e. selectable) to which the class is
  228. mapped. May be ``None`` if this mapper inherits from another mapper
  229. using single-table inheritance. When using Declarative, this
  230. argument is automatically passed by the extension, based on what is
  231. configured via the :attr:`_orm.DeclarativeBase.__table__` attribute
  232. or via the :class:`_schema.Table` produced as a result of
  233. the :attr:`_orm.DeclarativeBase.__tablename__` attribute being
  234. present.
  235. :param polymorphic_abstract: Indicates this class will be mapped in a
  236. polymorphic hierarchy, but not directly instantiated. The class is
  237. mapped normally, except that it has no requirement for a
  238. :paramref:`_orm.Mapper.polymorphic_identity` within an inheritance
  239. hierarchy. The class however must be part of a polymorphic
  240. inheritance scheme which uses
  241. :paramref:`_orm.Mapper.polymorphic_on` at the base.
  242. .. versionadded:: 2.0
  243. .. seealso::
  244. :ref:`orm_inheritance_abstract_poly`
  245. :param always_refresh: If True, all query operations for this mapped
  246. class will overwrite all data within object instances that already
  247. exist within the session, erasing any in-memory changes with
  248. whatever information was loaded from the database. Usage of this
  249. flag is highly discouraged; as an alternative, see the method
  250. :meth:`_query.Query.populate_existing`.
  251. :param allow_partial_pks: Defaults to True. Indicates that a
  252. composite primary key with some NULL values should be considered as
  253. possibly existing within the database. This affects whether a
  254. mapper will assign an incoming row to an existing identity, as well
  255. as if :meth:`.Session.merge` will check the database first for a
  256. particular primary key value. A "partial primary key" can occur if
  257. one has mapped to an OUTER JOIN, for example.
  258. :param batch: Defaults to ``True``, indicating that save operations
  259. of multiple entities can be batched together for efficiency.
  260. Setting to False indicates
  261. that an instance will be fully saved before saving the next
  262. instance. This is used in the extremely rare case that a
  263. :class:`.MapperEvents` listener requires being called
  264. in between individual row persistence operations.
  265. :param column_prefix: A string which will be prepended
  266. to the mapped attribute name when :class:`_schema.Column`
  267. objects are automatically assigned as attributes to the
  268. mapped class. Does not affect :class:`.Column` objects that
  269. are mapped explicitly in the :paramref:`.Mapper.properties`
  270. dictionary.
  271. This parameter is typically useful with imperative mappings
  272. that keep the :class:`.Table` object separate. Below, assuming
  273. the ``user_table`` :class:`.Table` object has columns named
  274. ``user_id``, ``user_name``, and ``password``::
  275. class User(Base):
  276. __table__ = user_table
  277. __mapper_args__ = {'column_prefix':'_'}
  278. The above mapping will assign the ``user_id``, ``user_name``, and
  279. ``password`` columns to attributes named ``_user_id``,
  280. ``_user_name``, and ``_password`` on the mapped ``User`` class.
  281. The :paramref:`.Mapper.column_prefix` parameter is uncommon in
  282. modern use. For dealing with reflected tables, a more flexible
  283. approach to automating a naming scheme is to intercept the
  284. :class:`.Column` objects as they are reflected; see the section
  285. :ref:`mapper_automated_reflection_schemes` for notes on this usage
  286. pattern.
  287. :param concrete: If True, indicates this mapper should use concrete
  288. table inheritance with its parent mapper.
  289. See the section :ref:`concrete_inheritance` for an example.
  290. :param confirm_deleted_rows: defaults to True; when a DELETE occurs
  291. of one more rows based on specific primary keys, a warning is
  292. emitted when the number of rows matched does not equal the number
  293. of rows expected. This parameter may be set to False to handle the
  294. case where database ON DELETE CASCADE rules may be deleting some of
  295. those rows automatically. The warning may be changed to an
  296. exception in a future release.
  297. :param eager_defaults: if True, the ORM will immediately fetch the
  298. value of server-generated default values after an INSERT or UPDATE,
  299. rather than leaving them as expired to be fetched on next access.
  300. This can be used for event schemes where the server-generated values
  301. are needed immediately before the flush completes.
  302. The fetch of values occurs either by using ``RETURNING`` inline
  303. with the ``INSERT`` or ``UPDATE`` statement, or by adding an
  304. additional ``SELECT`` statement subsequent to the ``INSERT`` or
  305. ``UPDATE``, if the backend does not support ``RETURNING``.
  306. The use of ``RETURNING`` is extremely performant in particular for
  307. ``INSERT`` statements where SQLAlchemy can take advantage of
  308. :ref:`insertmanyvalues <engine_insertmanyvalues>`, whereas the use of
  309. an additional ``SELECT`` is relatively poor performing, adding
  310. additional SQL round trips which would be unnecessary if these new
  311. attributes are not to be accessed in any case.
  312. For this reason, :paramref:`.Mapper.eager_defaults` defaults to the
  313. string value ``"auto"``, which indicates that server defaults for
  314. INSERT should be fetched using ``RETURNING`` if the backing database
  315. supports it and if the dialect in use supports "insertmanyreturning"
  316. for an INSERT statement. If the backing database does not support
  317. ``RETURNING`` or "insertmanyreturning" is not available, server
  318. defaults will not be fetched.
  319. .. versionchanged:: 2.0.0rc1 added the "auto" option for
  320. :paramref:`.Mapper.eager_defaults`
  321. .. seealso::
  322. :ref:`orm_server_defaults`
  323. .. versionchanged:: 2.0.0 RETURNING now works with multiple rows
  324. INSERTed at once using the
  325. :ref:`insertmanyvalues <engine_insertmanyvalues>` feature, which
  326. among other things allows the :paramref:`.Mapper.eager_defaults`
  327. feature to be very performant on supporting backends.
  328. :param exclude_properties: A list or set of string column names to
  329. be excluded from mapping.
  330. .. seealso::
  331. :ref:`include_exclude_cols`
  332. :param include_properties: An inclusive list or set of string column
  333. names to map.
  334. .. seealso::
  335. :ref:`include_exclude_cols`
  336. :param inherits: A mapped class or the corresponding
  337. :class:`_orm.Mapper`
  338. of one indicating a superclass to which this :class:`_orm.Mapper`
  339. should *inherit* from. The mapped class here must be a subclass
  340. of the other mapper's class. When using Declarative, this argument
  341. is passed automatically as a result of the natural class
  342. hierarchy of the declared classes.
  343. .. seealso::
  344. :ref:`inheritance_toplevel`
  345. :param inherit_condition: For joined table inheritance, a SQL
  346. expression which will
  347. define how the two tables are joined; defaults to a natural join
  348. between the two tables.
  349. :param inherit_foreign_keys: When ``inherit_condition`` is used and
  350. the columns present are missing a :class:`_schema.ForeignKey`
  351. configuration, this parameter can be used to specify which columns
  352. are "foreign". In most cases can be left as ``None``.
  353. :param legacy_is_orphan: Boolean, defaults to ``False``.
  354. When ``True``, specifies that "legacy" orphan consideration
  355. is to be applied to objects mapped by this mapper, which means
  356. that a pending (that is, not persistent) object is auto-expunged
  357. from an owning :class:`.Session` only when it is de-associated
  358. from *all* parents that specify a ``delete-orphan`` cascade towards
  359. this mapper. The new default behavior is that the object is
  360. auto-expunged when it is de-associated with *any* of its parents
  361. that specify ``delete-orphan`` cascade. This behavior is more
  362. consistent with that of a persistent object, and allows behavior to
  363. be consistent in more scenarios independently of whether or not an
  364. orphan object has been flushed yet or not.
  365. See the change note and example at :ref:`legacy_is_orphan_addition`
  366. for more detail on this change.
  367. :param non_primary: Specify that this :class:`_orm.Mapper`
  368. is in addition
  369. to the "primary" mapper, that is, the one used for persistence.
  370. The :class:`_orm.Mapper` created here may be used for ad-hoc
  371. mapping of the class to an alternate selectable, for loading
  372. only.
  373. .. seealso::
  374. :ref:`relationship_aliased_class` - the new pattern that removes
  375. the need for the :paramref:`_orm.Mapper.non_primary` flag.
  376. :param passive_deletes: Indicates DELETE behavior of foreign key
  377. columns when a joined-table inheritance entity is being deleted.
  378. Defaults to ``False`` for a base mapper; for an inheriting mapper,
  379. defaults to ``False`` unless the value is set to ``True``
  380. on the superclass mapper.
  381. When ``True``, it is assumed that ON DELETE CASCADE is configured
  382. on the foreign key relationships that link this mapper's table
  383. to its superclass table, so that when the unit of work attempts
  384. to delete the entity, it need only emit a DELETE statement for the
  385. superclass table, and not this table.
  386. When ``False``, a DELETE statement is emitted for this mapper's
  387. table individually. If the primary key attributes local to this
  388. table are unloaded, then a SELECT must be emitted in order to
  389. validate these attributes; note that the primary key columns
  390. of a joined-table subclass are not part of the "primary key" of
  391. the object as a whole.
  392. Note that a value of ``True`` is **always** forced onto the
  393. subclass mappers; that is, it's not possible for a superclass
  394. to specify passive_deletes without this taking effect for
  395. all subclass mappers.
  396. .. seealso::
  397. :ref:`passive_deletes` - description of similar feature as
  398. used with :func:`_orm.relationship`
  399. :paramref:`.mapper.passive_updates` - supporting ON UPDATE
  400. CASCADE for joined-table inheritance mappers
  401. :param passive_updates: Indicates UPDATE behavior of foreign key
  402. columns when a primary key column changes on a joined-table
  403. inheritance mapping. Defaults to ``True``.
  404. When True, it is assumed that ON UPDATE CASCADE is configured on
  405. the foreign key in the database, and that the database will handle
  406. propagation of an UPDATE from a source column to dependent columns
  407. on joined-table rows.
  408. When False, it is assumed that the database does not enforce
  409. referential integrity and will not be issuing its own CASCADE
  410. operation for an update. The unit of work process will
  411. emit an UPDATE statement for the dependent columns during a
  412. primary key change.
  413. .. seealso::
  414. :ref:`passive_updates` - description of a similar feature as
  415. used with :func:`_orm.relationship`
  416. :paramref:`.mapper.passive_deletes` - supporting ON DELETE
  417. CASCADE for joined-table inheritance mappers
  418. :param polymorphic_load: Specifies "polymorphic loading" behavior
  419. for a subclass in an inheritance hierarchy (joined and single
  420. table inheritance only). Valid values are:
  421. * "'inline'" - specifies this class should be part of
  422. the "with_polymorphic" mappers, e.g. its columns will be included
  423. in a SELECT query against the base.
  424. * "'selectin'" - specifies that when instances of this class
  425. are loaded, an additional SELECT will be emitted to retrieve
  426. the columns specific to this subclass. The SELECT uses
  427. IN to fetch multiple subclasses at once.
  428. .. versionadded:: 1.2
  429. .. seealso::
  430. :ref:`with_polymorphic_mapper_config`
  431. :ref:`polymorphic_selectin`
  432. :param polymorphic_on: Specifies the column, attribute, or
  433. SQL expression used to determine the target class for an
  434. incoming row, when inheriting classes are present.
  435. May be specified as a string attribute name, or as a SQL
  436. expression such as a :class:`_schema.Column` or in a Declarative
  437. mapping a :func:`_orm.mapped_column` object. It is typically
  438. expected that the SQL expression corresponds to a column in the
  439. base-most mapped :class:`.Table`::
  440. class Employee(Base):
  441. __tablename__ = 'employee'
  442. id: Mapped[int] = mapped_column(primary_key=True)
  443. discriminator: Mapped[str] = mapped_column(String(50))
  444. __mapper_args__ = {
  445. "polymorphic_on":discriminator,
  446. "polymorphic_identity":"employee"
  447. }
  448. It may also be specified
  449. as a SQL expression, as in this example where we
  450. use the :func:`.case` construct to provide a conditional
  451. approach::
  452. class Employee(Base):
  453. __tablename__ = 'employee'
  454. id: Mapped[int] = mapped_column(primary_key=True)
  455. discriminator: Mapped[str] = mapped_column(String(50))
  456. __mapper_args__ = {
  457. "polymorphic_on":case(
  458. (discriminator == "EN", "engineer"),
  459. (discriminator == "MA", "manager"),
  460. else_="employee"),
  461. "polymorphic_identity":"employee"
  462. }
  463. It may also refer to any attribute using its string name,
  464. which is of particular use when using annotated column
  465. configurations::
  466. class Employee(Base):
  467. __tablename__ = 'employee'
  468. id: Mapped[int] = mapped_column(primary_key=True)
  469. discriminator: Mapped[str]
  470. __mapper_args__ = {
  471. "polymorphic_on": "discriminator",
  472. "polymorphic_identity": "employee"
  473. }
  474. When setting ``polymorphic_on`` to reference an
  475. attribute or expression that's not present in the
  476. locally mapped :class:`_schema.Table`, yet the value
  477. of the discriminator should be persisted to the database,
  478. the value of the
  479. discriminator is not automatically set on new
  480. instances; this must be handled by the user,
  481. either through manual means or via event listeners.
  482. A typical approach to establishing such a listener
  483. looks like::
  484. from sqlalchemy import event
  485. from sqlalchemy.orm import object_mapper
  486. @event.listens_for(Employee, "init", propagate=True)
  487. def set_identity(instance, *arg, **kw):
  488. mapper = object_mapper(instance)
  489. instance.discriminator = mapper.polymorphic_identity
  490. Where above, we assign the value of ``polymorphic_identity``
  491. for the mapped class to the ``discriminator`` attribute,
  492. thus persisting the value to the ``discriminator`` column
  493. in the database.
  494. .. warning::
  495. Currently, **only one discriminator column may be set**, typically
  496. on the base-most class in the hierarchy. "Cascading" polymorphic
  497. columns are not yet supported.
  498. .. seealso::
  499. :ref:`inheritance_toplevel`
  500. :param polymorphic_identity: Specifies the value which
  501. identifies this particular class as returned by the column expression
  502. referred to by the :paramref:`_orm.Mapper.polymorphic_on` setting. As
  503. rows are received, the value corresponding to the
  504. :paramref:`_orm.Mapper.polymorphic_on` column expression is compared
  505. to this value, indicating which subclass should be used for the newly
  506. reconstructed object.
  507. .. seealso::
  508. :ref:`inheritance_toplevel`
  509. :param properties: A dictionary mapping the string names of object
  510. attributes to :class:`.MapperProperty` instances, which define the
  511. persistence behavior of that attribute. Note that
  512. :class:`_schema.Column`
  513. objects present in
  514. the mapped :class:`_schema.Table` are automatically placed into
  515. ``ColumnProperty`` instances upon mapping, unless overridden.
  516. When using Declarative, this argument is passed automatically,
  517. based on all those :class:`.MapperProperty` instances declared
  518. in the declared class body.
  519. .. seealso::
  520. :ref:`orm_mapping_properties` - in the
  521. :ref:`orm_mapping_classes_toplevel`
  522. :param primary_key: A list of :class:`_schema.Column`
  523. objects, or alternatively string names of attribute names which
  524. refer to :class:`_schema.Column`, which define
  525. the primary key to be used against this mapper's selectable unit.
  526. This is normally simply the primary key of the ``local_table``, but
  527. can be overridden here.
  528. .. versionchanged:: 2.0.2 :paramref:`_orm.Mapper.primary_key`
  529. arguments may be indicated as string attribute names as well.
  530. .. seealso::
  531. :ref:`mapper_primary_key` - background and example use
  532. :param version_id_col: A :class:`_schema.Column`
  533. that will be used to keep a running version id of rows
  534. in the table. This is used to detect concurrent updates or
  535. the presence of stale data in a flush. The methodology is to
  536. detect if an UPDATE statement does not match the last known
  537. version id, a
  538. :class:`~sqlalchemy.orm.exc.StaleDataError` exception is
  539. thrown.
  540. By default, the column must be of :class:`.Integer` type,
  541. unless ``version_id_generator`` specifies an alternative version
  542. generator.
  543. .. seealso::
  544. :ref:`mapper_version_counter` - discussion of version counting
  545. and rationale.
  546. :param version_id_generator: Define how new version ids should
  547. be generated. Defaults to ``None``, which indicates that
  548. a simple integer counting scheme be employed. To provide a custom
  549. versioning scheme, provide a callable function of the form::
  550. def generate_version(version):
  551. return next_version
  552. Alternatively, server-side versioning functions such as triggers,
  553. or programmatic versioning schemes outside of the version id
  554. generator may be used, by specifying the value ``False``.
  555. Please see :ref:`server_side_version_counter` for a discussion
  556. of important points when using this option.
  557. .. seealso::
  558. :ref:`custom_version_counter`
  559. :ref:`server_side_version_counter`
  560. :param with_polymorphic: A tuple in the form ``(<classes>,
  561. <selectable>)`` indicating the default style of "polymorphic"
  562. loading, that is, which tables are queried at once. <classes> is
  563. any single or list of mappers and/or classes indicating the
  564. inherited classes that should be loaded at once. The special value
  565. ``'*'`` may be used to indicate all descending classes should be
  566. loaded immediately. The second tuple argument <selectable>
  567. indicates a selectable that will be used to query for multiple
  568. classes.
  569. The :paramref:`_orm.Mapper.polymorphic_load` parameter may be
  570. preferable over the use of :paramref:`_orm.Mapper.with_polymorphic`
  571. in modern mappings to indicate a per-subclass technique of
  572. indicating polymorphic loading styles.
  573. .. seealso::
  574. :ref:`with_polymorphic_mapper_config`
  575. """
  576. self.class_ = util.assert_arg_type(class_, type, "class_")
  577. self._sort_key = "%s.%s" % (
  578. self.class_.__module__,
  579. self.class_.__name__,
  580. )
  581. self._primary_key_argument = util.to_list(primary_key)
  582. self.non_primary = non_primary
  583. self.always_refresh = always_refresh
  584. if isinstance(version_id_col, MapperProperty):
  585. self.version_id_prop = version_id_col
  586. self.version_id_col = None
  587. else:
  588. self.version_id_col = (
  589. coercions.expect(
  590. roles.ColumnArgumentOrKeyRole,
  591. version_id_col,
  592. argname="version_id_col",
  593. )
  594. if version_id_col is not None
  595. else None
  596. )
  597. if version_id_generator is False:
  598. self.version_id_generator = False
  599. elif version_id_generator is None:
  600. self.version_id_generator = lambda x: (x or 0) + 1
  601. else:
  602. self.version_id_generator = version_id_generator
  603. self.concrete = concrete
  604. self.single = False
  605. if inherits is not None:
  606. self.inherits = _parse_mapper_argument(inherits)
  607. else:
  608. self.inherits = None
  609. if local_table is not None:
  610. self.local_table = coercions.expect(
  611. roles.StrictFromClauseRole,
  612. local_table,
  613. disable_inspection=True,
  614. argname="local_table",
  615. )
  616. elif self.inherits:
  617. # note this is a new flow as of 2.0 so that
  618. # .local_table need not be Optional
  619. self.local_table = self.inherits.local_table
  620. self.single = True
  621. else:
  622. raise sa_exc.ArgumentError(
  623. f"Mapper[{self.class_.__name__}(None)] has None for a "
  624. "primary table argument and does not specify 'inherits'"
  625. )
  626. if inherit_condition is not None:
  627. self.inherit_condition = coercions.expect(
  628. roles.OnClauseRole, inherit_condition
  629. )
  630. else:
  631. self.inherit_condition = None
  632. self.inherit_foreign_keys = inherit_foreign_keys
  633. self._init_properties = dict(properties) if properties else {}
  634. self._delete_orphans = []
  635. self.batch = batch
  636. self.eager_defaults = eager_defaults
  637. self.column_prefix = column_prefix
  638. # interim - polymorphic_on is further refined in
  639. # _configure_polymorphic_setter
  640. self.polymorphic_on = (
  641. coercions.expect( # type: ignore
  642. roles.ColumnArgumentOrKeyRole,
  643. polymorphic_on,
  644. argname="polymorphic_on",
  645. )
  646. if polymorphic_on is not None
  647. else None
  648. )
  649. self.polymorphic_abstract = polymorphic_abstract
  650. self._dependency_processors = []
  651. self.validators = util.EMPTY_DICT
  652. self.passive_updates = passive_updates
  653. self.passive_deletes = passive_deletes
  654. self.legacy_is_orphan = legacy_is_orphan
  655. self._clause_adapter = None
  656. self._requires_row_aliasing = False
  657. self._inherits_equated_pairs = None
  658. self._memoized_values = {}
  659. self._compiled_cache_size = _compiled_cache_size
  660. self._reconstructor = None
  661. self.allow_partial_pks = allow_partial_pks
  662. if self.inherits and not self.concrete:
  663. self.confirm_deleted_rows = False
  664. else:
  665. self.confirm_deleted_rows = confirm_deleted_rows
  666. self._set_with_polymorphic(with_polymorphic)
  667. self.polymorphic_load = polymorphic_load
  668. # our 'polymorphic identity', a string name that when located in a
  669. # result set row indicates this Mapper should be used to construct
  670. # the object instance for that row.
  671. self.polymorphic_identity = polymorphic_identity
  672. # a dictionary of 'polymorphic identity' names, associating those
  673. # names with Mappers that will be used to construct object instances
  674. # upon a select operation.
  675. if _polymorphic_map is None:
  676. self.polymorphic_map = {}
  677. else:
  678. self.polymorphic_map = _polymorphic_map
  679. if include_properties is not None:
  680. self.include_properties = util.to_set(include_properties)
  681. else:
  682. self.include_properties = None
  683. if exclude_properties:
  684. self.exclude_properties = util.to_set(exclude_properties)
  685. else:
  686. self.exclude_properties = None
  687. # prevent this mapper from being constructed
  688. # while a configure_mappers() is occurring (and defer a
  689. # configure_mappers() until construction succeeds)
  690. with _CONFIGURE_MUTEX:
  691. cast("MapperEvents", self.dispatch._events)._new_mapper_instance(
  692. class_, self
  693. )
  694. self._configure_inheritance()
  695. self._configure_class_instrumentation()
  696. self._configure_properties()
  697. self._configure_polymorphic_setter()
  698. self._configure_pks()
  699. self.registry._flag_new_mapper(self)
  700. self._log("constructed")
  701. self._expire_memoizations()
  702. self.dispatch.after_mapper_constructed(self, self.class_)
  703. def _prefer_eager_defaults(self, dialect, table):
  704. if self.eager_defaults == "auto":
  705. if not table.implicit_returning:
  706. return False
  707. return (
  708. table in self._server_default_col_keys
  709. and dialect.insert_executemany_returning
  710. )
  711. else:
  712. return self.eager_defaults
  713. def _gen_cache_key(self, anon_map, bindparams):
  714. return (self,)
  715. # ### BEGIN
  716. # ATTRIBUTE DECLARATIONS START HERE
  717. is_mapper = True
  718. """Part of the inspection API."""
  719. represents_outer_join = False
  720. registry: _RegistryType
  721. @property
  722. def mapper(self) -> Mapper[_O]:
  723. """Part of the inspection API.
  724. Returns self.
  725. """
  726. return self
  727. @property
  728. def entity(self):
  729. r"""Part of the inspection API.
  730. Returns self.class\_.
  731. """
  732. return self.class_
  733. class_: Type[_O]
  734. """The class to which this :class:`_orm.Mapper` is mapped."""
  735. _identity_class: Type[_O]
  736. _delete_orphans: List[Tuple[str, Type[Any]]]
  737. _dependency_processors: List[DependencyProcessor]
  738. _memoized_values: Dict[Any, Callable[[], Any]]
  739. _inheriting_mappers: util.WeakSequence[Mapper[Any]]
  740. _all_tables: Set[TableClause]
  741. _polymorphic_attr_key: Optional[str]
  742. _pks_by_table: Dict[FromClause, OrderedSet[ColumnClause[Any]]]
  743. _cols_by_table: Dict[FromClause, OrderedSet[ColumnElement[Any]]]
  744. _props: util.OrderedDict[str, MapperProperty[Any]]
  745. _init_properties: Dict[str, MapperProperty[Any]]
  746. _columntoproperty: _ColumnMapping
  747. _set_polymorphic_identity: Optional[Callable[[InstanceState[_O]], None]]
  748. _validate_polymorphic_identity: Optional[
  749. Callable[[Mapper[_O], InstanceState[_O], _InstanceDict], None]
  750. ]
  751. tables: Sequence[TableClause]
  752. """A sequence containing the collection of :class:`_schema.Table`
  753. or :class:`_schema.TableClause` objects which this :class:`_orm.Mapper`
  754. is aware of.
  755. If the mapper is mapped to a :class:`_expression.Join`, or an
  756. :class:`_expression.Alias`
  757. representing a :class:`_expression.Select`, the individual
  758. :class:`_schema.Table`
  759. objects that comprise the full construct will be represented here.
  760. This is a *read only* attribute determined during mapper construction.
  761. Behavior is undefined if directly modified.
  762. """
  763. validators: util.immutabledict[str, Tuple[str, Dict[str, Any]]]
  764. """An immutable dictionary of attributes which have been decorated
  765. using the :func:`_orm.validates` decorator.
  766. The dictionary contains string attribute names as keys
  767. mapped to the actual validation method.
  768. """
  769. always_refresh: bool
  770. allow_partial_pks: bool
  771. version_id_col: Optional[ColumnElement[Any]]
  772. with_polymorphic: Optional[
  773. Tuple[
  774. Union[Literal["*"], Sequence[Union[Mapper[Any], Type[Any]]]],
  775. Optional[FromClause],
  776. ]
  777. ]
  778. version_id_generator: Optional[Union[Literal[False], Callable[[Any], Any]]]
  779. local_table: FromClause
  780. """The immediate :class:`_expression.FromClause` to which this
  781. :class:`_orm.Mapper` refers.
  782. Typically is an instance of :class:`_schema.Table`, may be any
  783. :class:`.FromClause`.
  784. The "local" table is the
  785. selectable that the :class:`_orm.Mapper` is directly responsible for
  786. managing from an attribute access and flush perspective. For
  787. non-inheriting mappers, :attr:`.Mapper.local_table` will be the same
  788. as :attr:`.Mapper.persist_selectable`. For inheriting mappers,
  789. :attr:`.Mapper.local_table` refers to the specific portion of
  790. :attr:`.Mapper.persist_selectable` that includes the columns to which
  791. this :class:`.Mapper` is loading/persisting, such as a particular
  792. :class:`.Table` within a join.
  793. .. seealso::
  794. :attr:`_orm.Mapper.persist_selectable`.
  795. :attr:`_orm.Mapper.selectable`.
  796. """
  797. persist_selectable: FromClause
  798. """The :class:`_expression.FromClause` to which this :class:`_orm.Mapper`
  799. is mapped.
  800. Typically is an instance of :class:`_schema.Table`, may be any
  801. :class:`.FromClause`.
  802. The :attr:`_orm.Mapper.persist_selectable` is similar to
  803. :attr:`.Mapper.local_table`, but represents the :class:`.FromClause` that
  804. represents the inheriting class hierarchy overall in an inheritance
  805. scenario.
  806. :attr.`.Mapper.persist_selectable` is also separate from the
  807. :attr:`.Mapper.selectable` attribute, the latter of which may be an
  808. alternate subquery used for selecting columns.
  809. :attr.`.Mapper.persist_selectable` is oriented towards columns that
  810. will be written on a persist operation.
  811. .. seealso::
  812. :attr:`_orm.Mapper.selectable`.
  813. :attr:`_orm.Mapper.local_table`.
  814. """
  815. inherits: Optional[Mapper[Any]]
  816. """References the :class:`_orm.Mapper` which this :class:`_orm.Mapper`
  817. inherits from, if any.
  818. """
  819. inherit_condition: Optional[ColumnElement[bool]]
  820. configured: bool = False
  821. """Represent ``True`` if this :class:`_orm.Mapper` has been configured.
  822. This is a *read only* attribute determined during mapper construction.
  823. Behavior is undefined if directly modified.
  824. .. seealso::
  825. :func:`.configure_mappers`.
  826. """
  827. concrete: bool
  828. """Represent ``True`` if this :class:`_orm.Mapper` is a concrete
  829. inheritance mapper.
  830. This is a *read only* attribute determined during mapper construction.
  831. Behavior is undefined if directly modified.
  832. """
  833. primary_key: Tuple[Column[Any], ...]
  834. """An iterable containing the collection of :class:`_schema.Column`
  835. objects
  836. which comprise the 'primary key' of the mapped table, from the
  837. perspective of this :class:`_orm.Mapper`.
  838. This list is against the selectable in
  839. :attr:`_orm.Mapper.persist_selectable`.
  840. In the case of inheriting mappers, some columns may be managed by a
  841. superclass mapper. For example, in the case of a
  842. :class:`_expression.Join`, the
  843. primary key is determined by all of the primary key columns across all
  844. tables referenced by the :class:`_expression.Join`.
  845. The list is also not necessarily the same as the primary key column
  846. collection associated with the underlying tables; the :class:`_orm.Mapper`
  847. features a ``primary_key`` argument that can override what the
  848. :class:`_orm.Mapper` considers as primary key columns.
  849. This is a *read only* attribute determined during mapper construction.
  850. Behavior is undefined if directly modified.
  851. """
  852. class_manager: ClassManager[_O]
  853. """The :class:`.ClassManager` which maintains event listeners
  854. and class-bound descriptors for this :class:`_orm.Mapper`.
  855. This is a *read only* attribute determined during mapper construction.
  856. Behavior is undefined if directly modified.
  857. """
  858. single: bool
  859. """Represent ``True`` if this :class:`_orm.Mapper` is a single table
  860. inheritance mapper.
  861. :attr:`_orm.Mapper.local_table` will be ``None`` if this flag is set.
  862. This is a *read only* attribute determined during mapper construction.
  863. Behavior is undefined if directly modified.
  864. """
  865. non_primary: bool
  866. """Represent ``True`` if this :class:`_orm.Mapper` is a "non-primary"
  867. mapper, e.g. a mapper that is used only to select rows but not for
  868. persistence management.
  869. This is a *read only* attribute determined during mapper construction.
  870. Behavior is undefined if directly modified.
  871. """
  872. polymorphic_on: Optional[KeyedColumnElement[Any]]
  873. """The :class:`_schema.Column` or SQL expression specified as the
  874. ``polymorphic_on`` argument
  875. for this :class:`_orm.Mapper`, within an inheritance scenario.
  876. This attribute is normally a :class:`_schema.Column` instance but
  877. may also be an expression, such as one derived from
  878. :func:`.cast`.
  879. This is a *read only* attribute determined during mapper construction.
  880. Behavior is undefined if directly modified.
  881. """
  882. polymorphic_map: Dict[Any, Mapper[Any]]
  883. """A mapping of "polymorphic identity" identifiers mapped to
  884. :class:`_orm.Mapper` instances, within an inheritance scenario.
  885. The identifiers can be of any type which is comparable to the
  886. type of column represented by :attr:`_orm.Mapper.polymorphic_on`.
  887. An inheritance chain of mappers will all reference the same
  888. polymorphic map object. The object is used to correlate incoming
  889. result rows to target mappers.
  890. This is a *read only* attribute determined during mapper construction.
  891. Behavior is undefined if directly modified.
  892. """
  893. polymorphic_identity: Optional[Any]
  894. """Represent an identifier which is matched against the
  895. :attr:`_orm.Mapper.polymorphic_on` column during result row loading.
  896. Used only with inheritance, this object can be of any type which is
  897. comparable to the type of column represented by
  898. :attr:`_orm.Mapper.polymorphic_on`.
  899. This is a *read only* attribute determined during mapper construction.
  900. Behavior is undefined if directly modified.
  901. """
  902. base_mapper: Mapper[Any]
  903. """The base-most :class:`_orm.Mapper` in an inheritance chain.
  904. In a non-inheriting scenario, this attribute will always be this
  905. :class:`_orm.Mapper`. In an inheritance scenario, it references
  906. the :class:`_orm.Mapper` which is parent to all other :class:`_orm.Mapper`
  907. objects in the inheritance chain.
  908. This is a *read only* attribute determined during mapper construction.
  909. Behavior is undefined if directly modified.
  910. """
  911. columns: ReadOnlyColumnCollection[str, Column[Any]]
  912. """A collection of :class:`_schema.Column` or other scalar expression
  913. objects maintained by this :class:`_orm.Mapper`.
  914. The collection behaves the same as that of the ``c`` attribute on
  915. any :class:`_schema.Table` object,
  916. except that only those columns included in
  917. this mapping are present, and are keyed based on the attribute name
  918. defined in the mapping, not necessarily the ``key`` attribute of the
  919. :class:`_schema.Column` itself. Additionally, scalar expressions mapped
  920. by :func:`.column_property` are also present here.
  921. This is a *read only* attribute determined during mapper construction.
  922. Behavior is undefined if directly modified.
  923. """
  924. c: ReadOnlyColumnCollection[str, Column[Any]]
  925. """A synonym for :attr:`_orm.Mapper.columns`."""
  926. @util.non_memoized_property
  927. @util.deprecated("1.3", "Use .persist_selectable")
  928. def mapped_table(self):
  929. return self.persist_selectable
  930. @util.memoized_property
  931. def _path_registry(self) -> CachingEntityRegistry:
  932. return PathRegistry.per_mapper(self)
  933. def _configure_inheritance(self):
  934. """Configure settings related to inheriting and/or inherited mappers
  935. being present."""
  936. # a set of all mappers which inherit from this one.
  937. self._inheriting_mappers = util.WeakSequence()
  938. if self.inherits:
  939. if not issubclass(self.class_, self.inherits.class_):
  940. raise sa_exc.ArgumentError(
  941. "Class '%s' does not inherit from '%s'"
  942. % (self.class_.__name__, self.inherits.class_.__name__)
  943. )
  944. self.dispatch._update(self.inherits.dispatch)
  945. if self.non_primary != self.inherits.non_primary:
  946. np = not self.non_primary and "primary" or "non-primary"
  947. raise sa_exc.ArgumentError(
  948. "Inheritance of %s mapper for class '%s' is "
  949. "only allowed from a %s mapper"
  950. % (np, self.class_.__name__, np)
  951. )
  952. if self.single:
  953. self.persist_selectable = self.inherits.persist_selectable
  954. elif self.local_table is not self.inherits.local_table:
  955. if self.concrete:
  956. self.persist_selectable = self.local_table
  957. for mapper in self.iterate_to_root():
  958. if mapper.polymorphic_on is not None:
  959. mapper._requires_row_aliasing = True
  960. else:
  961. if self.inherit_condition is None:
  962. # figure out inherit condition from our table to the
  963. # immediate table of the inherited mapper, not its
  964. # full table which could pull in other stuff we don't
  965. # want (allows test/inheritance.InheritTest4 to pass)
  966. try:
  967. self.inherit_condition = sql_util.join_condition(
  968. self.inherits.local_table, self.local_table
  969. )
  970. except sa_exc.NoForeignKeysError as nfe:
  971. assert self.inherits.local_table is not None
  972. assert self.local_table is not None
  973. raise sa_exc.NoForeignKeysError(
  974. "Can't determine the inherit condition "
  975. "between inherited table '%s' and "
  976. "inheriting "
  977. "table '%s'; tables have no "
  978. "foreign key relationships established. "
  979. "Please ensure the inheriting table has "
  980. "a foreign key relationship to the "
  981. "inherited "
  982. "table, or provide an "
  983. "'on clause' using "
  984. "the 'inherit_condition' mapper argument."
  985. % (
  986. self.inherits.local_table.description,
  987. self.local_table.description,
  988. )
  989. ) from nfe
  990. except sa_exc.AmbiguousForeignKeysError as afe:
  991. assert self.inherits.local_table is not None
  992. assert self.local_table is not None
  993. raise sa_exc.AmbiguousForeignKeysError(
  994. "Can't determine the inherit condition "
  995. "between inherited table '%s' and "
  996. "inheriting "
  997. "table '%s'; tables have more than one "
  998. "foreign key relationship established. "
  999. "Please specify the 'on clause' using "
  1000. "the 'inherit_condition' mapper argument."
  1001. % (
  1002. self.inherits.local_table.description,
  1003. self.local_table.description,
  1004. )
  1005. ) from afe
  1006. assert self.inherits.persist_selectable is not None
  1007. self.persist_selectable = sql.join(
  1008. self.inherits.persist_selectable,
  1009. self.local_table,
  1010. self.inherit_condition,
  1011. )
  1012. fks = util.to_set(self.inherit_foreign_keys)
  1013. self._inherits_equated_pairs = sql_util.criterion_as_pairs(
  1014. self.persist_selectable.onclause,
  1015. consider_as_foreign_keys=fks,
  1016. )
  1017. else:
  1018. self.persist_selectable = self.local_table
  1019. if self.polymorphic_identity is None:
  1020. self._identity_class = self.class_
  1021. if (
  1022. not self.polymorphic_abstract
  1023. and self.inherits.base_mapper.polymorphic_on is not None
  1024. ):
  1025. util.warn(
  1026. f"{self} does not indicate a 'polymorphic_identity', "
  1027. "yet is part of an inheritance hierarchy that has a "
  1028. f"'polymorphic_on' column of "
  1029. f"'{self.inherits.base_mapper.polymorphic_on}'. "
  1030. "If this is an intermediary class that should not be "
  1031. "instantiated, the class may either be left unmapped, "
  1032. "or may include the 'polymorphic_abstract=True' "
  1033. "parameter in its Mapper arguments. To leave the "
  1034. "class unmapped when using Declarative, set the "
  1035. "'__abstract__ = True' attribute on the class."
  1036. )
  1037. elif self.concrete:
  1038. self._identity_class = self.class_
  1039. else:
  1040. self._identity_class = self.inherits._identity_class
  1041. if self.version_id_col is None:
  1042. self.version_id_col = self.inherits.version_id_col
  1043. self.version_id_generator = self.inherits.version_id_generator
  1044. elif (
  1045. self.inherits.version_id_col is not None
  1046. and self.version_id_col is not self.inherits.version_id_col
  1047. ):
  1048. util.warn(
  1049. "Inheriting version_id_col '%s' does not match inherited "
  1050. "version_id_col '%s' and will not automatically populate "
  1051. "the inherited versioning column. "
  1052. "version_id_col should only be specified on "
  1053. "the base-most mapper that includes versioning."
  1054. % (
  1055. self.version_id_col.description,
  1056. self.inherits.version_id_col.description,
  1057. )
  1058. )
  1059. self.polymorphic_map = self.inherits.polymorphic_map
  1060. self.batch = self.inherits.batch
  1061. self.inherits._inheriting_mappers.append(self)
  1062. self.base_mapper = self.inherits.base_mapper
  1063. self.passive_updates = self.inherits.passive_updates
  1064. self.passive_deletes = (
  1065. self.inherits.passive_deletes or self.passive_deletes
  1066. )
  1067. self._all_tables = self.inherits._all_tables
  1068. if self.polymorphic_identity is not None:
  1069. if self.polymorphic_identity in self.polymorphic_map:
  1070. util.warn(
  1071. "Reassigning polymorphic association for identity %r "
  1072. "from %r to %r: Check for duplicate use of %r as "
  1073. "value for polymorphic_identity."
  1074. % (
  1075. self.polymorphic_identity,
  1076. self.polymorphic_map[self.polymorphic_identity],
  1077. self,
  1078. self.polymorphic_identity,
  1079. )
  1080. )
  1081. self.polymorphic_map[self.polymorphic_identity] = self
  1082. if self.polymorphic_load and self.concrete:
  1083. raise sa_exc.ArgumentError(
  1084. "polymorphic_load is not currently supported "
  1085. "with concrete table inheritance"
  1086. )
  1087. if self.polymorphic_load == "inline":
  1088. self.inherits._add_with_polymorphic_subclass(self)
  1089. elif self.polymorphic_load == "selectin":
  1090. pass
  1091. elif self.polymorphic_load is not None:
  1092. raise sa_exc.ArgumentError(
  1093. "unknown argument for polymorphic_load: %r"
  1094. % self.polymorphic_load
  1095. )
  1096. else:
  1097. self._all_tables = set()
  1098. self.base_mapper = self
  1099. assert self.local_table is not None
  1100. self.persist_selectable = self.local_table
  1101. if self.polymorphic_identity is not None:
  1102. self.polymorphic_map[self.polymorphic_identity] = self
  1103. self._identity_class = self.class_
  1104. if self.persist_selectable is None:
  1105. raise sa_exc.ArgumentError(
  1106. "Mapper '%s' does not have a persist_selectable specified."
  1107. % self
  1108. )
  1109. def _set_with_polymorphic(
  1110. self, with_polymorphic: Optional[_WithPolymorphicArg]
  1111. ) -> None:
  1112. if with_polymorphic == "*":
  1113. self.with_polymorphic = ("*", None)
  1114. elif isinstance(with_polymorphic, (tuple, list)):
  1115. if isinstance(with_polymorphic[0], (str, tuple, list)):
  1116. self.with_polymorphic = cast(
  1117. """Tuple[
  1118. Union[
  1119. Literal["*"],
  1120. Sequence[Union["Mapper[Any]", Type[Any]]],
  1121. ],
  1122. Optional["FromClause"],
  1123. ]""",
  1124. with_polymorphic,
  1125. )
  1126. else:
  1127. self.with_polymorphic = (with_polymorphic, None)
  1128. elif with_polymorphic is not None:
  1129. raise sa_exc.ArgumentError(
  1130. f"Invalid setting for with_polymorphic: {with_polymorphic!r}"
  1131. )
  1132. else:
  1133. self.with_polymorphic = None
  1134. if self.with_polymorphic and self.with_polymorphic[1] is not None:
  1135. self.with_polymorphic = (
  1136. self.with_polymorphic[0],
  1137. coercions.expect(
  1138. roles.StrictFromClauseRole,
  1139. self.with_polymorphic[1],
  1140. allow_select=True,
  1141. ),
  1142. )
  1143. if self.configured:
  1144. self._expire_memoizations()
  1145. def _add_with_polymorphic_subclass(self, mapper):
  1146. subcl = mapper.class_
  1147. if self.with_polymorphic is None:
  1148. self._set_with_polymorphic((subcl,))
  1149. elif self.with_polymorphic[0] != "*":
  1150. assert isinstance(self.with_polymorphic[0], tuple)
  1151. self._set_with_polymorphic(
  1152. (self.with_polymorphic[0] + (subcl,), self.with_polymorphic[1])
  1153. )
  1154. def _set_concrete_base(self, mapper):
  1155. """Set the given :class:`_orm.Mapper` as the 'inherits' for this
  1156. :class:`_orm.Mapper`, assuming this :class:`_orm.Mapper` is concrete
  1157. and does not already have an inherits."""
  1158. assert self.concrete
  1159. assert not self.inherits
  1160. assert isinstance(mapper, Mapper)
  1161. self.inherits = mapper
  1162. self.inherits.polymorphic_map.update(self.polymorphic_map)
  1163. self.polymorphic_map = self.inherits.polymorphic_map
  1164. for mapper in self.iterate_to_root():
  1165. if mapper.polymorphic_on is not None:
  1166. mapper._requires_row_aliasing = True
  1167. self.batch = self.inherits.batch
  1168. for mp in self.self_and_descendants:
  1169. mp.base_mapper = self.inherits.base_mapper
  1170. self.inherits._inheriting_mappers.append(self)
  1171. self.passive_updates = self.inherits.passive_updates
  1172. self._all_tables = self.inherits._all_tables
  1173. for key, prop in mapper._props.items():
  1174. if key not in self._props and not self._should_exclude(
  1175. key, key, local=False, column=None
  1176. ):
  1177. self._adapt_inherited_property(key, prop, False)
  1178. def _set_polymorphic_on(self, polymorphic_on):
  1179. self.polymorphic_on = polymorphic_on
  1180. self._configure_polymorphic_setter(True)
  1181. def _configure_class_instrumentation(self):
  1182. """If this mapper is to be a primary mapper (i.e. the
  1183. non_primary flag is not set), associate this Mapper with the
  1184. given class and entity name.
  1185. Subsequent calls to ``class_mapper()`` for the ``class_`` / ``entity``
  1186. name combination will return this mapper. Also decorate the
  1187. `__init__` method on the mapped class to include optional
  1188. auto-session attachment logic.
  1189. """
  1190. # we expect that declarative has applied the class manager
  1191. # already and set up a registry. if this is None,
  1192. # this raises as of 2.0.
  1193. manager = attributes.opt_manager_of_class(self.class_)
  1194. if self.non_primary:
  1195. if not manager or not manager.is_mapped:
  1196. raise sa_exc.InvalidRequestError(
  1197. "Class %s has no primary mapper configured. Configure "
  1198. "a primary mapper first before setting up a non primary "
  1199. "Mapper." % self.class_
  1200. )
  1201. self.class_manager = manager
  1202. assert manager.registry is not None
  1203. self.registry = manager.registry
  1204. self._identity_class = manager.mapper._identity_class
  1205. manager.registry._add_non_primary_mapper(self)
  1206. return
  1207. if manager is None or not manager.registry:
  1208. raise sa_exc.InvalidRequestError(
  1209. "The _mapper() function and Mapper() constructor may not be "
  1210. "invoked directly outside of a declarative registry."
  1211. " Please use the sqlalchemy.orm.registry.map_imperatively() "
  1212. "function for a classical mapping."
  1213. )
  1214. self.dispatch.instrument_class(self, self.class_)
  1215. # this invokes the class_instrument event and sets up
  1216. # the __init__ method. documented behavior is that this must
  1217. # occur after the instrument_class event above.
  1218. # yes two events with the same two words reversed and different APIs.
  1219. # :(
  1220. manager = instrumentation.register_class(
  1221. self.class_,
  1222. mapper=self,
  1223. expired_attribute_loader=util.partial(
  1224. loading.load_scalar_attributes, self
  1225. ),
  1226. # finalize flag means instrument the __init__ method
  1227. # and call the class_instrument event
  1228. finalize=True,
  1229. )
  1230. self.class_manager = manager
  1231. assert manager.registry is not None
  1232. self.registry = manager.registry
  1233. # The remaining members can be added by any mapper,
  1234. # e_name None or not.
  1235. if manager.mapper is None:
  1236. return
  1237. event.listen(manager, "init", _event_on_init, raw=True)
  1238. for key, method in util.iterate_attributes(self.class_):
  1239. if key == "__init__" and hasattr(method, "_sa_original_init"):
  1240. method = method._sa_original_init
  1241. if hasattr(method, "__func__"):
  1242. method = method.__func__
  1243. if callable(method):
  1244. if hasattr(method, "__sa_reconstructor__"):
  1245. self._reconstructor = method
  1246. event.listen(manager, "load", _event_on_load, raw=True)
  1247. elif hasattr(method, "__sa_validators__"):
  1248. validation_opts = method.__sa_validation_opts__
  1249. for name in method.__sa_validators__:
  1250. if name in self.validators:
  1251. raise sa_exc.InvalidRequestError(
  1252. "A validation function for mapped "
  1253. "attribute %r on mapper %s already exists."
  1254. % (name, self)
  1255. )
  1256. self.validators = self.validators.union(
  1257. {name: (method, validation_opts)}
  1258. )
  1259. def _set_dispose_flags(self) -> None:
  1260. self.configured = True
  1261. self._ready_for_configure = True
  1262. self._dispose_called = True
  1263. self.__dict__.pop("_configure_failed", None)
  1264. def _str_arg_to_mapped_col(self, argname: str, key: str) -> Column[Any]:
  1265. try:
  1266. prop = self._props[key]
  1267. except KeyError as err:
  1268. raise sa_exc.ArgumentError(
  1269. f"Can't determine {argname} column '{key}' - "
  1270. "no attribute is mapped to this name."
  1271. ) from err
  1272. try:
  1273. expr = prop.expression
  1274. except AttributeError as ae:
  1275. raise sa_exc.ArgumentError(
  1276. f"Can't determine {argname} column '{key}'; "
  1277. "property does not refer to a single mapped Column"
  1278. ) from ae
  1279. if not isinstance(expr, Column):
  1280. raise sa_exc.ArgumentError(
  1281. f"Can't determine {argname} column '{key}'; "
  1282. "property does not refer to a single "
  1283. "mapped Column"
  1284. )
  1285. return expr
  1286. def _configure_pks(self) -> None:
  1287. self.tables = sql_util.find_tables(self.persist_selectable)
  1288. self._all_tables.update(t for t in self.tables)
  1289. self._pks_by_table = {}
  1290. self._cols_by_table = {}
  1291. all_cols = util.column_set(
  1292. chain(*[col.proxy_set for col in self._columntoproperty])
  1293. )
  1294. pk_cols = util.column_set(c for c in all_cols if c.primary_key)
  1295. # identify primary key columns which are also mapped by this mapper.
  1296. for fc in set(self.tables).union([self.persist_selectable]):
  1297. if fc.primary_key and pk_cols.issuperset(fc.primary_key):
  1298. # ordering is important since it determines the ordering of
  1299. # mapper.primary_key (and therefore query.get())
  1300. self._pks_by_table[fc] = util.ordered_column_set( # type: ignore # noqa: E501
  1301. fc.primary_key
  1302. ).intersection(
  1303. pk_cols
  1304. )
  1305. self._cols_by_table[fc] = util.ordered_column_set(fc.c).intersection( # type: ignore # noqa: E501
  1306. all_cols
  1307. )
  1308. if self._primary_key_argument:
  1309. coerced_pk_arg = [
  1310. self._str_arg_to_mapped_col("primary_key", c)
  1311. if isinstance(c, str)
  1312. else c
  1313. for c in (
  1314. coercions.expect(
  1315. roles.DDLConstraintColumnRole,
  1316. coerce_pk,
  1317. argname="primary_key",
  1318. )
  1319. for coerce_pk in self._primary_key_argument
  1320. )
  1321. ]
  1322. else:
  1323. coerced_pk_arg = None
  1324. # if explicit PK argument sent, add those columns to the
  1325. # primary key mappings
  1326. if coerced_pk_arg:
  1327. for k in coerced_pk_arg:
  1328. if k.table not in self._pks_by_table:
  1329. self._pks_by_table[k.table] = util.OrderedSet()
  1330. self._pks_by_table[k.table].add(k)
  1331. # otherwise, see that we got a full PK for the mapped table
  1332. elif (
  1333. self.persist_selectable not in self._pks_by_table
  1334. or len(self._pks_by_table[self.persist_selectable]) == 0
  1335. ):
  1336. raise sa_exc.ArgumentError(
  1337. "Mapper %s could not assemble any primary "
  1338. "key columns for mapped table '%s'"
  1339. % (self, self.persist_selectable.description)
  1340. )
  1341. elif self.local_table not in self._pks_by_table and isinstance(
  1342. self.local_table, schema.Table
  1343. ):
  1344. util.warn(
  1345. "Could not assemble any primary "
  1346. "keys for locally mapped table '%s' - "
  1347. "no rows will be persisted in this Table."
  1348. % self.local_table.description
  1349. )
  1350. if (
  1351. self.inherits
  1352. and not self.concrete
  1353. and not self._primary_key_argument
  1354. ):
  1355. # if inheriting, the "primary key" for this mapper is
  1356. # that of the inheriting (unless concrete or explicit)
  1357. self.primary_key = self.inherits.primary_key
  1358. else:
  1359. # determine primary key from argument or persist_selectable pks
  1360. primary_key: Collection[ColumnElement[Any]]
  1361. if coerced_pk_arg:
  1362. primary_key = [
  1363. cc if cc is not None else c
  1364. for cc, c in (
  1365. (self.persist_selectable.corresponding_column(c), c)
  1366. for c in coerced_pk_arg
  1367. )
  1368. ]
  1369. else:
  1370. # if heuristically determined PKs, reduce to the minimal set
  1371. # of columns by eliminating FK->PK pairs for a multi-table
  1372. # expression. May over-reduce for some kinds of UNIONs
  1373. # / CTEs; use explicit PK argument for these special cases
  1374. primary_key = sql_util.reduce_columns(
  1375. self._pks_by_table[self.persist_selectable],
  1376. ignore_nonexistent_tables=True,
  1377. )
  1378. if len(primary_key) == 0:
  1379. raise sa_exc.ArgumentError(
  1380. "Mapper %s could not assemble any primary "
  1381. "key columns for mapped table '%s'"
  1382. % (self, self.persist_selectable.description)
  1383. )
  1384. self.primary_key = tuple(primary_key)
  1385. self._log("Identified primary key columns: %s", primary_key)
  1386. # determine cols that aren't expressed within our tables; mark these
  1387. # as "read only" properties which are refreshed upon INSERT/UPDATE
  1388. self._readonly_props = {
  1389. self._columntoproperty[col]
  1390. for col in self._columntoproperty
  1391. if self._columntoproperty[col] not in self._identity_key_props
  1392. and (
  1393. not hasattr(col, "table")
  1394. or col.table not in self._cols_by_table
  1395. )
  1396. }
  1397. def _configure_properties(self) -> None:
  1398. self.columns = self.c = sql_base.ColumnCollection() # type: ignore
  1399. # object attribute names mapped to MapperProperty objects
  1400. self._props = util.OrderedDict()
  1401. # table columns mapped to MapperProperty
  1402. self._columntoproperty = _ColumnMapping(self)
  1403. explicit_col_props_by_column: Dict[
  1404. KeyedColumnElement[Any], Tuple[str, ColumnProperty[Any]]
  1405. ] = {}
  1406. explicit_col_props_by_key: Dict[str, ColumnProperty[Any]] = {}
  1407. # step 1: go through properties that were explicitly passed
  1408. # in the properties dictionary. For Columns that are local, put them
  1409. # aside in a separate collection we will reconcile with the Table
  1410. # that's given. For other properties, set them up in _props now.
  1411. if self._init_properties:
  1412. for key, prop_arg in self._init_properties.items():
  1413. if not isinstance(prop_arg, MapperProperty):
  1414. possible_col_prop = self._make_prop_from_column(
  1415. key, prop_arg
  1416. )
  1417. else:
  1418. possible_col_prop = prop_arg
  1419. # issue #8705. if the explicit property is actually a
  1420. # Column that is local to the local Table, don't set it up
  1421. # in ._props yet, integrate it into the order given within
  1422. # the Table.
  1423. _map_as_property_now = True
  1424. if isinstance(possible_col_prop, properties.ColumnProperty):
  1425. for given_col in possible_col_prop.columns:
  1426. if self.local_table.c.contains_column(given_col):
  1427. _map_as_property_now = False
  1428. explicit_col_props_by_key[key] = possible_col_prop
  1429. explicit_col_props_by_column[given_col] = (
  1430. key,
  1431. possible_col_prop,
  1432. )
  1433. if _map_as_property_now:
  1434. self._configure_property(
  1435. key,
  1436. possible_col_prop,
  1437. init=False,
  1438. )
  1439. # step 2: pull properties from the inherited mapper. reconcile
  1440. # columns with those which are explicit above. for properties that
  1441. # are only in the inheriting mapper, set them up as local props
  1442. if self.inherits:
  1443. for key, inherited_prop in self.inherits._props.items():
  1444. if self._should_exclude(key, key, local=False, column=None):
  1445. continue
  1446. incoming_prop = explicit_col_props_by_key.get(key)
  1447. if incoming_prop:
  1448. new_prop = self._reconcile_prop_with_incoming_columns(
  1449. key,
  1450. inherited_prop,
  1451. warn_only=False,
  1452. incoming_prop=incoming_prop,
  1453. )
  1454. explicit_col_props_by_key[key] = new_prop
  1455. for inc_col in incoming_prop.columns:
  1456. explicit_col_props_by_column[inc_col] = (
  1457. key,
  1458. new_prop,
  1459. )
  1460. elif key not in self._props:
  1461. self._adapt_inherited_property(key, inherited_prop, False)
  1462. # step 3. Iterate through all columns in the persist selectable.
  1463. # this includes not only columns in the local table / fromclause,
  1464. # but also those columns in the superclass table if we are joined
  1465. # inh or single inh mapper. map these columns as well. additional
  1466. # reconciliation against inherited columns occurs here also.
  1467. for column in self.persist_selectable.columns:
  1468. if column in explicit_col_props_by_column:
  1469. # column was explicitly passed to properties; configure
  1470. # it now in the order in which it corresponds to the
  1471. # Table / selectable
  1472. key, prop = explicit_col_props_by_column[column]
  1473. self._configure_property(key, prop, init=False)
  1474. continue
  1475. elif column in self._columntoproperty:
  1476. continue
  1477. column_key = (self.column_prefix or "") + column.key
  1478. if self._should_exclude(
  1479. column.key,
  1480. column_key,
  1481. local=self.local_table.c.contains_column(column),
  1482. column=column,
  1483. ):
  1484. continue
  1485. # adjust the "key" used for this column to that
  1486. # of the inheriting mapper
  1487. for mapper in self.iterate_to_root():
  1488. if column in mapper._columntoproperty:
  1489. column_key = mapper._columntoproperty[column].key
  1490. self._configure_property(
  1491. column_key,
  1492. column,
  1493. init=False,
  1494. setparent=True,
  1495. )
  1496. def _configure_polymorphic_setter(self, init=False):
  1497. """Configure an attribute on the mapper representing the
  1498. 'polymorphic_on' column, if applicable, and not
  1499. already generated by _configure_properties (which is typical).
  1500. Also create a setter function which will assign this
  1501. attribute to the value of the 'polymorphic_identity'
  1502. upon instance construction, also if applicable. This
  1503. routine will run when an instance is created.
  1504. """
  1505. setter = False
  1506. polymorphic_key: Optional[str] = None
  1507. if self.polymorphic_on is not None:
  1508. setter = True
  1509. if isinstance(self.polymorphic_on, str):
  1510. # polymorphic_on specified as a string - link
  1511. # it to mapped ColumnProperty
  1512. try:
  1513. self.polymorphic_on = self._props[self.polymorphic_on]
  1514. except KeyError as err:
  1515. raise sa_exc.ArgumentError(
  1516. "Can't determine polymorphic_on "
  1517. "value '%s' - no attribute is "
  1518. "mapped to this name." % self.polymorphic_on
  1519. ) from err
  1520. if self.polymorphic_on in self._columntoproperty:
  1521. # polymorphic_on is a column that is already mapped
  1522. # to a ColumnProperty
  1523. prop = self._columntoproperty[self.polymorphic_on]
  1524. elif isinstance(self.polymorphic_on, MapperProperty):
  1525. # polymorphic_on is directly a MapperProperty,
  1526. # ensure it's a ColumnProperty
  1527. if not isinstance(
  1528. self.polymorphic_on, properties.ColumnProperty
  1529. ):
  1530. raise sa_exc.ArgumentError(
  1531. "Only direct column-mapped "
  1532. "property or SQL expression "
  1533. "can be passed for polymorphic_on"
  1534. )
  1535. prop = self.polymorphic_on
  1536. else:
  1537. # polymorphic_on is a Column or SQL expression and
  1538. # doesn't appear to be mapped. this means it can be 1.
  1539. # only present in the with_polymorphic selectable or
  1540. # 2. a totally standalone SQL expression which we'd
  1541. # hope is compatible with this mapper's persist_selectable
  1542. col = self.persist_selectable.corresponding_column(
  1543. self.polymorphic_on
  1544. )
  1545. if col is None:
  1546. # polymorphic_on doesn't derive from any
  1547. # column/expression isn't present in the mapped
  1548. # table. we will make a "hidden" ColumnProperty
  1549. # for it. Just check that if it's directly a
  1550. # schema.Column and we have with_polymorphic, it's
  1551. # likely a user error if the schema.Column isn't
  1552. # represented somehow in either persist_selectable or
  1553. # with_polymorphic. Otherwise as of 0.7.4 we
  1554. # just go with it and assume the user wants it
  1555. # that way (i.e. a CASE statement)
  1556. setter = False
  1557. instrument = False
  1558. col = self.polymorphic_on
  1559. if isinstance(col, schema.Column) and (
  1560. self.with_polymorphic is None
  1561. or self.with_polymorphic[1] is None
  1562. or self.with_polymorphic[1].corresponding_column(col)
  1563. is None
  1564. ):
  1565. raise sa_exc.InvalidRequestError(
  1566. "Could not map polymorphic_on column "
  1567. "'%s' to the mapped table - polymorphic "
  1568. "loads will not function properly"
  1569. % col.description
  1570. )
  1571. else:
  1572. # column/expression that polymorphic_on derives from
  1573. # is present in our mapped table
  1574. # and is probably mapped, but polymorphic_on itself
  1575. # is not. This happens when
  1576. # the polymorphic_on is only directly present in the
  1577. # with_polymorphic selectable, as when use
  1578. # polymorphic_union.
  1579. # we'll make a separate ColumnProperty for it.
  1580. instrument = True
  1581. key = getattr(col, "key", None)
  1582. if key:
  1583. if self._should_exclude(key, key, False, col):
  1584. raise sa_exc.InvalidRequestError(
  1585. "Cannot exclude or override the "
  1586. "discriminator column %r" % key
  1587. )
  1588. else:
  1589. self.polymorphic_on = col = col.label("_sa_polymorphic_on")
  1590. key = col.key
  1591. prop = properties.ColumnProperty(col, _instrument=instrument)
  1592. self._configure_property(key, prop, init=init, setparent=True)
  1593. # the actual polymorphic_on should be the first public-facing
  1594. # column in the property
  1595. self.polymorphic_on = prop.columns[0]
  1596. polymorphic_key = prop.key
  1597. else:
  1598. # no polymorphic_on was set.
  1599. # check inheriting mappers for one.
  1600. for mapper in self.iterate_to_root():
  1601. # determine if polymorphic_on of the parent
  1602. # should be propagated here. If the col
  1603. # is present in our mapped table, or if our mapped
  1604. # table is the same as the parent (i.e. single table
  1605. # inheritance), we can use it
  1606. if mapper.polymorphic_on is not None:
  1607. if self.persist_selectable is mapper.persist_selectable:
  1608. self.polymorphic_on = mapper.polymorphic_on
  1609. else:
  1610. self.polymorphic_on = (
  1611. self.persist_selectable
  1612. ).corresponding_column(mapper.polymorphic_on)
  1613. # we can use the parent mapper's _set_polymorphic_identity
  1614. # directly; it ensures the polymorphic_identity of the
  1615. # instance's mapper is used so is portable to subclasses.
  1616. if self.polymorphic_on is not None:
  1617. self._set_polymorphic_identity = (
  1618. mapper._set_polymorphic_identity
  1619. )
  1620. self._polymorphic_attr_key = (
  1621. mapper._polymorphic_attr_key
  1622. )
  1623. self._validate_polymorphic_identity = (
  1624. mapper._validate_polymorphic_identity
  1625. )
  1626. else:
  1627. self._set_polymorphic_identity = None
  1628. self._polymorphic_attr_key = None
  1629. return
  1630. if self.polymorphic_abstract and self.polymorphic_on is None:
  1631. raise sa_exc.InvalidRequestError(
  1632. "The Mapper.polymorphic_abstract parameter may only be used "
  1633. "on a mapper hierarchy which includes the "
  1634. "Mapper.polymorphic_on parameter at the base of the hierarchy."
  1635. )
  1636. if setter:
  1637. def _set_polymorphic_identity(state):
  1638. dict_ = state.dict
  1639. # TODO: what happens if polymorphic_on column attribute name
  1640. # does not match .key?
  1641. polymorphic_identity = (
  1642. state.manager.mapper.polymorphic_identity
  1643. )
  1644. if (
  1645. polymorphic_identity is None
  1646. and state.manager.mapper.polymorphic_abstract
  1647. ):
  1648. raise sa_exc.InvalidRequestError(
  1649. f"Can't instantiate class for {state.manager.mapper}; "
  1650. "mapper is marked polymorphic_abstract=True"
  1651. )
  1652. state.get_impl(polymorphic_key).set(
  1653. state,
  1654. dict_,
  1655. polymorphic_identity,
  1656. None,
  1657. )
  1658. self._polymorphic_attr_key = polymorphic_key
  1659. def _validate_polymorphic_identity(mapper, state, dict_):
  1660. if (
  1661. polymorphic_key in dict_
  1662. and dict_[polymorphic_key]
  1663. not in mapper._acceptable_polymorphic_identities
  1664. ):
  1665. util.warn_limited(
  1666. "Flushing object %s with "
  1667. "incompatible polymorphic identity %r; the "
  1668. "object may not refresh and/or load correctly",
  1669. (state_str(state), dict_[polymorphic_key]),
  1670. )
  1671. self._set_polymorphic_identity = _set_polymorphic_identity
  1672. self._validate_polymorphic_identity = (
  1673. _validate_polymorphic_identity
  1674. )
  1675. else:
  1676. self._polymorphic_attr_key = None
  1677. self._set_polymorphic_identity = None
  1678. _validate_polymorphic_identity = None
  1679. @HasMemoized.memoized_attribute
  1680. def _version_id_prop(self):
  1681. if self.version_id_col is not None:
  1682. return self._columntoproperty[self.version_id_col]
  1683. else:
  1684. return None
  1685. @HasMemoized.memoized_attribute
  1686. def _acceptable_polymorphic_identities(self):
  1687. identities = set()
  1688. stack = deque([self])
  1689. while stack:
  1690. item = stack.popleft()
  1691. if item.persist_selectable is self.persist_selectable:
  1692. identities.add(item.polymorphic_identity)
  1693. stack.extend(item._inheriting_mappers)
  1694. return identities
  1695. @HasMemoized.memoized_attribute
  1696. def _prop_set(self):
  1697. return frozenset(self._props.values())
  1698. @util.preload_module("sqlalchemy.orm.descriptor_props")
  1699. def _adapt_inherited_property(self, key, prop, init):
  1700. descriptor_props = util.preloaded.orm_descriptor_props
  1701. if not self.concrete:
  1702. self._configure_property(key, prop, init=False, setparent=False)
  1703. elif key not in self._props:
  1704. # determine if the class implements this attribute; if not,
  1705. # or if it is implemented by the attribute that is handling the
  1706. # given superclass-mapped property, then we need to report that we
  1707. # can't use this at the instance level since we are a concrete
  1708. # mapper and we don't map this. don't trip user-defined
  1709. # descriptors that might have side effects when invoked.
  1710. implementing_attribute = self.class_manager._get_class_attr_mro(
  1711. key, prop
  1712. )
  1713. if implementing_attribute is prop or (
  1714. isinstance(
  1715. implementing_attribute, attributes.InstrumentedAttribute
  1716. )
  1717. and implementing_attribute._parententity is prop.parent
  1718. ):
  1719. self._configure_property(
  1720. key,
  1721. descriptor_props.ConcreteInheritedProperty(),
  1722. init=init,
  1723. setparent=True,
  1724. )
  1725. @util.preload_module("sqlalchemy.orm.descriptor_props")
  1726. def _configure_property(
  1727. self,
  1728. key: str,
  1729. prop_arg: Union[KeyedColumnElement[Any], MapperProperty[Any]],
  1730. *,
  1731. init: bool = True,
  1732. setparent: bool = True,
  1733. warn_for_existing: bool = False,
  1734. ) -> MapperProperty[Any]:
  1735. descriptor_props = util.preloaded.orm_descriptor_props
  1736. self._log(
  1737. "_configure_property(%s, %s)", key, prop_arg.__class__.__name__
  1738. )
  1739. if not isinstance(prop_arg, MapperProperty):
  1740. prop: MapperProperty[Any] = self._property_from_column(
  1741. key, prop_arg
  1742. )
  1743. else:
  1744. prop = prop_arg
  1745. if isinstance(prop, properties.ColumnProperty):
  1746. col = self.persist_selectable.corresponding_column(prop.columns[0])
  1747. # if the column is not present in the mapped table,
  1748. # test if a column has been added after the fact to the
  1749. # parent table (or their parent, etc.) [ticket:1570]
  1750. if col is None and self.inherits:
  1751. path = [self]
  1752. for m in self.inherits.iterate_to_root():
  1753. col = m.local_table.corresponding_column(prop.columns[0])
  1754. if col is not None:
  1755. for m2 in path:
  1756. m2.persist_selectable._refresh_for_new_column(col)
  1757. col = self.persist_selectable.corresponding_column(
  1758. prop.columns[0]
  1759. )
  1760. break
  1761. path.append(m)
  1762. # subquery expression, column not present in the mapped
  1763. # selectable.
  1764. if col is None:
  1765. col = prop.columns[0]
  1766. # column is coming in after _readonly_props was
  1767. # initialized; check for 'readonly'
  1768. if hasattr(self, "_readonly_props") and (
  1769. not hasattr(col, "table")
  1770. or col.table not in self._cols_by_table
  1771. ):
  1772. self._readonly_props.add(prop)
  1773. else:
  1774. # if column is coming in after _cols_by_table was
  1775. # initialized, ensure the col is in the right set
  1776. if (
  1777. hasattr(self, "_cols_by_table")
  1778. and col.table in self._cols_by_table
  1779. and col not in self._cols_by_table[col.table]
  1780. ):
  1781. self._cols_by_table[col.table].add(col)
  1782. # if this properties.ColumnProperty represents the "polymorphic
  1783. # discriminator" column, mark it. We'll need this when rendering
  1784. # columns in SELECT statements.
  1785. if not hasattr(prop, "_is_polymorphic_discriminator"):
  1786. prop._is_polymorphic_discriminator = (
  1787. col is self.polymorphic_on
  1788. or prop.columns[0] is self.polymorphic_on
  1789. )
  1790. if isinstance(col, expression.Label):
  1791. # new in 1.4, get column property against expressions
  1792. # to be addressable in subqueries
  1793. col.key = col._tq_key_label = key
  1794. self.columns.add(col, key)
  1795. for col in prop.columns:
  1796. for proxy_col in col.proxy_set:
  1797. self._columntoproperty[proxy_col] = prop
  1798. if getattr(prop, "key", key) != key:
  1799. util.warn(
  1800. f"ORM mapped property {self.class_.__name__}.{prop.key} being "
  1801. "assigned to attribute "
  1802. f"{key!r} is already associated with "
  1803. f"attribute {prop.key!r}. The attribute will be de-associated "
  1804. f"from {prop.key!r}."
  1805. )
  1806. prop.key = key
  1807. if setparent:
  1808. prop.set_parent(self, init)
  1809. if key in self._props and getattr(
  1810. self._props[key], "_mapped_by_synonym", False
  1811. ):
  1812. syn = self._props[key]._mapped_by_synonym
  1813. raise sa_exc.ArgumentError(
  1814. "Can't call map_column=True for synonym %r=%r, "
  1815. "a ColumnProperty already exists keyed to the name "
  1816. "%r for column %r" % (syn, key, key, syn)
  1817. )
  1818. # replacement cases
  1819. # case one: prop is replacing a prop that we have mapped. this is
  1820. # independent of whatever might be in the actual class dictionary
  1821. if (
  1822. key in self._props
  1823. and not isinstance(
  1824. self._props[key], descriptor_props.ConcreteInheritedProperty
  1825. )
  1826. and not isinstance(prop, descriptor_props.SynonymProperty)
  1827. ):
  1828. if warn_for_existing:
  1829. util.warn_deprecated(
  1830. f"User-placed attribute {self.class_.__name__}.{key} on "
  1831. f"{self} is replacing an existing ORM-mapped attribute. "
  1832. "Behavior is not fully defined in this case. This "
  1833. "use is deprecated and will raise an error in a future "
  1834. "release",
  1835. "2.0",
  1836. )
  1837. oldprop = self._props[key]
  1838. self._path_registry.pop(oldprop, None)
  1839. # case two: prop is replacing an attribute on the class of some kind.
  1840. # we have to be more careful here since it's normal when using
  1841. # Declarative that all the "declared attributes" on the class
  1842. # get replaced.
  1843. elif (
  1844. warn_for_existing
  1845. and self.class_.__dict__.get(key, None) is not None
  1846. and not isinstance(prop, descriptor_props.SynonymProperty)
  1847. and not isinstance(
  1848. self._props.get(key, None),
  1849. descriptor_props.ConcreteInheritedProperty,
  1850. )
  1851. ):
  1852. util.warn_deprecated(
  1853. f"User-placed attribute {self.class_.__name__}.{key} on "
  1854. f"{self} is replacing an existing class-bound "
  1855. "attribute of the same name. "
  1856. "Behavior is not fully defined in this case. This "
  1857. "use is deprecated and will raise an error in a future "
  1858. "release",
  1859. "2.0",
  1860. )
  1861. self._props[key] = prop
  1862. if not self.non_primary:
  1863. prop.instrument_class(self)
  1864. for mapper in self._inheriting_mappers:
  1865. mapper._adapt_inherited_property(key, prop, init)
  1866. if init:
  1867. prop.init()
  1868. prop.post_instrument_class(self)
  1869. if self.configured:
  1870. self._expire_memoizations()
  1871. return prop
  1872. def _make_prop_from_column(
  1873. self,
  1874. key: str,
  1875. column: Union[
  1876. Sequence[KeyedColumnElement[Any]], KeyedColumnElement[Any]
  1877. ],
  1878. ) -> ColumnProperty[Any]:
  1879. columns = util.to_list(column)
  1880. mapped_column = []
  1881. for c in columns:
  1882. mc = self.persist_selectable.corresponding_column(c)
  1883. if mc is None:
  1884. mc = self.local_table.corresponding_column(c)
  1885. if mc is not None:
  1886. # if the column is in the local table but not the
  1887. # mapped table, this corresponds to adding a
  1888. # column after the fact to the local table.
  1889. # [ticket:1523]
  1890. self.persist_selectable._refresh_for_new_column(mc)
  1891. mc = self.persist_selectable.corresponding_column(c)
  1892. if mc is None:
  1893. raise sa_exc.ArgumentError(
  1894. "When configuring property '%s' on %s, "
  1895. "column '%s' is not represented in the mapper's "
  1896. "table. Use the `column_property()` function to "
  1897. "force this column to be mapped as a read-only "
  1898. "attribute." % (key, self, c)
  1899. )
  1900. mapped_column.append(mc)
  1901. return properties.ColumnProperty(*mapped_column)
  1902. def _reconcile_prop_with_incoming_columns(
  1903. self,
  1904. key: str,
  1905. existing_prop: MapperProperty[Any],
  1906. warn_only: bool,
  1907. incoming_prop: Optional[ColumnProperty[Any]] = None,
  1908. single_column: Optional[KeyedColumnElement[Any]] = None,
  1909. ) -> ColumnProperty[Any]:
  1910. if incoming_prop and (
  1911. self.concrete
  1912. or not isinstance(existing_prop, properties.ColumnProperty)
  1913. ):
  1914. return incoming_prop
  1915. existing_column = existing_prop.columns[0]
  1916. if incoming_prop and existing_column in incoming_prop.columns:
  1917. return incoming_prop
  1918. if incoming_prop is None:
  1919. assert single_column is not None
  1920. incoming_column = single_column
  1921. equated_pair_key = (existing_prop.columns[0], incoming_column)
  1922. else:
  1923. assert single_column is None
  1924. incoming_column = incoming_prop.columns[0]
  1925. equated_pair_key = (incoming_column, existing_prop.columns[0])
  1926. if (
  1927. (
  1928. not self._inherits_equated_pairs
  1929. or (equated_pair_key not in self._inherits_equated_pairs)
  1930. )
  1931. and not existing_column.shares_lineage(incoming_column)
  1932. and existing_column is not self.version_id_col
  1933. and incoming_column is not self.version_id_col
  1934. ):
  1935. msg = (
  1936. "Implicitly combining column %s with column "
  1937. "%s under attribute '%s'. Please configure one "
  1938. "or more attributes for these same-named columns "
  1939. "explicitly."
  1940. % (
  1941. existing_prop.columns[-1],
  1942. incoming_column,
  1943. key,
  1944. )
  1945. )
  1946. if warn_only:
  1947. util.warn(msg)
  1948. else:
  1949. raise sa_exc.InvalidRequestError(msg)
  1950. # existing properties.ColumnProperty from an inheriting
  1951. # mapper. make a copy and append our column to it
  1952. # breakpoint()
  1953. new_prop = existing_prop.copy()
  1954. new_prop.columns.insert(0, incoming_column)
  1955. self._log(
  1956. "inserting column to existing list "
  1957. "in properties.ColumnProperty %s",
  1958. key,
  1959. )
  1960. return new_prop # type: ignore
  1961. @util.preload_module("sqlalchemy.orm.descriptor_props")
  1962. def _property_from_column(
  1963. self,
  1964. key: str,
  1965. column: KeyedColumnElement[Any],
  1966. ) -> ColumnProperty[Any]:
  1967. """generate/update a :class:`.ColumnProperty` given a
  1968. :class:`_schema.Column` or other SQL expression object."""
  1969. descriptor_props = util.preloaded.orm_descriptor_props
  1970. prop = self._props.get(key)
  1971. if isinstance(prop, properties.ColumnProperty):
  1972. return self._reconcile_prop_with_incoming_columns(
  1973. key,
  1974. prop,
  1975. single_column=column,
  1976. warn_only=prop.parent is not self,
  1977. )
  1978. elif prop is None or isinstance(
  1979. prop, descriptor_props.ConcreteInheritedProperty
  1980. ):
  1981. return self._make_prop_from_column(key, column)
  1982. else:
  1983. raise sa_exc.ArgumentError(
  1984. "WARNING: when configuring property '%s' on %s, "
  1985. "column '%s' conflicts with property '%r'. "
  1986. "To resolve this, map the column to the class under a "
  1987. "different name in the 'properties' dictionary. Or, "
  1988. "to remove all awareness of the column entirely "
  1989. "(including its availability as a foreign key), "
  1990. "use the 'include_properties' or 'exclude_properties' "
  1991. "mapper arguments to control specifically which table "
  1992. "columns get mapped." % (key, self, column.key, prop)
  1993. )
  1994. @util.langhelpers.tag_method_for_warnings(
  1995. "This warning originated from the `configure_mappers()` process, "
  1996. "which was invoked automatically in response to a user-initiated "
  1997. "operation.",
  1998. sa_exc.SAWarning,
  1999. )
  2000. def _check_configure(self) -> None:
  2001. if self.registry._new_mappers:
  2002. _configure_registries({self.registry}, cascade=True)
  2003. def _post_configure_properties(self) -> None:
  2004. """Call the ``init()`` method on all ``MapperProperties``
  2005. attached to this mapper.
  2006. This is a deferred configuration step which is intended
  2007. to execute once all mappers have been constructed.
  2008. """
  2009. self._log("_post_configure_properties() started")
  2010. l = [(key, prop) for key, prop in self._props.items()]
  2011. for key, prop in l:
  2012. self._log("initialize prop %s", key)
  2013. if prop.parent is self and not prop._configure_started:
  2014. prop.init()
  2015. if prop._configure_finished:
  2016. prop.post_instrument_class(self)
  2017. self._log("_post_configure_properties() complete")
  2018. self.configured = True
  2019. def add_properties(self, dict_of_properties):
  2020. """Add the given dictionary of properties to this mapper,
  2021. using `add_property`.
  2022. """
  2023. for key, value in dict_of_properties.items():
  2024. self.add_property(key, value)
  2025. def add_property(
  2026. self, key: str, prop: Union[Column[Any], MapperProperty[Any]]
  2027. ) -> None:
  2028. """Add an individual MapperProperty to this mapper.
  2029. If the mapper has not been configured yet, just adds the
  2030. property to the initial properties dictionary sent to the
  2031. constructor. If this Mapper has already been configured, then
  2032. the given MapperProperty is configured immediately.
  2033. """
  2034. prop = self._configure_property(
  2035. key, prop, init=self.configured, warn_for_existing=True
  2036. )
  2037. assert isinstance(prop, MapperProperty)
  2038. self._init_properties[key] = prop
  2039. def _expire_memoizations(self) -> None:
  2040. for mapper in self.iterate_to_root():
  2041. mapper._reset_memoizations()
  2042. @property
  2043. def _log_desc(self) -> str:
  2044. return (
  2045. "("
  2046. + self.class_.__name__
  2047. + "|"
  2048. + (
  2049. self.local_table is not None
  2050. and self.local_table.description
  2051. or str(self.local_table)
  2052. )
  2053. + (self.non_primary and "|non-primary" or "")
  2054. + ")"
  2055. )
  2056. def _log(self, msg: str, *args: Any) -> None:
  2057. self.logger.info("%s " + msg, *((self._log_desc,) + args))
  2058. def _log_debug(self, msg: str, *args: Any) -> None:
  2059. self.logger.debug("%s " + msg, *((self._log_desc,) + args))
  2060. def __repr__(self) -> str:
  2061. return "<Mapper at 0x%x; %s>" % (id(self), self.class_.__name__)
  2062. def __str__(self) -> str:
  2063. return "Mapper[%s%s(%s)]" % (
  2064. self.class_.__name__,
  2065. self.non_primary and " (non-primary)" or "",
  2066. self.local_table.description
  2067. if self.local_table is not None
  2068. else self.persist_selectable.description,
  2069. )
  2070. def _is_orphan(self, state: InstanceState[_O]) -> bool:
  2071. orphan_possible = False
  2072. for mapper in self.iterate_to_root():
  2073. for key, cls in mapper._delete_orphans:
  2074. orphan_possible = True
  2075. has_parent = attributes.manager_of_class(cls).has_parent(
  2076. state, key, optimistic=state.has_identity
  2077. )
  2078. if self.legacy_is_orphan and has_parent:
  2079. return False
  2080. elif not self.legacy_is_orphan and not has_parent:
  2081. return True
  2082. if self.legacy_is_orphan:
  2083. return orphan_possible
  2084. else:
  2085. return False
  2086. def has_property(self, key: str) -> bool:
  2087. return key in self._props
  2088. def get_property(
  2089. self, key: str, _configure_mappers: bool = False
  2090. ) -> MapperProperty[Any]:
  2091. """return a MapperProperty associated with the given key."""
  2092. if _configure_mappers:
  2093. self._check_configure()
  2094. try:
  2095. return self._props[key]
  2096. except KeyError as err:
  2097. raise sa_exc.InvalidRequestError(
  2098. f"Mapper '{self}' has no property '{key}'. If this property "
  2099. "was indicated from other mappers or configure events, ensure "
  2100. "registry.configure() has been called."
  2101. ) from err
  2102. def get_property_by_column(
  2103. self, column: ColumnElement[_T]
  2104. ) -> MapperProperty[_T]:
  2105. """Given a :class:`_schema.Column` object, return the
  2106. :class:`.MapperProperty` which maps this column."""
  2107. return self._columntoproperty[column]
  2108. @property
  2109. def iterate_properties(self):
  2110. """return an iterator of all MapperProperty objects."""
  2111. return iter(self._props.values())
  2112. def _mappers_from_spec(
  2113. self, spec: Any, selectable: Optional[FromClause]
  2114. ) -> Sequence[Mapper[Any]]:
  2115. """given a with_polymorphic() argument, return the set of mappers it
  2116. represents.
  2117. Trims the list of mappers to just those represented within the given
  2118. selectable, if present. This helps some more legacy-ish mappings.
  2119. """
  2120. if spec == "*":
  2121. mappers = list(self.self_and_descendants)
  2122. elif spec:
  2123. mapper_set = set()
  2124. for m in util.to_list(spec):
  2125. m = _class_to_mapper(m)
  2126. if not m.isa(self):
  2127. raise sa_exc.InvalidRequestError(
  2128. "%r does not inherit from %r" % (m, self)
  2129. )
  2130. if selectable is None:
  2131. mapper_set.update(m.iterate_to_root())
  2132. else:
  2133. mapper_set.add(m)
  2134. mappers = [m for m in self.self_and_descendants if m in mapper_set]
  2135. else:
  2136. mappers = []
  2137. if selectable is not None:
  2138. tables = set(
  2139. sql_util.find_tables(selectable, include_aliases=True)
  2140. )
  2141. mappers = [m for m in mappers if m.local_table in tables]
  2142. return mappers
  2143. def _selectable_from_mappers(
  2144. self, mappers: Iterable[Mapper[Any]], innerjoin: bool
  2145. ) -> FromClause:
  2146. """given a list of mappers (assumed to be within this mapper's
  2147. inheritance hierarchy), construct an outerjoin amongst those mapper's
  2148. mapped tables.
  2149. """
  2150. from_obj = self.persist_selectable
  2151. for m in mappers:
  2152. if m is self:
  2153. continue
  2154. if m.concrete:
  2155. raise sa_exc.InvalidRequestError(
  2156. "'with_polymorphic()' requires 'selectable' argument "
  2157. "when concrete-inheriting mappers are used."
  2158. )
  2159. elif not m.single:
  2160. if innerjoin:
  2161. from_obj = from_obj.join(
  2162. m.local_table, m.inherit_condition
  2163. )
  2164. else:
  2165. from_obj = from_obj.outerjoin(
  2166. m.local_table, m.inherit_condition
  2167. )
  2168. return from_obj
  2169. @HasMemoized.memoized_attribute
  2170. def _version_id_has_server_side_value(self) -> bool:
  2171. vid_col = self.version_id_col
  2172. if vid_col is None:
  2173. return False
  2174. elif not isinstance(vid_col, Column):
  2175. return True
  2176. else:
  2177. return vid_col.server_default is not None or (
  2178. vid_col.default is not None
  2179. and (
  2180. not vid_col.default.is_scalar
  2181. and not vid_col.default.is_callable
  2182. )
  2183. )
  2184. @HasMemoized.memoized_attribute
  2185. def _single_table_criterion(self):
  2186. if self.single and self.inherits and self.polymorphic_on is not None:
  2187. return self.polymorphic_on._annotate(
  2188. {"parententity": self, "parentmapper": self}
  2189. ).in_(
  2190. [
  2191. m.polymorphic_identity
  2192. for m in self.self_and_descendants
  2193. if not m.polymorphic_abstract
  2194. ]
  2195. )
  2196. else:
  2197. return None
  2198. @HasMemoized.memoized_attribute
  2199. def _has_aliased_polymorphic_fromclause(self):
  2200. """return True if with_polymorphic[1] is an aliased fromclause,
  2201. like a subquery.
  2202. As of #8168, polymorphic adaption with ORMAdapter is used only
  2203. if this is present.
  2204. """
  2205. return self.with_polymorphic and isinstance(
  2206. self.with_polymorphic[1],
  2207. expression.AliasedReturnsRows,
  2208. )
  2209. @HasMemoized.memoized_attribute
  2210. def _should_select_with_poly_adapter(self):
  2211. """determine if _MapperEntity or _ORMColumnEntity will need to use
  2212. polymorphic adaption when setting up a SELECT as well as fetching
  2213. rows for mapped classes and subclasses against this Mapper.
  2214. moved here from context.py for #8456 to generalize the ruleset
  2215. for this condition.
  2216. """
  2217. # this has been simplified as of #8456.
  2218. # rule is: if we have a with_polymorphic or a concrete-style
  2219. # polymorphic selectable, *or* if the base mapper has either of those,
  2220. # we turn on the adaption thing. if not, we do *no* adaption.
  2221. #
  2222. # (UPDATE for #8168: the above comment was not accurate, as we were
  2223. # still saying "do polymorphic" if we were using an auto-generated
  2224. # flattened JOIN for with_polymorphic.)
  2225. #
  2226. # this splits the behavior among the "regular" joined inheritance
  2227. # and single inheritance mappers, vs. the "weird / difficult"
  2228. # concrete and joined inh mappings that use a with_polymorphic of
  2229. # some kind or polymorphic_union.
  2230. #
  2231. # note we have some tests in test_polymorphic_rel that query against
  2232. # a subclass, then refer to the superclass that has a with_polymorphic
  2233. # on it (such as test_join_from_polymorphic_explicit_aliased_three).
  2234. # these tests actually adapt the polymorphic selectable (like, the
  2235. # UNION or the SELECT subquery with JOIN in it) to be just the simple
  2236. # subclass table. Hence even if we are a "plain" inheriting mapper
  2237. # but our base has a wpoly on it, we turn on adaption. This is a
  2238. # legacy case we should probably disable.
  2239. #
  2240. #
  2241. # UPDATE: simplified way more as of #8168. polymorphic adaption
  2242. # is turned off even if with_polymorphic is set, as long as there
  2243. # is no user-defined aliased selectable / subquery configured.
  2244. # this scales back the use of polymorphic adaption in practice
  2245. # to basically no cases except for concrete inheritance with a
  2246. # polymorphic base class.
  2247. #
  2248. return (
  2249. self._has_aliased_polymorphic_fromclause
  2250. or self._requires_row_aliasing
  2251. or (self.base_mapper._has_aliased_polymorphic_fromclause)
  2252. or self.base_mapper._requires_row_aliasing
  2253. )
  2254. @HasMemoized.memoized_attribute
  2255. def _with_polymorphic_mappers(self) -> Sequence[Mapper[Any]]:
  2256. self._check_configure()
  2257. if not self.with_polymorphic:
  2258. return []
  2259. return self._mappers_from_spec(*self.with_polymorphic)
  2260. @HasMemoized.memoized_attribute
  2261. def _post_inspect(self):
  2262. """This hook is invoked by attribute inspection.
  2263. E.g. when Query calls:
  2264. coercions.expect(roles.ColumnsClauseRole, ent, keep_inspect=True)
  2265. This allows the inspection process run a configure mappers hook.
  2266. """
  2267. self._check_configure()
  2268. @HasMemoized_ro_memoized_attribute
  2269. def _with_polymorphic_selectable(self) -> FromClause:
  2270. if not self.with_polymorphic:
  2271. return self.persist_selectable
  2272. spec, selectable = self.with_polymorphic
  2273. if selectable is not None:
  2274. return selectable
  2275. else:
  2276. return self._selectable_from_mappers(
  2277. self._mappers_from_spec(spec, selectable), False
  2278. )
  2279. with_polymorphic_mappers = _with_polymorphic_mappers
  2280. """The list of :class:`_orm.Mapper` objects included in the
  2281. default "polymorphic" query.
  2282. """
  2283. @HasMemoized_ro_memoized_attribute
  2284. def _insert_cols_evaluating_none(self):
  2285. return {
  2286. table: frozenset(
  2287. col for col in columns if col.type.should_evaluate_none
  2288. )
  2289. for table, columns in self._cols_by_table.items()
  2290. }
  2291. @HasMemoized.memoized_attribute
  2292. def _insert_cols_as_none(self):
  2293. return {
  2294. table: frozenset(
  2295. col.key
  2296. for col in columns
  2297. if not col.primary_key
  2298. and not col.server_default
  2299. and not col.default
  2300. and not col.type.should_evaluate_none
  2301. )
  2302. for table, columns in self._cols_by_table.items()
  2303. }
  2304. @HasMemoized.memoized_attribute
  2305. def _propkey_to_col(self):
  2306. return {
  2307. table: {self._columntoproperty[col].key: col for col in columns}
  2308. for table, columns in self._cols_by_table.items()
  2309. }
  2310. @HasMemoized.memoized_attribute
  2311. def _pk_keys_by_table(self):
  2312. return {
  2313. table: frozenset([col.key for col in pks])
  2314. for table, pks in self._pks_by_table.items()
  2315. }
  2316. @HasMemoized.memoized_attribute
  2317. def _pk_attr_keys_by_table(self):
  2318. return {
  2319. table: frozenset([self._columntoproperty[col].key for col in pks])
  2320. for table, pks in self._pks_by_table.items()
  2321. }
  2322. @HasMemoized.memoized_attribute
  2323. def _server_default_cols(
  2324. self,
  2325. ) -> Mapping[FromClause, FrozenSet[Column[Any]]]:
  2326. return {
  2327. table: frozenset(
  2328. [
  2329. col
  2330. for col in cast("Iterable[Column[Any]]", columns)
  2331. if col.server_default is not None
  2332. or (
  2333. col.default is not None
  2334. and col.default.is_clause_element
  2335. )
  2336. ]
  2337. )
  2338. for table, columns in self._cols_by_table.items()
  2339. }
  2340. @HasMemoized.memoized_attribute
  2341. def _server_onupdate_default_cols(
  2342. self,
  2343. ) -> Mapping[FromClause, FrozenSet[Column[Any]]]:
  2344. return {
  2345. table: frozenset(
  2346. [
  2347. col
  2348. for col in cast("Iterable[Column[Any]]", columns)
  2349. if col.server_onupdate is not None
  2350. or (
  2351. col.onupdate is not None
  2352. and col.onupdate.is_clause_element
  2353. )
  2354. ]
  2355. )
  2356. for table, columns in self._cols_by_table.items()
  2357. }
  2358. @HasMemoized.memoized_attribute
  2359. def _server_default_col_keys(self) -> Mapping[FromClause, FrozenSet[str]]:
  2360. return {
  2361. table: frozenset(col.key for col in cols if col.key is not None)
  2362. for table, cols in self._server_default_cols.items()
  2363. }
  2364. @HasMemoized.memoized_attribute
  2365. def _server_onupdate_default_col_keys(
  2366. self,
  2367. ) -> Mapping[FromClause, FrozenSet[str]]:
  2368. return {
  2369. table: frozenset(col.key for col in cols if col.key is not None)
  2370. for table, cols in self._server_onupdate_default_cols.items()
  2371. }
  2372. @HasMemoized.memoized_attribute
  2373. def _server_default_plus_onupdate_propkeys(self) -> Set[str]:
  2374. result: Set[str] = set()
  2375. col_to_property = self._columntoproperty
  2376. for table, columns in self._server_default_cols.items():
  2377. result.update(
  2378. col_to_property[col].key
  2379. for col in columns.intersection(col_to_property)
  2380. )
  2381. for table, columns in self._server_onupdate_default_cols.items():
  2382. result.update(
  2383. col_to_property[col].key
  2384. for col in columns.intersection(col_to_property)
  2385. )
  2386. return result
  2387. @HasMemoized.memoized_instancemethod
  2388. def __clause_element__(self):
  2389. annotations: Dict[str, Any] = {
  2390. "entity_namespace": self,
  2391. "parententity": self,
  2392. "parentmapper": self,
  2393. }
  2394. if self.persist_selectable is not self.local_table:
  2395. # joined table inheritance, with polymorphic selectable,
  2396. # etc.
  2397. annotations["dml_table"] = self.local_table._annotate(
  2398. {
  2399. "entity_namespace": self,
  2400. "parententity": self,
  2401. "parentmapper": self,
  2402. }
  2403. )._set_propagate_attrs(
  2404. {"compile_state_plugin": "orm", "plugin_subject": self}
  2405. )
  2406. return self.selectable._annotate(annotations)._set_propagate_attrs(
  2407. {"compile_state_plugin": "orm", "plugin_subject": self}
  2408. )
  2409. @util.memoized_property
  2410. def select_identity_token(self):
  2411. return (
  2412. expression.null()
  2413. ._annotate(
  2414. {
  2415. "entity_namespace": self,
  2416. "parententity": self,
  2417. "parentmapper": self,
  2418. "identity_token": True,
  2419. }
  2420. )
  2421. ._set_propagate_attrs(
  2422. {"compile_state_plugin": "orm", "plugin_subject": self}
  2423. )
  2424. )
  2425. @property
  2426. def selectable(self) -> FromClause:
  2427. """The :class:`_schema.FromClause` construct this
  2428. :class:`_orm.Mapper` selects from by default.
  2429. Normally, this is equivalent to :attr:`.persist_selectable`, unless
  2430. the ``with_polymorphic`` feature is in use, in which case the
  2431. full "polymorphic" selectable is returned.
  2432. """
  2433. return self._with_polymorphic_selectable
  2434. def _with_polymorphic_args(
  2435. self,
  2436. spec: Any = None,
  2437. selectable: Union[Literal[False, None], FromClause] = False,
  2438. innerjoin: bool = False,
  2439. ) -> Tuple[Sequence[Mapper[Any]], FromClause]:
  2440. if selectable not in (None, False):
  2441. selectable = coercions.expect(
  2442. roles.StrictFromClauseRole, selectable, allow_select=True
  2443. )
  2444. if self.with_polymorphic:
  2445. if not spec:
  2446. spec = self.with_polymorphic[0]
  2447. if selectable is False:
  2448. selectable = self.with_polymorphic[1]
  2449. elif selectable is False:
  2450. selectable = None
  2451. mappers = self._mappers_from_spec(spec, selectable)
  2452. if selectable is not None:
  2453. return mappers, selectable
  2454. else:
  2455. return mappers, self._selectable_from_mappers(mappers, innerjoin)
  2456. @HasMemoized.memoized_attribute
  2457. def _polymorphic_properties(self):
  2458. return list(
  2459. self._iterate_polymorphic_properties(
  2460. self._with_polymorphic_mappers
  2461. )
  2462. )
  2463. @property
  2464. def _all_column_expressions(self):
  2465. poly_properties = self._polymorphic_properties
  2466. adapter = self._polymorphic_adapter
  2467. return [
  2468. adapter.columns[c] if adapter else c
  2469. for prop in poly_properties
  2470. if isinstance(prop, properties.ColumnProperty)
  2471. and prop._renders_in_subqueries
  2472. for c in prop.columns
  2473. ]
  2474. def _columns_plus_keys(self, polymorphic_mappers=()):
  2475. if polymorphic_mappers:
  2476. poly_properties = self._iterate_polymorphic_properties(
  2477. polymorphic_mappers
  2478. )
  2479. else:
  2480. poly_properties = self._polymorphic_properties
  2481. return [
  2482. (prop.key, prop.columns[0])
  2483. for prop in poly_properties
  2484. if isinstance(prop, properties.ColumnProperty)
  2485. ]
  2486. @HasMemoized.memoized_attribute
  2487. def _polymorphic_adapter(self) -> Optional[orm_util.ORMAdapter]:
  2488. if self._has_aliased_polymorphic_fromclause:
  2489. return orm_util.ORMAdapter(
  2490. orm_util._TraceAdaptRole.MAPPER_POLYMORPHIC_ADAPTER,
  2491. self,
  2492. selectable=self.selectable,
  2493. equivalents=self._equivalent_columns,
  2494. limit_on_entity=False,
  2495. )
  2496. else:
  2497. return None
  2498. def _iterate_polymorphic_properties(self, mappers=None):
  2499. """Return an iterator of MapperProperty objects which will render into
  2500. a SELECT."""
  2501. if mappers is None:
  2502. mappers = self._with_polymorphic_mappers
  2503. if not mappers:
  2504. for c in self.iterate_properties:
  2505. yield c
  2506. else:
  2507. # in the polymorphic case, filter out discriminator columns
  2508. # from other mappers, as these are sometimes dependent on that
  2509. # mapper's polymorphic selectable (which we don't want rendered)
  2510. for c in util.unique_list(
  2511. chain(
  2512. *[
  2513. list(mapper.iterate_properties)
  2514. for mapper in [self] + mappers
  2515. ]
  2516. )
  2517. ):
  2518. if getattr(c, "_is_polymorphic_discriminator", False) and (
  2519. self.polymorphic_on is None
  2520. or c.columns[0] is not self.polymorphic_on
  2521. ):
  2522. continue
  2523. yield c
  2524. @HasMemoized.memoized_attribute
  2525. def attrs(self) -> util.ReadOnlyProperties[MapperProperty[Any]]:
  2526. """A namespace of all :class:`.MapperProperty` objects
  2527. associated this mapper.
  2528. This is an object that provides each property based on
  2529. its key name. For instance, the mapper for a
  2530. ``User`` class which has ``User.name`` attribute would
  2531. provide ``mapper.attrs.name``, which would be the
  2532. :class:`.ColumnProperty` representing the ``name``
  2533. column. The namespace object can also be iterated,
  2534. which would yield each :class:`.MapperProperty`.
  2535. :class:`_orm.Mapper` has several pre-filtered views
  2536. of this attribute which limit the types of properties
  2537. returned, including :attr:`.synonyms`, :attr:`.column_attrs`,
  2538. :attr:`.relationships`, and :attr:`.composites`.
  2539. .. warning::
  2540. The :attr:`_orm.Mapper.attrs` accessor namespace is an
  2541. instance of :class:`.OrderedProperties`. This is
  2542. a dictionary-like object which includes a small number of
  2543. named methods such as :meth:`.OrderedProperties.items`
  2544. and :meth:`.OrderedProperties.values`. When
  2545. accessing attributes dynamically, favor using the dict-access
  2546. scheme, e.g. ``mapper.attrs[somename]`` over
  2547. ``getattr(mapper.attrs, somename)`` to avoid name collisions.
  2548. .. seealso::
  2549. :attr:`_orm.Mapper.all_orm_descriptors`
  2550. """
  2551. self._check_configure()
  2552. return util.ReadOnlyProperties(self._props)
  2553. @HasMemoized.memoized_attribute
  2554. def all_orm_descriptors(self) -> util.ReadOnlyProperties[InspectionAttr]:
  2555. """A namespace of all :class:`.InspectionAttr` attributes associated
  2556. with the mapped class.
  2557. These attributes are in all cases Python :term:`descriptors`
  2558. associated with the mapped class or its superclasses.
  2559. This namespace includes attributes that are mapped to the class
  2560. as well as attributes declared by extension modules.
  2561. It includes any Python descriptor type that inherits from
  2562. :class:`.InspectionAttr`. This includes
  2563. :class:`.QueryableAttribute`, as well as extension types such as
  2564. :class:`.hybrid_property`, :class:`.hybrid_method` and
  2565. :class:`.AssociationProxy`.
  2566. To distinguish between mapped attributes and extension attributes,
  2567. the attribute :attr:`.InspectionAttr.extension_type` will refer
  2568. to a constant that distinguishes between different extension types.
  2569. The sorting of the attributes is based on the following rules:
  2570. 1. Iterate through the class and its superclasses in order from
  2571. subclass to superclass (i.e. iterate through ``cls.__mro__``)
  2572. 2. For each class, yield the attributes in the order in which they
  2573. appear in ``__dict__``, with the exception of those in step
  2574. 3 below. In Python 3.6 and above this ordering will be the
  2575. same as that of the class' construction, with the exception
  2576. of attributes that were added after the fact by the application
  2577. or the mapper.
  2578. 3. If a certain attribute key is also in the superclass ``__dict__``,
  2579. then it's included in the iteration for that class, and not the
  2580. class in which it first appeared.
  2581. The above process produces an ordering that is deterministic in terms
  2582. of the order in which attributes were assigned to the class.
  2583. .. versionchanged:: 1.3.19 ensured deterministic ordering for
  2584. :meth:`_orm.Mapper.all_orm_descriptors`.
  2585. When dealing with a :class:`.QueryableAttribute`, the
  2586. :attr:`.QueryableAttribute.property` attribute refers to the
  2587. :class:`.MapperProperty` property, which is what you get when
  2588. referring to the collection of mapped properties via
  2589. :attr:`_orm.Mapper.attrs`.
  2590. .. warning::
  2591. The :attr:`_orm.Mapper.all_orm_descriptors`
  2592. accessor namespace is an
  2593. instance of :class:`.OrderedProperties`. This is
  2594. a dictionary-like object which includes a small number of
  2595. named methods such as :meth:`.OrderedProperties.items`
  2596. and :meth:`.OrderedProperties.values`. When
  2597. accessing attributes dynamically, favor using the dict-access
  2598. scheme, e.g. ``mapper.all_orm_descriptors[somename]`` over
  2599. ``getattr(mapper.all_orm_descriptors, somename)`` to avoid name
  2600. collisions.
  2601. .. seealso::
  2602. :attr:`_orm.Mapper.attrs`
  2603. """
  2604. return util.ReadOnlyProperties(
  2605. dict(self.class_manager._all_sqla_attributes())
  2606. )
  2607. @HasMemoized.memoized_attribute
  2608. @util.preload_module("sqlalchemy.orm.descriptor_props")
  2609. def _pk_synonyms(self) -> Dict[str, str]:
  2610. """return a dictionary of {syn_attribute_name: pk_attr_name} for
  2611. all synonyms that refer to primary key columns
  2612. """
  2613. descriptor_props = util.preloaded.orm_descriptor_props
  2614. pk_keys = {prop.key for prop in self._identity_key_props}
  2615. return {
  2616. syn.key: syn.name
  2617. for k, syn in self._props.items()
  2618. if isinstance(syn, descriptor_props.SynonymProperty)
  2619. and syn.name in pk_keys
  2620. }
  2621. @HasMemoized.memoized_attribute
  2622. @util.preload_module("sqlalchemy.orm.descriptor_props")
  2623. def synonyms(self) -> util.ReadOnlyProperties[SynonymProperty[Any]]:
  2624. """Return a namespace of all :class:`.Synonym`
  2625. properties maintained by this :class:`_orm.Mapper`.
  2626. .. seealso::
  2627. :attr:`_orm.Mapper.attrs` - namespace of all
  2628. :class:`.MapperProperty`
  2629. objects.
  2630. """
  2631. descriptor_props = util.preloaded.orm_descriptor_props
  2632. return self._filter_properties(descriptor_props.SynonymProperty)
  2633. @property
  2634. def entity_namespace(self):
  2635. return self.class_
  2636. @HasMemoized.memoized_attribute
  2637. def column_attrs(self) -> util.ReadOnlyProperties[ColumnProperty[Any]]:
  2638. """Return a namespace of all :class:`.ColumnProperty`
  2639. properties maintained by this :class:`_orm.Mapper`.
  2640. .. seealso::
  2641. :attr:`_orm.Mapper.attrs` - namespace of all
  2642. :class:`.MapperProperty`
  2643. objects.
  2644. """
  2645. return self._filter_properties(properties.ColumnProperty)
  2646. @HasMemoized.memoized_attribute
  2647. @util.preload_module("sqlalchemy.orm.relationships")
  2648. def relationships(
  2649. self,
  2650. ) -> util.ReadOnlyProperties[RelationshipProperty[Any]]:
  2651. """A namespace of all :class:`.Relationship` properties
  2652. maintained by this :class:`_orm.Mapper`.
  2653. .. warning::
  2654. the :attr:`_orm.Mapper.relationships` accessor namespace is an
  2655. instance of :class:`.OrderedProperties`. This is
  2656. a dictionary-like object which includes a small number of
  2657. named methods such as :meth:`.OrderedProperties.items`
  2658. and :meth:`.OrderedProperties.values`. When
  2659. accessing attributes dynamically, favor using the dict-access
  2660. scheme, e.g. ``mapper.relationships[somename]`` over
  2661. ``getattr(mapper.relationships, somename)`` to avoid name
  2662. collisions.
  2663. .. seealso::
  2664. :attr:`_orm.Mapper.attrs` - namespace of all
  2665. :class:`.MapperProperty`
  2666. objects.
  2667. """
  2668. return self._filter_properties(
  2669. util.preloaded.orm_relationships.RelationshipProperty
  2670. )
  2671. @HasMemoized.memoized_attribute
  2672. @util.preload_module("sqlalchemy.orm.descriptor_props")
  2673. def composites(self) -> util.ReadOnlyProperties[CompositeProperty[Any]]:
  2674. """Return a namespace of all :class:`.Composite`
  2675. properties maintained by this :class:`_orm.Mapper`.
  2676. .. seealso::
  2677. :attr:`_orm.Mapper.attrs` - namespace of all
  2678. :class:`.MapperProperty`
  2679. objects.
  2680. """
  2681. return self._filter_properties(
  2682. util.preloaded.orm_descriptor_props.CompositeProperty
  2683. )
  2684. def _filter_properties(
  2685. self, type_: Type[_MP]
  2686. ) -> util.ReadOnlyProperties[_MP]:
  2687. self._check_configure()
  2688. return util.ReadOnlyProperties(
  2689. util.OrderedDict(
  2690. (k, v) for k, v in self._props.items() if isinstance(v, type_)
  2691. )
  2692. )
  2693. @HasMemoized.memoized_attribute
  2694. def _get_clause(self):
  2695. """create a "get clause" based on the primary key. this is used
  2696. by query.get() and many-to-one lazyloads to load this item
  2697. by primary key.
  2698. """
  2699. params = [
  2700. (
  2701. primary_key,
  2702. sql.bindparam("pk_%d" % idx, type_=primary_key.type),
  2703. )
  2704. for idx, primary_key in enumerate(self.primary_key, 1)
  2705. ]
  2706. return (
  2707. sql.and_(*[k == v for (k, v) in params]),
  2708. util.column_dict(params),
  2709. )
  2710. @HasMemoized.memoized_attribute
  2711. def _equivalent_columns(self) -> _EquivalentColumnMap:
  2712. """Create a map of all equivalent columns, based on
  2713. the determination of column pairs that are equated to
  2714. one another based on inherit condition. This is designed
  2715. to work with the queries that util.polymorphic_union
  2716. comes up with, which often don't include the columns from
  2717. the base table directly (including the subclass table columns
  2718. only).
  2719. The resulting structure is a dictionary of columns mapped
  2720. to lists of equivalent columns, e.g.::
  2721. {
  2722. tablea.col1:
  2723. {tableb.col1, tablec.col1},
  2724. tablea.col2:
  2725. {tabled.col2}
  2726. }
  2727. """
  2728. result: _EquivalentColumnMap = {}
  2729. def visit_binary(binary):
  2730. if binary.operator == operators.eq:
  2731. if binary.left in result:
  2732. result[binary.left].add(binary.right)
  2733. else:
  2734. result[binary.left] = {binary.right}
  2735. if binary.right in result:
  2736. result[binary.right].add(binary.left)
  2737. else:
  2738. result[binary.right] = {binary.left}
  2739. for mapper in self.base_mapper.self_and_descendants:
  2740. if mapper.inherit_condition is not None:
  2741. visitors.traverse(
  2742. mapper.inherit_condition, {}, {"binary": visit_binary}
  2743. )
  2744. return result
  2745. def _is_userland_descriptor(self, assigned_name: str, obj: Any) -> bool:
  2746. if isinstance(
  2747. obj,
  2748. (
  2749. _MappedAttribute,
  2750. instrumentation.ClassManager,
  2751. expression.ColumnElement,
  2752. ),
  2753. ):
  2754. return False
  2755. else:
  2756. return assigned_name not in self._dataclass_fields
  2757. @HasMemoized.memoized_attribute
  2758. def _dataclass_fields(self):
  2759. return [f.name for f in util.dataclass_fields(self.class_)]
  2760. def _should_exclude(self, name, assigned_name, local, column):
  2761. """determine whether a particular property should be implicitly
  2762. present on the class.
  2763. This occurs when properties are propagated from an inherited class, or
  2764. are applied from the columns present in the mapped table.
  2765. """
  2766. if column is not None and sql_base._never_select_column(column):
  2767. return True
  2768. # check for class-bound attributes and/or descriptors,
  2769. # either local or from an inherited class
  2770. # ignore dataclass field default values
  2771. if local:
  2772. if self.class_.__dict__.get(
  2773. assigned_name, None
  2774. ) is not None and self._is_userland_descriptor(
  2775. assigned_name, self.class_.__dict__[assigned_name]
  2776. ):
  2777. return True
  2778. else:
  2779. attr = self.class_manager._get_class_attr_mro(assigned_name, None)
  2780. if attr is not None and self._is_userland_descriptor(
  2781. assigned_name, attr
  2782. ):
  2783. return True
  2784. if (
  2785. self.include_properties is not None
  2786. and name not in self.include_properties
  2787. and (column is None or column not in self.include_properties)
  2788. ):
  2789. self._log("not including property %s" % (name))
  2790. return True
  2791. if self.exclude_properties is not None and (
  2792. name in self.exclude_properties
  2793. or (column is not None and column in self.exclude_properties)
  2794. ):
  2795. self._log("excluding property %s" % (name))
  2796. return True
  2797. return False
  2798. def common_parent(self, other: Mapper[Any]) -> bool:
  2799. """Return true if the given mapper shares a
  2800. common inherited parent as this mapper."""
  2801. return self.base_mapper is other.base_mapper
  2802. def is_sibling(self, other: Mapper[Any]) -> bool:
  2803. """return true if the other mapper is an inheriting sibling to this
  2804. one. common parent but different branch
  2805. """
  2806. return (
  2807. self.base_mapper is other.base_mapper
  2808. and not self.isa(other)
  2809. and not other.isa(self)
  2810. )
  2811. def _canload(
  2812. self, state: InstanceState[Any], allow_subtypes: bool
  2813. ) -> bool:
  2814. s = self.primary_mapper()
  2815. if self.polymorphic_on is not None or allow_subtypes:
  2816. return _state_mapper(state).isa(s)
  2817. else:
  2818. return _state_mapper(state) is s
  2819. def isa(self, other: Mapper[Any]) -> bool:
  2820. """Return True if the this mapper inherits from the given mapper."""
  2821. m: Optional[Mapper[Any]] = self
  2822. while m and m is not other:
  2823. m = m.inherits
  2824. return bool(m)
  2825. def iterate_to_root(self) -> Iterator[Mapper[Any]]:
  2826. m: Optional[Mapper[Any]] = self
  2827. while m:
  2828. yield m
  2829. m = m.inherits
  2830. @HasMemoized.memoized_attribute
  2831. def self_and_descendants(self) -> Sequence[Mapper[Any]]:
  2832. """The collection including this mapper and all descendant mappers.
  2833. This includes not just the immediately inheriting mappers but
  2834. all their inheriting mappers as well.
  2835. """
  2836. descendants = []
  2837. stack = deque([self])
  2838. while stack:
  2839. item = stack.popleft()
  2840. descendants.append(item)
  2841. stack.extend(item._inheriting_mappers)
  2842. return util.WeakSequence(descendants)
  2843. def polymorphic_iterator(self) -> Iterator[Mapper[Any]]:
  2844. """Iterate through the collection including this mapper and
  2845. all descendant mappers.
  2846. This includes not just the immediately inheriting mappers but
  2847. all their inheriting mappers as well.
  2848. To iterate through an entire hierarchy, use
  2849. ``mapper.base_mapper.polymorphic_iterator()``.
  2850. """
  2851. return iter(self.self_and_descendants)
  2852. def primary_mapper(self) -> Mapper[Any]:
  2853. """Return the primary mapper corresponding to this mapper's class key
  2854. (class)."""
  2855. return self.class_manager.mapper
  2856. @property
  2857. def primary_base_mapper(self) -> Mapper[Any]:
  2858. return self.class_manager.mapper.base_mapper
  2859. def _result_has_identity_key(self, result, adapter=None):
  2860. pk_cols: Sequence[ColumnClause[Any]] = self.primary_key
  2861. if adapter:
  2862. pk_cols = [adapter.columns[c] for c in pk_cols]
  2863. rk = result.keys()
  2864. for col in pk_cols:
  2865. if col not in rk:
  2866. return False
  2867. else:
  2868. return True
  2869. def identity_key_from_row(
  2870. self,
  2871. row: Optional[Union[Row[Any], RowMapping]],
  2872. identity_token: Optional[Any] = None,
  2873. adapter: Optional[ORMAdapter] = None,
  2874. ) -> _IdentityKeyType[_O]:
  2875. """Return an identity-map key for use in storing/retrieving an
  2876. item from the identity map.
  2877. :param row: A :class:`.Row` or :class:`.RowMapping` produced from a
  2878. result set that selected from the ORM mapped primary key columns.
  2879. .. versionchanged:: 2.0
  2880. :class:`.Row` or :class:`.RowMapping` are accepted
  2881. for the "row" argument
  2882. """
  2883. pk_cols: Sequence[ColumnClause[Any]] = self.primary_key
  2884. if adapter:
  2885. pk_cols = [adapter.columns[c] for c in pk_cols]
  2886. if hasattr(row, "_mapping"):
  2887. mapping = row._mapping # type: ignore
  2888. else:
  2889. mapping = cast("Mapping[Any, Any]", row)
  2890. return (
  2891. self._identity_class,
  2892. tuple(mapping[column] for column in pk_cols), # type: ignore
  2893. identity_token,
  2894. )
  2895. def identity_key_from_primary_key(
  2896. self,
  2897. primary_key: Tuple[Any, ...],
  2898. identity_token: Optional[Any] = None,
  2899. ) -> _IdentityKeyType[_O]:
  2900. """Return an identity-map key for use in storing/retrieving an
  2901. item from an identity map.
  2902. :param primary_key: A list of values indicating the identifier.
  2903. """
  2904. return (
  2905. self._identity_class,
  2906. tuple(primary_key),
  2907. identity_token,
  2908. )
  2909. def identity_key_from_instance(self, instance: _O) -> _IdentityKeyType[_O]:
  2910. """Return the identity key for the given instance, based on
  2911. its primary key attributes.
  2912. If the instance's state is expired, calling this method
  2913. will result in a database check to see if the object has been deleted.
  2914. If the row no longer exists,
  2915. :class:`~sqlalchemy.orm.exc.ObjectDeletedError` is raised.
  2916. This value is typically also found on the instance state under the
  2917. attribute name `key`.
  2918. """
  2919. state = attributes.instance_state(instance)
  2920. return self._identity_key_from_state(state, PassiveFlag.PASSIVE_OFF)
  2921. def _identity_key_from_state(
  2922. self,
  2923. state: InstanceState[_O],
  2924. passive: PassiveFlag = PassiveFlag.PASSIVE_RETURN_NO_VALUE,
  2925. ) -> _IdentityKeyType[_O]:
  2926. dict_ = state.dict
  2927. manager = state.manager
  2928. return (
  2929. self._identity_class,
  2930. tuple(
  2931. [
  2932. manager[prop.key].impl.get(state, dict_, passive)
  2933. for prop in self._identity_key_props
  2934. ]
  2935. ),
  2936. state.identity_token,
  2937. )
  2938. def primary_key_from_instance(self, instance: _O) -> Tuple[Any, ...]:
  2939. """Return the list of primary key values for the given
  2940. instance.
  2941. If the instance's state is expired, calling this method
  2942. will result in a database check to see if the object has been deleted.
  2943. If the row no longer exists,
  2944. :class:`~sqlalchemy.orm.exc.ObjectDeletedError` is raised.
  2945. """
  2946. state = attributes.instance_state(instance)
  2947. identity_key = self._identity_key_from_state(
  2948. state, PassiveFlag.PASSIVE_OFF
  2949. )
  2950. return identity_key[1]
  2951. @HasMemoized.memoized_attribute
  2952. def _persistent_sortkey_fn(self):
  2953. key_fns = [col.type.sort_key_function for col in self.primary_key]
  2954. if set(key_fns).difference([None]):
  2955. def key(state):
  2956. return tuple(
  2957. key_fn(val) if key_fn is not None else val
  2958. for key_fn, val in zip(key_fns, state.key[1])
  2959. )
  2960. else:
  2961. def key(state):
  2962. return state.key[1]
  2963. return key
  2964. @HasMemoized.memoized_attribute
  2965. def _identity_key_props(self):
  2966. return [self._columntoproperty[col] for col in self.primary_key]
  2967. @HasMemoized.memoized_attribute
  2968. def _all_pk_cols(self):
  2969. collection: Set[ColumnClause[Any]] = set()
  2970. for table in self.tables:
  2971. collection.update(self._pks_by_table[table])
  2972. return collection
  2973. @HasMemoized.memoized_attribute
  2974. def _should_undefer_in_wildcard(self):
  2975. cols: Set[ColumnElement[Any]] = set(self.primary_key)
  2976. if self.polymorphic_on is not None:
  2977. cols.add(self.polymorphic_on)
  2978. return cols
  2979. @HasMemoized.memoized_attribute
  2980. def _primary_key_propkeys(self):
  2981. return {self._columntoproperty[col].key for col in self._all_pk_cols}
  2982. def _get_state_attr_by_column(
  2983. self,
  2984. state: InstanceState[_O],
  2985. dict_: _InstanceDict,
  2986. column: ColumnElement[Any],
  2987. passive: PassiveFlag = PassiveFlag.PASSIVE_RETURN_NO_VALUE,
  2988. ) -> Any:
  2989. prop = self._columntoproperty[column]
  2990. return state.manager[prop.key].impl.get(state, dict_, passive=passive)
  2991. def _set_committed_state_attr_by_column(self, state, dict_, column, value):
  2992. prop = self._columntoproperty[column]
  2993. state.manager[prop.key].impl.set_committed_value(state, dict_, value)
  2994. def _set_state_attr_by_column(self, state, dict_, column, value):
  2995. prop = self._columntoproperty[column]
  2996. state.manager[prop.key].impl.set(state, dict_, value, None)
  2997. def _get_committed_attr_by_column(self, obj, column):
  2998. state = attributes.instance_state(obj)
  2999. dict_ = attributes.instance_dict(obj)
  3000. return self._get_committed_state_attr_by_column(
  3001. state, dict_, column, passive=PassiveFlag.PASSIVE_OFF
  3002. )
  3003. def _get_committed_state_attr_by_column(
  3004. self, state, dict_, column, passive=PassiveFlag.PASSIVE_RETURN_NO_VALUE
  3005. ):
  3006. prop = self._columntoproperty[column]
  3007. return state.manager[prop.key].impl.get_committed_value(
  3008. state, dict_, passive=passive
  3009. )
  3010. def _optimized_get_statement(self, state, attribute_names):
  3011. """assemble a WHERE clause which retrieves a given state by primary
  3012. key, using a minimized set of tables.
  3013. Applies to a joined-table inheritance mapper where the
  3014. requested attribute names are only present on joined tables,
  3015. not the base table. The WHERE clause attempts to include
  3016. only those tables to minimize joins.
  3017. """
  3018. props = self._props
  3019. col_attribute_names = set(attribute_names).intersection(
  3020. state.mapper.column_attrs.keys()
  3021. )
  3022. tables: Set[FromClause] = set(
  3023. chain(
  3024. *[
  3025. sql_util.find_tables(c, check_columns=True)
  3026. for key in col_attribute_names
  3027. for c in props[key].columns
  3028. ]
  3029. )
  3030. )
  3031. if self.base_mapper.local_table in tables:
  3032. return None
  3033. def visit_binary(binary):
  3034. leftcol = binary.left
  3035. rightcol = binary.right
  3036. if leftcol is None or rightcol is None:
  3037. return
  3038. if leftcol.table not in tables:
  3039. leftval = self._get_committed_state_attr_by_column(
  3040. state,
  3041. state.dict,
  3042. leftcol,
  3043. passive=PassiveFlag.PASSIVE_NO_INITIALIZE,
  3044. )
  3045. if leftval in orm_util._none_set:
  3046. raise _OptGetColumnsNotAvailable()
  3047. binary.left = sql.bindparam(
  3048. None, leftval, type_=binary.right.type
  3049. )
  3050. elif rightcol.table not in tables:
  3051. rightval = self._get_committed_state_attr_by_column(
  3052. state,
  3053. state.dict,
  3054. rightcol,
  3055. passive=PassiveFlag.PASSIVE_NO_INITIALIZE,
  3056. )
  3057. if rightval in orm_util._none_set:
  3058. raise _OptGetColumnsNotAvailable()
  3059. binary.right = sql.bindparam(
  3060. None, rightval, type_=binary.right.type
  3061. )
  3062. allconds: List[ColumnElement[bool]] = []
  3063. start = False
  3064. # as of #7507, from the lowest base table on upwards,
  3065. # we include all intermediary tables.
  3066. for mapper in reversed(list(self.iterate_to_root())):
  3067. if mapper.local_table in tables:
  3068. start = True
  3069. elif not isinstance(mapper.local_table, expression.TableClause):
  3070. return None
  3071. if start and not mapper.single:
  3072. assert mapper.inherits
  3073. assert not mapper.concrete
  3074. assert mapper.inherit_condition is not None
  3075. allconds.append(mapper.inherit_condition)
  3076. tables.add(mapper.local_table)
  3077. # only the bottom table needs its criteria to be altered to fit
  3078. # the primary key ident - the rest of the tables upwards to the
  3079. # descendant-most class should all be present and joined to each
  3080. # other.
  3081. try:
  3082. _traversed = visitors.cloned_traverse(
  3083. allconds[0], {}, {"binary": visit_binary}
  3084. )
  3085. except _OptGetColumnsNotAvailable:
  3086. return None
  3087. else:
  3088. allconds[0] = _traversed
  3089. cond = sql.and_(*allconds)
  3090. cols = []
  3091. for key in col_attribute_names:
  3092. cols.extend(props[key].columns)
  3093. return (
  3094. sql.select(*cols)
  3095. .where(cond)
  3096. .set_label_style(LABEL_STYLE_TABLENAME_PLUS_COL)
  3097. )
  3098. def _iterate_to_target_viawpoly(self, mapper):
  3099. if self.isa(mapper):
  3100. prev = self
  3101. for m in self.iterate_to_root():
  3102. yield m
  3103. if m is not prev and prev not in m._with_polymorphic_mappers:
  3104. break
  3105. prev = m
  3106. if m is mapper:
  3107. break
  3108. @HasMemoized.memoized_attribute
  3109. def _would_selectinload_combinations_cache(self):
  3110. return {}
  3111. def _would_selectin_load_only_from_given_mapper(self, super_mapper):
  3112. """return True if this mapper would "selectin" polymorphic load based
  3113. on the given super mapper, and not from a setting from a subclass.
  3114. given::
  3115. class A:
  3116. ...
  3117. class B(A):
  3118. __mapper_args__ = {"polymorphic_load": "selectin"}
  3119. class C(B):
  3120. ...
  3121. class D(B):
  3122. __mapper_args__ = {"polymorphic_load": "selectin"}
  3123. ``inspect(C)._would_selectin_load_only_from_given_mapper(inspect(B))``
  3124. returns True, because C does selectin loading because of B's setting.
  3125. OTOH, ``inspect(D)
  3126. ._would_selectin_load_only_from_given_mapper(inspect(B))``
  3127. returns False, because D does selectin loading because of its own
  3128. setting; when we are doing a selectin poly load from B, we want to
  3129. filter out D because it would already have its own selectin poly load
  3130. set up separately.
  3131. Added as part of #9373.
  3132. """
  3133. cache = self._would_selectinload_combinations_cache
  3134. try:
  3135. return cache[super_mapper]
  3136. except KeyError:
  3137. pass
  3138. # assert that given object is a supermapper, meaning we already
  3139. # strong reference it directly or indirectly. this allows us
  3140. # to not worry that we are creating new strongrefs to unrelated
  3141. # mappers or other objects.
  3142. assert self.isa(super_mapper)
  3143. mapper = super_mapper
  3144. for m in self._iterate_to_target_viawpoly(mapper):
  3145. if m.polymorphic_load == "selectin":
  3146. retval = m is super_mapper
  3147. break
  3148. else:
  3149. retval = False
  3150. cache[super_mapper] = retval
  3151. return retval
  3152. def _should_selectin_load(self, enabled_via_opt, polymorphic_from):
  3153. if not enabled_via_opt:
  3154. # common case, takes place for all polymorphic loads
  3155. mapper = polymorphic_from
  3156. for m in self._iterate_to_target_viawpoly(mapper):
  3157. if m.polymorphic_load == "selectin":
  3158. return m
  3159. else:
  3160. # uncommon case, selectin load options were used
  3161. enabled_via_opt = set(enabled_via_opt)
  3162. enabled_via_opt_mappers = {e.mapper: e for e in enabled_via_opt}
  3163. for entity in enabled_via_opt.union([polymorphic_from]):
  3164. mapper = entity.mapper
  3165. for m in self._iterate_to_target_viawpoly(mapper):
  3166. if (
  3167. m.polymorphic_load == "selectin"
  3168. or m in enabled_via_opt_mappers
  3169. ):
  3170. return enabled_via_opt_mappers.get(m, m)
  3171. return None
  3172. @util.preload_module("sqlalchemy.orm.strategy_options")
  3173. def _subclass_load_via_in(self, entity, polymorphic_from):
  3174. """Assemble a that can load the columns local to
  3175. this subclass as a SELECT with IN.
  3176. """
  3177. strategy_options = util.preloaded.orm_strategy_options
  3178. assert self.inherits
  3179. if self.polymorphic_on is not None:
  3180. polymorphic_prop = self._columntoproperty[self.polymorphic_on]
  3181. keep_props = set([polymorphic_prop] + self._identity_key_props)
  3182. else:
  3183. keep_props = set(self._identity_key_props)
  3184. disable_opt = strategy_options.Load(entity)
  3185. enable_opt = strategy_options.Load(entity)
  3186. classes_to_include = {self}
  3187. m: Optional[Mapper[Any]] = self.inherits
  3188. while (
  3189. m is not None
  3190. and m is not polymorphic_from
  3191. and m.polymorphic_load == "selectin"
  3192. ):
  3193. classes_to_include.add(m)
  3194. m = m.inherits
  3195. for prop in self.attrs:
  3196. # skip prop keys that are not instrumented on the mapped class.
  3197. # this is primarily the "_sa_polymorphic_on" property that gets
  3198. # created for an ad-hoc polymorphic_on SQL expression, issue #8704
  3199. if prop.key not in self.class_manager:
  3200. continue
  3201. if prop.parent in classes_to_include or prop in keep_props:
  3202. # "enable" options, to turn on the properties that we want to
  3203. # load by default (subject to options from the query)
  3204. if not isinstance(prop, StrategizedProperty):
  3205. continue
  3206. enable_opt = enable_opt._set_generic_strategy(
  3207. # convert string name to an attribute before passing
  3208. # to loader strategy. note this must be in terms
  3209. # of given entity, such as AliasedClass, etc.
  3210. (getattr(entity.entity_namespace, prop.key),),
  3211. dict(prop.strategy_key),
  3212. _reconcile_to_other=True,
  3213. )
  3214. else:
  3215. # "disable" options, to turn off the properties from the
  3216. # superclass that we *don't* want to load, applied after
  3217. # the options from the query to override them
  3218. disable_opt = disable_opt._set_generic_strategy(
  3219. # convert string name to an attribute before passing
  3220. # to loader strategy. note this must be in terms
  3221. # of given entity, such as AliasedClass, etc.
  3222. (getattr(entity.entity_namespace, prop.key),),
  3223. {"do_nothing": True},
  3224. _reconcile_to_other=False,
  3225. )
  3226. primary_key = [
  3227. sql_util._deep_annotate(pk, {"_orm_adapt": True})
  3228. for pk in self.primary_key
  3229. ]
  3230. in_expr: ColumnElement[Any]
  3231. if len(primary_key) > 1:
  3232. in_expr = sql.tuple_(*primary_key)
  3233. else:
  3234. in_expr = primary_key[0]
  3235. if entity.is_aliased_class:
  3236. assert entity.mapper is self
  3237. q = sql.select(entity).set_label_style(
  3238. LABEL_STYLE_TABLENAME_PLUS_COL
  3239. )
  3240. in_expr = entity._adapter.traverse(in_expr)
  3241. primary_key = [entity._adapter.traverse(k) for k in primary_key]
  3242. q = q.where(
  3243. in_expr.in_(sql.bindparam("primary_keys", expanding=True))
  3244. ).order_by(*primary_key)
  3245. else:
  3246. q = sql.select(self).set_label_style(
  3247. LABEL_STYLE_TABLENAME_PLUS_COL
  3248. )
  3249. q = q.where(
  3250. in_expr.in_(sql.bindparam("primary_keys", expanding=True))
  3251. ).order_by(*primary_key)
  3252. return q, enable_opt, disable_opt
  3253. @HasMemoized.memoized_attribute
  3254. def _subclass_load_via_in_mapper(self):
  3255. # the default is loading this mapper against the basemost mapper
  3256. return self._subclass_load_via_in(self, self.base_mapper)
  3257. def cascade_iterator(
  3258. self,
  3259. type_: str,
  3260. state: InstanceState[_O],
  3261. halt_on: Optional[Callable[[InstanceState[Any]], bool]] = None,
  3262. ) -> Iterator[
  3263. Tuple[object, Mapper[Any], InstanceState[Any], _InstanceDict]
  3264. ]:
  3265. r"""Iterate each element and its mapper in an object graph,
  3266. for all relationships that meet the given cascade rule.
  3267. :param type\_:
  3268. The name of the cascade rule (i.e. ``"save-update"``, ``"delete"``,
  3269. etc.).
  3270. .. note:: the ``"all"`` cascade is not accepted here. For a generic
  3271. object traversal function, see :ref:`faq_walk_objects`.
  3272. :param state:
  3273. The lead InstanceState. child items will be processed per
  3274. the relationships defined for this object's mapper.
  3275. :return: the method yields individual object instances.
  3276. .. seealso::
  3277. :ref:`unitofwork_cascades`
  3278. :ref:`faq_walk_objects` - illustrates a generic function to
  3279. traverse all objects without relying on cascades.
  3280. """
  3281. visited_states: Set[InstanceState[Any]] = set()
  3282. prp, mpp = object(), object()
  3283. assert state.mapper.isa(self)
  3284. # this is actually a recursive structure, fully typing it seems
  3285. # a little too difficult for what it's worth here
  3286. visitables: Deque[
  3287. Tuple[
  3288. Deque[Any],
  3289. object,
  3290. Optional[InstanceState[Any]],
  3291. Optional[_InstanceDict],
  3292. ]
  3293. ]
  3294. visitables = deque(
  3295. [(deque(state.mapper._props.values()), prp, state, state.dict)]
  3296. )
  3297. while visitables:
  3298. iterator, item_type, parent_state, parent_dict = visitables[-1]
  3299. if not iterator:
  3300. visitables.pop()
  3301. continue
  3302. if item_type is prp:
  3303. prop = iterator.popleft()
  3304. if not prop.cascade or type_ not in prop.cascade:
  3305. continue
  3306. assert parent_state is not None
  3307. assert parent_dict is not None
  3308. queue = deque(
  3309. prop.cascade_iterator(
  3310. type_,
  3311. parent_state,
  3312. parent_dict,
  3313. visited_states,
  3314. halt_on,
  3315. )
  3316. )
  3317. if queue:
  3318. visitables.append((queue, mpp, None, None))
  3319. elif item_type is mpp:
  3320. (
  3321. instance,
  3322. instance_mapper,
  3323. corresponding_state,
  3324. corresponding_dict,
  3325. ) = iterator.popleft()
  3326. yield (
  3327. instance,
  3328. instance_mapper,
  3329. corresponding_state,
  3330. corresponding_dict,
  3331. )
  3332. visitables.append(
  3333. (
  3334. deque(instance_mapper._props.values()),
  3335. prp,
  3336. corresponding_state,
  3337. corresponding_dict,
  3338. )
  3339. )
  3340. @HasMemoized.memoized_attribute
  3341. def _compiled_cache(self):
  3342. return util.LRUCache(self._compiled_cache_size)
  3343. @HasMemoized.memoized_attribute
  3344. def _multiple_persistence_tables(self):
  3345. return len(self.tables) > 1
  3346. @HasMemoized.memoized_attribute
  3347. def _sorted_tables(self):
  3348. table_to_mapper: Dict[TableClause, Mapper[Any]] = {}
  3349. for mapper in self.base_mapper.self_and_descendants:
  3350. for t in mapper.tables:
  3351. table_to_mapper.setdefault(t, mapper)
  3352. extra_dependencies = []
  3353. for table, mapper in table_to_mapper.items():
  3354. super_ = mapper.inherits
  3355. if super_:
  3356. extra_dependencies.extend(
  3357. [(super_table, table) for super_table in super_.tables]
  3358. )
  3359. def skip(fk):
  3360. # attempt to skip dependencies that are not
  3361. # significant to the inheritance chain
  3362. # for two tables that are related by inheritance.
  3363. # while that dependency may be important, it's technically
  3364. # not what we mean to sort on here.
  3365. parent = table_to_mapper.get(fk.parent.table)
  3366. dep = table_to_mapper.get(fk.column.table)
  3367. if (
  3368. parent is not None
  3369. and dep is not None
  3370. and dep is not parent
  3371. and dep.inherit_condition is not None
  3372. ):
  3373. cols = set(sql_util._find_columns(dep.inherit_condition))
  3374. if parent.inherit_condition is not None:
  3375. cols = cols.union(
  3376. sql_util._find_columns(parent.inherit_condition)
  3377. )
  3378. return fk.parent not in cols and fk.column not in cols
  3379. else:
  3380. return fk.parent not in cols
  3381. return False
  3382. sorted_ = sql_util.sort_tables(
  3383. table_to_mapper,
  3384. skip_fn=skip,
  3385. extra_dependencies=extra_dependencies,
  3386. )
  3387. ret = util.OrderedDict()
  3388. for t in sorted_:
  3389. ret[t] = table_to_mapper[t]
  3390. return ret
  3391. def _memo(self, key: Any, callable_: Callable[[], _T]) -> _T:
  3392. if key in self._memoized_values:
  3393. return cast(_T, self._memoized_values[key])
  3394. else:
  3395. self._memoized_values[key] = value = callable_()
  3396. return value
  3397. @util.memoized_property
  3398. def _table_to_equated(self):
  3399. """memoized map of tables to collections of columns to be
  3400. synchronized upwards to the base mapper."""
  3401. result: util.defaultdict[
  3402. Table,
  3403. List[
  3404. Tuple[
  3405. Mapper[Any],
  3406. List[Tuple[ColumnElement[Any], ColumnElement[Any]]],
  3407. ]
  3408. ],
  3409. ] = util.defaultdict(list)
  3410. def set_union(x, y):
  3411. return x.union(y)
  3412. for table in self._sorted_tables:
  3413. cols = set(table.c)
  3414. for m in self.iterate_to_root():
  3415. if m._inherits_equated_pairs and cols.intersection(
  3416. reduce(
  3417. set_union,
  3418. [l.proxy_set for l, r in m._inherits_equated_pairs],
  3419. )
  3420. ):
  3421. result[table].append((m, m._inherits_equated_pairs))
  3422. return result
  3423. class _OptGetColumnsNotAvailable(Exception):
  3424. pass
  3425. def configure_mappers() -> None:
  3426. """Initialize the inter-mapper relationships of all mappers that
  3427. have been constructed thus far across all :class:`_orm.registry`
  3428. collections.
  3429. The configure step is used to reconcile and initialize the
  3430. :func:`_orm.relationship` linkages between mapped classes, as well as to
  3431. invoke configuration events such as the
  3432. :meth:`_orm.MapperEvents.before_configured` and
  3433. :meth:`_orm.MapperEvents.after_configured`, which may be used by ORM
  3434. extensions or user-defined extension hooks.
  3435. Mapper configuration is normally invoked automatically, the first time
  3436. mappings from a particular :class:`_orm.registry` are used, as well as
  3437. whenever mappings are used and additional not-yet-configured mappers have
  3438. been constructed. The automatic configuration process however is local only
  3439. to the :class:`_orm.registry` involving the target mapper and any related
  3440. :class:`_orm.registry` objects which it may depend on; this is
  3441. equivalent to invoking the :meth:`_orm.registry.configure` method
  3442. on a particular :class:`_orm.registry`.
  3443. By contrast, the :func:`_orm.configure_mappers` function will invoke the
  3444. configuration process on all :class:`_orm.registry` objects that
  3445. exist in memory, and may be useful for scenarios where many individual
  3446. :class:`_orm.registry` objects that are nonetheless interrelated are
  3447. in use.
  3448. .. versionchanged:: 1.4
  3449. As of SQLAlchemy 1.4.0b2, this function works on a
  3450. per-:class:`_orm.registry` basis, locating all :class:`_orm.registry`
  3451. objects present and invoking the :meth:`_orm.registry.configure` method
  3452. on each. The :meth:`_orm.registry.configure` method may be preferred to
  3453. limit the configuration of mappers to those local to a particular
  3454. :class:`_orm.registry` and/or declarative base class.
  3455. Points at which automatic configuration is invoked include when a mapped
  3456. class is instantiated into an instance, as well as when ORM queries
  3457. are emitted using :meth:`.Session.query` or :meth:`_orm.Session.execute`
  3458. with an ORM-enabled statement.
  3459. The mapper configure process, whether invoked by
  3460. :func:`_orm.configure_mappers` or from :meth:`_orm.registry.configure`,
  3461. provides several event hooks that can be used to augment the mapper
  3462. configuration step. These hooks include:
  3463. * :meth:`.MapperEvents.before_configured` - called once before
  3464. :func:`.configure_mappers` or :meth:`_orm.registry.configure` does any
  3465. work; this can be used to establish additional options, properties, or
  3466. related mappings before the operation proceeds.
  3467. * :meth:`.MapperEvents.mapper_configured` - called as each individual
  3468. :class:`_orm.Mapper` is configured within the process; will include all
  3469. mapper state except for backrefs set up by other mappers that are still
  3470. to be configured.
  3471. * :meth:`.MapperEvents.after_configured` - called once after
  3472. :func:`.configure_mappers` or :meth:`_orm.registry.configure` is
  3473. complete; at this stage, all :class:`_orm.Mapper` objects that fall
  3474. within the scope of the configuration operation will be fully configured.
  3475. Note that the calling application may still have other mappings that
  3476. haven't been produced yet, such as if they are in modules as yet
  3477. unimported, and may also have mappings that are still to be configured,
  3478. if they are in other :class:`_orm.registry` collections not part of the
  3479. current scope of configuration.
  3480. """
  3481. _configure_registries(_all_registries(), cascade=True)
  3482. def _configure_registries(
  3483. registries: Set[_RegistryType], cascade: bool
  3484. ) -> None:
  3485. for reg in registries:
  3486. if reg._new_mappers:
  3487. break
  3488. else:
  3489. return
  3490. with _CONFIGURE_MUTEX:
  3491. global _already_compiling
  3492. if _already_compiling:
  3493. return
  3494. _already_compiling = True
  3495. try:
  3496. # double-check inside mutex
  3497. for reg in registries:
  3498. if reg._new_mappers:
  3499. break
  3500. else:
  3501. return
  3502. Mapper.dispatch._for_class(Mapper).before_configured() # type: ignore # noqa: E501
  3503. # initialize properties on all mappers
  3504. # note that _mapper_registry is unordered, which
  3505. # may randomly conceal/reveal issues related to
  3506. # the order of mapper compilation
  3507. _do_configure_registries(registries, cascade)
  3508. finally:
  3509. _already_compiling = False
  3510. Mapper.dispatch._for_class(Mapper).after_configured() # type: ignore
  3511. @util.preload_module("sqlalchemy.orm.decl_api")
  3512. def _do_configure_registries(
  3513. registries: Set[_RegistryType], cascade: bool
  3514. ) -> None:
  3515. registry = util.preloaded.orm_decl_api.registry
  3516. orig = set(registries)
  3517. for reg in registry._recurse_with_dependencies(registries):
  3518. has_skip = False
  3519. for mapper in reg._mappers_to_configure():
  3520. run_configure = None
  3521. for fn in mapper.dispatch.before_mapper_configured:
  3522. run_configure = fn(mapper, mapper.class_)
  3523. if run_configure is EXT_SKIP:
  3524. has_skip = True
  3525. break
  3526. if run_configure is EXT_SKIP:
  3527. continue
  3528. if getattr(mapper, "_configure_failed", False):
  3529. e = sa_exc.InvalidRequestError(
  3530. "One or more mappers failed to initialize - "
  3531. "can't proceed with initialization of other "
  3532. "mappers. Triggering mapper: '%s'. "
  3533. "Original exception was: %s"
  3534. % (mapper, mapper._configure_failed)
  3535. )
  3536. e._configure_failed = mapper._configure_failed # type: ignore
  3537. raise e
  3538. if not mapper.configured:
  3539. try:
  3540. mapper._post_configure_properties()
  3541. mapper._expire_memoizations()
  3542. mapper.dispatch.mapper_configured(mapper, mapper.class_)
  3543. except Exception:
  3544. exc = sys.exc_info()[1]
  3545. if not hasattr(exc, "_configure_failed"):
  3546. mapper._configure_failed = exc
  3547. raise
  3548. if not has_skip:
  3549. reg._new_mappers = False
  3550. if not cascade and reg._dependencies.difference(orig):
  3551. raise sa_exc.InvalidRequestError(
  3552. "configure was called with cascade=False but "
  3553. "additional registries remain"
  3554. )
  3555. @util.preload_module("sqlalchemy.orm.decl_api")
  3556. def _dispose_registries(registries: Set[_RegistryType], cascade: bool) -> None:
  3557. registry = util.preloaded.orm_decl_api.registry
  3558. orig = set(registries)
  3559. for reg in registry._recurse_with_dependents(registries):
  3560. if not cascade and reg._dependents.difference(orig):
  3561. raise sa_exc.InvalidRequestError(
  3562. "Registry has dependent registries that are not disposed; "
  3563. "pass cascade=True to clear these also"
  3564. )
  3565. while reg._managers:
  3566. try:
  3567. manager, _ = reg._managers.popitem()
  3568. except KeyError:
  3569. # guard against race between while and popitem
  3570. pass
  3571. else:
  3572. reg._dispose_manager_and_mapper(manager)
  3573. reg._non_primary_mappers.clear()
  3574. reg._dependents.clear()
  3575. for dep in reg._dependencies:
  3576. dep._dependents.discard(reg)
  3577. reg._dependencies.clear()
  3578. # this wasn't done in the 1.3 clear_mappers() and in fact it
  3579. # was a bug, as it could cause configure_mappers() to invoke
  3580. # the "before_configured" event even though mappers had all been
  3581. # disposed.
  3582. reg._new_mappers = False
  3583. def reconstructor(fn):
  3584. """Decorate a method as the 'reconstructor' hook.
  3585. Designates a single method as the "reconstructor", an ``__init__``-like
  3586. method that will be called by the ORM after the instance has been
  3587. loaded from the database or otherwise reconstituted.
  3588. .. tip::
  3589. The :func:`_orm.reconstructor` decorator makes use of the
  3590. :meth:`_orm.InstanceEvents.load` event hook, which can be
  3591. used directly.
  3592. The reconstructor will be invoked with no arguments. Scalar
  3593. (non-collection) database-mapped attributes of the instance will
  3594. be available for use within the function. Eagerly-loaded
  3595. collections are generally not yet available and will usually only
  3596. contain the first element. ORM state changes made to objects at
  3597. this stage will not be recorded for the next flush() operation, so
  3598. the activity within a reconstructor should be conservative.
  3599. .. seealso::
  3600. :meth:`.InstanceEvents.load`
  3601. """
  3602. fn.__sa_reconstructor__ = True
  3603. return fn
  3604. def validates(
  3605. *names: str, include_removes: bool = False, include_backrefs: bool = True
  3606. ) -> Callable[[_Fn], _Fn]:
  3607. r"""Decorate a method as a 'validator' for one or more named properties.
  3608. Designates a method as a validator, a method which receives the
  3609. name of the attribute as well as a value to be assigned, or in the
  3610. case of a collection, the value to be added to the collection.
  3611. The function can then raise validation exceptions to halt the
  3612. process from continuing (where Python's built-in ``ValueError``
  3613. and ``AssertionError`` exceptions are reasonable choices), or can
  3614. modify or replace the value before proceeding. The function should
  3615. otherwise return the given value.
  3616. Note that a validator for a collection **cannot** issue a load of that
  3617. collection within the validation routine - this usage raises
  3618. an assertion to avoid recursion overflows. This is a reentrant
  3619. condition which is not supported.
  3620. :param \*names: list of attribute names to be validated.
  3621. :param include_removes: if True, "remove" events will be
  3622. sent as well - the validation function must accept an additional
  3623. argument "is_remove" which will be a boolean.
  3624. :param include_backrefs: defaults to ``True``; if ``False``, the
  3625. validation function will not emit if the originator is an attribute
  3626. event related via a backref. This can be used for bi-directional
  3627. :func:`.validates` usage where only one validator should emit per
  3628. attribute operation.
  3629. .. versionchanged:: 2.0.16 This paramter inadvertently defaulted to
  3630. ``False`` for releases 2.0.0 through 2.0.15. Its correct default
  3631. of ``True`` is restored in 2.0.16.
  3632. .. seealso::
  3633. :ref:`simple_validators` - usage examples for :func:`.validates`
  3634. """
  3635. def wrap(fn: _Fn) -> _Fn:
  3636. fn.__sa_validators__ = names # type: ignore[attr-defined]
  3637. fn.__sa_validation_opts__ = { # type: ignore[attr-defined]
  3638. "include_removes": include_removes,
  3639. "include_backrefs": include_backrefs,
  3640. }
  3641. return fn
  3642. return wrap
  3643. def _event_on_load(state, ctx):
  3644. instrumenting_mapper = state.manager.mapper
  3645. if instrumenting_mapper._reconstructor:
  3646. instrumenting_mapper._reconstructor(state.obj())
  3647. def _event_on_init(state, args, kwargs):
  3648. """Run init_instance hooks.
  3649. This also includes mapper compilation, normally not needed
  3650. here but helps with some piecemeal configuration
  3651. scenarios (such as in the ORM tutorial).
  3652. """
  3653. instrumenting_mapper = state.manager.mapper
  3654. if instrumenting_mapper:
  3655. instrumenting_mapper._check_configure()
  3656. if instrumenting_mapper._set_polymorphic_identity:
  3657. instrumenting_mapper._set_polymorphic_identity(state)
  3658. class _ColumnMapping(Dict["ColumnElement[Any]", "MapperProperty[Any]"]):
  3659. """Error reporting helper for mapper._columntoproperty."""
  3660. __slots__ = ("mapper",)
  3661. def __init__(self, mapper):
  3662. # TODO: weakref would be a good idea here
  3663. self.mapper = mapper
  3664. def __missing__(self, column):
  3665. prop = self.mapper._props.get(column)
  3666. if prop:
  3667. raise orm_exc.UnmappedColumnError(
  3668. "Column '%s.%s' is not available, due to "
  3669. "conflicting property '%s':%r"
  3670. % (column.table.name, column.name, column.key, prop)
  3671. )
  3672. raise orm_exc.UnmappedColumnError(
  3673. "No column %s is configured on mapper %s..."
  3674. % (column, self.mapper)
  3675. )