session.py 189 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256
  1. # orm/session.py
  2. # Copyright (C) 2005-2024 the SQLAlchemy authors and contributors
  3. # <see AUTHORS file>
  4. #
  5. # This module is part of SQLAlchemy and is released under
  6. # the MIT License: https://www.opensource.org/licenses/mit-license.php
  7. """Provides the Session class and related utilities."""
  8. from __future__ import annotations
  9. import contextlib
  10. from enum import Enum
  11. import itertools
  12. import sys
  13. import typing
  14. from typing import Any
  15. from typing import Callable
  16. from typing import cast
  17. from typing import Dict
  18. from typing import Generic
  19. from typing import Iterable
  20. from typing import Iterator
  21. from typing import List
  22. from typing import NoReturn
  23. from typing import Optional
  24. from typing import overload
  25. from typing import Sequence
  26. from typing import Set
  27. from typing import Tuple
  28. from typing import Type
  29. from typing import TYPE_CHECKING
  30. from typing import TypeVar
  31. from typing import Union
  32. import weakref
  33. from . import attributes
  34. from . import bulk_persistence
  35. from . import context
  36. from . import descriptor_props
  37. from . import exc
  38. from . import identity
  39. from . import loading
  40. from . import query
  41. from . import state as statelib
  42. from ._typing import _O
  43. from ._typing import insp_is_mapper
  44. from ._typing import is_composite_class
  45. from ._typing import is_orm_option
  46. from ._typing import is_user_defined_option
  47. from .base import _class_to_mapper
  48. from .base import _none_set
  49. from .base import _state_mapper
  50. from .base import instance_str
  51. from .base import LoaderCallableStatus
  52. from .base import object_mapper
  53. from .base import object_state
  54. from .base import PassiveFlag
  55. from .base import state_str
  56. from .context import FromStatement
  57. from .context import ORMCompileState
  58. from .identity import IdentityMap
  59. from .query import Query
  60. from .state import InstanceState
  61. from .state_changes import _StateChange
  62. from .state_changes import _StateChangeState
  63. from .state_changes import _StateChangeStates
  64. from .unitofwork import UOWTransaction
  65. from .. import engine
  66. from .. import exc as sa_exc
  67. from .. import sql
  68. from .. import util
  69. from ..engine import Connection
  70. from ..engine import Engine
  71. from ..engine.util import TransactionalContext
  72. from ..event import dispatcher
  73. from ..event import EventTarget
  74. from ..inspection import inspect
  75. from ..inspection import Inspectable
  76. from ..sql import coercions
  77. from ..sql import dml
  78. from ..sql import roles
  79. from ..sql import Select
  80. from ..sql import TableClause
  81. from ..sql import visitors
  82. from ..sql.base import _NoArg
  83. from ..sql.base import CompileState
  84. from ..sql.schema import Table
  85. from ..sql.selectable import ForUpdateArg
  86. from ..sql.selectable import LABEL_STYLE_TABLENAME_PLUS_COL
  87. from ..util import IdentitySet
  88. from ..util.typing import Literal
  89. from ..util.typing import Protocol
  90. if typing.TYPE_CHECKING:
  91. from ._typing import _EntityType
  92. from ._typing import _IdentityKeyType
  93. from ._typing import _InstanceDict
  94. from ._typing import OrmExecuteOptionsParameter
  95. from .interfaces import ORMOption
  96. from .interfaces import UserDefinedOption
  97. from .mapper import Mapper
  98. from .path_registry import PathRegistry
  99. from .query import RowReturningQuery
  100. from ..engine import CursorResult
  101. from ..engine import Result
  102. from ..engine import Row
  103. from ..engine import RowMapping
  104. from ..engine.base import Transaction
  105. from ..engine.base import TwoPhaseTransaction
  106. from ..engine.interfaces import _CoreAnyExecuteParams
  107. from ..engine.interfaces import _CoreSingleExecuteParams
  108. from ..engine.interfaces import _ExecuteOptions
  109. from ..engine.interfaces import CoreExecuteOptionsParameter
  110. from ..engine.result import ScalarResult
  111. from ..event import _InstanceLevelDispatch
  112. from ..sql._typing import _ColumnsClauseArgument
  113. from ..sql._typing import _InfoType
  114. from ..sql._typing import _T0
  115. from ..sql._typing import _T1
  116. from ..sql._typing import _T2
  117. from ..sql._typing import _T3
  118. from ..sql._typing import _T4
  119. from ..sql._typing import _T5
  120. from ..sql._typing import _T6
  121. from ..sql._typing import _T7
  122. from ..sql._typing import _TypedColumnClauseArgument as _TCCA
  123. from ..sql.base import Executable
  124. from ..sql.base import ExecutableOption
  125. from ..sql.dml import UpdateBase
  126. from ..sql.elements import ClauseElement
  127. from ..sql.roles import TypedColumnsClauseRole
  128. from ..sql.selectable import ForUpdateParameter
  129. from ..sql.selectable import TypedReturnsRows
  130. _T = TypeVar("_T", bound=Any)
  131. __all__ = [
  132. "Session",
  133. "SessionTransaction",
  134. "sessionmaker",
  135. "ORMExecuteState",
  136. "close_all_sessions",
  137. "make_transient",
  138. "make_transient_to_detached",
  139. "object_session",
  140. ]
  141. _sessions: weakref.WeakValueDictionary[
  142. int, Session
  143. ] = weakref.WeakValueDictionary()
  144. """Weak-referencing dictionary of :class:`.Session` objects.
  145. """
  146. statelib._sessions = _sessions
  147. _PKIdentityArgument = Union[Any, Tuple[Any, ...]]
  148. _BindArguments = Dict[str, Any]
  149. _EntityBindKey = Union[Type[_O], "Mapper[_O]"]
  150. _SessionBindKey = Union[Type[Any], "Mapper[Any]", "TableClause", str]
  151. _SessionBind = Union["Engine", "Connection"]
  152. JoinTransactionMode = Literal[
  153. "conditional_savepoint",
  154. "rollback_only",
  155. "control_fully",
  156. "create_savepoint",
  157. ]
  158. class _ConnectionCallableProto(Protocol):
  159. """a callable that returns a :class:`.Connection` given an instance.
  160. This callable, when present on a :class:`.Session`, is called only from the
  161. ORM's persistence mechanism (i.e. the unit of work flush process) to allow
  162. for connection-per-instance schemes (i.e. horizontal sharding) to be used
  163. as persistence time.
  164. This callable is not present on a plain :class:`.Session`, however
  165. is established when using the horizontal sharding extension.
  166. """
  167. def __call__(
  168. self,
  169. mapper: Optional[Mapper[Any]] = None,
  170. instance: Optional[object] = None,
  171. **kw: Any,
  172. ) -> Connection:
  173. ...
  174. def _state_session(state: InstanceState[Any]) -> Optional[Session]:
  175. """Given an :class:`.InstanceState`, return the :class:`.Session`
  176. associated, if any.
  177. """
  178. return state.session
  179. class _SessionClassMethods:
  180. """Class-level methods for :class:`.Session`, :class:`.sessionmaker`."""
  181. @classmethod
  182. @util.deprecated(
  183. "1.3",
  184. "The :meth:`.Session.close_all` method is deprecated and will be "
  185. "removed in a future release. Please refer to "
  186. ":func:`.session.close_all_sessions`.",
  187. )
  188. def close_all(cls) -> None:
  189. """Close *all* sessions in memory."""
  190. close_all_sessions()
  191. @classmethod
  192. @util.preload_module("sqlalchemy.orm.util")
  193. def identity_key(
  194. cls,
  195. class_: Optional[Type[Any]] = None,
  196. ident: Union[Any, Tuple[Any, ...]] = None,
  197. *,
  198. instance: Optional[Any] = None,
  199. row: Optional[Union[Row[Any], RowMapping]] = None,
  200. identity_token: Optional[Any] = None,
  201. ) -> _IdentityKeyType[Any]:
  202. """Return an identity key.
  203. This is an alias of :func:`.util.identity_key`.
  204. """
  205. return util.preloaded.orm_util.identity_key(
  206. class_,
  207. ident,
  208. instance=instance,
  209. row=row,
  210. identity_token=identity_token,
  211. )
  212. @classmethod
  213. def object_session(cls, instance: object) -> Optional[Session]:
  214. """Return the :class:`.Session` to which an object belongs.
  215. This is an alias of :func:`.object_session`.
  216. """
  217. return object_session(instance)
  218. class SessionTransactionState(_StateChangeState):
  219. ACTIVE = 1
  220. PREPARED = 2
  221. COMMITTED = 3
  222. DEACTIVE = 4
  223. CLOSED = 5
  224. PROVISIONING_CONNECTION = 6
  225. # backwards compatibility
  226. ACTIVE, PREPARED, COMMITTED, DEACTIVE, CLOSED, PROVISIONING_CONNECTION = tuple(
  227. SessionTransactionState
  228. )
  229. class ORMExecuteState(util.MemoizedSlots):
  230. """Represents a call to the :meth:`_orm.Session.execute` method, as passed
  231. to the :meth:`.SessionEvents.do_orm_execute` event hook.
  232. .. versionadded:: 1.4
  233. .. seealso::
  234. :ref:`session_execute_events` - top level documentation on how
  235. to use :meth:`_orm.SessionEvents.do_orm_execute`
  236. """
  237. __slots__ = (
  238. "session",
  239. "statement",
  240. "parameters",
  241. "execution_options",
  242. "local_execution_options",
  243. "bind_arguments",
  244. "identity_token",
  245. "_compile_state_cls",
  246. "_starting_event_idx",
  247. "_events_todo",
  248. "_update_execution_options",
  249. )
  250. session: Session
  251. """The :class:`_orm.Session` in use."""
  252. statement: Executable
  253. """The SQL statement being invoked.
  254. For an ORM selection as would
  255. be retrieved from :class:`_orm.Query`, this is an instance of
  256. :class:`_sql.select` that was generated from the ORM query.
  257. """
  258. parameters: Optional[_CoreAnyExecuteParams]
  259. """Dictionary of parameters that was passed to
  260. :meth:`_orm.Session.execute`."""
  261. execution_options: _ExecuteOptions
  262. """The complete dictionary of current execution options.
  263. This is a merge of the statement level options with the
  264. locally passed execution options.
  265. .. seealso::
  266. :attr:`_orm.ORMExecuteState.local_execution_options`
  267. :meth:`_sql.Executable.execution_options`
  268. :ref:`orm_queryguide_execution_options`
  269. """
  270. local_execution_options: _ExecuteOptions
  271. """Dictionary view of the execution options passed to the
  272. :meth:`.Session.execute` method.
  273. This does not include options that may be associated with the statement
  274. being invoked.
  275. .. seealso::
  276. :attr:`_orm.ORMExecuteState.execution_options`
  277. """
  278. bind_arguments: _BindArguments
  279. """The dictionary passed as the
  280. :paramref:`_orm.Session.execute.bind_arguments` dictionary.
  281. This dictionary may be used by extensions to :class:`_orm.Session` to pass
  282. arguments that will assist in determining amongst a set of database
  283. connections which one should be used to invoke this statement.
  284. """
  285. _compile_state_cls: Optional[Type[ORMCompileState]]
  286. _starting_event_idx: int
  287. _events_todo: List[Any]
  288. _update_execution_options: Optional[_ExecuteOptions]
  289. def __init__(
  290. self,
  291. session: Session,
  292. statement: Executable,
  293. parameters: Optional[_CoreAnyExecuteParams],
  294. execution_options: _ExecuteOptions,
  295. bind_arguments: _BindArguments,
  296. compile_state_cls: Optional[Type[ORMCompileState]],
  297. events_todo: List[_InstanceLevelDispatch[Session]],
  298. ):
  299. """Construct a new :class:`_orm.ORMExecuteState`.
  300. this object is constructed internally.
  301. """
  302. self.session = session
  303. self.statement = statement
  304. self.parameters = parameters
  305. self.local_execution_options = execution_options
  306. self.execution_options = statement._execution_options.union(
  307. execution_options
  308. )
  309. self.bind_arguments = bind_arguments
  310. self._compile_state_cls = compile_state_cls
  311. self._events_todo = list(events_todo)
  312. def _remaining_events(self) -> List[_InstanceLevelDispatch[Session]]:
  313. return self._events_todo[self._starting_event_idx + 1 :]
  314. def invoke_statement(
  315. self,
  316. statement: Optional[Executable] = None,
  317. params: Optional[_CoreAnyExecuteParams] = None,
  318. execution_options: Optional[OrmExecuteOptionsParameter] = None,
  319. bind_arguments: Optional[_BindArguments] = None,
  320. ) -> Result[Any]:
  321. """Execute the statement represented by this
  322. :class:`.ORMExecuteState`, without re-invoking events that have
  323. already proceeded.
  324. This method essentially performs a re-entrant execution of the current
  325. statement for which the :meth:`.SessionEvents.do_orm_execute` event is
  326. being currently invoked. The use case for this is for event handlers
  327. that want to override how the ultimate
  328. :class:`_engine.Result` object is returned, such as for schemes that
  329. retrieve results from an offline cache or which concatenate results
  330. from multiple executions.
  331. When the :class:`_engine.Result` object is returned by the actual
  332. handler function within :meth:`_orm.SessionEvents.do_orm_execute` and
  333. is propagated to the calling
  334. :meth:`_orm.Session.execute` method, the remainder of the
  335. :meth:`_orm.Session.execute` method is preempted and the
  336. :class:`_engine.Result` object is returned to the caller of
  337. :meth:`_orm.Session.execute` immediately.
  338. :param statement: optional statement to be invoked, in place of the
  339. statement currently represented by :attr:`.ORMExecuteState.statement`.
  340. :param params: optional dictionary of parameters or list of parameters
  341. which will be merged into the existing
  342. :attr:`.ORMExecuteState.parameters` of this :class:`.ORMExecuteState`.
  343. .. versionchanged:: 2.0 a list of parameter dictionaries is accepted
  344. for executemany executions.
  345. :param execution_options: optional dictionary of execution options
  346. will be merged into the existing
  347. :attr:`.ORMExecuteState.execution_options` of this
  348. :class:`.ORMExecuteState`.
  349. :param bind_arguments: optional dictionary of bind_arguments
  350. which will be merged amongst the current
  351. :attr:`.ORMExecuteState.bind_arguments`
  352. of this :class:`.ORMExecuteState`.
  353. :return: a :class:`_engine.Result` object with ORM-level results.
  354. .. seealso::
  355. :ref:`do_orm_execute_re_executing` - background and examples on the
  356. appropriate usage of :meth:`_orm.ORMExecuteState.invoke_statement`.
  357. """
  358. if statement is None:
  359. statement = self.statement
  360. _bind_arguments = dict(self.bind_arguments)
  361. if bind_arguments:
  362. _bind_arguments.update(bind_arguments)
  363. _bind_arguments["_sa_skip_events"] = True
  364. _params: Optional[_CoreAnyExecuteParams]
  365. if params:
  366. if self.is_executemany:
  367. _params = []
  368. exec_many_parameters = cast(
  369. "List[Dict[str, Any]]", self.parameters
  370. )
  371. for _existing_params, _new_params in itertools.zip_longest(
  372. exec_many_parameters,
  373. cast("List[Dict[str, Any]]", params),
  374. ):
  375. if _existing_params is None or _new_params is None:
  376. raise sa_exc.InvalidRequestError(
  377. f"Can't apply executemany parameters to "
  378. f"statement; number of parameter sets passed to "
  379. f"Session.execute() ({len(exec_many_parameters)}) "
  380. f"does not match number of parameter sets given "
  381. f"to ORMExecuteState.invoke_statement() "
  382. f"({len(params)})"
  383. )
  384. _existing_params = dict(_existing_params)
  385. _existing_params.update(_new_params)
  386. _params.append(_existing_params)
  387. else:
  388. _params = dict(cast("Dict[str, Any]", self.parameters))
  389. _params.update(cast("Dict[str, Any]", params))
  390. else:
  391. _params = self.parameters
  392. _execution_options = self.local_execution_options
  393. if execution_options:
  394. _execution_options = _execution_options.union(execution_options)
  395. return self.session._execute_internal(
  396. statement,
  397. _params,
  398. execution_options=_execution_options,
  399. bind_arguments=_bind_arguments,
  400. _parent_execute_state=self,
  401. )
  402. @property
  403. def bind_mapper(self) -> Optional[Mapper[Any]]:
  404. """Return the :class:`_orm.Mapper` that is the primary "bind" mapper.
  405. For an :class:`_orm.ORMExecuteState` object invoking an ORM
  406. statement, that is, the :attr:`_orm.ORMExecuteState.is_orm_statement`
  407. attribute is ``True``, this attribute will return the
  408. :class:`_orm.Mapper` that is considered to be the "primary" mapper
  409. of the statement. The term "bind mapper" refers to the fact that
  410. a :class:`_orm.Session` object may be "bound" to multiple
  411. :class:`_engine.Engine` objects keyed to mapped classes, and the
  412. "bind mapper" determines which of those :class:`_engine.Engine` objects
  413. would be selected.
  414. For a statement that is invoked against a single mapped class,
  415. :attr:`_orm.ORMExecuteState.bind_mapper` is intended to be a reliable
  416. way of getting this mapper.
  417. .. versionadded:: 1.4.0b2
  418. .. seealso::
  419. :attr:`_orm.ORMExecuteState.all_mappers`
  420. """
  421. mp: Optional[Mapper[Any]] = self.bind_arguments.get("mapper", None)
  422. return mp
  423. @property
  424. def all_mappers(self) -> Sequence[Mapper[Any]]:
  425. """Return a sequence of all :class:`_orm.Mapper` objects that are
  426. involved at the top level of this statement.
  427. By "top level" we mean those :class:`_orm.Mapper` objects that would
  428. be represented in the result set rows for a :func:`_sql.select`
  429. query, or for a :func:`_dml.update` or :func:`_dml.delete` query,
  430. the mapper that is the main subject of the UPDATE or DELETE.
  431. .. versionadded:: 1.4.0b2
  432. .. seealso::
  433. :attr:`_orm.ORMExecuteState.bind_mapper`
  434. """
  435. if not self.is_orm_statement:
  436. return []
  437. elif isinstance(self.statement, (Select, FromStatement)):
  438. result = []
  439. seen = set()
  440. for d in self.statement.column_descriptions:
  441. ent = d["entity"]
  442. if ent:
  443. insp = inspect(ent, raiseerr=False)
  444. if insp and insp.mapper and insp.mapper not in seen:
  445. seen.add(insp.mapper)
  446. result.append(insp.mapper)
  447. return result
  448. elif self.statement.is_dml and self.bind_mapper:
  449. return [self.bind_mapper]
  450. else:
  451. return []
  452. @property
  453. def is_orm_statement(self) -> bool:
  454. """return True if the operation is an ORM statement.
  455. This indicates that the select(), insert(), update(), or delete()
  456. being invoked contains ORM entities as subjects. For a statement
  457. that does not have ORM entities and instead refers only to
  458. :class:`.Table` metadata, it is invoked as a Core SQL statement
  459. and no ORM-level automation takes place.
  460. """
  461. return self._compile_state_cls is not None
  462. @property
  463. def is_executemany(self) -> bool:
  464. """return True if the parameters are a multi-element list of
  465. dictionaries with more than one dictionary.
  466. .. versionadded:: 2.0
  467. """
  468. return isinstance(self.parameters, list)
  469. @property
  470. def is_select(self) -> bool:
  471. """return True if this is a SELECT operation."""
  472. return self.statement.is_select
  473. @property
  474. def is_insert(self) -> bool:
  475. """return True if this is an INSERT operation."""
  476. return self.statement.is_dml and self.statement.is_insert
  477. @property
  478. def is_update(self) -> bool:
  479. """return True if this is an UPDATE operation."""
  480. return self.statement.is_dml and self.statement.is_update
  481. @property
  482. def is_delete(self) -> bool:
  483. """return True if this is a DELETE operation."""
  484. return self.statement.is_dml and self.statement.is_delete
  485. @property
  486. def _is_crud(self) -> bool:
  487. return isinstance(self.statement, (dml.Update, dml.Delete))
  488. def update_execution_options(self, **opts: Any) -> None:
  489. """Update the local execution options with new values."""
  490. self.local_execution_options = self.local_execution_options.union(opts)
  491. def _orm_compile_options(
  492. self,
  493. ) -> Optional[
  494. Union[
  495. context.ORMCompileState.default_compile_options,
  496. Type[context.ORMCompileState.default_compile_options],
  497. ]
  498. ]:
  499. if not self.is_select:
  500. return None
  501. try:
  502. opts = self.statement._compile_options
  503. except AttributeError:
  504. return None
  505. if opts is not None and opts.isinstance(
  506. context.ORMCompileState.default_compile_options
  507. ):
  508. return opts # type: ignore
  509. else:
  510. return None
  511. @property
  512. def lazy_loaded_from(self) -> Optional[InstanceState[Any]]:
  513. """An :class:`.InstanceState` that is using this statement execution
  514. for a lazy load operation.
  515. The primary rationale for this attribute is to support the horizontal
  516. sharding extension, where it is available within specific query
  517. execution time hooks created by this extension. To that end, the
  518. attribute is only intended to be meaningful at **query execution
  519. time**, and importantly not any time prior to that, including query
  520. compilation time.
  521. """
  522. return self.load_options._lazy_loaded_from
  523. @property
  524. def loader_strategy_path(self) -> Optional[PathRegistry]:
  525. """Return the :class:`.PathRegistry` for the current load path.
  526. This object represents the "path" in a query along relationships
  527. when a particular object or collection is being loaded.
  528. """
  529. opts = self._orm_compile_options()
  530. if opts is not None:
  531. return opts._current_path
  532. else:
  533. return None
  534. @property
  535. def is_column_load(self) -> bool:
  536. """Return True if the operation is refreshing column-oriented
  537. attributes on an existing ORM object.
  538. This occurs during operations such as :meth:`_orm.Session.refresh`,
  539. as well as when an attribute deferred by :func:`_orm.defer` is
  540. being loaded, or an attribute that was expired either directly
  541. by :meth:`_orm.Session.expire` or via a commit operation is being
  542. loaded.
  543. Handlers will very likely not want to add any options to queries
  544. when such an operation is occurring as the query should be a straight
  545. primary key fetch which should not have any additional WHERE criteria,
  546. and loader options travelling with the instance
  547. will have already been added to the query.
  548. .. versionadded:: 1.4.0b2
  549. .. seealso::
  550. :attr:`_orm.ORMExecuteState.is_relationship_load`
  551. """
  552. opts = self._orm_compile_options()
  553. return opts is not None and opts._for_refresh_state
  554. @property
  555. def is_relationship_load(self) -> bool:
  556. """Return True if this load is loading objects on behalf of a
  557. relationship.
  558. This means, the loader in effect is either a LazyLoader,
  559. SelectInLoader, SubqueryLoader, or similar, and the entire
  560. SELECT statement being emitted is on behalf of a relationship
  561. load.
  562. Handlers will very likely not want to add any options to queries
  563. when such an operation is occurring, as loader options are already
  564. capable of being propagated to relationship loaders and should
  565. be already present.
  566. .. seealso::
  567. :attr:`_orm.ORMExecuteState.is_column_load`
  568. """
  569. opts = self._orm_compile_options()
  570. if opts is None:
  571. return False
  572. path = self.loader_strategy_path
  573. return path is not None and not path.is_root
  574. @property
  575. def load_options(
  576. self,
  577. ) -> Union[
  578. context.QueryContext.default_load_options,
  579. Type[context.QueryContext.default_load_options],
  580. ]:
  581. """Return the load_options that will be used for this execution."""
  582. if not self.is_select:
  583. raise sa_exc.InvalidRequestError(
  584. "This ORM execution is not against a SELECT statement "
  585. "so there are no load options."
  586. )
  587. lo: Union[
  588. context.QueryContext.default_load_options,
  589. Type[context.QueryContext.default_load_options],
  590. ] = self.execution_options.get(
  591. "_sa_orm_load_options", context.QueryContext.default_load_options
  592. )
  593. return lo
  594. @property
  595. def update_delete_options(
  596. self,
  597. ) -> Union[
  598. bulk_persistence.BulkUDCompileState.default_update_options,
  599. Type[bulk_persistence.BulkUDCompileState.default_update_options],
  600. ]:
  601. """Return the update_delete_options that will be used for this
  602. execution."""
  603. if not self._is_crud:
  604. raise sa_exc.InvalidRequestError(
  605. "This ORM execution is not against an UPDATE or DELETE "
  606. "statement so there are no update options."
  607. )
  608. uo: Union[
  609. bulk_persistence.BulkUDCompileState.default_update_options,
  610. Type[bulk_persistence.BulkUDCompileState.default_update_options],
  611. ] = self.execution_options.get(
  612. "_sa_orm_update_options",
  613. bulk_persistence.BulkUDCompileState.default_update_options,
  614. )
  615. return uo
  616. @property
  617. def _non_compile_orm_options(self) -> Sequence[ORMOption]:
  618. return [
  619. opt
  620. for opt in self.statement._with_options
  621. if is_orm_option(opt) and not opt._is_compile_state
  622. ]
  623. @property
  624. def user_defined_options(self) -> Sequence[UserDefinedOption]:
  625. """The sequence of :class:`.UserDefinedOptions` that have been
  626. associated with the statement being invoked.
  627. """
  628. return [
  629. opt
  630. for opt in self.statement._with_options
  631. if is_user_defined_option(opt)
  632. ]
  633. class SessionTransactionOrigin(Enum):
  634. """indicates the origin of a :class:`.SessionTransaction`.
  635. This enumeration is present on the
  636. :attr:`.SessionTransaction.origin` attribute of any
  637. :class:`.SessionTransaction` object.
  638. .. versionadded:: 2.0
  639. """
  640. AUTOBEGIN = 0
  641. """transaction were started by autobegin"""
  642. BEGIN = 1
  643. """transaction were started by calling :meth:`_orm.Session.begin`"""
  644. BEGIN_NESTED = 2
  645. """tranaction were started by :meth:`_orm.Session.begin_nested`"""
  646. SUBTRANSACTION = 3
  647. """transaction is an internal "subtransaction" """
  648. class SessionTransaction(_StateChange, TransactionalContext):
  649. """A :class:`.Session`-level transaction.
  650. :class:`.SessionTransaction` is produced from the
  651. :meth:`_orm.Session.begin`
  652. and :meth:`_orm.Session.begin_nested` methods. It's largely an internal
  653. object that in modern use provides a context manager for session
  654. transactions.
  655. Documentation on interacting with :class:`_orm.SessionTransaction` is
  656. at: :ref:`unitofwork_transaction`.
  657. .. versionchanged:: 1.4 The scoping and API methods to work with the
  658. :class:`_orm.SessionTransaction` object directly have been simplified.
  659. .. seealso::
  660. :ref:`unitofwork_transaction`
  661. :meth:`.Session.begin`
  662. :meth:`.Session.begin_nested`
  663. :meth:`.Session.rollback`
  664. :meth:`.Session.commit`
  665. :meth:`.Session.in_transaction`
  666. :meth:`.Session.in_nested_transaction`
  667. :meth:`.Session.get_transaction`
  668. :meth:`.Session.get_nested_transaction`
  669. """
  670. _rollback_exception: Optional[BaseException] = None
  671. _connections: Dict[
  672. Union[Engine, Connection], Tuple[Connection, Transaction, bool, bool]
  673. ]
  674. session: Session
  675. _parent: Optional[SessionTransaction]
  676. _state: SessionTransactionState
  677. _new: weakref.WeakKeyDictionary[InstanceState[Any], object]
  678. _deleted: weakref.WeakKeyDictionary[InstanceState[Any], object]
  679. _dirty: weakref.WeakKeyDictionary[InstanceState[Any], object]
  680. _key_switches: weakref.WeakKeyDictionary[
  681. InstanceState[Any], Tuple[Any, Any]
  682. ]
  683. origin: SessionTransactionOrigin
  684. """Origin of this :class:`_orm.SessionTransaction`.
  685. Refers to a :class:`.SessionTransactionOrigin` instance which is an
  686. enumeration indicating the source event that led to constructing
  687. this :class:`_orm.SessionTransaction`.
  688. .. versionadded:: 2.0
  689. """
  690. nested: bool = False
  691. """Indicates if this is a nested, or SAVEPOINT, transaction.
  692. When :attr:`.SessionTransaction.nested` is True, it is expected
  693. that :attr:`.SessionTransaction.parent` will be present as well,
  694. linking to the enclosing :class:`.SessionTransaction`.
  695. .. seealso::
  696. :attr:`.SessionTransaction.origin`
  697. """
  698. def __init__(
  699. self,
  700. session: Session,
  701. origin: SessionTransactionOrigin,
  702. parent: Optional[SessionTransaction] = None,
  703. ):
  704. TransactionalContext._trans_ctx_check(session)
  705. self.session = session
  706. self._connections = {}
  707. self._parent = parent
  708. self.nested = nested = origin is SessionTransactionOrigin.BEGIN_NESTED
  709. self.origin = origin
  710. if session._close_state is _SessionCloseState.CLOSED:
  711. raise sa_exc.InvalidRequestError(
  712. "This Session has been permanently closed and is unable "
  713. "to handle any more transaction requests."
  714. )
  715. if nested:
  716. if not parent:
  717. raise sa_exc.InvalidRequestError(
  718. "Can't start a SAVEPOINT transaction when no existing "
  719. "transaction is in progress"
  720. )
  721. self._previous_nested_transaction = session._nested_transaction
  722. elif origin is SessionTransactionOrigin.SUBTRANSACTION:
  723. assert parent is not None
  724. else:
  725. assert parent is None
  726. self._state = SessionTransactionState.ACTIVE
  727. self._take_snapshot()
  728. # make sure transaction is assigned before we call the
  729. # dispatch
  730. self.session._transaction = self
  731. self.session.dispatch.after_transaction_create(self.session, self)
  732. def _raise_for_prerequisite_state(
  733. self, operation_name: str, state: _StateChangeState
  734. ) -> NoReturn:
  735. if state is SessionTransactionState.DEACTIVE:
  736. if self._rollback_exception:
  737. raise sa_exc.PendingRollbackError(
  738. "This Session's transaction has been rolled back "
  739. "due to a previous exception during flush."
  740. " To begin a new transaction with this Session, "
  741. "first issue Session.rollback()."
  742. f" Original exception was: {self._rollback_exception}",
  743. code="7s2a",
  744. )
  745. else:
  746. raise sa_exc.InvalidRequestError(
  747. "This session is in 'inactive' state, due to the "
  748. "SQL transaction being rolled back; no further SQL "
  749. "can be emitted within this transaction."
  750. )
  751. elif state is SessionTransactionState.CLOSED:
  752. raise sa_exc.ResourceClosedError("This transaction is closed")
  753. elif state is SessionTransactionState.PROVISIONING_CONNECTION:
  754. raise sa_exc.InvalidRequestError(
  755. "This session is provisioning a new connection; concurrent "
  756. "operations are not permitted",
  757. code="isce",
  758. )
  759. else:
  760. raise sa_exc.InvalidRequestError(
  761. f"This session is in '{state.name.lower()}' state; no "
  762. "further SQL can be emitted within this transaction."
  763. )
  764. @property
  765. def parent(self) -> Optional[SessionTransaction]:
  766. """The parent :class:`.SessionTransaction` of this
  767. :class:`.SessionTransaction`.
  768. If this attribute is ``None``, indicates this
  769. :class:`.SessionTransaction` is at the top of the stack, and
  770. corresponds to a real "COMMIT"/"ROLLBACK"
  771. block. If non-``None``, then this is either a "subtransaction"
  772. (an internal marker object used by the flush process) or a
  773. "nested" / SAVEPOINT transaction. If the
  774. :attr:`.SessionTransaction.nested` attribute is ``True``, then
  775. this is a SAVEPOINT, and if ``False``, indicates this a subtransaction.
  776. """
  777. return self._parent
  778. @property
  779. def is_active(self) -> bool:
  780. return (
  781. self.session is not None
  782. and self._state is SessionTransactionState.ACTIVE
  783. )
  784. @property
  785. def _is_transaction_boundary(self) -> bool:
  786. return self.nested or not self._parent
  787. @_StateChange.declare_states(
  788. (SessionTransactionState.ACTIVE,), _StateChangeStates.NO_CHANGE
  789. )
  790. def connection(
  791. self,
  792. bindkey: Optional[Mapper[Any]],
  793. execution_options: Optional[_ExecuteOptions] = None,
  794. **kwargs: Any,
  795. ) -> Connection:
  796. bind = self.session.get_bind(bindkey, **kwargs)
  797. return self._connection_for_bind(bind, execution_options)
  798. @_StateChange.declare_states(
  799. (SessionTransactionState.ACTIVE,), _StateChangeStates.NO_CHANGE
  800. )
  801. def _begin(self, nested: bool = False) -> SessionTransaction:
  802. return SessionTransaction(
  803. self.session,
  804. SessionTransactionOrigin.BEGIN_NESTED
  805. if nested
  806. else SessionTransactionOrigin.SUBTRANSACTION,
  807. self,
  808. )
  809. def _iterate_self_and_parents(
  810. self, upto: Optional[SessionTransaction] = None
  811. ) -> Iterable[SessionTransaction]:
  812. current = self
  813. result: Tuple[SessionTransaction, ...] = ()
  814. while current:
  815. result += (current,)
  816. if current._parent is upto:
  817. break
  818. elif current._parent is None:
  819. raise sa_exc.InvalidRequestError(
  820. "Transaction %s is not on the active transaction list"
  821. % (upto)
  822. )
  823. else:
  824. current = current._parent
  825. return result
  826. def _take_snapshot(self) -> None:
  827. if not self._is_transaction_boundary:
  828. parent = self._parent
  829. assert parent is not None
  830. self._new = parent._new
  831. self._deleted = parent._deleted
  832. self._dirty = parent._dirty
  833. self._key_switches = parent._key_switches
  834. return
  835. is_begin = self.origin in (
  836. SessionTransactionOrigin.BEGIN,
  837. SessionTransactionOrigin.AUTOBEGIN,
  838. )
  839. if not is_begin and not self.session._flushing:
  840. self.session.flush()
  841. self._new = weakref.WeakKeyDictionary()
  842. self._deleted = weakref.WeakKeyDictionary()
  843. self._dirty = weakref.WeakKeyDictionary()
  844. self._key_switches = weakref.WeakKeyDictionary()
  845. def _restore_snapshot(self, dirty_only: bool = False) -> None:
  846. """Restore the restoration state taken before a transaction began.
  847. Corresponds to a rollback.
  848. """
  849. assert self._is_transaction_boundary
  850. to_expunge = set(self._new).union(self.session._new)
  851. self.session._expunge_states(to_expunge, to_transient=True)
  852. for s, (oldkey, newkey) in self._key_switches.items():
  853. # we probably can do this conditionally based on
  854. # if we expunged or not, but safe_discard does that anyway
  855. self.session.identity_map.safe_discard(s)
  856. # restore the old key
  857. s.key = oldkey
  858. # now restore the object, but only if we didn't expunge
  859. if s not in to_expunge:
  860. self.session.identity_map.replace(s)
  861. for s in set(self._deleted).union(self.session._deleted):
  862. self.session._update_impl(s, revert_deletion=True)
  863. assert not self.session._deleted
  864. for s in self.session.identity_map.all_states():
  865. if not dirty_only or s.modified or s in self._dirty:
  866. s._expire(s.dict, self.session.identity_map._modified)
  867. def _remove_snapshot(self) -> None:
  868. """Remove the restoration state taken before a transaction began.
  869. Corresponds to a commit.
  870. """
  871. assert self._is_transaction_boundary
  872. if not self.nested and self.session.expire_on_commit:
  873. for s in self.session.identity_map.all_states():
  874. s._expire(s.dict, self.session.identity_map._modified)
  875. statelib.InstanceState._detach_states(
  876. list(self._deleted), self.session
  877. )
  878. self._deleted.clear()
  879. elif self.nested:
  880. parent = self._parent
  881. assert parent is not None
  882. parent._new.update(self._new)
  883. parent._dirty.update(self._dirty)
  884. parent._deleted.update(self._deleted)
  885. parent._key_switches.update(self._key_switches)
  886. @_StateChange.declare_states(
  887. (SessionTransactionState.ACTIVE,), _StateChangeStates.NO_CHANGE
  888. )
  889. def _connection_for_bind(
  890. self,
  891. bind: _SessionBind,
  892. execution_options: Optional[CoreExecuteOptionsParameter],
  893. ) -> Connection:
  894. if bind in self._connections:
  895. if execution_options:
  896. util.warn(
  897. "Connection is already established for the "
  898. "given bind; execution_options ignored"
  899. )
  900. return self._connections[bind][0]
  901. self._state = SessionTransactionState.PROVISIONING_CONNECTION
  902. local_connect = False
  903. should_commit = True
  904. try:
  905. if self._parent:
  906. conn = self._parent._connection_for_bind(
  907. bind, execution_options
  908. )
  909. if not self.nested:
  910. return conn
  911. else:
  912. if isinstance(bind, engine.Connection):
  913. conn = bind
  914. if conn.engine in self._connections:
  915. raise sa_exc.InvalidRequestError(
  916. "Session already has a Connection associated "
  917. "for the given Connection's Engine"
  918. )
  919. else:
  920. conn = bind.connect()
  921. local_connect = True
  922. try:
  923. if execution_options:
  924. conn = conn.execution_options(**execution_options)
  925. transaction: Transaction
  926. if self.session.twophase and self._parent is None:
  927. # TODO: shouldn't we only be here if not
  928. # conn.in_transaction() ?
  929. # if twophase is set and conn.in_transaction(), validate
  930. # that it is in fact twophase.
  931. transaction = conn.begin_twophase()
  932. elif self.nested:
  933. transaction = conn.begin_nested()
  934. elif conn.in_transaction():
  935. join_transaction_mode = self.session.join_transaction_mode
  936. if join_transaction_mode == "conditional_savepoint":
  937. if conn.in_nested_transaction():
  938. join_transaction_mode = "create_savepoint"
  939. else:
  940. join_transaction_mode = "rollback_only"
  941. if join_transaction_mode in (
  942. "control_fully",
  943. "rollback_only",
  944. ):
  945. if conn.in_nested_transaction():
  946. transaction = (
  947. conn._get_required_nested_transaction()
  948. )
  949. else:
  950. transaction = conn._get_required_transaction()
  951. if join_transaction_mode == "rollback_only":
  952. should_commit = False
  953. elif join_transaction_mode == "create_savepoint":
  954. transaction = conn.begin_nested()
  955. else:
  956. assert False, join_transaction_mode
  957. else:
  958. transaction = conn.begin()
  959. except:
  960. # connection will not not be associated with this Session;
  961. # close it immediately so that it isn't closed under GC
  962. if local_connect:
  963. conn.close()
  964. raise
  965. else:
  966. bind_is_connection = isinstance(bind, engine.Connection)
  967. self._connections[conn] = self._connections[conn.engine] = (
  968. conn,
  969. transaction,
  970. should_commit,
  971. not bind_is_connection,
  972. )
  973. self.session.dispatch.after_begin(self.session, self, conn)
  974. return conn
  975. finally:
  976. self._state = SessionTransactionState.ACTIVE
  977. def prepare(self) -> None:
  978. if self._parent is not None or not self.session.twophase:
  979. raise sa_exc.InvalidRequestError(
  980. "'twophase' mode not enabled, or not root transaction; "
  981. "can't prepare."
  982. )
  983. self._prepare_impl()
  984. @_StateChange.declare_states(
  985. (SessionTransactionState.ACTIVE,), SessionTransactionState.PREPARED
  986. )
  987. def _prepare_impl(self) -> None:
  988. if self._parent is None or self.nested:
  989. self.session.dispatch.before_commit(self.session)
  990. stx = self.session._transaction
  991. assert stx is not None
  992. if stx is not self:
  993. for subtransaction in stx._iterate_self_and_parents(upto=self):
  994. subtransaction.commit()
  995. if not self.session._flushing:
  996. for _flush_guard in range(100):
  997. if self.session._is_clean():
  998. break
  999. self.session.flush()
  1000. else:
  1001. raise exc.FlushError(
  1002. "Over 100 subsequent flushes have occurred within "
  1003. "session.commit() - is an after_flush() hook "
  1004. "creating new objects?"
  1005. )
  1006. if self._parent is None and self.session.twophase:
  1007. try:
  1008. for t in set(self._connections.values()):
  1009. cast("TwoPhaseTransaction", t[1]).prepare()
  1010. except:
  1011. with util.safe_reraise():
  1012. self.rollback()
  1013. self._state = SessionTransactionState.PREPARED
  1014. @_StateChange.declare_states(
  1015. (SessionTransactionState.ACTIVE, SessionTransactionState.PREPARED),
  1016. SessionTransactionState.CLOSED,
  1017. )
  1018. def commit(self, _to_root: bool = False) -> None:
  1019. if self._state is not SessionTransactionState.PREPARED:
  1020. with self._expect_state(SessionTransactionState.PREPARED):
  1021. self._prepare_impl()
  1022. if self._parent is None or self.nested:
  1023. for conn, trans, should_commit, autoclose in set(
  1024. self._connections.values()
  1025. ):
  1026. if should_commit:
  1027. trans.commit()
  1028. self._state = SessionTransactionState.COMMITTED
  1029. self.session.dispatch.after_commit(self.session)
  1030. self._remove_snapshot()
  1031. with self._expect_state(SessionTransactionState.CLOSED):
  1032. self.close()
  1033. if _to_root and self._parent:
  1034. self._parent.commit(_to_root=True)
  1035. @_StateChange.declare_states(
  1036. (
  1037. SessionTransactionState.ACTIVE,
  1038. SessionTransactionState.DEACTIVE,
  1039. SessionTransactionState.PREPARED,
  1040. ),
  1041. SessionTransactionState.CLOSED,
  1042. )
  1043. def rollback(
  1044. self, _capture_exception: bool = False, _to_root: bool = False
  1045. ) -> None:
  1046. stx = self.session._transaction
  1047. assert stx is not None
  1048. if stx is not self:
  1049. for subtransaction in stx._iterate_self_and_parents(upto=self):
  1050. subtransaction.close()
  1051. boundary = self
  1052. rollback_err = None
  1053. if self._state in (
  1054. SessionTransactionState.ACTIVE,
  1055. SessionTransactionState.PREPARED,
  1056. ):
  1057. for transaction in self._iterate_self_and_parents():
  1058. if transaction._parent is None or transaction.nested:
  1059. try:
  1060. for t in set(transaction._connections.values()):
  1061. t[1].rollback()
  1062. transaction._state = SessionTransactionState.DEACTIVE
  1063. self.session.dispatch.after_rollback(self.session)
  1064. except:
  1065. rollback_err = sys.exc_info()
  1066. finally:
  1067. transaction._state = SessionTransactionState.DEACTIVE
  1068. transaction._restore_snapshot(
  1069. dirty_only=transaction.nested
  1070. )
  1071. boundary = transaction
  1072. break
  1073. else:
  1074. transaction._state = SessionTransactionState.DEACTIVE
  1075. sess = self.session
  1076. if not rollback_err and not sess._is_clean():
  1077. # if items were added, deleted, or mutated
  1078. # here, we need to re-restore the snapshot
  1079. util.warn(
  1080. "Session's state has been changed on "
  1081. "a non-active transaction - this state "
  1082. "will be discarded."
  1083. )
  1084. boundary._restore_snapshot(dirty_only=boundary.nested)
  1085. with self._expect_state(SessionTransactionState.CLOSED):
  1086. self.close()
  1087. if self._parent and _capture_exception:
  1088. self._parent._rollback_exception = sys.exc_info()[1]
  1089. if rollback_err and rollback_err[1]:
  1090. raise rollback_err[1].with_traceback(rollback_err[2])
  1091. sess.dispatch.after_soft_rollback(sess, self)
  1092. if _to_root and self._parent:
  1093. self._parent.rollback(_to_root=True)
  1094. @_StateChange.declare_states(
  1095. _StateChangeStates.ANY, SessionTransactionState.CLOSED
  1096. )
  1097. def close(self, invalidate: bool = False) -> None:
  1098. if self.nested:
  1099. self.session._nested_transaction = (
  1100. self._previous_nested_transaction
  1101. )
  1102. self.session._transaction = self._parent
  1103. for connection, transaction, should_commit, autoclose in set(
  1104. self._connections.values()
  1105. ):
  1106. if invalidate and self._parent is None:
  1107. connection.invalidate()
  1108. if should_commit and transaction.is_active:
  1109. transaction.close()
  1110. if autoclose and self._parent is None:
  1111. connection.close()
  1112. self._state = SessionTransactionState.CLOSED
  1113. sess = self.session
  1114. # TODO: these two None sets were historically after the
  1115. # event hook below, and in 2.0 I changed it this way for some reason,
  1116. # and I remember there being a reason, but not what it was.
  1117. # Why do we need to get rid of them at all? test_memusage::CycleTest
  1118. # passes with these commented out.
  1119. # self.session = None # type: ignore
  1120. # self._connections = None # type: ignore
  1121. sess.dispatch.after_transaction_end(sess, self)
  1122. def _get_subject(self) -> Session:
  1123. return self.session
  1124. def _transaction_is_active(self) -> bool:
  1125. return self._state is SessionTransactionState.ACTIVE
  1126. def _transaction_is_closed(self) -> bool:
  1127. return self._state is SessionTransactionState.CLOSED
  1128. def _rollback_can_be_called(self) -> bool:
  1129. return self._state not in (COMMITTED, CLOSED)
  1130. class _SessionCloseState(Enum):
  1131. ACTIVE = 1
  1132. CLOSED = 2
  1133. CLOSE_IS_RESET = 3
  1134. class Session(_SessionClassMethods, EventTarget):
  1135. """Manages persistence operations for ORM-mapped objects.
  1136. The :class:`_orm.Session` is **not safe for use in concurrent threads.**.
  1137. See :ref:`session_faq_threadsafe` for background.
  1138. The Session's usage paradigm is described at :doc:`/orm/session`.
  1139. """
  1140. _is_asyncio = False
  1141. dispatch: dispatcher[Session]
  1142. identity_map: IdentityMap
  1143. """A mapping of object identities to objects themselves.
  1144. Iterating through ``Session.identity_map.values()`` provides
  1145. access to the full set of persistent objects (i.e., those
  1146. that have row identity) currently in the session.
  1147. .. seealso::
  1148. :func:`.identity_key` - helper function to produce the keys used
  1149. in this dictionary.
  1150. """
  1151. _new: Dict[InstanceState[Any], Any]
  1152. _deleted: Dict[InstanceState[Any], Any]
  1153. bind: Optional[Union[Engine, Connection]]
  1154. __binds: Dict[_SessionBindKey, _SessionBind]
  1155. _flushing: bool
  1156. _warn_on_events: bool
  1157. _transaction: Optional[SessionTransaction]
  1158. _nested_transaction: Optional[SessionTransaction]
  1159. hash_key: int
  1160. autoflush: bool
  1161. expire_on_commit: bool
  1162. enable_baked_queries: bool
  1163. twophase: bool
  1164. join_transaction_mode: JoinTransactionMode
  1165. _query_cls: Type[Query[Any]]
  1166. _close_state: _SessionCloseState
  1167. def __init__(
  1168. self,
  1169. bind: Optional[_SessionBind] = None,
  1170. *,
  1171. autoflush: bool = True,
  1172. future: Literal[True] = True,
  1173. expire_on_commit: bool = True,
  1174. autobegin: bool = True,
  1175. twophase: bool = False,
  1176. binds: Optional[Dict[_SessionBindKey, _SessionBind]] = None,
  1177. enable_baked_queries: bool = True,
  1178. info: Optional[_InfoType] = None,
  1179. query_cls: Optional[Type[Query[Any]]] = None,
  1180. autocommit: Literal[False] = False,
  1181. join_transaction_mode: JoinTransactionMode = "conditional_savepoint",
  1182. close_resets_only: Union[bool, _NoArg] = _NoArg.NO_ARG,
  1183. ):
  1184. r"""Construct a new :class:`_orm.Session`.
  1185. See also the :class:`.sessionmaker` function which is used to
  1186. generate a :class:`.Session`-producing callable with a given
  1187. set of arguments.
  1188. :param autoflush: When ``True``, all query operations will issue a
  1189. :meth:`~.Session.flush` call to this ``Session`` before proceeding.
  1190. This is a convenience feature so that :meth:`~.Session.flush` need
  1191. not be called repeatedly in order for database queries to retrieve
  1192. results.
  1193. .. seealso::
  1194. :ref:`session_flushing` - additional background on autoflush
  1195. :param autobegin: Automatically start transactions (i.e. equivalent to
  1196. invoking :meth:`_orm.Session.begin`) when database access is
  1197. requested by an operation. Defaults to ``True``. Set to
  1198. ``False`` to prevent a :class:`_orm.Session` from implicitly
  1199. beginning transactions after construction, as well as after any of
  1200. the :meth:`_orm.Session.rollback`, :meth:`_orm.Session.commit`,
  1201. or :meth:`_orm.Session.close` methods are called.
  1202. .. versionadded:: 2.0
  1203. .. seealso::
  1204. :ref:`session_autobegin_disable`
  1205. :param bind: An optional :class:`_engine.Engine` or
  1206. :class:`_engine.Connection` to
  1207. which this ``Session`` should be bound. When specified, all SQL
  1208. operations performed by this session will execute via this
  1209. connectable.
  1210. :param binds: A dictionary which may specify any number of
  1211. :class:`_engine.Engine` or :class:`_engine.Connection`
  1212. objects as the source of
  1213. connectivity for SQL operations on a per-entity basis. The keys
  1214. of the dictionary consist of any series of mapped classes,
  1215. arbitrary Python classes that are bases for mapped classes,
  1216. :class:`_schema.Table` objects and :class:`_orm.Mapper` objects.
  1217. The
  1218. values of the dictionary are then instances of
  1219. :class:`_engine.Engine`
  1220. or less commonly :class:`_engine.Connection` objects.
  1221. Operations which
  1222. proceed relative to a particular mapped class will consult this
  1223. dictionary for the closest matching entity in order to determine
  1224. which :class:`_engine.Engine` should be used for a particular SQL
  1225. operation. The complete heuristics for resolution are
  1226. described at :meth:`.Session.get_bind`. Usage looks like::
  1227. Session = sessionmaker(binds={
  1228. SomeMappedClass: create_engine('postgresql+psycopg2://engine1'),
  1229. SomeDeclarativeBase: create_engine('postgresql+psycopg2://engine2'),
  1230. some_mapper: create_engine('postgresql+psycopg2://engine3'),
  1231. some_table: create_engine('postgresql+psycopg2://engine4'),
  1232. })
  1233. .. seealso::
  1234. :ref:`session_partitioning`
  1235. :meth:`.Session.bind_mapper`
  1236. :meth:`.Session.bind_table`
  1237. :meth:`.Session.get_bind`
  1238. :param \class_: Specify an alternate class other than
  1239. ``sqlalchemy.orm.session.Session`` which should be used by the
  1240. returned class. This is the only argument that is local to the
  1241. :class:`.sessionmaker` function, and is not sent directly to the
  1242. constructor for ``Session``.
  1243. :param enable_baked_queries: legacy; defaults to ``True``.
  1244. A parameter consumed
  1245. by the :mod:`sqlalchemy.ext.baked` extension to determine if
  1246. "baked queries" should be cached, as is the normal operation
  1247. of this extension. When set to ``False``, caching as used by
  1248. this particular extension is disabled.
  1249. .. versionchanged:: 1.4 The ``sqlalchemy.ext.baked`` extension is
  1250. legacy and is not used by any of SQLAlchemy's internals. This
  1251. flag therefore only affects applications that are making explicit
  1252. use of this extension within their own code.
  1253. :param expire_on_commit: Defaults to ``True``. When ``True``, all
  1254. instances will be fully expired after each :meth:`~.commit`,
  1255. so that all attribute/object access subsequent to a completed
  1256. transaction will load from the most recent database state.
  1257. .. seealso::
  1258. :ref:`session_committing`
  1259. :param future: Deprecated; this flag is always True.
  1260. .. seealso::
  1261. :ref:`migration_20_toplevel`
  1262. :param info: optional dictionary of arbitrary data to be associated
  1263. with this :class:`.Session`. Is available via the
  1264. :attr:`.Session.info` attribute. Note the dictionary is copied at
  1265. construction time so that modifications to the per-
  1266. :class:`.Session` dictionary will be local to that
  1267. :class:`.Session`.
  1268. :param query_cls: Class which should be used to create new Query
  1269. objects, as returned by the :meth:`~.Session.query` method.
  1270. Defaults to :class:`_query.Query`.
  1271. :param twophase: When ``True``, all transactions will be started as
  1272. a "two phase" transaction, i.e. using the "two phase" semantics
  1273. of the database in use along with an XID. During a
  1274. :meth:`~.commit`, after :meth:`~.flush` has been issued for all
  1275. attached databases, the :meth:`~.TwoPhaseTransaction.prepare`
  1276. method on each database's :class:`.TwoPhaseTransaction` will be
  1277. called. This allows each database to roll back the entire
  1278. transaction, before each transaction is committed.
  1279. :param autocommit: the "autocommit" keyword is present for backwards
  1280. compatibility but must remain at its default value of ``False``.
  1281. :param join_transaction_mode: Describes the transactional behavior to
  1282. take when a given bind is a :class:`_engine.Connection` that
  1283. has already begun a transaction outside the scope of this
  1284. :class:`_orm.Session`; in other words the
  1285. :meth:`_engine.Connection.in_transaction()` method returns True.
  1286. The following behaviors only take effect when the :class:`_orm.Session`
  1287. **actually makes use of the connection given**; that is, a method
  1288. such as :meth:`_orm.Session.execute`, :meth:`_orm.Session.connection`,
  1289. etc. are actually invoked:
  1290. * ``"conditional_savepoint"`` - this is the default. if the given
  1291. :class:`_engine.Connection` is begun within a transaction but
  1292. does not have a SAVEPOINT, then ``"rollback_only"`` is used.
  1293. If the :class:`_engine.Connection` is additionally within
  1294. a SAVEPOINT, in other words
  1295. :meth:`_engine.Connection.in_nested_transaction()` method returns
  1296. True, then ``"create_savepoint"`` is used.
  1297. ``"conditional_savepoint"`` behavior attempts to make use of
  1298. savepoints in order to keep the state of the existing transaction
  1299. unchanged, but only if there is already a savepoint in progress;
  1300. otherwise, it is not assumed that the backend in use has adequate
  1301. support for SAVEPOINT, as availability of this feature varies.
  1302. ``"conditional_savepoint"`` also seeks to establish approximate
  1303. backwards compatibility with previous :class:`_orm.Session`
  1304. behavior, for applications that are not setting a specific mode. It
  1305. is recommended that one of the explicit settings be used.
  1306. * ``"create_savepoint"`` - the :class:`_orm.Session` will use
  1307. :meth:`_engine.Connection.begin_nested()` in all cases to create
  1308. its own transaction. This transaction by its nature rides
  1309. "on top" of any existing transaction that's opened on the given
  1310. :class:`_engine.Connection`; if the underlying database and
  1311. the driver in use has full, non-broken support for SAVEPOINT, the
  1312. external transaction will remain unaffected throughout the
  1313. lifespan of the :class:`_orm.Session`.
  1314. The ``"create_savepoint"`` mode is the most useful for integrating
  1315. a :class:`_orm.Session` into a test suite where an externally
  1316. initiated transaction should remain unaffected; however, it relies
  1317. on proper SAVEPOINT support from the underlying driver and
  1318. database.
  1319. .. tip:: When using SQLite, the SQLite driver included through
  1320. Python 3.11 does not handle SAVEPOINTs correctly in all cases
  1321. without workarounds. See the sections
  1322. :ref:`pysqlite_serializable` and :ref:`aiosqlite_serializable`
  1323. for details on current workarounds.
  1324. * ``"control_fully"`` - the :class:`_orm.Session` will take
  1325. control of the given transaction as its own;
  1326. :meth:`_orm.Session.commit` will call ``.commit()`` on the
  1327. transaction, :meth:`_orm.Session.rollback` will call
  1328. ``.rollback()`` on the transaction, :meth:`_orm.Session.close` will
  1329. call ``.rollback`` on the transaction.
  1330. .. tip:: This mode of use is equivalent to how SQLAlchemy 1.4 would
  1331. handle a :class:`_engine.Connection` given with an existing
  1332. SAVEPOINT (i.e. :meth:`_engine.Connection.begin_nested`); the
  1333. :class:`_orm.Session` would take full control of the existing
  1334. SAVEPOINT.
  1335. * ``"rollback_only"`` - the :class:`_orm.Session` will take control
  1336. of the given transaction for ``.rollback()`` calls only;
  1337. ``.commit()`` calls will not be propagated to the given
  1338. transaction. ``.close()`` calls will have no effect on the
  1339. given transaction.
  1340. .. tip:: This mode of use is equivalent to how SQLAlchemy 1.4 would
  1341. handle a :class:`_engine.Connection` given with an existing
  1342. regular database transaction (i.e.
  1343. :meth:`_engine.Connection.begin`); the :class:`_orm.Session`
  1344. would propagate :meth:`_orm.Session.rollback` calls to the
  1345. underlying transaction, but not :meth:`_orm.Session.commit` or
  1346. :meth:`_orm.Session.close` calls.
  1347. .. versionadded:: 2.0.0rc1
  1348. :param close_resets_only: Defaults to ``True``. Determines if
  1349. the session should reset itself after calling ``.close()``
  1350. or should pass in a no longer usable state, disabling re-use.
  1351. .. versionadded:: 2.0.22 added flag ``close_resets_only``.
  1352. A future SQLAlchemy version may change the default value of
  1353. this flag to ``False``.
  1354. .. seealso::
  1355. :ref:`session_closing` - Detail on the semantics of
  1356. :meth:`_orm.Session.close` and :meth:`_orm.Session.reset`.
  1357. """ # noqa
  1358. # considering allowing the "autocommit" keyword to still be accepted
  1359. # as long as it's False, so that external test suites, oslo.db etc
  1360. # continue to function as the argument appears to be passed in lots
  1361. # of cases including in our own test suite
  1362. if autocommit:
  1363. raise sa_exc.ArgumentError(
  1364. "autocommit=True is no longer supported"
  1365. )
  1366. self.identity_map = identity.WeakInstanceDict()
  1367. if not future:
  1368. raise sa_exc.ArgumentError(
  1369. "The 'future' parameter passed to "
  1370. "Session() may only be set to True."
  1371. )
  1372. self._new = {} # InstanceState->object, strong refs object
  1373. self._deleted = {} # same
  1374. self.bind = bind
  1375. self.__binds = {}
  1376. self._flushing = False
  1377. self._warn_on_events = False
  1378. self._transaction = None
  1379. self._nested_transaction = None
  1380. self.hash_key = _new_sessionid()
  1381. self.autobegin = autobegin
  1382. self.autoflush = autoflush
  1383. self.expire_on_commit = expire_on_commit
  1384. self.enable_baked_queries = enable_baked_queries
  1385. # the idea is that at some point NO_ARG will warn that in the future
  1386. # the default will switch to close_resets_only=False.
  1387. if close_resets_only or close_resets_only is _NoArg.NO_ARG:
  1388. self._close_state = _SessionCloseState.CLOSE_IS_RESET
  1389. else:
  1390. self._close_state = _SessionCloseState.ACTIVE
  1391. if (
  1392. join_transaction_mode
  1393. and join_transaction_mode
  1394. not in JoinTransactionMode.__args__ # type: ignore
  1395. ):
  1396. raise sa_exc.ArgumentError(
  1397. f"invalid selection for join_transaction_mode: "
  1398. f'"{join_transaction_mode}"'
  1399. )
  1400. self.join_transaction_mode = join_transaction_mode
  1401. self.twophase = twophase
  1402. self._query_cls = query_cls if query_cls else query.Query
  1403. if info:
  1404. self.info.update(info)
  1405. if binds is not None:
  1406. for key, bind in binds.items():
  1407. self._add_bind(key, bind)
  1408. _sessions[self.hash_key] = self
  1409. # used by sqlalchemy.engine.util.TransactionalContext
  1410. _trans_context_manager: Optional[TransactionalContext] = None
  1411. connection_callable: Optional[_ConnectionCallableProto] = None
  1412. def __enter__(self: _S) -> _S:
  1413. return self
  1414. def __exit__(self, type_: Any, value: Any, traceback: Any) -> None:
  1415. self.close()
  1416. @contextlib.contextmanager
  1417. def _maker_context_manager(self: _S) -> Iterator[_S]:
  1418. with self:
  1419. with self.begin():
  1420. yield self
  1421. def in_transaction(self) -> bool:
  1422. """Return True if this :class:`_orm.Session` has begun a transaction.
  1423. .. versionadded:: 1.4
  1424. .. seealso::
  1425. :attr:`_orm.Session.is_active`
  1426. """
  1427. return self._transaction is not None
  1428. def in_nested_transaction(self) -> bool:
  1429. """Return True if this :class:`_orm.Session` has begun a nested
  1430. transaction, e.g. SAVEPOINT.
  1431. .. versionadded:: 1.4
  1432. """
  1433. return self._nested_transaction is not None
  1434. def get_transaction(self) -> Optional[SessionTransaction]:
  1435. """Return the current root transaction in progress, if any.
  1436. .. versionadded:: 1.4
  1437. """
  1438. trans = self._transaction
  1439. while trans is not None and trans._parent is not None:
  1440. trans = trans._parent
  1441. return trans
  1442. def get_nested_transaction(self) -> Optional[SessionTransaction]:
  1443. """Return the current nested transaction in progress, if any.
  1444. .. versionadded:: 1.4
  1445. """
  1446. return self._nested_transaction
  1447. @util.memoized_property
  1448. def info(self) -> _InfoType:
  1449. """A user-modifiable dictionary.
  1450. The initial value of this dictionary can be populated using the
  1451. ``info`` argument to the :class:`.Session` constructor or
  1452. :class:`.sessionmaker` constructor or factory methods. The dictionary
  1453. here is always local to this :class:`.Session` and can be modified
  1454. independently of all other :class:`.Session` objects.
  1455. """
  1456. return {}
  1457. def _autobegin_t(self, begin: bool = False) -> SessionTransaction:
  1458. if self._transaction is None:
  1459. if not begin and not self.autobegin:
  1460. raise sa_exc.InvalidRequestError(
  1461. "Autobegin is disabled on this Session; please call "
  1462. "session.begin() to start a new transaction"
  1463. )
  1464. trans = SessionTransaction(
  1465. self,
  1466. SessionTransactionOrigin.BEGIN
  1467. if begin
  1468. else SessionTransactionOrigin.AUTOBEGIN,
  1469. )
  1470. assert self._transaction is trans
  1471. return trans
  1472. return self._transaction
  1473. def begin(self, nested: bool = False) -> SessionTransaction:
  1474. """Begin a transaction, or nested transaction,
  1475. on this :class:`.Session`, if one is not already begun.
  1476. The :class:`_orm.Session` object features **autobegin** behavior,
  1477. so that normally it is not necessary to call the
  1478. :meth:`_orm.Session.begin`
  1479. method explicitly. However, it may be used in order to control
  1480. the scope of when the transactional state is begun.
  1481. When used to begin the outermost transaction, an error is raised
  1482. if this :class:`.Session` is already inside of a transaction.
  1483. :param nested: if True, begins a SAVEPOINT transaction and is
  1484. equivalent to calling :meth:`~.Session.begin_nested`. For
  1485. documentation on SAVEPOINT transactions, please see
  1486. :ref:`session_begin_nested`.
  1487. :return: the :class:`.SessionTransaction` object. Note that
  1488. :class:`.SessionTransaction`
  1489. acts as a Python context manager, allowing :meth:`.Session.begin`
  1490. to be used in a "with" block. See :ref:`session_explicit_begin` for
  1491. an example.
  1492. .. seealso::
  1493. :ref:`session_autobegin`
  1494. :ref:`unitofwork_transaction`
  1495. :meth:`.Session.begin_nested`
  1496. """
  1497. trans = self._transaction
  1498. if trans is None:
  1499. trans = self._autobegin_t(begin=True)
  1500. if not nested:
  1501. return trans
  1502. assert trans is not None
  1503. if nested:
  1504. trans = trans._begin(nested=nested)
  1505. assert self._transaction is trans
  1506. self._nested_transaction = trans
  1507. else:
  1508. raise sa_exc.InvalidRequestError(
  1509. "A transaction is already begun on this Session."
  1510. )
  1511. return trans # needed for __enter__/__exit__ hook
  1512. def begin_nested(self) -> SessionTransaction:
  1513. """Begin a "nested" transaction on this Session, e.g. SAVEPOINT.
  1514. The target database(s) and associated drivers must support SQL
  1515. SAVEPOINT for this method to function correctly.
  1516. For documentation on SAVEPOINT
  1517. transactions, please see :ref:`session_begin_nested`.
  1518. :return: the :class:`.SessionTransaction` object. Note that
  1519. :class:`.SessionTransaction` acts as a context manager, allowing
  1520. :meth:`.Session.begin_nested` to be used in a "with" block.
  1521. See :ref:`session_begin_nested` for a usage example.
  1522. .. seealso::
  1523. :ref:`session_begin_nested`
  1524. :ref:`pysqlite_serializable` - special workarounds required
  1525. with the SQLite driver in order for SAVEPOINT to work
  1526. correctly. For asyncio use cases, see the section
  1527. :ref:`aiosqlite_serializable`.
  1528. """
  1529. return self.begin(nested=True)
  1530. def rollback(self) -> None:
  1531. """Rollback the current transaction in progress.
  1532. If no transaction is in progress, this method is a pass-through.
  1533. The method always rolls back
  1534. the topmost database transaction, discarding any nested
  1535. transactions that may be in progress.
  1536. .. seealso::
  1537. :ref:`session_rollback`
  1538. :ref:`unitofwork_transaction`
  1539. """
  1540. if self._transaction is None:
  1541. pass
  1542. else:
  1543. self._transaction.rollback(_to_root=True)
  1544. def commit(self) -> None:
  1545. """Flush pending changes and commit the current transaction.
  1546. When the COMMIT operation is complete, all objects are fully
  1547. :term:`expired`, erasing their internal contents, which will be
  1548. automatically re-loaded when the objects are next accessed. In the
  1549. interim, these objects are in an expired state and will not function if
  1550. they are :term:`detached` from the :class:`.Session`. Additionally,
  1551. this re-load operation is not supported when using asyncio-oriented
  1552. APIs. The :paramref:`.Session.expire_on_commit` parameter may be used
  1553. to disable this behavior.
  1554. When there is no transaction in place for the :class:`.Session`,
  1555. indicating that no operations were invoked on this :class:`.Session`
  1556. since the previous call to :meth:`.Session.commit`, the method will
  1557. begin and commit an internal-only "logical" transaction, that does not
  1558. normally affect the database unless pending flush changes were
  1559. detected, but will still invoke event handlers and object expiration
  1560. rules.
  1561. The outermost database transaction is committed unconditionally,
  1562. automatically releasing any SAVEPOINTs in effect.
  1563. .. seealso::
  1564. :ref:`session_committing`
  1565. :ref:`unitofwork_transaction`
  1566. :ref:`asyncio_orm_avoid_lazyloads`
  1567. """
  1568. trans = self._transaction
  1569. if trans is None:
  1570. trans = self._autobegin_t()
  1571. trans.commit(_to_root=True)
  1572. def prepare(self) -> None:
  1573. """Prepare the current transaction in progress for two phase commit.
  1574. If no transaction is in progress, this method raises an
  1575. :exc:`~sqlalchemy.exc.InvalidRequestError`.
  1576. Only root transactions of two phase sessions can be prepared. If the
  1577. current transaction is not such, an
  1578. :exc:`~sqlalchemy.exc.InvalidRequestError` is raised.
  1579. """
  1580. trans = self._transaction
  1581. if trans is None:
  1582. trans = self._autobegin_t()
  1583. trans.prepare()
  1584. def connection(
  1585. self,
  1586. bind_arguments: Optional[_BindArguments] = None,
  1587. execution_options: Optional[CoreExecuteOptionsParameter] = None,
  1588. ) -> Connection:
  1589. r"""Return a :class:`_engine.Connection` object corresponding to this
  1590. :class:`.Session` object's transactional state.
  1591. Either the :class:`_engine.Connection` corresponding to the current
  1592. transaction is returned, or if no transaction is in progress, a new
  1593. one is begun and the :class:`_engine.Connection`
  1594. returned (note that no
  1595. transactional state is established with the DBAPI until the first
  1596. SQL statement is emitted).
  1597. Ambiguity in multi-bind or unbound :class:`.Session` objects can be
  1598. resolved through any of the optional keyword arguments. This
  1599. ultimately makes usage of the :meth:`.get_bind` method for resolution.
  1600. :param bind_arguments: dictionary of bind arguments. May include
  1601. "mapper", "bind", "clause", other custom arguments that are passed
  1602. to :meth:`.Session.get_bind`.
  1603. :param execution_options: a dictionary of execution options that will
  1604. be passed to :meth:`_engine.Connection.execution_options`, **when the
  1605. connection is first procured only**. If the connection is already
  1606. present within the :class:`.Session`, a warning is emitted and
  1607. the arguments are ignored.
  1608. .. seealso::
  1609. :ref:`session_transaction_isolation`
  1610. """
  1611. if bind_arguments:
  1612. bind = bind_arguments.pop("bind", None)
  1613. if bind is None:
  1614. bind = self.get_bind(**bind_arguments)
  1615. else:
  1616. bind = self.get_bind()
  1617. return self._connection_for_bind(
  1618. bind,
  1619. execution_options=execution_options,
  1620. )
  1621. def _connection_for_bind(
  1622. self,
  1623. engine: _SessionBind,
  1624. execution_options: Optional[CoreExecuteOptionsParameter] = None,
  1625. **kw: Any,
  1626. ) -> Connection:
  1627. TransactionalContext._trans_ctx_check(self)
  1628. trans = self._transaction
  1629. if trans is None:
  1630. trans = self._autobegin_t()
  1631. return trans._connection_for_bind(engine, execution_options)
  1632. @overload
  1633. def _execute_internal(
  1634. self,
  1635. statement: Executable,
  1636. params: Optional[_CoreSingleExecuteParams] = None,
  1637. *,
  1638. execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
  1639. bind_arguments: Optional[_BindArguments] = None,
  1640. _parent_execute_state: Optional[Any] = None,
  1641. _add_event: Optional[Any] = None,
  1642. _scalar_result: Literal[True] = ...,
  1643. ) -> Any:
  1644. ...
  1645. @overload
  1646. def _execute_internal(
  1647. self,
  1648. statement: Executable,
  1649. params: Optional[_CoreAnyExecuteParams] = None,
  1650. *,
  1651. execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
  1652. bind_arguments: Optional[_BindArguments] = None,
  1653. _parent_execute_state: Optional[Any] = None,
  1654. _add_event: Optional[Any] = None,
  1655. _scalar_result: bool = ...,
  1656. ) -> Result[Any]:
  1657. ...
  1658. def _execute_internal(
  1659. self,
  1660. statement: Executable,
  1661. params: Optional[_CoreAnyExecuteParams] = None,
  1662. *,
  1663. execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
  1664. bind_arguments: Optional[_BindArguments] = None,
  1665. _parent_execute_state: Optional[Any] = None,
  1666. _add_event: Optional[Any] = None,
  1667. _scalar_result: bool = False,
  1668. ) -> Any:
  1669. statement = coercions.expect(roles.StatementRole, statement)
  1670. if not bind_arguments:
  1671. bind_arguments = {}
  1672. else:
  1673. bind_arguments = dict(bind_arguments)
  1674. if (
  1675. statement._propagate_attrs.get("compile_state_plugin", None)
  1676. == "orm"
  1677. ):
  1678. compile_state_cls = CompileState._get_plugin_class_for_plugin(
  1679. statement, "orm"
  1680. )
  1681. if TYPE_CHECKING:
  1682. assert isinstance(
  1683. compile_state_cls, context.AbstractORMCompileState
  1684. )
  1685. else:
  1686. compile_state_cls = None
  1687. bind_arguments.setdefault("clause", statement)
  1688. execution_options = util.coerce_to_immutabledict(execution_options)
  1689. if _parent_execute_state:
  1690. events_todo = _parent_execute_state._remaining_events()
  1691. else:
  1692. events_todo = self.dispatch.do_orm_execute
  1693. if _add_event:
  1694. events_todo = list(events_todo) + [_add_event]
  1695. if events_todo:
  1696. if compile_state_cls is not None:
  1697. # for event handlers, do the orm_pre_session_exec
  1698. # pass ahead of the event handlers, so that things like
  1699. # .load_options, .update_delete_options etc. are populated.
  1700. # is_pre_event=True allows the hook to hold off on things
  1701. # it doesn't want to do twice, including autoflush as well
  1702. # as "pre fetch" for DML, etc.
  1703. (
  1704. statement,
  1705. execution_options,
  1706. ) = compile_state_cls.orm_pre_session_exec(
  1707. self,
  1708. statement,
  1709. params,
  1710. execution_options,
  1711. bind_arguments,
  1712. True,
  1713. )
  1714. orm_exec_state = ORMExecuteState(
  1715. self,
  1716. statement,
  1717. params,
  1718. execution_options,
  1719. bind_arguments,
  1720. compile_state_cls,
  1721. events_todo,
  1722. )
  1723. for idx, fn in enumerate(events_todo):
  1724. orm_exec_state._starting_event_idx = idx
  1725. fn_result: Optional[Result[Any]] = fn(orm_exec_state)
  1726. if fn_result:
  1727. if _scalar_result:
  1728. return fn_result.scalar()
  1729. else:
  1730. return fn_result
  1731. statement = orm_exec_state.statement
  1732. execution_options = orm_exec_state.local_execution_options
  1733. if compile_state_cls is not None:
  1734. # now run orm_pre_session_exec() "for real". if there were
  1735. # event hooks, this will re-run the steps that interpret
  1736. # new execution_options into load_options / update_delete_options,
  1737. # which we assume the event hook might have updated.
  1738. # autoflush will also be invoked in this step if enabled.
  1739. (
  1740. statement,
  1741. execution_options,
  1742. ) = compile_state_cls.orm_pre_session_exec(
  1743. self,
  1744. statement,
  1745. params,
  1746. execution_options,
  1747. bind_arguments,
  1748. False,
  1749. )
  1750. bind = self.get_bind(**bind_arguments)
  1751. conn = self._connection_for_bind(bind)
  1752. if _scalar_result and not compile_state_cls:
  1753. if TYPE_CHECKING:
  1754. params = cast(_CoreSingleExecuteParams, params)
  1755. return conn.scalar(
  1756. statement, params or {}, execution_options=execution_options
  1757. )
  1758. if compile_state_cls:
  1759. result: Result[Any] = compile_state_cls.orm_execute_statement(
  1760. self,
  1761. statement,
  1762. params or {},
  1763. execution_options,
  1764. bind_arguments,
  1765. conn,
  1766. )
  1767. else:
  1768. result = conn.execute(
  1769. statement, params or {}, execution_options=execution_options
  1770. )
  1771. if _scalar_result:
  1772. return result.scalar()
  1773. else:
  1774. return result
  1775. @overload
  1776. def execute(
  1777. self,
  1778. statement: TypedReturnsRows[_T],
  1779. params: Optional[_CoreAnyExecuteParams] = None,
  1780. *,
  1781. execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
  1782. bind_arguments: Optional[_BindArguments] = None,
  1783. _parent_execute_state: Optional[Any] = None,
  1784. _add_event: Optional[Any] = None,
  1785. ) -> Result[_T]:
  1786. ...
  1787. @overload
  1788. def execute(
  1789. self,
  1790. statement: UpdateBase,
  1791. params: Optional[_CoreAnyExecuteParams] = None,
  1792. *,
  1793. execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
  1794. bind_arguments: Optional[_BindArguments] = None,
  1795. _parent_execute_state: Optional[Any] = None,
  1796. _add_event: Optional[Any] = None,
  1797. ) -> CursorResult[Any]:
  1798. ...
  1799. @overload
  1800. def execute(
  1801. self,
  1802. statement: Executable,
  1803. params: Optional[_CoreAnyExecuteParams] = None,
  1804. *,
  1805. execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
  1806. bind_arguments: Optional[_BindArguments] = None,
  1807. _parent_execute_state: Optional[Any] = None,
  1808. _add_event: Optional[Any] = None,
  1809. ) -> Result[Any]:
  1810. ...
  1811. def execute(
  1812. self,
  1813. statement: Executable,
  1814. params: Optional[_CoreAnyExecuteParams] = None,
  1815. *,
  1816. execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
  1817. bind_arguments: Optional[_BindArguments] = None,
  1818. _parent_execute_state: Optional[Any] = None,
  1819. _add_event: Optional[Any] = None,
  1820. ) -> Result[Any]:
  1821. r"""Execute a SQL expression construct.
  1822. Returns a :class:`_engine.Result` object representing
  1823. results of the statement execution.
  1824. E.g.::
  1825. from sqlalchemy import select
  1826. result = session.execute(
  1827. select(User).where(User.id == 5)
  1828. )
  1829. The API contract of :meth:`_orm.Session.execute` is similar to that
  1830. of :meth:`_engine.Connection.execute`, the :term:`2.0 style` version
  1831. of :class:`_engine.Connection`.
  1832. .. versionchanged:: 1.4 the :meth:`_orm.Session.execute` method is
  1833. now the primary point of ORM statement execution when using
  1834. :term:`2.0 style` ORM usage.
  1835. :param statement:
  1836. An executable statement (i.e. an :class:`.Executable` expression
  1837. such as :func:`_expression.select`).
  1838. :param params:
  1839. Optional dictionary, or list of dictionaries, containing
  1840. bound parameter values. If a single dictionary, single-row
  1841. execution occurs; if a list of dictionaries, an
  1842. "executemany" will be invoked. The keys in each dictionary
  1843. must correspond to parameter names present in the statement.
  1844. :param execution_options: optional dictionary of execution options,
  1845. which will be associated with the statement execution. This
  1846. dictionary can provide a subset of the options that are accepted
  1847. by :meth:`_engine.Connection.execution_options`, and may also
  1848. provide additional options understood only in an ORM context.
  1849. .. seealso::
  1850. :ref:`orm_queryguide_execution_options` - ORM-specific execution
  1851. options
  1852. :param bind_arguments: dictionary of additional arguments to determine
  1853. the bind. May include "mapper", "bind", or other custom arguments.
  1854. Contents of this dictionary are passed to the
  1855. :meth:`.Session.get_bind` method.
  1856. :return: a :class:`_engine.Result` object.
  1857. """
  1858. return self._execute_internal(
  1859. statement,
  1860. params,
  1861. execution_options=execution_options,
  1862. bind_arguments=bind_arguments,
  1863. _parent_execute_state=_parent_execute_state,
  1864. _add_event=_add_event,
  1865. )
  1866. @overload
  1867. def scalar(
  1868. self,
  1869. statement: TypedReturnsRows[Tuple[_T]],
  1870. params: Optional[_CoreSingleExecuteParams] = None,
  1871. *,
  1872. execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
  1873. bind_arguments: Optional[_BindArguments] = None,
  1874. **kw: Any,
  1875. ) -> Optional[_T]:
  1876. ...
  1877. @overload
  1878. def scalar(
  1879. self,
  1880. statement: Executable,
  1881. params: Optional[_CoreSingleExecuteParams] = None,
  1882. *,
  1883. execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
  1884. bind_arguments: Optional[_BindArguments] = None,
  1885. **kw: Any,
  1886. ) -> Any:
  1887. ...
  1888. def scalar(
  1889. self,
  1890. statement: Executable,
  1891. params: Optional[_CoreSingleExecuteParams] = None,
  1892. *,
  1893. execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
  1894. bind_arguments: Optional[_BindArguments] = None,
  1895. **kw: Any,
  1896. ) -> Any:
  1897. """Execute a statement and return a scalar result.
  1898. Usage and parameters are the same as that of
  1899. :meth:`_orm.Session.execute`; the return result is a scalar Python
  1900. value.
  1901. """
  1902. return self._execute_internal(
  1903. statement,
  1904. params,
  1905. execution_options=execution_options,
  1906. bind_arguments=bind_arguments,
  1907. _scalar_result=True,
  1908. **kw,
  1909. )
  1910. @overload
  1911. def scalars(
  1912. self,
  1913. statement: TypedReturnsRows[Tuple[_T]],
  1914. params: Optional[_CoreAnyExecuteParams] = None,
  1915. *,
  1916. execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
  1917. bind_arguments: Optional[_BindArguments] = None,
  1918. **kw: Any,
  1919. ) -> ScalarResult[_T]:
  1920. ...
  1921. @overload
  1922. def scalars(
  1923. self,
  1924. statement: Executable,
  1925. params: Optional[_CoreAnyExecuteParams] = None,
  1926. *,
  1927. execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
  1928. bind_arguments: Optional[_BindArguments] = None,
  1929. **kw: Any,
  1930. ) -> ScalarResult[Any]:
  1931. ...
  1932. def scalars(
  1933. self,
  1934. statement: Executable,
  1935. params: Optional[_CoreAnyExecuteParams] = None,
  1936. *,
  1937. execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
  1938. bind_arguments: Optional[_BindArguments] = None,
  1939. **kw: Any,
  1940. ) -> ScalarResult[Any]:
  1941. """Execute a statement and return the results as scalars.
  1942. Usage and parameters are the same as that of
  1943. :meth:`_orm.Session.execute`; the return result is a
  1944. :class:`_result.ScalarResult` filtering object which
  1945. will return single elements rather than :class:`_row.Row` objects.
  1946. :return: a :class:`_result.ScalarResult` object
  1947. .. versionadded:: 1.4.24 Added :meth:`_orm.Session.scalars`
  1948. .. versionadded:: 1.4.26 Added :meth:`_orm.scoped_session.scalars`
  1949. .. seealso::
  1950. :ref:`orm_queryguide_select_orm_entities` - contrasts the behavior
  1951. of :meth:`_orm.Session.execute` to :meth:`_orm.Session.scalars`
  1952. """
  1953. return self._execute_internal(
  1954. statement,
  1955. params=params,
  1956. execution_options=execution_options,
  1957. bind_arguments=bind_arguments,
  1958. _scalar_result=False, # mypy appreciates this
  1959. **kw,
  1960. ).scalars()
  1961. def close(self) -> None:
  1962. """Close out the transactional resources and ORM objects used by this
  1963. :class:`_orm.Session`.
  1964. This expunges all ORM objects associated with this
  1965. :class:`_orm.Session`, ends any transaction in progress and
  1966. :term:`releases` any :class:`_engine.Connection` objects which this
  1967. :class:`_orm.Session` itself has checked out from associated
  1968. :class:`_engine.Engine` objects. The operation then leaves the
  1969. :class:`_orm.Session` in a state which it may be used again.
  1970. .. tip::
  1971. In the default running mode the :meth:`_orm.Session.close`
  1972. method **does not prevent the Session from being used again**.
  1973. The :class:`_orm.Session` itself does not actually have a
  1974. distinct "closed" state; it merely means
  1975. the :class:`_orm.Session` will release all database connections
  1976. and ORM objects.
  1977. Setting the parameter :paramref:`_orm.Session.close_resets_only`
  1978. to ``False`` will instead make the ``close`` final, meaning that
  1979. any further action on the session will be forbidden.
  1980. .. versionchanged:: 1.4 The :meth:`.Session.close` method does not
  1981. immediately create a new :class:`.SessionTransaction` object;
  1982. instead, the new :class:`.SessionTransaction` is created only if
  1983. the :class:`.Session` is used again for a database operation.
  1984. .. seealso::
  1985. :ref:`session_closing` - detail on the semantics of
  1986. :meth:`_orm.Session.close` and :meth:`_orm.Session.reset`.
  1987. :meth:`_orm.Session.reset` - a similar method that behaves like
  1988. ``close()`` with the parameter
  1989. :paramref:`_orm.Session.close_resets_only` set to ``True``.
  1990. """
  1991. self._close_impl(invalidate=False)
  1992. def reset(self) -> None:
  1993. """Close out the transactional resources and ORM objects used by this
  1994. :class:`_orm.Session`, resetting the session to its initial state.
  1995. This method provides for same "reset-only" behavior that the
  1996. :meth:`_orm.Session.close` method has provided historically, where the
  1997. state of the :class:`_orm.Session` is reset as though the object were
  1998. brand new, and ready to be used again.
  1999. This method may then be useful for :class:`_orm.Session` objects
  2000. which set :paramref:`_orm.Session.close_resets_only` to ``False``,
  2001. so that "reset only" behavior is still available.
  2002. .. versionadded:: 2.0.22
  2003. .. seealso::
  2004. :ref:`session_closing` - detail on the semantics of
  2005. :meth:`_orm.Session.close` and :meth:`_orm.Session.reset`.
  2006. :meth:`_orm.Session.close` - a similar method will additionally
  2007. prevent re-use of the Session when the parameter
  2008. :paramref:`_orm.Session.close_resets_only` is set to ``False``.
  2009. """
  2010. self._close_impl(invalidate=False, is_reset=True)
  2011. def invalidate(self) -> None:
  2012. """Close this Session, using connection invalidation.
  2013. This is a variant of :meth:`.Session.close` that will additionally
  2014. ensure that the :meth:`_engine.Connection.invalidate`
  2015. method will be called on each :class:`_engine.Connection` object
  2016. that is currently in use for a transaction (typically there is only
  2017. one connection unless the :class:`_orm.Session` is used with
  2018. multiple engines).
  2019. This can be called when the database is known to be in a state where
  2020. the connections are no longer safe to be used.
  2021. Below illustrates a scenario when using `gevent
  2022. <https://www.gevent.org/>`_, which can produce ``Timeout`` exceptions
  2023. that may mean the underlying connection should be discarded::
  2024. import gevent
  2025. try:
  2026. sess = Session()
  2027. sess.add(User())
  2028. sess.commit()
  2029. except gevent.Timeout:
  2030. sess.invalidate()
  2031. raise
  2032. except:
  2033. sess.rollback()
  2034. raise
  2035. The method additionally does everything that :meth:`_orm.Session.close`
  2036. does, including that all ORM objects are expunged.
  2037. """
  2038. self._close_impl(invalidate=True)
  2039. def _close_impl(self, invalidate: bool, is_reset: bool = False) -> None:
  2040. if not is_reset and self._close_state is _SessionCloseState.ACTIVE:
  2041. self._close_state = _SessionCloseState.CLOSED
  2042. self.expunge_all()
  2043. if self._transaction is not None:
  2044. for transaction in self._transaction._iterate_self_and_parents():
  2045. transaction.close(invalidate)
  2046. def expunge_all(self) -> None:
  2047. """Remove all object instances from this ``Session``.
  2048. This is equivalent to calling ``expunge(obj)`` on all objects in this
  2049. ``Session``.
  2050. """
  2051. all_states = self.identity_map.all_states() + list(self._new)
  2052. self.identity_map._kill()
  2053. self.identity_map = identity.WeakInstanceDict()
  2054. self._new = {}
  2055. self._deleted = {}
  2056. statelib.InstanceState._detach_states(all_states, self)
  2057. def _add_bind(self, key: _SessionBindKey, bind: _SessionBind) -> None:
  2058. try:
  2059. insp = inspect(key)
  2060. except sa_exc.NoInspectionAvailable as err:
  2061. if not isinstance(key, type):
  2062. raise sa_exc.ArgumentError(
  2063. "Not an acceptable bind target: %s" % key
  2064. ) from err
  2065. else:
  2066. self.__binds[key] = bind
  2067. else:
  2068. if TYPE_CHECKING:
  2069. assert isinstance(insp, Inspectable)
  2070. if isinstance(insp, TableClause):
  2071. self.__binds[insp] = bind
  2072. elif insp_is_mapper(insp):
  2073. self.__binds[insp.class_] = bind
  2074. for _selectable in insp._all_tables:
  2075. self.__binds[_selectable] = bind
  2076. else:
  2077. raise sa_exc.ArgumentError(
  2078. "Not an acceptable bind target: %s" % key
  2079. )
  2080. def bind_mapper(
  2081. self, mapper: _EntityBindKey[_O], bind: _SessionBind
  2082. ) -> None:
  2083. """Associate a :class:`_orm.Mapper` or arbitrary Python class with a
  2084. "bind", e.g. an :class:`_engine.Engine` or
  2085. :class:`_engine.Connection`.
  2086. The given entity is added to a lookup used by the
  2087. :meth:`.Session.get_bind` method.
  2088. :param mapper: a :class:`_orm.Mapper` object,
  2089. or an instance of a mapped
  2090. class, or any Python class that is the base of a set of mapped
  2091. classes.
  2092. :param bind: an :class:`_engine.Engine` or :class:`_engine.Connection`
  2093. object.
  2094. .. seealso::
  2095. :ref:`session_partitioning`
  2096. :paramref:`.Session.binds`
  2097. :meth:`.Session.bind_table`
  2098. """
  2099. self._add_bind(mapper, bind)
  2100. def bind_table(self, table: TableClause, bind: _SessionBind) -> None:
  2101. """Associate a :class:`_schema.Table` with a "bind", e.g. an
  2102. :class:`_engine.Engine`
  2103. or :class:`_engine.Connection`.
  2104. The given :class:`_schema.Table` is added to a lookup used by the
  2105. :meth:`.Session.get_bind` method.
  2106. :param table: a :class:`_schema.Table` object,
  2107. which is typically the target
  2108. of an ORM mapping, or is present within a selectable that is
  2109. mapped.
  2110. :param bind: an :class:`_engine.Engine` or :class:`_engine.Connection`
  2111. object.
  2112. .. seealso::
  2113. :ref:`session_partitioning`
  2114. :paramref:`.Session.binds`
  2115. :meth:`.Session.bind_mapper`
  2116. """
  2117. self._add_bind(table, bind)
  2118. def get_bind(
  2119. self,
  2120. mapper: Optional[_EntityBindKey[_O]] = None,
  2121. *,
  2122. clause: Optional[ClauseElement] = None,
  2123. bind: Optional[_SessionBind] = None,
  2124. _sa_skip_events: Optional[bool] = None,
  2125. _sa_skip_for_implicit_returning: bool = False,
  2126. **kw: Any,
  2127. ) -> Union[Engine, Connection]:
  2128. """Return a "bind" to which this :class:`.Session` is bound.
  2129. The "bind" is usually an instance of :class:`_engine.Engine`,
  2130. except in the case where the :class:`.Session` has been
  2131. explicitly bound directly to a :class:`_engine.Connection`.
  2132. For a multiply-bound or unbound :class:`.Session`, the
  2133. ``mapper`` or ``clause`` arguments are used to determine the
  2134. appropriate bind to return.
  2135. Note that the "mapper" argument is usually present
  2136. when :meth:`.Session.get_bind` is called via an ORM
  2137. operation such as a :meth:`.Session.query`, each
  2138. individual INSERT/UPDATE/DELETE operation within a
  2139. :meth:`.Session.flush`, call, etc.
  2140. The order of resolution is:
  2141. 1. if mapper given and :paramref:`.Session.binds` is present,
  2142. locate a bind based first on the mapper in use, then
  2143. on the mapped class in use, then on any base classes that are
  2144. present in the ``__mro__`` of the mapped class, from more specific
  2145. superclasses to more general.
  2146. 2. if clause given and ``Session.binds`` is present,
  2147. locate a bind based on :class:`_schema.Table` objects
  2148. found in the given clause present in ``Session.binds``.
  2149. 3. if ``Session.binds`` is present, return that.
  2150. 4. if clause given, attempt to return a bind
  2151. linked to the :class:`_schema.MetaData` ultimately
  2152. associated with the clause.
  2153. 5. if mapper given, attempt to return a bind
  2154. linked to the :class:`_schema.MetaData` ultimately
  2155. associated with the :class:`_schema.Table` or other
  2156. selectable to which the mapper is mapped.
  2157. 6. No bind can be found, :exc:`~sqlalchemy.exc.UnboundExecutionError`
  2158. is raised.
  2159. Note that the :meth:`.Session.get_bind` method can be overridden on
  2160. a user-defined subclass of :class:`.Session` to provide any kind
  2161. of bind resolution scheme. See the example at
  2162. :ref:`session_custom_partitioning`.
  2163. :param mapper:
  2164. Optional mapped class or corresponding :class:`_orm.Mapper` instance.
  2165. The bind can be derived from a :class:`_orm.Mapper` first by
  2166. consulting the "binds" map associated with this :class:`.Session`,
  2167. and secondly by consulting the :class:`_schema.MetaData` associated
  2168. with the :class:`_schema.Table` to which the :class:`_orm.Mapper` is
  2169. mapped for a bind.
  2170. :param clause:
  2171. A :class:`_expression.ClauseElement` (i.e.
  2172. :func:`_expression.select`,
  2173. :func:`_expression.text`,
  2174. etc.). If the ``mapper`` argument is not present or could not
  2175. produce a bind, the given expression construct will be searched
  2176. for a bound element, typically a :class:`_schema.Table`
  2177. associated with
  2178. bound :class:`_schema.MetaData`.
  2179. .. seealso::
  2180. :ref:`session_partitioning`
  2181. :paramref:`.Session.binds`
  2182. :meth:`.Session.bind_mapper`
  2183. :meth:`.Session.bind_table`
  2184. """
  2185. # this function is documented as a subclassing hook, so we have
  2186. # to call this method even if the return is simple
  2187. if bind:
  2188. return bind
  2189. elif not self.__binds and self.bind:
  2190. # simplest and most common case, we have a bind and no
  2191. # per-mapper/table binds, we're done
  2192. return self.bind
  2193. # we don't have self.bind and either have self.__binds
  2194. # or we don't have self.__binds (which is legacy). Look at the
  2195. # mapper and the clause
  2196. if mapper is None and clause is None:
  2197. if self.bind:
  2198. return self.bind
  2199. else:
  2200. raise sa_exc.UnboundExecutionError(
  2201. "This session is not bound to a single Engine or "
  2202. "Connection, and no context was provided to locate "
  2203. "a binding."
  2204. )
  2205. # look more closely at the mapper.
  2206. if mapper is not None:
  2207. try:
  2208. inspected_mapper = inspect(mapper)
  2209. except sa_exc.NoInspectionAvailable as err:
  2210. if isinstance(mapper, type):
  2211. raise exc.UnmappedClassError(mapper) from err
  2212. else:
  2213. raise
  2214. else:
  2215. inspected_mapper = None
  2216. # match up the mapper or clause in the __binds
  2217. if self.__binds:
  2218. # matching mappers and selectables to entries in the
  2219. # binds dictionary; supported use case.
  2220. if inspected_mapper:
  2221. for cls in inspected_mapper.class_.__mro__:
  2222. if cls in self.__binds:
  2223. return self.__binds[cls]
  2224. if clause is None:
  2225. clause = inspected_mapper.persist_selectable
  2226. if clause is not None:
  2227. plugin_subject = clause._propagate_attrs.get(
  2228. "plugin_subject", None
  2229. )
  2230. if plugin_subject is not None:
  2231. for cls in plugin_subject.mapper.class_.__mro__:
  2232. if cls in self.__binds:
  2233. return self.__binds[cls]
  2234. for obj in visitors.iterate(clause):
  2235. if obj in self.__binds:
  2236. if TYPE_CHECKING:
  2237. assert isinstance(obj, Table)
  2238. return self.__binds[obj]
  2239. # none of the __binds matched, but we have a fallback bind.
  2240. # return that
  2241. if self.bind:
  2242. return self.bind
  2243. context = []
  2244. if inspected_mapper is not None:
  2245. context.append(f"mapper {inspected_mapper}")
  2246. if clause is not None:
  2247. context.append("SQL expression")
  2248. raise sa_exc.UnboundExecutionError(
  2249. f"Could not locate a bind configured on "
  2250. f'{", ".join(context)} or this Session.'
  2251. )
  2252. @overload
  2253. def query(self, _entity: _EntityType[_O]) -> Query[_O]:
  2254. ...
  2255. @overload
  2256. def query(
  2257. self, _colexpr: TypedColumnsClauseRole[_T]
  2258. ) -> RowReturningQuery[Tuple[_T]]:
  2259. ...
  2260. # START OVERLOADED FUNCTIONS self.query RowReturningQuery 2-8
  2261. # code within this block is **programmatically,
  2262. # statically generated** by tools/generate_tuple_map_overloads.py
  2263. @overload
  2264. def query(
  2265. self, __ent0: _TCCA[_T0], __ent1: _TCCA[_T1]
  2266. ) -> RowReturningQuery[Tuple[_T0, _T1]]:
  2267. ...
  2268. @overload
  2269. def query(
  2270. self, __ent0: _TCCA[_T0], __ent1: _TCCA[_T1], __ent2: _TCCA[_T2]
  2271. ) -> RowReturningQuery[Tuple[_T0, _T1, _T2]]:
  2272. ...
  2273. @overload
  2274. def query(
  2275. self,
  2276. __ent0: _TCCA[_T0],
  2277. __ent1: _TCCA[_T1],
  2278. __ent2: _TCCA[_T2],
  2279. __ent3: _TCCA[_T3],
  2280. ) -> RowReturningQuery[Tuple[_T0, _T1, _T2, _T3]]:
  2281. ...
  2282. @overload
  2283. def query(
  2284. self,
  2285. __ent0: _TCCA[_T0],
  2286. __ent1: _TCCA[_T1],
  2287. __ent2: _TCCA[_T2],
  2288. __ent3: _TCCA[_T3],
  2289. __ent4: _TCCA[_T4],
  2290. ) -> RowReturningQuery[Tuple[_T0, _T1, _T2, _T3, _T4]]:
  2291. ...
  2292. @overload
  2293. def query(
  2294. self,
  2295. __ent0: _TCCA[_T0],
  2296. __ent1: _TCCA[_T1],
  2297. __ent2: _TCCA[_T2],
  2298. __ent3: _TCCA[_T3],
  2299. __ent4: _TCCA[_T4],
  2300. __ent5: _TCCA[_T5],
  2301. ) -> RowReturningQuery[Tuple[_T0, _T1, _T2, _T3, _T4, _T5]]:
  2302. ...
  2303. @overload
  2304. def query(
  2305. self,
  2306. __ent0: _TCCA[_T0],
  2307. __ent1: _TCCA[_T1],
  2308. __ent2: _TCCA[_T2],
  2309. __ent3: _TCCA[_T3],
  2310. __ent4: _TCCA[_T4],
  2311. __ent5: _TCCA[_T5],
  2312. __ent6: _TCCA[_T6],
  2313. ) -> RowReturningQuery[Tuple[_T0, _T1, _T2, _T3, _T4, _T5, _T6]]:
  2314. ...
  2315. @overload
  2316. def query(
  2317. self,
  2318. __ent0: _TCCA[_T0],
  2319. __ent1: _TCCA[_T1],
  2320. __ent2: _TCCA[_T2],
  2321. __ent3: _TCCA[_T3],
  2322. __ent4: _TCCA[_T4],
  2323. __ent5: _TCCA[_T5],
  2324. __ent6: _TCCA[_T6],
  2325. __ent7: _TCCA[_T7],
  2326. ) -> RowReturningQuery[Tuple[_T0, _T1, _T2, _T3, _T4, _T5, _T6, _T7]]:
  2327. ...
  2328. # END OVERLOADED FUNCTIONS self.query
  2329. @overload
  2330. def query(
  2331. self, *entities: _ColumnsClauseArgument[Any], **kwargs: Any
  2332. ) -> Query[Any]:
  2333. ...
  2334. def query(
  2335. self, *entities: _ColumnsClauseArgument[Any], **kwargs: Any
  2336. ) -> Query[Any]:
  2337. """Return a new :class:`_query.Query` object corresponding to this
  2338. :class:`_orm.Session`.
  2339. Note that the :class:`_query.Query` object is legacy as of
  2340. SQLAlchemy 2.0; the :func:`_sql.select` construct is now used
  2341. to construct ORM queries.
  2342. .. seealso::
  2343. :ref:`unified_tutorial`
  2344. :ref:`queryguide_toplevel`
  2345. :ref:`query_api_toplevel` - legacy API doc
  2346. """
  2347. return self._query_cls(entities, self, **kwargs)
  2348. def _identity_lookup(
  2349. self,
  2350. mapper: Mapper[_O],
  2351. primary_key_identity: Union[Any, Tuple[Any, ...]],
  2352. identity_token: Any = None,
  2353. passive: PassiveFlag = PassiveFlag.PASSIVE_OFF,
  2354. lazy_loaded_from: Optional[InstanceState[Any]] = None,
  2355. execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
  2356. bind_arguments: Optional[_BindArguments] = None,
  2357. ) -> Union[Optional[_O], LoaderCallableStatus]:
  2358. """Locate an object in the identity map.
  2359. Given a primary key identity, constructs an identity key and then
  2360. looks in the session's identity map. If present, the object may
  2361. be run through unexpiration rules (e.g. load unloaded attributes,
  2362. check if was deleted).
  2363. e.g.::
  2364. obj = session._identity_lookup(inspect(SomeClass), (1, ))
  2365. :param mapper: mapper in use
  2366. :param primary_key_identity: the primary key we are searching for, as
  2367. a tuple.
  2368. :param identity_token: identity token that should be used to create
  2369. the identity key. Used as is, however overriding subclasses can
  2370. repurpose this in order to interpret the value in a special way,
  2371. such as if None then look among multiple target tokens.
  2372. :param passive: passive load flag passed to
  2373. :func:`.loading.get_from_identity`, which impacts the behavior if
  2374. the object is found; the object may be validated and/or unexpired
  2375. if the flag allows for SQL to be emitted.
  2376. :param lazy_loaded_from: an :class:`.InstanceState` that is
  2377. specifically asking for this identity as a related identity. Used
  2378. for sharding schemes where there is a correspondence between an object
  2379. and a related object being lazy-loaded (or otherwise
  2380. relationship-loaded).
  2381. :return: None if the object is not found in the identity map, *or*
  2382. if the object was unexpired and found to have been deleted.
  2383. if passive flags disallow SQL and the object is expired, returns
  2384. PASSIVE_NO_RESULT. In all other cases the instance is returned.
  2385. .. versionchanged:: 1.4.0 - the :meth:`.Session._identity_lookup`
  2386. method was moved from :class:`_query.Query` to
  2387. :class:`.Session`, to avoid having to instantiate the
  2388. :class:`_query.Query` object.
  2389. """
  2390. key = mapper.identity_key_from_primary_key(
  2391. primary_key_identity, identity_token=identity_token
  2392. )
  2393. # work around: https://github.com/python/typing/discussions/1143
  2394. return_value = loading.get_from_identity(self, mapper, key, passive)
  2395. return return_value
  2396. @util.non_memoized_property
  2397. @contextlib.contextmanager
  2398. def no_autoflush(self) -> Iterator[Session]:
  2399. """Return a context manager that disables autoflush.
  2400. e.g.::
  2401. with session.no_autoflush:
  2402. some_object = SomeClass()
  2403. session.add(some_object)
  2404. # won't autoflush
  2405. some_object.related_thing = session.query(SomeRelated).first()
  2406. Operations that proceed within the ``with:`` block
  2407. will not be subject to flushes occurring upon query
  2408. access. This is useful when initializing a series
  2409. of objects which involve existing database queries,
  2410. where the uncompleted object should not yet be flushed.
  2411. """
  2412. autoflush = self.autoflush
  2413. self.autoflush = False
  2414. try:
  2415. yield self
  2416. finally:
  2417. self.autoflush = autoflush
  2418. @util.langhelpers.tag_method_for_warnings(
  2419. "This warning originated from the Session 'autoflush' process, "
  2420. "which was invoked automatically in response to a user-initiated "
  2421. "operation.",
  2422. sa_exc.SAWarning,
  2423. )
  2424. def _autoflush(self) -> None:
  2425. if self.autoflush and not self._flushing:
  2426. try:
  2427. self.flush()
  2428. except sa_exc.StatementError as e:
  2429. # note we are reraising StatementError as opposed to
  2430. # raising FlushError with "chaining" to remain compatible
  2431. # with code that catches StatementError, IntegrityError,
  2432. # etc.
  2433. e.add_detail(
  2434. "raised as a result of Query-invoked autoflush; "
  2435. "consider using a session.no_autoflush block if this "
  2436. "flush is occurring prematurely"
  2437. )
  2438. raise e.with_traceback(sys.exc_info()[2])
  2439. def refresh(
  2440. self,
  2441. instance: object,
  2442. attribute_names: Optional[Iterable[str]] = None,
  2443. with_for_update: ForUpdateParameter = None,
  2444. ) -> None:
  2445. """Expire and refresh attributes on the given instance.
  2446. The selected attributes will first be expired as they would when using
  2447. :meth:`_orm.Session.expire`; then a SELECT statement will be issued to
  2448. the database to refresh column-oriented attributes with the current
  2449. value available in the current transaction.
  2450. :func:`_orm.relationship` oriented attributes will also be immediately
  2451. loaded if they were already eagerly loaded on the object, using the
  2452. same eager loading strategy that they were loaded with originally.
  2453. .. versionadded:: 1.4 - the :meth:`_orm.Session.refresh` method
  2454. can also refresh eagerly loaded attributes.
  2455. :func:`_orm.relationship` oriented attributes that would normally
  2456. load using the ``select`` (or "lazy") loader strategy will also
  2457. load **if they are named explicitly in the attribute_names
  2458. collection**, emitting a SELECT statement for the attribute using the
  2459. ``immediate`` loader strategy. If lazy-loaded relationships are not
  2460. named in :paramref:`_orm.Session.refresh.attribute_names`, then
  2461. they remain as "lazy loaded" attributes and are not implicitly
  2462. refreshed.
  2463. .. versionchanged:: 2.0.4 The :meth:`_orm.Session.refresh` method
  2464. will now refresh lazy-loaded :func:`_orm.relationship` oriented
  2465. attributes for those which are named explicitly in the
  2466. :paramref:`_orm.Session.refresh.attribute_names` collection.
  2467. .. tip::
  2468. While the :meth:`_orm.Session.refresh` method is capable of
  2469. refreshing both column and relationship oriented attributes, its
  2470. primary focus is on refreshing of local column-oriented attributes
  2471. on a single instance. For more open ended "refresh" functionality,
  2472. including the ability to refresh the attributes on many objects at
  2473. once while having explicit control over relationship loader
  2474. strategies, use the
  2475. :ref:`populate existing <orm_queryguide_populate_existing>` feature
  2476. instead.
  2477. Note that a highly isolated transaction will return the same values as
  2478. were previously read in that same transaction, regardless of changes
  2479. in database state outside of that transaction. Refreshing
  2480. attributes usually only makes sense at the start of a transaction
  2481. where database rows have not yet been accessed.
  2482. :param attribute_names: optional. An iterable collection of
  2483. string attribute names indicating a subset of attributes to
  2484. be refreshed.
  2485. :param with_for_update: optional boolean ``True`` indicating FOR UPDATE
  2486. should be used, or may be a dictionary containing flags to
  2487. indicate a more specific set of FOR UPDATE flags for the SELECT;
  2488. flags should match the parameters of
  2489. :meth:`_query.Query.with_for_update`.
  2490. Supersedes the :paramref:`.Session.refresh.lockmode` parameter.
  2491. .. seealso::
  2492. :ref:`session_expire` - introductory material
  2493. :meth:`.Session.expire`
  2494. :meth:`.Session.expire_all`
  2495. :ref:`orm_queryguide_populate_existing` - allows any ORM query
  2496. to refresh objects as they would be loaded normally.
  2497. """
  2498. try:
  2499. state = attributes.instance_state(instance)
  2500. except exc.NO_STATE as err:
  2501. raise exc.UnmappedInstanceError(instance) from err
  2502. self._expire_state(state, attribute_names)
  2503. # this autoflush previously used to occur as a secondary effect
  2504. # of the load_on_ident below. Meaning we'd organize the SELECT
  2505. # based on current DB pks, then flush, then if pks changed in that
  2506. # flush, crash. this was unticketed but discovered as part of
  2507. # #8703. So here, autoflush up front, dont autoflush inside
  2508. # load_on_ident.
  2509. self._autoflush()
  2510. if with_for_update == {}:
  2511. raise sa_exc.ArgumentError(
  2512. "with_for_update should be the boolean value "
  2513. "True, or a dictionary with options. "
  2514. "A blank dictionary is ambiguous."
  2515. )
  2516. with_for_update = ForUpdateArg._from_argument(with_for_update)
  2517. stmt: Select[Any] = sql.select(object_mapper(instance))
  2518. if (
  2519. loading.load_on_ident(
  2520. self,
  2521. stmt,
  2522. state.key,
  2523. refresh_state=state,
  2524. with_for_update=with_for_update,
  2525. only_load_props=attribute_names,
  2526. require_pk_cols=True,
  2527. # technically unnecessary as we just did autoflush
  2528. # above, however removes the additional unnecessary
  2529. # call to _autoflush()
  2530. no_autoflush=True,
  2531. is_user_refresh=True,
  2532. )
  2533. is None
  2534. ):
  2535. raise sa_exc.InvalidRequestError(
  2536. "Could not refresh instance '%s'" % instance_str(instance)
  2537. )
  2538. def expire_all(self) -> None:
  2539. """Expires all persistent instances within this Session.
  2540. When any attributes on a persistent instance is next accessed,
  2541. a query will be issued using the
  2542. :class:`.Session` object's current transactional context in order to
  2543. load all expired attributes for the given instance. Note that
  2544. a highly isolated transaction will return the same values as were
  2545. previously read in that same transaction, regardless of changes
  2546. in database state outside of that transaction.
  2547. To expire individual objects and individual attributes
  2548. on those objects, use :meth:`Session.expire`.
  2549. The :class:`.Session` object's default behavior is to
  2550. expire all state whenever the :meth:`Session.rollback`
  2551. or :meth:`Session.commit` methods are called, so that new
  2552. state can be loaded for the new transaction. For this reason,
  2553. calling :meth:`Session.expire_all` is not usually needed,
  2554. assuming the transaction is isolated.
  2555. .. seealso::
  2556. :ref:`session_expire` - introductory material
  2557. :meth:`.Session.expire`
  2558. :meth:`.Session.refresh`
  2559. :meth:`_orm.Query.populate_existing`
  2560. """
  2561. for state in self.identity_map.all_states():
  2562. state._expire(state.dict, self.identity_map._modified)
  2563. def expire(
  2564. self, instance: object, attribute_names: Optional[Iterable[str]] = None
  2565. ) -> None:
  2566. """Expire the attributes on an instance.
  2567. Marks the attributes of an instance as out of date. When an expired
  2568. attribute is next accessed, a query will be issued to the
  2569. :class:`.Session` object's current transactional context in order to
  2570. load all expired attributes for the given instance. Note that
  2571. a highly isolated transaction will return the same values as were
  2572. previously read in that same transaction, regardless of changes
  2573. in database state outside of that transaction.
  2574. To expire all objects in the :class:`.Session` simultaneously,
  2575. use :meth:`Session.expire_all`.
  2576. The :class:`.Session` object's default behavior is to
  2577. expire all state whenever the :meth:`Session.rollback`
  2578. or :meth:`Session.commit` methods are called, so that new
  2579. state can be loaded for the new transaction. For this reason,
  2580. calling :meth:`Session.expire` only makes sense for the specific
  2581. case that a non-ORM SQL statement was emitted in the current
  2582. transaction.
  2583. :param instance: The instance to be refreshed.
  2584. :param attribute_names: optional list of string attribute names
  2585. indicating a subset of attributes to be expired.
  2586. .. seealso::
  2587. :ref:`session_expire` - introductory material
  2588. :meth:`.Session.expire`
  2589. :meth:`.Session.refresh`
  2590. :meth:`_orm.Query.populate_existing`
  2591. """
  2592. try:
  2593. state = attributes.instance_state(instance)
  2594. except exc.NO_STATE as err:
  2595. raise exc.UnmappedInstanceError(instance) from err
  2596. self._expire_state(state, attribute_names)
  2597. def _expire_state(
  2598. self,
  2599. state: InstanceState[Any],
  2600. attribute_names: Optional[Iterable[str]],
  2601. ) -> None:
  2602. self._validate_persistent(state)
  2603. if attribute_names:
  2604. state._expire_attributes(state.dict, attribute_names)
  2605. else:
  2606. # pre-fetch the full cascade since the expire is going to
  2607. # remove associations
  2608. cascaded = list(
  2609. state.manager.mapper.cascade_iterator("refresh-expire", state)
  2610. )
  2611. self._conditional_expire(state)
  2612. for o, m, st_, dct_ in cascaded:
  2613. self._conditional_expire(st_)
  2614. def _conditional_expire(
  2615. self, state: InstanceState[Any], autoflush: Optional[bool] = None
  2616. ) -> None:
  2617. """Expire a state if persistent, else expunge if pending"""
  2618. if state.key:
  2619. state._expire(state.dict, self.identity_map._modified)
  2620. elif state in self._new:
  2621. self._new.pop(state)
  2622. state._detach(self)
  2623. def expunge(self, instance: object) -> None:
  2624. """Remove the `instance` from this ``Session``.
  2625. This will free all internal references to the instance. Cascading
  2626. will be applied according to the *expunge* cascade rule.
  2627. """
  2628. try:
  2629. state = attributes.instance_state(instance)
  2630. except exc.NO_STATE as err:
  2631. raise exc.UnmappedInstanceError(instance) from err
  2632. if state.session_id is not self.hash_key:
  2633. raise sa_exc.InvalidRequestError(
  2634. "Instance %s is not present in this Session" % state_str(state)
  2635. )
  2636. cascaded = list(
  2637. state.manager.mapper.cascade_iterator("expunge", state)
  2638. )
  2639. self._expunge_states([state] + [st_ for o, m, st_, dct_ in cascaded])
  2640. def _expunge_states(
  2641. self, states: Iterable[InstanceState[Any]], to_transient: bool = False
  2642. ) -> None:
  2643. for state in states:
  2644. if state in self._new:
  2645. self._new.pop(state)
  2646. elif self.identity_map.contains_state(state):
  2647. self.identity_map.safe_discard(state)
  2648. self._deleted.pop(state, None)
  2649. elif self._transaction:
  2650. # state is "detached" from being deleted, but still present
  2651. # in the transaction snapshot
  2652. self._transaction._deleted.pop(state, None)
  2653. statelib.InstanceState._detach_states(
  2654. states, self, to_transient=to_transient
  2655. )
  2656. def _register_persistent(self, states: Set[InstanceState[Any]]) -> None:
  2657. """Register all persistent objects from a flush.
  2658. This is used both for pending objects moving to the persistent
  2659. state as well as already persistent objects.
  2660. """
  2661. pending_to_persistent = self.dispatch.pending_to_persistent or None
  2662. for state in states:
  2663. mapper = _state_mapper(state)
  2664. # prevent against last minute dereferences of the object
  2665. obj = state.obj()
  2666. if obj is not None:
  2667. instance_key = mapper._identity_key_from_state(state)
  2668. if (
  2669. _none_set.intersection(instance_key[1])
  2670. and not mapper.allow_partial_pks
  2671. or _none_set.issuperset(instance_key[1])
  2672. ):
  2673. raise exc.FlushError(
  2674. "Instance %s has a NULL identity key. If this is an "
  2675. "auto-generated value, check that the database table "
  2676. "allows generation of new primary key values, and "
  2677. "that the mapped Column object is configured to "
  2678. "expect these generated values. Ensure also that "
  2679. "this flush() is not occurring at an inappropriate "
  2680. "time, such as within a load() event."
  2681. % state_str(state)
  2682. )
  2683. if state.key is None:
  2684. state.key = instance_key
  2685. elif state.key != instance_key:
  2686. # primary key switch. use safe_discard() in case another
  2687. # state has already replaced this one in the identity
  2688. # map (see test/orm/test_naturalpks.py ReversePKsTest)
  2689. self.identity_map.safe_discard(state)
  2690. trans = self._transaction
  2691. assert trans is not None
  2692. if state in trans._key_switches:
  2693. orig_key = trans._key_switches[state][0]
  2694. else:
  2695. orig_key = state.key
  2696. trans._key_switches[state] = (
  2697. orig_key,
  2698. instance_key,
  2699. )
  2700. state.key = instance_key
  2701. # there can be an existing state in the identity map
  2702. # that is replaced when the primary keys of two instances
  2703. # are swapped; see test/orm/test_naturalpks.py -> test_reverse
  2704. old = self.identity_map.replace(state)
  2705. if (
  2706. old is not None
  2707. and mapper._identity_key_from_state(old) == instance_key
  2708. and old.obj() is not None
  2709. ):
  2710. util.warn(
  2711. "Identity map already had an identity for %s, "
  2712. "replacing it with newly flushed object. Are there "
  2713. "load operations occurring inside of an event handler "
  2714. "within the flush?" % (instance_key,)
  2715. )
  2716. state._orphaned_outside_of_session = False
  2717. statelib.InstanceState._commit_all_states(
  2718. ((state, state.dict) for state in states), self.identity_map
  2719. )
  2720. self._register_altered(states)
  2721. if pending_to_persistent is not None:
  2722. for state in states.intersection(self._new):
  2723. pending_to_persistent(self, state)
  2724. # remove from new last, might be the last strong ref
  2725. for state in set(states).intersection(self._new):
  2726. self._new.pop(state)
  2727. def _register_altered(self, states: Iterable[InstanceState[Any]]) -> None:
  2728. if self._transaction:
  2729. for state in states:
  2730. if state in self._new:
  2731. self._transaction._new[state] = True
  2732. else:
  2733. self._transaction._dirty[state] = True
  2734. def _remove_newly_deleted(
  2735. self, states: Iterable[InstanceState[Any]]
  2736. ) -> None:
  2737. persistent_to_deleted = self.dispatch.persistent_to_deleted or None
  2738. for state in states:
  2739. if self._transaction:
  2740. self._transaction._deleted[state] = True
  2741. if persistent_to_deleted is not None:
  2742. # get a strong reference before we pop out of
  2743. # self._deleted
  2744. obj = state.obj() # noqa
  2745. self.identity_map.safe_discard(state)
  2746. self._deleted.pop(state, None)
  2747. state._deleted = True
  2748. # can't call state._detach() here, because this state
  2749. # is still in the transaction snapshot and needs to be
  2750. # tracked as part of that
  2751. if persistent_to_deleted is not None:
  2752. persistent_to_deleted(self, state)
  2753. def add(self, instance: object, _warn: bool = True) -> None:
  2754. """Place an object into this :class:`_orm.Session`.
  2755. Objects that are in the :term:`transient` state when passed to the
  2756. :meth:`_orm.Session.add` method will move to the
  2757. :term:`pending` state, until the next flush, at which point they
  2758. will move to the :term:`persistent` state.
  2759. Objects that are in the :term:`detached` state when passed to the
  2760. :meth:`_orm.Session.add` method will move to the :term:`persistent`
  2761. state directly.
  2762. If the transaction used by the :class:`_orm.Session` is rolled back,
  2763. objects which were transient when they were passed to
  2764. :meth:`_orm.Session.add` will be moved back to the
  2765. :term:`transient` state, and will no longer be present within this
  2766. :class:`_orm.Session`.
  2767. .. seealso::
  2768. :meth:`_orm.Session.add_all`
  2769. :ref:`session_adding` - at :ref:`session_basics`
  2770. """
  2771. if _warn and self._warn_on_events:
  2772. self._flush_warning("Session.add()")
  2773. try:
  2774. state = attributes.instance_state(instance)
  2775. except exc.NO_STATE as err:
  2776. raise exc.UnmappedInstanceError(instance) from err
  2777. self._save_or_update_state(state)
  2778. def add_all(self, instances: Iterable[object]) -> None:
  2779. """Add the given collection of instances to this :class:`_orm.Session`.
  2780. See the documentation for :meth:`_orm.Session.add` for a general
  2781. behavioral description.
  2782. .. seealso::
  2783. :meth:`_orm.Session.add`
  2784. :ref:`session_adding` - at :ref:`session_basics`
  2785. """
  2786. if self._warn_on_events:
  2787. self._flush_warning("Session.add_all()")
  2788. for instance in instances:
  2789. self.add(instance, _warn=False)
  2790. def _save_or_update_state(self, state: InstanceState[Any]) -> None:
  2791. state._orphaned_outside_of_session = False
  2792. self._save_or_update_impl(state)
  2793. mapper = _state_mapper(state)
  2794. for o, m, st_, dct_ in mapper.cascade_iterator(
  2795. "save-update", state, halt_on=self._contains_state
  2796. ):
  2797. self._save_or_update_impl(st_)
  2798. def delete(self, instance: object) -> None:
  2799. """Mark an instance as deleted.
  2800. The object is assumed to be either :term:`persistent` or
  2801. :term:`detached` when passed; after the method is called, the
  2802. object will remain in the :term:`persistent` state until the next
  2803. flush proceeds. During this time, the object will also be a member
  2804. of the :attr:`_orm.Session.deleted` collection.
  2805. When the next flush proceeds, the object will move to the
  2806. :term:`deleted` state, indicating a ``DELETE`` statement was emitted
  2807. for its row within the current transaction. When the transaction
  2808. is successfully committed,
  2809. the deleted object is moved to the :term:`detached` state and is
  2810. no longer present within this :class:`_orm.Session`.
  2811. .. seealso::
  2812. :ref:`session_deleting` - at :ref:`session_basics`
  2813. """
  2814. if self._warn_on_events:
  2815. self._flush_warning("Session.delete()")
  2816. try:
  2817. state = attributes.instance_state(instance)
  2818. except exc.NO_STATE as err:
  2819. raise exc.UnmappedInstanceError(instance) from err
  2820. self._delete_impl(state, instance, head=True)
  2821. def _delete_impl(
  2822. self, state: InstanceState[Any], obj: object, head: bool
  2823. ) -> None:
  2824. if state.key is None:
  2825. if head:
  2826. raise sa_exc.InvalidRequestError(
  2827. "Instance '%s' is not persisted" % state_str(state)
  2828. )
  2829. else:
  2830. return
  2831. to_attach = self._before_attach(state, obj)
  2832. if state in self._deleted:
  2833. return
  2834. self.identity_map.add(state)
  2835. if to_attach:
  2836. self._after_attach(state, obj)
  2837. if head:
  2838. # grab the cascades before adding the item to the deleted list
  2839. # so that autoflush does not delete the item
  2840. # the strong reference to the instance itself is significant here
  2841. cascade_states = list(
  2842. state.manager.mapper.cascade_iterator("delete", state)
  2843. )
  2844. else:
  2845. cascade_states = None
  2846. self._deleted[state] = obj
  2847. if head:
  2848. if TYPE_CHECKING:
  2849. assert cascade_states is not None
  2850. for o, m, st_, dct_ in cascade_states:
  2851. self._delete_impl(st_, o, False)
  2852. def get(
  2853. self,
  2854. entity: _EntityBindKey[_O],
  2855. ident: _PKIdentityArgument,
  2856. *,
  2857. options: Optional[Sequence[ORMOption]] = None,
  2858. populate_existing: bool = False,
  2859. with_for_update: ForUpdateParameter = None,
  2860. identity_token: Optional[Any] = None,
  2861. execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
  2862. bind_arguments: Optional[_BindArguments] = None,
  2863. ) -> Optional[_O]:
  2864. """Return an instance based on the given primary key identifier,
  2865. or ``None`` if not found.
  2866. E.g.::
  2867. my_user = session.get(User, 5)
  2868. some_object = session.get(VersionedFoo, (5, 10))
  2869. some_object = session.get(
  2870. VersionedFoo,
  2871. {"id": 5, "version_id": 10}
  2872. )
  2873. .. versionadded:: 1.4 Added :meth:`_orm.Session.get`, which is moved
  2874. from the now legacy :meth:`_orm.Query.get` method.
  2875. :meth:`_orm.Session.get` is special in that it provides direct
  2876. access to the identity map of the :class:`.Session`.
  2877. If the given primary key identifier is present
  2878. in the local identity map, the object is returned
  2879. directly from this collection and no SQL is emitted,
  2880. unless the object has been marked fully expired.
  2881. If not present,
  2882. a SELECT is performed in order to locate the object.
  2883. :meth:`_orm.Session.get` also will perform a check if
  2884. the object is present in the identity map and
  2885. marked as expired - a SELECT
  2886. is emitted to refresh the object as well as to
  2887. ensure that the row is still present.
  2888. If not, :class:`~sqlalchemy.orm.exc.ObjectDeletedError` is raised.
  2889. :param entity: a mapped class or :class:`.Mapper` indicating the
  2890. type of entity to be loaded.
  2891. :param ident: A scalar, tuple, or dictionary representing the
  2892. primary key. For a composite (e.g. multiple column) primary key,
  2893. a tuple or dictionary should be passed.
  2894. For a single-column primary key, the scalar calling form is typically
  2895. the most expedient. If the primary key of a row is the value "5",
  2896. the call looks like::
  2897. my_object = session.get(SomeClass, 5)
  2898. The tuple form contains primary key values typically in
  2899. the order in which they correspond to the mapped
  2900. :class:`_schema.Table`
  2901. object's primary key columns, or if the
  2902. :paramref:`_orm.Mapper.primary_key` configuration parameter were
  2903. used, in
  2904. the order used for that parameter. For example, if the primary key
  2905. of a row is represented by the integer
  2906. digits "5, 10" the call would look like::
  2907. my_object = session.get(SomeClass, (5, 10))
  2908. The dictionary form should include as keys the mapped attribute names
  2909. corresponding to each element of the primary key. If the mapped class
  2910. has the attributes ``id``, ``version_id`` as the attributes which
  2911. store the object's primary key value, the call would look like::
  2912. my_object = session.get(SomeClass, {"id": 5, "version_id": 10})
  2913. :param options: optional sequence of loader options which will be
  2914. applied to the query, if one is emitted.
  2915. :param populate_existing: causes the method to unconditionally emit
  2916. a SQL query and refresh the object with the newly loaded data,
  2917. regardless of whether or not the object is already present.
  2918. :param with_for_update: optional boolean ``True`` indicating FOR UPDATE
  2919. should be used, or may be a dictionary containing flags to
  2920. indicate a more specific set of FOR UPDATE flags for the SELECT;
  2921. flags should match the parameters of
  2922. :meth:`_query.Query.with_for_update`.
  2923. Supersedes the :paramref:`.Session.refresh.lockmode` parameter.
  2924. :param execution_options: optional dictionary of execution options,
  2925. which will be associated with the query execution if one is emitted.
  2926. This dictionary can provide a subset of the options that are
  2927. accepted by :meth:`_engine.Connection.execution_options`, and may
  2928. also provide additional options understood only in an ORM context.
  2929. .. versionadded:: 1.4.29
  2930. .. seealso::
  2931. :ref:`orm_queryguide_execution_options` - ORM-specific execution
  2932. options
  2933. :param bind_arguments: dictionary of additional arguments to determine
  2934. the bind. May include "mapper", "bind", or other custom arguments.
  2935. Contents of this dictionary are passed to the
  2936. :meth:`.Session.get_bind` method.
  2937. .. versionadded: 2.0.0rc1
  2938. :return: The object instance, or ``None``.
  2939. """
  2940. return self._get_impl(
  2941. entity,
  2942. ident,
  2943. loading.load_on_pk_identity,
  2944. options=options,
  2945. populate_existing=populate_existing,
  2946. with_for_update=with_for_update,
  2947. identity_token=identity_token,
  2948. execution_options=execution_options,
  2949. bind_arguments=bind_arguments,
  2950. )
  2951. def get_one(
  2952. self,
  2953. entity: _EntityBindKey[_O],
  2954. ident: _PKIdentityArgument,
  2955. *,
  2956. options: Optional[Sequence[ORMOption]] = None,
  2957. populate_existing: bool = False,
  2958. with_for_update: ForUpdateParameter = None,
  2959. identity_token: Optional[Any] = None,
  2960. execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
  2961. bind_arguments: Optional[_BindArguments] = None,
  2962. ) -> _O:
  2963. """Return exactly one instance based on the given primary key
  2964. identifier, or raise an exception if not found.
  2965. Raises ``sqlalchemy.orm.exc.NoResultFound`` if the query
  2966. selects no rows.
  2967. For a detailed documentation of the arguments see the
  2968. method :meth:`.Session.get`.
  2969. .. versionadded:: 2.0.22
  2970. :return: The object instance.
  2971. .. seealso::
  2972. :meth:`.Session.get` - equivalent method that instead
  2973. returns ``None`` if no row was found with the provided primary
  2974. key
  2975. """
  2976. instance = self.get(
  2977. entity,
  2978. ident,
  2979. options=options,
  2980. populate_existing=populate_existing,
  2981. with_for_update=with_for_update,
  2982. identity_token=identity_token,
  2983. execution_options=execution_options,
  2984. bind_arguments=bind_arguments,
  2985. )
  2986. if instance is None:
  2987. raise sa_exc.NoResultFound(
  2988. "No row was found when one was required"
  2989. )
  2990. return instance
  2991. def _get_impl(
  2992. self,
  2993. entity: _EntityBindKey[_O],
  2994. primary_key_identity: _PKIdentityArgument,
  2995. db_load_fn: Callable[..., _O],
  2996. *,
  2997. options: Optional[Sequence[ExecutableOption]] = None,
  2998. populate_existing: bool = False,
  2999. with_for_update: ForUpdateParameter = None,
  3000. identity_token: Optional[Any] = None,
  3001. execution_options: OrmExecuteOptionsParameter = util.EMPTY_DICT,
  3002. bind_arguments: Optional[_BindArguments] = None,
  3003. ) -> Optional[_O]:
  3004. # convert composite types to individual args
  3005. if (
  3006. is_composite_class(primary_key_identity)
  3007. and type(primary_key_identity)
  3008. in descriptor_props._composite_getters
  3009. ):
  3010. getter = descriptor_props._composite_getters[
  3011. type(primary_key_identity)
  3012. ]
  3013. primary_key_identity = getter(primary_key_identity)
  3014. mapper: Optional[Mapper[_O]] = inspect(entity)
  3015. if mapper is None or not mapper.is_mapper:
  3016. raise sa_exc.ArgumentError(
  3017. "Expected mapped class or mapper, got: %r" % entity
  3018. )
  3019. is_dict = isinstance(primary_key_identity, dict)
  3020. if not is_dict:
  3021. primary_key_identity = util.to_list(
  3022. primary_key_identity, default=[None]
  3023. )
  3024. if len(primary_key_identity) != len(mapper.primary_key):
  3025. raise sa_exc.InvalidRequestError(
  3026. "Incorrect number of values in identifier to formulate "
  3027. "primary key for session.get(); primary key columns "
  3028. "are %s" % ",".join("'%s'" % c for c in mapper.primary_key)
  3029. )
  3030. if is_dict:
  3031. pk_synonyms = mapper._pk_synonyms
  3032. if pk_synonyms:
  3033. correct_keys = set(pk_synonyms).intersection(
  3034. primary_key_identity
  3035. )
  3036. if correct_keys:
  3037. primary_key_identity = dict(primary_key_identity)
  3038. for k in correct_keys:
  3039. primary_key_identity[
  3040. pk_synonyms[k]
  3041. ] = primary_key_identity[k]
  3042. try:
  3043. primary_key_identity = list(
  3044. primary_key_identity[prop.key]
  3045. for prop in mapper._identity_key_props
  3046. )
  3047. except KeyError as err:
  3048. raise sa_exc.InvalidRequestError(
  3049. "Incorrect names of values in identifier to formulate "
  3050. "primary key for session.get(); primary key attribute "
  3051. "names are %s (synonym names are also accepted)"
  3052. % ",".join(
  3053. "'%s'" % prop.key
  3054. for prop in mapper._identity_key_props
  3055. )
  3056. ) from err
  3057. if (
  3058. not populate_existing
  3059. and not mapper.always_refresh
  3060. and with_for_update is None
  3061. ):
  3062. instance = self._identity_lookup(
  3063. mapper,
  3064. primary_key_identity,
  3065. identity_token=identity_token,
  3066. execution_options=execution_options,
  3067. bind_arguments=bind_arguments,
  3068. )
  3069. if instance is not None:
  3070. # reject calls for id in identity map but class
  3071. # mismatch.
  3072. if not isinstance(instance, mapper.class_):
  3073. return None
  3074. return instance
  3075. # TODO: this was being tested before, but this is not possible
  3076. assert instance is not LoaderCallableStatus.PASSIVE_CLASS_MISMATCH
  3077. # set_label_style() not strictly necessary, however this will ensure
  3078. # that tablename_colname style is used which at the moment is
  3079. # asserted in a lot of unit tests :)
  3080. load_options = context.QueryContext.default_load_options
  3081. if populate_existing:
  3082. load_options += {"_populate_existing": populate_existing}
  3083. statement = sql.select(mapper).set_label_style(
  3084. LABEL_STYLE_TABLENAME_PLUS_COL
  3085. )
  3086. if with_for_update is not None:
  3087. statement._for_update_arg = ForUpdateArg._from_argument(
  3088. with_for_update
  3089. )
  3090. if options:
  3091. statement = statement.options(*options)
  3092. return db_load_fn(
  3093. self,
  3094. statement,
  3095. primary_key_identity,
  3096. load_options=load_options,
  3097. identity_token=identity_token,
  3098. execution_options=execution_options,
  3099. bind_arguments=bind_arguments,
  3100. )
  3101. def merge(
  3102. self,
  3103. instance: _O,
  3104. *,
  3105. load: bool = True,
  3106. options: Optional[Sequence[ORMOption]] = None,
  3107. ) -> _O:
  3108. """Copy the state of a given instance into a corresponding instance
  3109. within this :class:`.Session`.
  3110. :meth:`.Session.merge` examines the primary key attributes of the
  3111. source instance, and attempts to reconcile it with an instance of the
  3112. same primary key in the session. If not found locally, it attempts
  3113. to load the object from the database based on primary key, and if
  3114. none can be located, creates a new instance. The state of each
  3115. attribute on the source instance is then copied to the target
  3116. instance. The resulting target instance is then returned by the
  3117. method; the original source instance is left unmodified, and
  3118. un-associated with the :class:`.Session` if not already.
  3119. This operation cascades to associated instances if the association is
  3120. mapped with ``cascade="merge"``.
  3121. See :ref:`unitofwork_merging` for a detailed discussion of merging.
  3122. :param instance: Instance to be merged.
  3123. :param load: Boolean, when False, :meth:`.merge` switches into
  3124. a "high performance" mode which causes it to forego emitting history
  3125. events as well as all database access. This flag is used for
  3126. cases such as transferring graphs of objects into a :class:`.Session`
  3127. from a second level cache, or to transfer just-loaded objects
  3128. into the :class:`.Session` owned by a worker thread or process
  3129. without re-querying the database.
  3130. The ``load=False`` use case adds the caveat that the given
  3131. object has to be in a "clean" state, that is, has no pending changes
  3132. to be flushed - even if the incoming object is detached from any
  3133. :class:`.Session`. This is so that when
  3134. the merge operation populates local attributes and
  3135. cascades to related objects and
  3136. collections, the values can be "stamped" onto the
  3137. target object as is, without generating any history or attribute
  3138. events, and without the need to reconcile the incoming data with
  3139. any existing related objects or collections that might not
  3140. be loaded. The resulting objects from ``load=False`` are always
  3141. produced as "clean", so it is only appropriate that the given objects
  3142. should be "clean" as well, else this suggests a mis-use of the
  3143. method.
  3144. :param options: optional sequence of loader options which will be
  3145. applied to the :meth:`_orm.Session.get` method when the merge
  3146. operation loads the existing version of the object from the database.
  3147. .. versionadded:: 1.4.24
  3148. .. seealso::
  3149. :func:`.make_transient_to_detached` - provides for an alternative
  3150. means of "merging" a single object into the :class:`.Session`
  3151. """
  3152. if self._warn_on_events:
  3153. self._flush_warning("Session.merge()")
  3154. _recursive: Dict[InstanceState[Any], object] = {}
  3155. _resolve_conflict_map: Dict[_IdentityKeyType[Any], object] = {}
  3156. if load:
  3157. # flush current contents if we expect to load data
  3158. self._autoflush()
  3159. object_mapper(instance) # verify mapped
  3160. autoflush = self.autoflush
  3161. try:
  3162. self.autoflush = False
  3163. return self._merge(
  3164. attributes.instance_state(instance),
  3165. attributes.instance_dict(instance),
  3166. load=load,
  3167. options=options,
  3168. _recursive=_recursive,
  3169. _resolve_conflict_map=_resolve_conflict_map,
  3170. )
  3171. finally:
  3172. self.autoflush = autoflush
  3173. def _merge(
  3174. self,
  3175. state: InstanceState[_O],
  3176. state_dict: _InstanceDict,
  3177. *,
  3178. options: Optional[Sequence[ORMOption]] = None,
  3179. load: bool,
  3180. _recursive: Dict[Any, object],
  3181. _resolve_conflict_map: Dict[_IdentityKeyType[Any], object],
  3182. ) -> _O:
  3183. mapper: Mapper[_O] = _state_mapper(state)
  3184. if state in _recursive:
  3185. return cast(_O, _recursive[state])
  3186. new_instance = False
  3187. key = state.key
  3188. merged: Optional[_O]
  3189. if key is None:
  3190. if state in self._new:
  3191. util.warn(
  3192. "Instance %s is already pending in this Session yet is "
  3193. "being merged again; this is probably not what you want "
  3194. "to do" % state_str(state)
  3195. )
  3196. if not load:
  3197. raise sa_exc.InvalidRequestError(
  3198. "merge() with load=False option does not support "
  3199. "objects transient (i.e. unpersisted) objects. flush() "
  3200. "all changes on mapped instances before merging with "
  3201. "load=False."
  3202. )
  3203. key = mapper._identity_key_from_state(state)
  3204. key_is_persistent = LoaderCallableStatus.NEVER_SET not in key[
  3205. 1
  3206. ] and (
  3207. not _none_set.intersection(key[1])
  3208. or (
  3209. mapper.allow_partial_pks
  3210. and not _none_set.issuperset(key[1])
  3211. )
  3212. )
  3213. else:
  3214. key_is_persistent = True
  3215. if key in self.identity_map:
  3216. try:
  3217. merged = self.identity_map[key]
  3218. except KeyError:
  3219. # object was GC'ed right as we checked for it
  3220. merged = None
  3221. else:
  3222. merged = None
  3223. if merged is None:
  3224. if key_is_persistent and key in _resolve_conflict_map:
  3225. merged = cast(_O, _resolve_conflict_map[key])
  3226. elif not load:
  3227. if state.modified:
  3228. raise sa_exc.InvalidRequestError(
  3229. "merge() with load=False option does not support "
  3230. "objects marked as 'dirty'. flush() all changes on "
  3231. "mapped instances before merging with load=False."
  3232. )
  3233. merged = mapper.class_manager.new_instance()
  3234. merged_state = attributes.instance_state(merged)
  3235. merged_state.key = key
  3236. self._update_impl(merged_state)
  3237. new_instance = True
  3238. elif key_is_persistent:
  3239. merged = self.get(
  3240. mapper.class_,
  3241. key[1],
  3242. identity_token=key[2],
  3243. options=options,
  3244. )
  3245. if merged is None:
  3246. merged = mapper.class_manager.new_instance()
  3247. merged_state = attributes.instance_state(merged)
  3248. merged_dict = attributes.instance_dict(merged)
  3249. new_instance = True
  3250. self._save_or_update_state(merged_state)
  3251. else:
  3252. merged_state = attributes.instance_state(merged)
  3253. merged_dict = attributes.instance_dict(merged)
  3254. _recursive[state] = merged
  3255. _resolve_conflict_map[key] = merged
  3256. # check that we didn't just pull the exact same
  3257. # state out.
  3258. if state is not merged_state:
  3259. # version check if applicable
  3260. if mapper.version_id_col is not None:
  3261. existing_version = mapper._get_state_attr_by_column(
  3262. state,
  3263. state_dict,
  3264. mapper.version_id_col,
  3265. passive=PassiveFlag.PASSIVE_NO_INITIALIZE,
  3266. )
  3267. merged_version = mapper._get_state_attr_by_column(
  3268. merged_state,
  3269. merged_dict,
  3270. mapper.version_id_col,
  3271. passive=PassiveFlag.PASSIVE_NO_INITIALIZE,
  3272. )
  3273. if (
  3274. existing_version
  3275. is not LoaderCallableStatus.PASSIVE_NO_RESULT
  3276. and merged_version
  3277. is not LoaderCallableStatus.PASSIVE_NO_RESULT
  3278. and existing_version != merged_version
  3279. ):
  3280. raise exc.StaleDataError(
  3281. "Version id '%s' on merged state %s "
  3282. "does not match existing version '%s'. "
  3283. "Leave the version attribute unset when "
  3284. "merging to update the most recent version."
  3285. % (
  3286. existing_version,
  3287. state_str(merged_state),
  3288. merged_version,
  3289. )
  3290. )
  3291. merged_state.load_path = state.load_path
  3292. merged_state.load_options = state.load_options
  3293. # since we are copying load_options, we need to copy
  3294. # the callables_ that would have been generated by those
  3295. # load_options.
  3296. # assumes that the callables we put in state.callables_
  3297. # are not instance-specific (which they should not be)
  3298. merged_state._copy_callables(state)
  3299. for prop in mapper.iterate_properties:
  3300. prop.merge(
  3301. self,
  3302. state,
  3303. state_dict,
  3304. merged_state,
  3305. merged_dict,
  3306. load,
  3307. _recursive,
  3308. _resolve_conflict_map,
  3309. )
  3310. if not load:
  3311. # remove any history
  3312. merged_state._commit_all(merged_dict, self.identity_map)
  3313. merged_state.manager.dispatch._sa_event_merge_wo_load(
  3314. merged_state, None
  3315. )
  3316. if new_instance:
  3317. merged_state.manager.dispatch.load(merged_state, None)
  3318. return merged
  3319. def _validate_persistent(self, state: InstanceState[Any]) -> None:
  3320. if not self.identity_map.contains_state(state):
  3321. raise sa_exc.InvalidRequestError(
  3322. "Instance '%s' is not persistent within this Session"
  3323. % state_str(state)
  3324. )
  3325. def _save_impl(self, state: InstanceState[Any]) -> None:
  3326. if state.key is not None:
  3327. raise sa_exc.InvalidRequestError(
  3328. "Object '%s' already has an identity - "
  3329. "it can't be registered as pending" % state_str(state)
  3330. )
  3331. obj = state.obj()
  3332. to_attach = self._before_attach(state, obj)
  3333. if state not in self._new:
  3334. self._new[state] = obj
  3335. state.insert_order = len(self._new)
  3336. if to_attach:
  3337. self._after_attach(state, obj)
  3338. def _update_impl(
  3339. self, state: InstanceState[Any], revert_deletion: bool = False
  3340. ) -> None:
  3341. if state.key is None:
  3342. raise sa_exc.InvalidRequestError(
  3343. "Instance '%s' is not persisted" % state_str(state)
  3344. )
  3345. if state._deleted:
  3346. if revert_deletion:
  3347. if not state._attached:
  3348. return
  3349. del state._deleted
  3350. else:
  3351. raise sa_exc.InvalidRequestError(
  3352. "Instance '%s' has been deleted. "
  3353. "Use the make_transient() "
  3354. "function to send this object back "
  3355. "to the transient state." % state_str(state)
  3356. )
  3357. obj = state.obj()
  3358. # check for late gc
  3359. if obj is None:
  3360. return
  3361. to_attach = self._before_attach(state, obj)
  3362. self._deleted.pop(state, None)
  3363. if revert_deletion:
  3364. self.identity_map.replace(state)
  3365. else:
  3366. self.identity_map.add(state)
  3367. if to_attach:
  3368. self._after_attach(state, obj)
  3369. elif revert_deletion:
  3370. self.dispatch.deleted_to_persistent(self, state)
  3371. def _save_or_update_impl(self, state: InstanceState[Any]) -> None:
  3372. if state.key is None:
  3373. self._save_impl(state)
  3374. else:
  3375. self._update_impl(state)
  3376. def enable_relationship_loading(self, obj: object) -> None:
  3377. """Associate an object with this :class:`.Session` for related
  3378. object loading.
  3379. .. warning::
  3380. :meth:`.enable_relationship_loading` exists to serve special
  3381. use cases and is not recommended for general use.
  3382. Accesses of attributes mapped with :func:`_orm.relationship`
  3383. will attempt to load a value from the database using this
  3384. :class:`.Session` as the source of connectivity. The values
  3385. will be loaded based on foreign key and primary key values
  3386. present on this object - if not present, then those relationships
  3387. will be unavailable.
  3388. The object will be attached to this session, but will
  3389. **not** participate in any persistence operations; its state
  3390. for almost all purposes will remain either "transient" or
  3391. "detached", except for the case of relationship loading.
  3392. Also note that backrefs will often not work as expected.
  3393. Altering a relationship-bound attribute on the target object
  3394. may not fire off a backref event, if the effective value
  3395. is what was already loaded from a foreign-key-holding value.
  3396. The :meth:`.Session.enable_relationship_loading` method is
  3397. similar to the ``load_on_pending`` flag on :func:`_orm.relationship`.
  3398. Unlike that flag, :meth:`.Session.enable_relationship_loading` allows
  3399. an object to remain transient while still being able to load
  3400. related items.
  3401. To make a transient object associated with a :class:`.Session`
  3402. via :meth:`.Session.enable_relationship_loading` pending, add
  3403. it to the :class:`.Session` using :meth:`.Session.add` normally.
  3404. If the object instead represents an existing identity in the database,
  3405. it should be merged using :meth:`.Session.merge`.
  3406. :meth:`.Session.enable_relationship_loading` does not improve
  3407. behavior when the ORM is used normally - object references should be
  3408. constructed at the object level, not at the foreign key level, so
  3409. that they are present in an ordinary way before flush()
  3410. proceeds. This method is not intended for general use.
  3411. .. seealso::
  3412. :paramref:`_orm.relationship.load_on_pending` - this flag
  3413. allows per-relationship loading of many-to-ones on items that
  3414. are pending.
  3415. :func:`.make_transient_to_detached` - allows for an object to
  3416. be added to a :class:`.Session` without SQL emitted, which then
  3417. will unexpire attributes on access.
  3418. """
  3419. try:
  3420. state = attributes.instance_state(obj)
  3421. except exc.NO_STATE as err:
  3422. raise exc.UnmappedInstanceError(obj) from err
  3423. to_attach = self._before_attach(state, obj)
  3424. state._load_pending = True
  3425. if to_attach:
  3426. self._after_attach(state, obj)
  3427. def _before_attach(self, state: InstanceState[Any], obj: object) -> bool:
  3428. self._autobegin_t()
  3429. if state.session_id == self.hash_key:
  3430. return False
  3431. if state.session_id and state.session_id in _sessions:
  3432. raise sa_exc.InvalidRequestError(
  3433. "Object '%s' is already attached to session '%s' "
  3434. "(this is '%s')"
  3435. % (state_str(state), state.session_id, self.hash_key)
  3436. )
  3437. self.dispatch.before_attach(self, state)
  3438. return True
  3439. def _after_attach(self, state: InstanceState[Any], obj: object) -> None:
  3440. state.session_id = self.hash_key
  3441. if state.modified and state._strong_obj is None:
  3442. state._strong_obj = obj
  3443. self.dispatch.after_attach(self, state)
  3444. if state.key:
  3445. self.dispatch.detached_to_persistent(self, state)
  3446. else:
  3447. self.dispatch.transient_to_pending(self, state)
  3448. def __contains__(self, instance: object) -> bool:
  3449. """Return True if the instance is associated with this session.
  3450. The instance may be pending or persistent within the Session for a
  3451. result of True.
  3452. """
  3453. try:
  3454. state = attributes.instance_state(instance)
  3455. except exc.NO_STATE as err:
  3456. raise exc.UnmappedInstanceError(instance) from err
  3457. return self._contains_state(state)
  3458. def __iter__(self) -> Iterator[object]:
  3459. """Iterate over all pending or persistent instances within this
  3460. Session.
  3461. """
  3462. return iter(
  3463. list(self._new.values()) + list(self.identity_map.values())
  3464. )
  3465. def _contains_state(self, state: InstanceState[Any]) -> bool:
  3466. return state in self._new or self.identity_map.contains_state(state)
  3467. def flush(self, objects: Optional[Sequence[Any]] = None) -> None:
  3468. """Flush all the object changes to the database.
  3469. Writes out all pending object creations, deletions and modifications
  3470. to the database as INSERTs, DELETEs, UPDATEs, etc. Operations are
  3471. automatically ordered by the Session's unit of work dependency
  3472. solver.
  3473. Database operations will be issued in the current transactional
  3474. context and do not affect the state of the transaction, unless an
  3475. error occurs, in which case the entire transaction is rolled back.
  3476. You may flush() as often as you like within a transaction to move
  3477. changes from Python to the database's transaction buffer.
  3478. :param objects: Optional; restricts the flush operation to operate
  3479. only on elements that are in the given collection.
  3480. This feature is for an extremely narrow set of use cases where
  3481. particular objects may need to be operated upon before the
  3482. full flush() occurs. It is not intended for general use.
  3483. """
  3484. if self._flushing:
  3485. raise sa_exc.InvalidRequestError("Session is already flushing")
  3486. if self._is_clean():
  3487. return
  3488. try:
  3489. self._flushing = True
  3490. self._flush(objects)
  3491. finally:
  3492. self._flushing = False
  3493. def _flush_warning(self, method: Any) -> None:
  3494. util.warn(
  3495. "Usage of the '%s' operation is not currently supported "
  3496. "within the execution stage of the flush process. "
  3497. "Results may not be consistent. Consider using alternative "
  3498. "event listeners or connection-level operations instead." % method
  3499. )
  3500. def _is_clean(self) -> bool:
  3501. return (
  3502. not self.identity_map.check_modified()
  3503. and not self._deleted
  3504. and not self._new
  3505. )
  3506. def _flush(self, objects: Optional[Sequence[object]] = None) -> None:
  3507. dirty = self._dirty_states
  3508. if not dirty and not self._deleted and not self._new:
  3509. self.identity_map._modified.clear()
  3510. return
  3511. flush_context = UOWTransaction(self)
  3512. if self.dispatch.before_flush:
  3513. self.dispatch.before_flush(self, flush_context, objects)
  3514. # re-establish "dirty states" in case the listeners
  3515. # added
  3516. dirty = self._dirty_states
  3517. deleted = set(self._deleted)
  3518. new = set(self._new)
  3519. dirty = set(dirty).difference(deleted)
  3520. # create the set of all objects we want to operate upon
  3521. if objects:
  3522. # specific list passed in
  3523. objset = set()
  3524. for o in objects:
  3525. try:
  3526. state = attributes.instance_state(o)
  3527. except exc.NO_STATE as err:
  3528. raise exc.UnmappedInstanceError(o) from err
  3529. objset.add(state)
  3530. else:
  3531. objset = None
  3532. # store objects whose fate has been decided
  3533. processed = set()
  3534. # put all saves/updates into the flush context. detect top-level
  3535. # orphans and throw them into deleted.
  3536. if objset:
  3537. proc = new.union(dirty).intersection(objset).difference(deleted)
  3538. else:
  3539. proc = new.union(dirty).difference(deleted)
  3540. for state in proc:
  3541. is_orphan = _state_mapper(state)._is_orphan(state)
  3542. is_persistent_orphan = is_orphan and state.has_identity
  3543. if (
  3544. is_orphan
  3545. and not is_persistent_orphan
  3546. and state._orphaned_outside_of_session
  3547. ):
  3548. self._expunge_states([state])
  3549. else:
  3550. _reg = flush_context.register_object(
  3551. state, isdelete=is_persistent_orphan
  3552. )
  3553. assert _reg, "Failed to add object to the flush context!"
  3554. processed.add(state)
  3555. # put all remaining deletes into the flush context.
  3556. if objset:
  3557. proc = deleted.intersection(objset).difference(processed)
  3558. else:
  3559. proc = deleted.difference(processed)
  3560. for state in proc:
  3561. _reg = flush_context.register_object(state, isdelete=True)
  3562. assert _reg, "Failed to add object to the flush context!"
  3563. if not flush_context.has_work:
  3564. return
  3565. flush_context.transaction = transaction = self._autobegin_t()._begin()
  3566. try:
  3567. self._warn_on_events = True
  3568. try:
  3569. flush_context.execute()
  3570. finally:
  3571. self._warn_on_events = False
  3572. self.dispatch.after_flush(self, flush_context)
  3573. flush_context.finalize_flush_changes()
  3574. if not objects and self.identity_map._modified:
  3575. len_ = len(self.identity_map._modified)
  3576. statelib.InstanceState._commit_all_states(
  3577. [
  3578. (state, state.dict)
  3579. for state in self.identity_map._modified
  3580. ],
  3581. instance_dict=self.identity_map,
  3582. )
  3583. util.warn(
  3584. "Attribute history events accumulated on %d "
  3585. "previously clean instances "
  3586. "within inner-flush event handlers have been "
  3587. "reset, and will not result in database updates. "
  3588. "Consider using set_committed_value() within "
  3589. "inner-flush event handlers to avoid this warning." % len_
  3590. )
  3591. # useful assertions:
  3592. # if not objects:
  3593. # assert not self.identity_map._modified
  3594. # else:
  3595. # assert self.identity_map._modified == \
  3596. # self.identity_map._modified.difference(objects)
  3597. self.dispatch.after_flush_postexec(self, flush_context)
  3598. transaction.commit()
  3599. except:
  3600. with util.safe_reraise():
  3601. transaction.rollback(_capture_exception=True)
  3602. def bulk_save_objects(
  3603. self,
  3604. objects: Iterable[object],
  3605. return_defaults: bool = False,
  3606. update_changed_only: bool = True,
  3607. preserve_order: bool = True,
  3608. ) -> None:
  3609. """Perform a bulk save of the given list of objects.
  3610. .. legacy::
  3611. This method is a legacy feature as of the 2.0 series of
  3612. SQLAlchemy. For modern bulk INSERT and UPDATE, see
  3613. the sections :ref:`orm_queryguide_bulk_insert` and
  3614. :ref:`orm_queryguide_bulk_update`.
  3615. For general INSERT and UPDATE of existing ORM mapped objects,
  3616. prefer standard :term:`unit of work` data management patterns,
  3617. introduced in the :ref:`unified_tutorial` at
  3618. :ref:`tutorial_orm_data_manipulation`. SQLAlchemy 2.0
  3619. now uses :ref:`engine_insertmanyvalues` with modern dialects
  3620. which solves previous issues of bulk INSERT slowness.
  3621. :param objects: a sequence of mapped object instances. The mapped
  3622. objects are persisted as is, and are **not** associated with the
  3623. :class:`.Session` afterwards.
  3624. For each object, whether the object is sent as an INSERT or an
  3625. UPDATE is dependent on the same rules used by the :class:`.Session`
  3626. in traditional operation; if the object has the
  3627. :attr:`.InstanceState.key`
  3628. attribute set, then the object is assumed to be "detached" and
  3629. will result in an UPDATE. Otherwise, an INSERT is used.
  3630. In the case of an UPDATE, statements are grouped based on which
  3631. attributes have changed, and are thus to be the subject of each
  3632. SET clause. If ``update_changed_only`` is False, then all
  3633. attributes present within each object are applied to the UPDATE
  3634. statement, which may help in allowing the statements to be grouped
  3635. together into a larger executemany(), and will also reduce the
  3636. overhead of checking history on attributes.
  3637. :param return_defaults: when True, rows that are missing values which
  3638. generate defaults, namely integer primary key defaults and sequences,
  3639. will be inserted **one at a time**, so that the primary key value
  3640. is available. In particular this will allow joined-inheritance
  3641. and other multi-table mappings to insert correctly without the need
  3642. to provide primary key values ahead of time; however,
  3643. :paramref:`.Session.bulk_save_objects.return_defaults` **greatly
  3644. reduces the performance gains** of the method overall. It is strongly
  3645. advised to please use the standard :meth:`_orm.Session.add_all`
  3646. approach.
  3647. :param update_changed_only: when True, UPDATE statements are rendered
  3648. based on those attributes in each state that have logged changes.
  3649. When False, all attributes present are rendered into the SET clause
  3650. with the exception of primary key attributes.
  3651. :param preserve_order: when True, the order of inserts and updates
  3652. matches exactly the order in which the objects are given. When
  3653. False, common types of objects are grouped into inserts
  3654. and updates, to allow for more batching opportunities.
  3655. .. seealso::
  3656. :doc:`queryguide/dml`
  3657. :meth:`.Session.bulk_insert_mappings`
  3658. :meth:`.Session.bulk_update_mappings`
  3659. """
  3660. obj_states: Iterable[InstanceState[Any]]
  3661. obj_states = (attributes.instance_state(obj) for obj in objects)
  3662. if not preserve_order:
  3663. # the purpose of this sort is just so that common mappers
  3664. # and persistence states are grouped together, so that groupby
  3665. # will return a single group for a particular type of mapper.
  3666. # it's not trying to be deterministic beyond that.
  3667. obj_states = sorted(
  3668. obj_states,
  3669. key=lambda state: (id(state.mapper), state.key is not None),
  3670. )
  3671. def grouping_key(
  3672. state: InstanceState[_O],
  3673. ) -> Tuple[Mapper[_O], bool]:
  3674. return (state.mapper, state.key is not None)
  3675. for (mapper, isupdate), states in itertools.groupby(
  3676. obj_states, grouping_key
  3677. ):
  3678. self._bulk_save_mappings(
  3679. mapper,
  3680. states,
  3681. isupdate,
  3682. True,
  3683. return_defaults,
  3684. update_changed_only,
  3685. False,
  3686. )
  3687. def bulk_insert_mappings(
  3688. self,
  3689. mapper: Mapper[Any],
  3690. mappings: Iterable[Dict[str, Any]],
  3691. return_defaults: bool = False,
  3692. render_nulls: bool = False,
  3693. ) -> None:
  3694. """Perform a bulk insert of the given list of mapping dictionaries.
  3695. .. legacy::
  3696. This method is a legacy feature as of the 2.0 series of
  3697. SQLAlchemy. For modern bulk INSERT and UPDATE, see
  3698. the sections :ref:`orm_queryguide_bulk_insert` and
  3699. :ref:`orm_queryguide_bulk_update`. The 2.0 API shares
  3700. implementation details with this method and adds new features
  3701. as well.
  3702. :param mapper: a mapped class, or the actual :class:`_orm.Mapper`
  3703. object,
  3704. representing the single kind of object represented within the mapping
  3705. list.
  3706. :param mappings: a sequence of dictionaries, each one containing the
  3707. state of the mapped row to be inserted, in terms of the attribute
  3708. names on the mapped class. If the mapping refers to multiple tables,
  3709. such as a joined-inheritance mapping, each dictionary must contain all
  3710. keys to be populated into all tables.
  3711. :param return_defaults: when True, the INSERT process will be altered
  3712. to ensure that newly generated primary key values will be fetched.
  3713. The rationale for this parameter is typically to enable
  3714. :ref:`Joined Table Inheritance <joined_inheritance>` mappings to
  3715. be bulk inserted.
  3716. .. note:: for backends that don't support RETURNING, the
  3717. :paramref:`_orm.Session.bulk_insert_mappings.return_defaults`
  3718. parameter can significantly decrease performance as INSERT
  3719. statements can no longer be batched. See
  3720. :ref:`engine_insertmanyvalues`
  3721. for background on which backends are affected.
  3722. :param render_nulls: When True, a value of ``None`` will result
  3723. in a NULL value being included in the INSERT statement, rather
  3724. than the column being omitted from the INSERT. This allows all
  3725. the rows being INSERTed to have the identical set of columns which
  3726. allows the full set of rows to be batched to the DBAPI. Normally,
  3727. each column-set that contains a different combination of NULL values
  3728. than the previous row must omit a different series of columns from
  3729. the rendered INSERT statement, which means it must be emitted as a
  3730. separate statement. By passing this flag, the full set of rows
  3731. are guaranteed to be batchable into one batch; the cost however is
  3732. that server-side defaults which are invoked by an omitted column will
  3733. be skipped, so care must be taken to ensure that these are not
  3734. necessary.
  3735. .. warning::
  3736. When this flag is set, **server side default SQL values will
  3737. not be invoked** for those columns that are inserted as NULL;
  3738. the NULL value will be sent explicitly. Care must be taken
  3739. to ensure that no server-side default functions need to be
  3740. invoked for the operation as a whole.
  3741. .. seealso::
  3742. :doc:`queryguide/dml`
  3743. :meth:`.Session.bulk_save_objects`
  3744. :meth:`.Session.bulk_update_mappings`
  3745. """
  3746. self._bulk_save_mappings(
  3747. mapper,
  3748. mappings,
  3749. False,
  3750. False,
  3751. return_defaults,
  3752. False,
  3753. render_nulls,
  3754. )
  3755. def bulk_update_mappings(
  3756. self, mapper: Mapper[Any], mappings: Iterable[Dict[str, Any]]
  3757. ) -> None:
  3758. """Perform a bulk update of the given list of mapping dictionaries.
  3759. .. legacy::
  3760. This method is a legacy feature as of the 2.0 series of
  3761. SQLAlchemy. For modern bulk INSERT and UPDATE, see
  3762. the sections :ref:`orm_queryguide_bulk_insert` and
  3763. :ref:`orm_queryguide_bulk_update`. The 2.0 API shares
  3764. implementation details with this method and adds new features
  3765. as well.
  3766. :param mapper: a mapped class, or the actual :class:`_orm.Mapper`
  3767. object,
  3768. representing the single kind of object represented within the mapping
  3769. list.
  3770. :param mappings: a sequence of dictionaries, each one containing the
  3771. state of the mapped row to be updated, in terms of the attribute names
  3772. on the mapped class. If the mapping refers to multiple tables, such
  3773. as a joined-inheritance mapping, each dictionary may contain keys
  3774. corresponding to all tables. All those keys which are present and
  3775. are not part of the primary key are applied to the SET clause of the
  3776. UPDATE statement; the primary key values, which are required, are
  3777. applied to the WHERE clause.
  3778. .. seealso::
  3779. :doc:`queryguide/dml`
  3780. :meth:`.Session.bulk_insert_mappings`
  3781. :meth:`.Session.bulk_save_objects`
  3782. """
  3783. self._bulk_save_mappings(
  3784. mapper, mappings, True, False, False, False, False
  3785. )
  3786. def _bulk_save_mappings(
  3787. self,
  3788. mapper: Mapper[_O],
  3789. mappings: Union[Iterable[InstanceState[_O]], Iterable[Dict[str, Any]]],
  3790. isupdate: bool,
  3791. isstates: bool,
  3792. return_defaults: bool,
  3793. update_changed_only: bool,
  3794. render_nulls: bool,
  3795. ) -> None:
  3796. mapper = _class_to_mapper(mapper)
  3797. self._flushing = True
  3798. transaction = self._autobegin_t()._begin()
  3799. try:
  3800. if isupdate:
  3801. bulk_persistence._bulk_update(
  3802. mapper,
  3803. mappings,
  3804. transaction,
  3805. isstates,
  3806. update_changed_only,
  3807. )
  3808. else:
  3809. bulk_persistence._bulk_insert(
  3810. mapper,
  3811. mappings,
  3812. transaction,
  3813. isstates,
  3814. return_defaults,
  3815. render_nulls,
  3816. )
  3817. transaction.commit()
  3818. except:
  3819. with util.safe_reraise():
  3820. transaction.rollback(_capture_exception=True)
  3821. finally:
  3822. self._flushing = False
  3823. def is_modified(
  3824. self, instance: object, include_collections: bool = True
  3825. ) -> bool:
  3826. r"""Return ``True`` if the given instance has locally
  3827. modified attributes.
  3828. This method retrieves the history for each instrumented
  3829. attribute on the instance and performs a comparison of the current
  3830. value to its previously committed value, if any.
  3831. It is in effect a more expensive and accurate
  3832. version of checking for the given instance in the
  3833. :attr:`.Session.dirty` collection; a full test for
  3834. each attribute's net "dirty" status is performed.
  3835. E.g.::
  3836. return session.is_modified(someobject)
  3837. A few caveats to this method apply:
  3838. * Instances present in the :attr:`.Session.dirty` collection may
  3839. report ``False`` when tested with this method. This is because
  3840. the object may have received change events via attribute mutation,
  3841. thus placing it in :attr:`.Session.dirty`, but ultimately the state
  3842. is the same as that loaded from the database, resulting in no net
  3843. change here.
  3844. * Scalar attributes may not have recorded the previously set
  3845. value when a new value was applied, if the attribute was not loaded,
  3846. or was expired, at the time the new value was received - in these
  3847. cases, the attribute is assumed to have a change, even if there is
  3848. ultimately no net change against its database value. SQLAlchemy in
  3849. most cases does not need the "old" value when a set event occurs, so
  3850. it skips the expense of a SQL call if the old value isn't present,
  3851. based on the assumption that an UPDATE of the scalar value is
  3852. usually needed, and in those few cases where it isn't, is less
  3853. expensive on average than issuing a defensive SELECT.
  3854. The "old" value is fetched unconditionally upon set only if the
  3855. attribute container has the ``active_history`` flag set to ``True``.
  3856. This flag is set typically for primary key attributes and scalar
  3857. object references that are not a simple many-to-one. To set this
  3858. flag for any arbitrary mapped column, use the ``active_history``
  3859. argument with :func:`.column_property`.
  3860. :param instance: mapped instance to be tested for pending changes.
  3861. :param include_collections: Indicates if multivalued collections
  3862. should be included in the operation. Setting this to ``False`` is a
  3863. way to detect only local-column based properties (i.e. scalar columns
  3864. or many-to-one foreign keys) that would result in an UPDATE for this
  3865. instance upon flush.
  3866. """
  3867. state = object_state(instance)
  3868. if not state.modified:
  3869. return False
  3870. dict_ = state.dict
  3871. for attr in state.manager.attributes:
  3872. if (
  3873. not include_collections
  3874. and hasattr(attr.impl, "get_collection")
  3875. ) or not hasattr(attr.impl, "get_history"):
  3876. continue
  3877. (added, unchanged, deleted) = attr.impl.get_history(
  3878. state, dict_, passive=PassiveFlag.NO_CHANGE
  3879. )
  3880. if added or deleted:
  3881. return True
  3882. else:
  3883. return False
  3884. @property
  3885. def is_active(self) -> bool:
  3886. """True if this :class:`.Session` not in "partial rollback" state.
  3887. .. versionchanged:: 1.4 The :class:`_orm.Session` no longer begins
  3888. a new transaction immediately, so this attribute will be False
  3889. when the :class:`_orm.Session` is first instantiated.
  3890. "partial rollback" state typically indicates that the flush process
  3891. of the :class:`_orm.Session` has failed, and that the
  3892. :meth:`_orm.Session.rollback` method must be emitted in order to
  3893. fully roll back the transaction.
  3894. If this :class:`_orm.Session` is not in a transaction at all, the
  3895. :class:`_orm.Session` will autobegin when it is first used, so in this
  3896. case :attr:`_orm.Session.is_active` will return True.
  3897. Otherwise, if this :class:`_orm.Session` is within a transaction,
  3898. and that transaction has not been rolled back internally, the
  3899. :attr:`_orm.Session.is_active` will also return True.
  3900. .. seealso::
  3901. :ref:`faq_session_rollback`
  3902. :meth:`_orm.Session.in_transaction`
  3903. """
  3904. return self._transaction is None or self._transaction.is_active
  3905. @property
  3906. def _dirty_states(self) -> Iterable[InstanceState[Any]]:
  3907. """The set of all persistent states considered dirty.
  3908. This method returns all states that were modified including
  3909. those that were possibly deleted.
  3910. """
  3911. return self.identity_map._dirty_states()
  3912. @property
  3913. def dirty(self) -> IdentitySet:
  3914. """The set of all persistent instances considered dirty.
  3915. E.g.::
  3916. some_mapped_object in session.dirty
  3917. Instances are considered dirty when they were modified but not
  3918. deleted.
  3919. Note that this 'dirty' calculation is 'optimistic'; most
  3920. attribute-setting or collection modification operations will
  3921. mark an instance as 'dirty' and place it in this set, even if
  3922. there is no net change to the attribute's value. At flush
  3923. time, the value of each attribute is compared to its
  3924. previously saved value, and if there's no net change, no SQL
  3925. operation will occur (this is a more expensive operation so
  3926. it's only done at flush time).
  3927. To check if an instance has actionable net changes to its
  3928. attributes, use the :meth:`.Session.is_modified` method.
  3929. """
  3930. return IdentitySet(
  3931. [
  3932. state.obj()
  3933. for state in self._dirty_states
  3934. if state not in self._deleted
  3935. ]
  3936. )
  3937. @property
  3938. def deleted(self) -> IdentitySet:
  3939. "The set of all instances marked as 'deleted' within this ``Session``"
  3940. return util.IdentitySet(list(self._deleted.values()))
  3941. @property
  3942. def new(self) -> IdentitySet:
  3943. "The set of all instances marked as 'new' within this ``Session``."
  3944. return util.IdentitySet(list(self._new.values()))
  3945. _S = TypeVar("_S", bound="Session")
  3946. class sessionmaker(_SessionClassMethods, Generic[_S]):
  3947. """A configurable :class:`.Session` factory.
  3948. The :class:`.sessionmaker` factory generates new
  3949. :class:`.Session` objects when called, creating them given
  3950. the configurational arguments established here.
  3951. e.g.::
  3952. from sqlalchemy import create_engine
  3953. from sqlalchemy.orm import sessionmaker
  3954. # an Engine, which the Session will use for connection
  3955. # resources
  3956. engine = create_engine('postgresql+psycopg2://scott:tiger@localhost/')
  3957. Session = sessionmaker(engine)
  3958. with Session() as session:
  3959. session.add(some_object)
  3960. session.add(some_other_object)
  3961. session.commit()
  3962. Context manager use is optional; otherwise, the returned
  3963. :class:`_orm.Session` object may be closed explicitly via the
  3964. :meth:`_orm.Session.close` method. Using a
  3965. ``try:/finally:`` block is optional, however will ensure that the close
  3966. takes place even if there are database errors::
  3967. session = Session()
  3968. try:
  3969. session.add(some_object)
  3970. session.add(some_other_object)
  3971. session.commit()
  3972. finally:
  3973. session.close()
  3974. :class:`.sessionmaker` acts as a factory for :class:`_orm.Session`
  3975. objects in the same way as an :class:`_engine.Engine` acts as a factory
  3976. for :class:`_engine.Connection` objects. In this way it also includes
  3977. a :meth:`_orm.sessionmaker.begin` method, that provides a context
  3978. manager which both begins and commits a transaction, as well as closes
  3979. out the :class:`_orm.Session` when complete, rolling back the transaction
  3980. if any errors occur::
  3981. Session = sessionmaker(engine)
  3982. with Session.begin() as session:
  3983. session.add(some_object)
  3984. session.add(some_other_object)
  3985. # commits transaction, closes session
  3986. .. versionadded:: 1.4
  3987. When calling upon :class:`_orm.sessionmaker` to construct a
  3988. :class:`_orm.Session`, keyword arguments may also be passed to the
  3989. method; these arguments will override that of the globally configured
  3990. parameters. Below we use a :class:`_orm.sessionmaker` bound to a certain
  3991. :class:`_engine.Engine` to produce a :class:`_orm.Session` that is instead
  3992. bound to a specific :class:`_engine.Connection` procured from that engine::
  3993. Session = sessionmaker(engine)
  3994. # bind an individual session to a connection
  3995. with engine.connect() as connection:
  3996. with Session(bind=connection) as session:
  3997. # work with session
  3998. The class also includes a method :meth:`_orm.sessionmaker.configure`, which
  3999. can be used to specify additional keyword arguments to the factory, which
  4000. will take effect for subsequent :class:`.Session` objects generated. This
  4001. is usually used to associate one or more :class:`_engine.Engine` objects
  4002. with an existing
  4003. :class:`.sessionmaker` factory before it is first used::
  4004. # application starts, sessionmaker does not have
  4005. # an engine bound yet
  4006. Session = sessionmaker()
  4007. # ... later, when an engine URL is read from a configuration
  4008. # file or other events allow the engine to be created
  4009. engine = create_engine('sqlite:///foo.db')
  4010. Session.configure(bind=engine)
  4011. sess = Session()
  4012. # work with session
  4013. .. seealso::
  4014. :ref:`session_getting` - introductory text on creating
  4015. sessions using :class:`.sessionmaker`.
  4016. """
  4017. class_: Type[_S]
  4018. @overload
  4019. def __init__(
  4020. self,
  4021. bind: Optional[_SessionBind] = ...,
  4022. *,
  4023. class_: Type[_S],
  4024. autoflush: bool = ...,
  4025. expire_on_commit: bool = ...,
  4026. info: Optional[_InfoType] = ...,
  4027. **kw: Any,
  4028. ):
  4029. ...
  4030. @overload
  4031. def __init__(
  4032. self: "sessionmaker[Session]",
  4033. bind: Optional[_SessionBind] = ...,
  4034. *,
  4035. autoflush: bool = ...,
  4036. expire_on_commit: bool = ...,
  4037. info: Optional[_InfoType] = ...,
  4038. **kw: Any,
  4039. ):
  4040. ...
  4041. def __init__(
  4042. self,
  4043. bind: Optional[_SessionBind] = None,
  4044. *,
  4045. class_: Type[_S] = Session, # type: ignore
  4046. autoflush: bool = True,
  4047. expire_on_commit: bool = True,
  4048. info: Optional[_InfoType] = None,
  4049. **kw: Any,
  4050. ):
  4051. r"""Construct a new :class:`.sessionmaker`.
  4052. All arguments here except for ``class_`` correspond to arguments
  4053. accepted by :class:`.Session` directly. See the
  4054. :meth:`.Session.__init__` docstring for more details on parameters.
  4055. :param bind: a :class:`_engine.Engine` or other :class:`.Connectable`
  4056. with
  4057. which newly created :class:`.Session` objects will be associated.
  4058. :param class\_: class to use in order to create new :class:`.Session`
  4059. objects. Defaults to :class:`.Session`.
  4060. :param autoflush: The autoflush setting to use with newly created
  4061. :class:`.Session` objects.
  4062. .. seealso::
  4063. :ref:`session_flushing` - additional background on autoflush
  4064. :param expire_on_commit=True: the
  4065. :paramref:`_orm.Session.expire_on_commit` setting to use
  4066. with newly created :class:`.Session` objects.
  4067. :param info: optional dictionary of information that will be available
  4068. via :attr:`.Session.info`. Note this dictionary is *updated*, not
  4069. replaced, when the ``info`` parameter is specified to the specific
  4070. :class:`.Session` construction operation.
  4071. :param \**kw: all other keyword arguments are passed to the
  4072. constructor of newly created :class:`.Session` objects.
  4073. """
  4074. kw["bind"] = bind
  4075. kw["autoflush"] = autoflush
  4076. kw["expire_on_commit"] = expire_on_commit
  4077. if info is not None:
  4078. kw["info"] = info
  4079. self.kw = kw
  4080. # make our own subclass of the given class, so that
  4081. # events can be associated with it specifically.
  4082. self.class_ = type(class_.__name__, (class_,), {})
  4083. def begin(self) -> contextlib.AbstractContextManager[_S]:
  4084. """Produce a context manager that both provides a new
  4085. :class:`_orm.Session` as well as a transaction that commits.
  4086. e.g.::
  4087. Session = sessionmaker(some_engine)
  4088. with Session.begin() as session:
  4089. session.add(some_object)
  4090. # commits transaction, closes session
  4091. .. versionadded:: 1.4
  4092. """
  4093. session = self()
  4094. return session._maker_context_manager()
  4095. def __call__(self, **local_kw: Any) -> _S:
  4096. """Produce a new :class:`.Session` object using the configuration
  4097. established in this :class:`.sessionmaker`.
  4098. In Python, the ``__call__`` method is invoked on an object when
  4099. it is "called" in the same way as a function::
  4100. Session = sessionmaker(some_engine)
  4101. session = Session() # invokes sessionmaker.__call__()
  4102. """
  4103. for k, v in self.kw.items():
  4104. if k == "info" and "info" in local_kw:
  4105. d = v.copy()
  4106. d.update(local_kw["info"])
  4107. local_kw["info"] = d
  4108. else:
  4109. local_kw.setdefault(k, v)
  4110. return self.class_(**local_kw)
  4111. def configure(self, **new_kw: Any) -> None:
  4112. """(Re)configure the arguments for this sessionmaker.
  4113. e.g.::
  4114. Session = sessionmaker()
  4115. Session.configure(bind=create_engine('sqlite://'))
  4116. """
  4117. self.kw.update(new_kw)
  4118. def __repr__(self) -> str:
  4119. return "%s(class_=%r, %s)" % (
  4120. self.__class__.__name__,
  4121. self.class_.__name__,
  4122. ", ".join("%s=%r" % (k, v) for k, v in self.kw.items()),
  4123. )
  4124. def close_all_sessions() -> None:
  4125. """Close all sessions in memory.
  4126. This function consults a global registry of all :class:`.Session` objects
  4127. and calls :meth:`.Session.close` on them, which resets them to a clean
  4128. state.
  4129. This function is not for general use but may be useful for test suites
  4130. within the teardown scheme.
  4131. .. versionadded:: 1.3
  4132. """
  4133. for sess in _sessions.values():
  4134. sess.close()
  4135. def make_transient(instance: object) -> None:
  4136. """Alter the state of the given instance so that it is :term:`transient`.
  4137. .. note::
  4138. :func:`.make_transient` is a special-case function for
  4139. advanced use cases only.
  4140. The given mapped instance is assumed to be in the :term:`persistent` or
  4141. :term:`detached` state. The function will remove its association with any
  4142. :class:`.Session` as well as its :attr:`.InstanceState.identity`. The
  4143. effect is that the object will behave as though it were newly constructed,
  4144. except retaining any attribute / collection values that were loaded at the
  4145. time of the call. The :attr:`.InstanceState.deleted` flag is also reset
  4146. if this object had been deleted as a result of using
  4147. :meth:`.Session.delete`.
  4148. .. warning::
  4149. :func:`.make_transient` does **not** "unexpire" or otherwise eagerly
  4150. load ORM-mapped attributes that are not currently loaded at the time
  4151. the function is called. This includes attributes which:
  4152. * were expired via :meth:`.Session.expire`
  4153. * were expired as the natural effect of committing a session
  4154. transaction, e.g. :meth:`.Session.commit`
  4155. * are normally :term:`lazy loaded` but are not currently loaded
  4156. * are "deferred" (see :ref:`orm_queryguide_column_deferral`) and are
  4157. not yet loaded
  4158. * were not present in the query which loaded this object, such as that
  4159. which is common in joined table inheritance and other scenarios.
  4160. After :func:`.make_transient` is called, unloaded attributes such
  4161. as those above will normally resolve to the value ``None`` when
  4162. accessed, or an empty collection for a collection-oriented attribute.
  4163. As the object is transient and un-associated with any database
  4164. identity, it will no longer retrieve these values.
  4165. .. seealso::
  4166. :func:`.make_transient_to_detached`
  4167. """
  4168. state = attributes.instance_state(instance)
  4169. s = _state_session(state)
  4170. if s:
  4171. s._expunge_states([state])
  4172. # remove expired state
  4173. state.expired_attributes.clear()
  4174. # remove deferred callables
  4175. if state.callables:
  4176. del state.callables
  4177. if state.key:
  4178. del state.key
  4179. if state._deleted:
  4180. del state._deleted
  4181. def make_transient_to_detached(instance: object) -> None:
  4182. """Make the given transient instance :term:`detached`.
  4183. .. note::
  4184. :func:`.make_transient_to_detached` is a special-case function for
  4185. advanced use cases only.
  4186. All attribute history on the given instance
  4187. will be reset as though the instance were freshly loaded
  4188. from a query. Missing attributes will be marked as expired.
  4189. The primary key attributes of the object, which are required, will be made
  4190. into the "key" of the instance.
  4191. The object can then be added to a session, or merged
  4192. possibly with the load=False flag, at which point it will look
  4193. as if it were loaded that way, without emitting SQL.
  4194. This is a special use case function that differs from a normal
  4195. call to :meth:`.Session.merge` in that a given persistent state
  4196. can be manufactured without any SQL calls.
  4197. .. seealso::
  4198. :func:`.make_transient`
  4199. :meth:`.Session.enable_relationship_loading`
  4200. """
  4201. state = attributes.instance_state(instance)
  4202. if state.session_id or state.key:
  4203. raise sa_exc.InvalidRequestError("Given object must be transient")
  4204. state.key = state.mapper._identity_key_from_state(state)
  4205. if state._deleted:
  4206. del state._deleted
  4207. state._commit_all(state.dict)
  4208. state._expire_attributes(state.dict, state.unloaded)
  4209. def object_session(instance: object) -> Optional[Session]:
  4210. """Return the :class:`.Session` to which the given instance belongs.
  4211. This is essentially the same as the :attr:`.InstanceState.session`
  4212. accessor. See that attribute for details.
  4213. """
  4214. try:
  4215. state = attributes.instance_state(instance)
  4216. except exc.NO_STATE as err:
  4217. raise exc.UnmappedInstanceError(instance) from err
  4218. else:
  4219. return _state_session(state)
  4220. _new_sessionid = util.counter()