session.py 191 KB

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