mapper.py 168 KB

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