session.py 191 KB

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