schema.py 225 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218
  1. # sql/schema.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. """The schema module provides the building blocks for database metadata.
  8. Each element within this module describes a database entity which can be
  9. created and dropped, or is otherwise part of such an entity. Examples include
  10. tables, columns, sequences, and indexes.
  11. All entities are subclasses of :class:`~sqlalchemy.schema.SchemaItem`, and as
  12. defined in this module they are intended to be agnostic of any vendor-specific
  13. constructs.
  14. A collection of entities are grouped into a unit called
  15. :class:`~sqlalchemy.schema.MetaData`. MetaData serves as a logical grouping of
  16. schema elements, and can also be associated with an actual database connection
  17. such that operations involving the contained elements can contact the database
  18. as needed.
  19. Two of the elements here also build upon their "syntactic" counterparts, which
  20. are defined in :class:`~sqlalchemy.sql.expression.`, specifically
  21. :class:`~sqlalchemy.schema.Table` and :class:`~sqlalchemy.schema.Column`.
  22. Since these objects are part of the SQL expression language, they are usable
  23. as components in SQL expressions.
  24. """
  25. from __future__ import annotations
  26. from abc import ABC
  27. import collections
  28. from enum import Enum
  29. import operator
  30. import typing
  31. from typing import Any
  32. from typing import Callable
  33. from typing import cast
  34. from typing import Collection
  35. from typing import Dict
  36. from typing import Iterable
  37. from typing import Iterator
  38. from typing import List
  39. from typing import Mapping
  40. from typing import NoReturn
  41. from typing import Optional
  42. from typing import overload
  43. from typing import Sequence as _typing_Sequence
  44. from typing import Set
  45. from typing import Tuple
  46. from typing import TYPE_CHECKING
  47. from typing import TypeVar
  48. from typing import Union
  49. from . import coercions
  50. from . import ddl
  51. from . import roles
  52. from . import type_api
  53. from . import visitors
  54. from .base import _DefaultDescriptionTuple
  55. from .base import _NoArg
  56. from .base import _NoneName
  57. from .base import _SentinelColumnCharacterization
  58. from .base import _SentinelDefaultCharacterization
  59. from .base import DedupeColumnCollection
  60. from .base import DialectKWArgs
  61. from .base import Executable
  62. from .base import SchemaEventTarget as SchemaEventTarget
  63. from .base import SchemaVisitable as SchemaVisitable
  64. from .coercions import _document_text_coercion
  65. from .elements import ClauseElement
  66. from .elements import ColumnClause
  67. from .elements import ColumnElement
  68. from .elements import quoted_name
  69. from .elements import TextClause
  70. from .selectable import TableClause
  71. from .type_api import to_instance
  72. from .visitors import ExternallyTraversible
  73. from .. import event
  74. from .. import exc
  75. from .. import inspection
  76. from .. import util
  77. from ..util import HasMemoized
  78. from ..util.typing import Final
  79. from ..util.typing import Literal
  80. from ..util.typing import Protocol
  81. from ..util.typing import Self
  82. from ..util.typing import TypedDict
  83. from ..util.typing import TypeGuard
  84. if typing.TYPE_CHECKING:
  85. from ._typing import _AutoIncrementType
  86. from ._typing import _CreateDropBind
  87. from ._typing import _DDLColumnArgument
  88. from ._typing import _InfoType
  89. from ._typing import _TextCoercedExpressionArgument
  90. from ._typing import _TypeEngineArgument
  91. from .base import ColumnSet
  92. from .base import ReadOnlyColumnCollection
  93. from .compiler import DDLCompiler
  94. from .elements import BindParameter
  95. from .elements import KeyedColumnElement
  96. from .functions import Function
  97. from .type_api import TypeEngine
  98. from .visitors import anon_map
  99. from ..engine import Connection
  100. from ..engine import Engine
  101. from ..engine.interfaces import _CoreMultiExecuteParams
  102. from ..engine.interfaces import CoreExecuteOptionsParameter
  103. from ..engine.interfaces import ExecutionContext
  104. from ..engine.reflection import _ReflectionInfo
  105. from ..sql.selectable import FromClause
  106. _T = TypeVar("_T", bound="Any")
  107. _SI = TypeVar("_SI", bound="SchemaItem")
  108. _TAB = TypeVar("_TAB", bound="Table")
  109. _ConstraintNameArgument = Optional[Union[str, _NoneName]]
  110. _ServerDefaultArgument = Union[
  111. "FetchedValue", str, TextClause, ColumnElement[Any]
  112. ]
  113. _ServerOnUpdateArgument = _ServerDefaultArgument
  114. class SchemaConst(Enum):
  115. RETAIN_SCHEMA = 1
  116. """Symbol indicating that a :class:`_schema.Table`, :class:`.Sequence`
  117. or in some cases a :class:`_schema.ForeignKey` object, in situations
  118. where the object is being copied for a :meth:`.Table.to_metadata`
  119. operation, should retain the schema name that it already has.
  120. """
  121. BLANK_SCHEMA = 2
  122. """Symbol indicating that a :class:`_schema.Table` or :class:`.Sequence`
  123. should have 'None' for its schema, even if the parent
  124. :class:`_schema.MetaData` has specified a schema.
  125. .. seealso::
  126. :paramref:`_schema.MetaData.schema`
  127. :paramref:`_schema.Table.schema`
  128. :paramref:`.Sequence.schema`
  129. """
  130. NULL_UNSPECIFIED = 3
  131. """Symbol indicating the "nullable" keyword was not passed to a Column.
  132. This is used to distinguish between the use case of passing
  133. ``nullable=None`` to a :class:`.Column`, which has special meaning
  134. on some backends such as SQL Server.
  135. """
  136. RETAIN_SCHEMA: Final[Literal[SchemaConst.RETAIN_SCHEMA]] = (
  137. SchemaConst.RETAIN_SCHEMA
  138. )
  139. BLANK_SCHEMA: Final[Literal[SchemaConst.BLANK_SCHEMA]] = (
  140. SchemaConst.BLANK_SCHEMA
  141. )
  142. NULL_UNSPECIFIED: Final[Literal[SchemaConst.NULL_UNSPECIFIED]] = (
  143. SchemaConst.NULL_UNSPECIFIED
  144. )
  145. def _get_table_key(name: str, schema: Optional[str]) -> str:
  146. if schema is None:
  147. return name
  148. else:
  149. return schema + "." + name
  150. # this should really be in sql/util.py but we'd have to
  151. # break an import cycle
  152. def _copy_expression(
  153. expression: ColumnElement[Any],
  154. source_table: Optional[Table],
  155. target_table: Optional[Table],
  156. ) -> ColumnElement[Any]:
  157. if source_table is None or target_table is None:
  158. return expression
  159. fixed_source_table = source_table
  160. fixed_target_table = target_table
  161. def replace(
  162. element: ExternallyTraversible, **kw: Any
  163. ) -> Optional[ExternallyTraversible]:
  164. if (
  165. isinstance(element, Column)
  166. and element.table is fixed_source_table
  167. and element.key in fixed_source_table.c
  168. ):
  169. return fixed_target_table.c[element.key]
  170. else:
  171. return None
  172. return cast(
  173. ColumnElement[Any],
  174. visitors.replacement_traverse(expression, {}, replace),
  175. )
  176. @inspection._self_inspects
  177. class SchemaItem(SchemaVisitable):
  178. """Base class for items that define a database schema."""
  179. __visit_name__ = "schema_item"
  180. create_drop_stringify_dialect = "default"
  181. def _init_items(self, *args: SchemaItem, **kw: Any) -> None:
  182. """Initialize the list of child items for this SchemaItem."""
  183. for item in args:
  184. if item is not None:
  185. try:
  186. spwd = item._set_parent_with_dispatch
  187. except AttributeError as err:
  188. raise exc.ArgumentError(
  189. "'SchemaItem' object, such as a 'Column' or a "
  190. f"'Constraint' expected, got {item!r}"
  191. ) from err
  192. else:
  193. spwd(self, **kw)
  194. def __repr__(self) -> str:
  195. return util.generic_repr(self, omit_kwarg=["info"])
  196. @util.memoized_property
  197. def info(self) -> _InfoType:
  198. """Info dictionary associated with the object, allowing user-defined
  199. data to be associated with this :class:`.SchemaItem`.
  200. The dictionary is automatically generated when first accessed.
  201. It can also be specified in the constructor of some objects,
  202. such as :class:`_schema.Table` and :class:`_schema.Column`.
  203. """
  204. return {}
  205. def _schema_item_copy(self, schema_item: _SI) -> _SI:
  206. if "info" in self.__dict__:
  207. schema_item.info = self.info.copy()
  208. schema_item.dispatch._update(self.dispatch)
  209. return schema_item
  210. _use_schema_map = True
  211. class HasConditionalDDL:
  212. """define a class that includes the :meth:`.HasConditionalDDL.ddl_if`
  213. method, allowing for conditional rendering of DDL.
  214. Currently applies to constraints and indexes.
  215. .. versionadded:: 2.0
  216. """
  217. _ddl_if: Optional[ddl.DDLIf] = None
  218. def ddl_if(
  219. self,
  220. dialect: Optional[str] = None,
  221. callable_: Optional[ddl.DDLIfCallable] = None,
  222. state: Optional[Any] = None,
  223. ) -> Self:
  224. r"""apply a conditional DDL rule to this schema item.
  225. These rules work in a similar manner to the
  226. :meth:`.ExecutableDDLElement.execute_if` callable, with the added
  227. feature that the criteria may be checked within the DDL compilation
  228. phase for a construct such as :class:`.CreateTable`.
  229. :meth:`.HasConditionalDDL.ddl_if` currently applies towards the
  230. :class:`.Index` construct as well as all :class:`.Constraint`
  231. constructs.
  232. :param dialect: string name of a dialect, or a tuple of string names
  233. to indicate multiple dialect types.
  234. :param callable\_: a callable that is constructed using the same form
  235. as that described in
  236. :paramref:`.ExecutableDDLElement.execute_if.callable_`.
  237. :param state: any arbitrary object that will be passed to the
  238. callable, if present.
  239. .. versionadded:: 2.0
  240. .. seealso::
  241. :ref:`schema_ddl_ddl_if` - background and usage examples
  242. """
  243. self._ddl_if = ddl.DDLIf(dialect, callable_, state)
  244. return self
  245. class HasSchemaAttr(SchemaItem):
  246. """schema item that includes a top-level schema name"""
  247. schema: Optional[str]
  248. class Table(
  249. DialectKWArgs, HasSchemaAttr, TableClause, inspection.Inspectable["Table"]
  250. ):
  251. r"""Represent a table in a database.
  252. e.g.::
  253. mytable = Table(
  254. "mytable",
  255. metadata,
  256. Column("mytable_id", Integer, primary_key=True),
  257. Column("value", String(50)),
  258. )
  259. The :class:`_schema.Table`
  260. object constructs a unique instance of itself based
  261. on its name and optional schema name within the given
  262. :class:`_schema.MetaData` object. Calling the :class:`_schema.Table`
  263. constructor with the same name and same :class:`_schema.MetaData` argument
  264. a second time will return the *same* :class:`_schema.Table`
  265. object - in this way
  266. the :class:`_schema.Table` constructor acts as a registry function.
  267. .. seealso::
  268. :ref:`metadata_describing` - Introduction to database metadata
  269. """
  270. __visit_name__ = "table"
  271. if TYPE_CHECKING:
  272. @util.ro_non_memoized_property
  273. def primary_key(self) -> PrimaryKeyConstraint: ...
  274. @util.ro_non_memoized_property
  275. def foreign_keys(self) -> Set[ForeignKey]: ...
  276. _columns: DedupeColumnCollection[Column[Any]]
  277. _sentinel_column: Optional[Column[Any]]
  278. constraints: Set[Constraint]
  279. """A collection of all :class:`_schema.Constraint` objects associated with
  280. this :class:`_schema.Table`.
  281. Includes :class:`_schema.PrimaryKeyConstraint`,
  282. :class:`_schema.ForeignKeyConstraint`, :class:`_schema.UniqueConstraint`,
  283. :class:`_schema.CheckConstraint`. A separate collection
  284. :attr:`_schema.Table.foreign_key_constraints` refers to the collection
  285. of all :class:`_schema.ForeignKeyConstraint` objects, and the
  286. :attr:`_schema.Table.primary_key` attribute refers to the single
  287. :class:`_schema.PrimaryKeyConstraint` associated with the
  288. :class:`_schema.Table`.
  289. .. seealso::
  290. :attr:`_schema.Table.constraints`
  291. :attr:`_schema.Table.primary_key`
  292. :attr:`_schema.Table.foreign_key_constraints`
  293. :attr:`_schema.Table.indexes`
  294. :class:`_reflection.Inspector`
  295. """
  296. indexes: Set[Index]
  297. """A collection of all :class:`_schema.Index` objects associated with this
  298. :class:`_schema.Table`.
  299. .. seealso::
  300. :meth:`_reflection.Inspector.get_indexes`
  301. """
  302. if TYPE_CHECKING:
  303. @util.ro_non_memoized_property
  304. def columns(self) -> ReadOnlyColumnCollection[str, Column[Any]]: ...
  305. @util.ro_non_memoized_property
  306. def exported_columns(
  307. self,
  308. ) -> ReadOnlyColumnCollection[str, Column[Any]]: ...
  309. @util.ro_non_memoized_property
  310. def c(self) -> ReadOnlyColumnCollection[str, Column[Any]]: ...
  311. def _gen_cache_key(
  312. self, anon_map: anon_map, bindparams: List[BindParameter[Any]]
  313. ) -> Tuple[Any, ...]:
  314. if self._annotations:
  315. return (self,) + self._annotations_cache_key
  316. else:
  317. return (self,)
  318. if not typing.TYPE_CHECKING:
  319. # typing tools seem to be inconsistent in how they handle
  320. # __new__, so suggest this pattern for classes that use
  321. # __new__. apply typing to the __init__ method normally
  322. @util.deprecated_params(
  323. mustexist=(
  324. "1.4",
  325. "Deprecated alias of :paramref:`_schema.Table.must_exist`",
  326. ),
  327. )
  328. def __new__(cls, *args: Any, **kw: Any) -> Any:
  329. return cls._new(*args, **kw)
  330. @classmethod
  331. def _new(cls, *args: Any, **kw: Any) -> Any:
  332. if not args and not kw:
  333. # python3k pickle seems to call this
  334. return object.__new__(cls)
  335. try:
  336. name, metadata, args = args[0], args[1], args[2:]
  337. except IndexError:
  338. raise TypeError(
  339. "Table() takes at least two positional-only "
  340. "arguments 'name' and 'metadata'"
  341. )
  342. schema = kw.get("schema", None)
  343. if schema is None:
  344. schema = metadata.schema
  345. elif schema is BLANK_SCHEMA:
  346. schema = None
  347. keep_existing = kw.get("keep_existing", False)
  348. extend_existing = kw.get("extend_existing", False)
  349. if keep_existing and extend_existing:
  350. msg = "keep_existing and extend_existing are mutually exclusive."
  351. raise exc.ArgumentError(msg)
  352. must_exist = kw.pop("must_exist", kw.pop("mustexist", False))
  353. key = _get_table_key(name, schema)
  354. if key in metadata.tables:
  355. if not keep_existing and not extend_existing and bool(args):
  356. raise exc.InvalidRequestError(
  357. f"Table '{key}' is already defined for this MetaData "
  358. "instance. Specify 'extend_existing=True' "
  359. "to redefine "
  360. "options and columns on an "
  361. "existing Table object."
  362. )
  363. table = metadata.tables[key]
  364. if extend_existing:
  365. table._init_existing(*args, **kw)
  366. return table
  367. else:
  368. if must_exist:
  369. raise exc.InvalidRequestError(f"Table '{key}' not defined")
  370. table = object.__new__(cls)
  371. table.dispatch.before_parent_attach(table, metadata)
  372. metadata._add_table(name, schema, table)
  373. try:
  374. table.__init__(name, metadata, *args, _no_init=False, **kw) # type: ignore[misc] # noqa: E501
  375. table.dispatch.after_parent_attach(table, metadata)
  376. return table
  377. except Exception:
  378. with util.safe_reraise():
  379. metadata._remove_table(name, schema)
  380. def __init__(
  381. self,
  382. name: str,
  383. metadata: MetaData,
  384. *args: SchemaItem,
  385. schema: Optional[Union[str, Literal[SchemaConst.BLANK_SCHEMA]]] = None,
  386. quote: Optional[bool] = None,
  387. quote_schema: Optional[bool] = None,
  388. autoload_with: Optional[Union[Engine, Connection]] = None,
  389. autoload_replace: bool = True,
  390. keep_existing: bool = False,
  391. extend_existing: bool = False,
  392. resolve_fks: bool = True,
  393. include_columns: Optional[Collection[str]] = None,
  394. implicit_returning: bool = True,
  395. comment: Optional[str] = None,
  396. info: Optional[Dict[Any, Any]] = None,
  397. listeners: Optional[
  398. _typing_Sequence[Tuple[str, Callable[..., Any]]]
  399. ] = None,
  400. prefixes: Optional[_typing_Sequence[str]] = None,
  401. # used internally in the metadata.reflect() process
  402. _extend_on: Optional[Set[Table]] = None,
  403. # used by __new__ to bypass __init__
  404. _no_init: bool = True,
  405. # dialect-specific keyword args
  406. **kw: Any,
  407. ) -> None:
  408. r"""Constructor for :class:`_schema.Table`.
  409. :param name: The name of this table as represented in the database.
  410. The table name, along with the value of the ``schema`` parameter,
  411. forms a key which uniquely identifies this :class:`_schema.Table`
  412. within
  413. the owning :class:`_schema.MetaData` collection.
  414. Additional calls to :class:`_schema.Table` with the same name,
  415. metadata,
  416. and schema name will return the same :class:`_schema.Table` object.
  417. Names which contain no upper case characters
  418. will be treated as case insensitive names, and will not be quoted
  419. unless they are a reserved word or contain special characters.
  420. A name with any number of upper case characters is considered
  421. to be case sensitive, and will be sent as quoted.
  422. To enable unconditional quoting for the table name, specify the flag
  423. ``quote=True`` to the constructor, or use the :class:`.quoted_name`
  424. construct to specify the name.
  425. :param metadata: a :class:`_schema.MetaData`
  426. object which will contain this
  427. table. The metadata is used as a point of association of this table
  428. with other tables which are referenced via foreign key. It also
  429. may be used to associate this table with a particular
  430. :class:`.Connection` or :class:`.Engine`.
  431. :param \*args: Additional positional arguments are used primarily
  432. to add the list of :class:`_schema.Column`
  433. objects contained within this
  434. table. Similar to the style of a CREATE TABLE statement, other
  435. :class:`.SchemaItem` constructs may be added here, including
  436. :class:`.PrimaryKeyConstraint`, and
  437. :class:`_schema.ForeignKeyConstraint`.
  438. :param autoload_replace: Defaults to ``True``; when using
  439. :paramref:`_schema.Table.autoload_with`
  440. in conjunction with :paramref:`_schema.Table.extend_existing`,
  441. indicates
  442. that :class:`_schema.Column` objects present in the already-existing
  443. :class:`_schema.Table`
  444. object should be replaced with columns of the same
  445. name retrieved from the autoload process. When ``False``, columns
  446. already present under existing names will be omitted from the
  447. reflection process.
  448. Note that this setting does not impact :class:`_schema.Column` objects
  449. specified programmatically within the call to :class:`_schema.Table`
  450. that
  451. also is autoloading; those :class:`_schema.Column` objects will always
  452. replace existing columns of the same name when
  453. :paramref:`_schema.Table.extend_existing` is ``True``.
  454. .. seealso::
  455. :paramref:`_schema.Table.autoload_with`
  456. :paramref:`_schema.Table.extend_existing`
  457. :param autoload_with: An :class:`_engine.Engine` or
  458. :class:`_engine.Connection` object,
  459. or a :class:`_reflection.Inspector` object as returned by
  460. :func:`_sa.inspect`
  461. against one, with which this :class:`_schema.Table`
  462. object will be reflected.
  463. When set to a non-None value, the autoload process will take place
  464. for this table against the given engine or connection.
  465. .. seealso::
  466. :ref:`metadata_reflection_toplevel`
  467. :meth:`_events.DDLEvents.column_reflect`
  468. :ref:`metadata_reflection_dbagnostic_types`
  469. :param extend_existing: When ``True``, indicates that if this
  470. :class:`_schema.Table` is already present in the given
  471. :class:`_schema.MetaData`,
  472. apply further arguments within the constructor to the existing
  473. :class:`_schema.Table`.
  474. If :paramref:`_schema.Table.extend_existing` or
  475. :paramref:`_schema.Table.keep_existing` are not set,
  476. and the given name
  477. of the new :class:`_schema.Table` refers to a :class:`_schema.Table`
  478. that is
  479. already present in the target :class:`_schema.MetaData` collection,
  480. and
  481. this :class:`_schema.Table`
  482. specifies additional columns or other constructs
  483. or flags that modify the table's state, an
  484. error is raised. The purpose of these two mutually-exclusive flags
  485. is to specify what action should be taken when a
  486. :class:`_schema.Table`
  487. is specified that matches an existing :class:`_schema.Table`,
  488. yet specifies
  489. additional constructs.
  490. :paramref:`_schema.Table.extend_existing`
  491. will also work in conjunction
  492. with :paramref:`_schema.Table.autoload_with` to run a new reflection
  493. operation against the database, even if a :class:`_schema.Table`
  494. of the same name is already present in the target
  495. :class:`_schema.MetaData`; newly reflected :class:`_schema.Column`
  496. objects
  497. and other options will be added into the state of the
  498. :class:`_schema.Table`, potentially overwriting existing columns
  499. and options of the same name.
  500. As is always the case with :paramref:`_schema.Table.autoload_with`,
  501. :class:`_schema.Column` objects can be specified in the same
  502. :class:`_schema.Table`
  503. constructor, which will take precedence. Below, the existing
  504. table ``mytable`` will be augmented with :class:`_schema.Column`
  505. objects
  506. both reflected from the database, as well as the given
  507. :class:`_schema.Column`
  508. named "y"::
  509. Table(
  510. "mytable",
  511. metadata,
  512. Column("y", Integer),
  513. extend_existing=True,
  514. autoload_with=engine,
  515. )
  516. .. seealso::
  517. :paramref:`_schema.Table.autoload_with`
  518. :paramref:`_schema.Table.autoload_replace`
  519. :paramref:`_schema.Table.keep_existing`
  520. :param implicit_returning: True by default - indicates that
  521. RETURNING can be used, typically by the ORM, in order to fetch
  522. server-generated values such as primary key values and
  523. server side defaults, on those backends which support RETURNING.
  524. In modern SQLAlchemy there is generally no reason to alter this
  525. setting, except for some backend specific cases
  526. (see :ref:`mssql_triggers` in the SQL Server dialect documentation
  527. for one such example).
  528. :param include_columns: A list of strings indicating a subset of
  529. columns to be loaded via the ``autoload`` operation; table columns who
  530. aren't present in this list will not be represented on the resulting
  531. ``Table`` object. Defaults to ``None`` which indicates all columns
  532. should be reflected.
  533. :param resolve_fks: Whether or not to reflect :class:`_schema.Table`
  534. objects
  535. related to this one via :class:`_schema.ForeignKey` objects, when
  536. :paramref:`_schema.Table.autoload_with` is
  537. specified. Defaults to True. Set to False to disable reflection of
  538. related tables as :class:`_schema.ForeignKey`
  539. objects are encountered; may be
  540. used either to save on SQL calls or to avoid issues with related tables
  541. that can't be accessed. Note that if a related table is already present
  542. in the :class:`_schema.MetaData` collection, or becomes present later,
  543. a
  544. :class:`_schema.ForeignKey` object associated with this
  545. :class:`_schema.Table` will
  546. resolve to that table normally.
  547. .. versionadded:: 1.3
  548. .. seealso::
  549. :paramref:`.MetaData.reflect.resolve_fks`
  550. :param info: Optional data dictionary which will be populated into the
  551. :attr:`.SchemaItem.info` attribute of this object.
  552. :param keep_existing: When ``True``, indicates that if this Table
  553. is already present in the given :class:`_schema.MetaData`, ignore
  554. further arguments within the constructor to the existing
  555. :class:`_schema.Table`, and return the :class:`_schema.Table`
  556. object as
  557. originally created. This is to allow a function that wishes
  558. to define a new :class:`_schema.Table` on first call, but on
  559. subsequent calls will return the same :class:`_schema.Table`,
  560. without any of the declarations (particularly constraints)
  561. being applied a second time.
  562. If :paramref:`_schema.Table.extend_existing` or
  563. :paramref:`_schema.Table.keep_existing` are not set,
  564. and the given name
  565. of the new :class:`_schema.Table` refers to a :class:`_schema.Table`
  566. that is
  567. already present in the target :class:`_schema.MetaData` collection,
  568. and
  569. this :class:`_schema.Table`
  570. specifies additional columns or other constructs
  571. or flags that modify the table's state, an
  572. error is raised. The purpose of these two mutually-exclusive flags
  573. is to specify what action should be taken when a
  574. :class:`_schema.Table`
  575. is specified that matches an existing :class:`_schema.Table`,
  576. yet specifies
  577. additional constructs.
  578. .. seealso::
  579. :paramref:`_schema.Table.extend_existing`
  580. :param listeners: A list of tuples of the form ``(<eventname>, <fn>)``
  581. which will be passed to :func:`.event.listen` upon construction.
  582. This alternate hook to :func:`.event.listen` allows the establishment
  583. of a listener function specific to this :class:`_schema.Table` before
  584. the "autoload" process begins. Historically this has been intended
  585. for use with the :meth:`.DDLEvents.column_reflect` event, however
  586. note that this event hook may now be associated with the
  587. :class:`_schema.MetaData` object directly::
  588. def listen_for_reflect(table, column_info):
  589. "handle the column reflection event"
  590. # ...
  591. t = Table(
  592. "sometable",
  593. autoload_with=engine,
  594. listeners=[("column_reflect", listen_for_reflect)],
  595. )
  596. .. seealso::
  597. :meth:`_events.DDLEvents.column_reflect`
  598. :param must_exist: When ``True``, indicates that this Table must already
  599. be present in the given :class:`_schema.MetaData` collection, else
  600. an exception is raised.
  601. :param prefixes:
  602. A list of strings to insert after CREATE in the CREATE TABLE
  603. statement. They will be separated by spaces.
  604. :param quote: Force quoting of this table's name on or off, corresponding
  605. to ``True`` or ``False``. When left at its default of ``None``,
  606. the column identifier will be quoted according to whether the name is
  607. case sensitive (identifiers with at least one upper case character are
  608. treated as case sensitive), or if it's a reserved word. This flag
  609. is only needed to force quoting of a reserved word which is not known
  610. by the SQLAlchemy dialect.
  611. .. note:: setting this flag to ``False`` will not provide
  612. case-insensitive behavior for table reflection; table reflection
  613. will always search for a mixed-case name in a case sensitive
  614. fashion. Case insensitive names are specified in SQLAlchemy only
  615. by stating the name with all lower case characters.
  616. :param quote_schema: same as 'quote' but applies to the schema identifier.
  617. :param schema: The schema name for this table, which is required if
  618. the table resides in a schema other than the default selected schema
  619. for the engine's database connection. Defaults to ``None``.
  620. If the owning :class:`_schema.MetaData` of this :class:`_schema.Table`
  621. specifies its
  622. own :paramref:`_schema.MetaData.schema` parameter,
  623. then that schema name will
  624. be applied to this :class:`_schema.Table`
  625. if the schema parameter here is set
  626. to ``None``. To set a blank schema name on a :class:`_schema.Table`
  627. that
  628. would otherwise use the schema set on the owning
  629. :class:`_schema.MetaData`,
  630. specify the special symbol :attr:`.BLANK_SCHEMA`.
  631. The quoting rules for the schema name are the same as those for the
  632. ``name`` parameter, in that quoting is applied for reserved words or
  633. case-sensitive names; to enable unconditional quoting for the schema
  634. name, specify the flag ``quote_schema=True`` to the constructor, or use
  635. the :class:`.quoted_name` construct to specify the name.
  636. :param comment: Optional string that will render an SQL comment on table
  637. creation.
  638. .. versionadded:: 1.2 Added the :paramref:`_schema.Table.comment`
  639. parameter
  640. to :class:`_schema.Table`.
  641. :param \**kw: Additional keyword arguments not mentioned above are
  642. dialect specific, and passed in the form ``<dialectname>_<argname>``.
  643. See the documentation regarding an individual dialect at
  644. :ref:`dialect_toplevel` for detail on documented arguments.
  645. """ # noqa: E501
  646. if _no_init:
  647. # don't run __init__ from __new__ by default;
  648. # __new__ has a specific place that __init__ is called
  649. return
  650. super().__init__(quoted_name(name, quote))
  651. self.metadata = metadata
  652. if schema is None:
  653. self.schema = metadata.schema
  654. elif schema is BLANK_SCHEMA:
  655. self.schema = None
  656. else:
  657. quote_schema = quote_schema
  658. assert isinstance(schema, str)
  659. self.schema = quoted_name(schema, quote_schema)
  660. self._sentinel_column = None
  661. self.indexes = set()
  662. self.constraints = set()
  663. PrimaryKeyConstraint(
  664. _implicit_generated=True
  665. )._set_parent_with_dispatch(self)
  666. self.foreign_keys = set() # type: ignore
  667. self._extra_dependencies: Set[Table] = set()
  668. if self.schema is not None:
  669. self.fullname = "%s.%s" % (self.schema, self.name)
  670. else:
  671. self.fullname = self.name
  672. self.implicit_returning = implicit_returning
  673. _reflect_info = kw.pop("_reflect_info", None)
  674. self.comment = comment
  675. if info is not None:
  676. self.info = info
  677. if listeners is not None:
  678. for evt, fn in listeners:
  679. event.listen(self, evt, fn)
  680. self._prefixes = prefixes if prefixes else []
  681. self._extra_kwargs(**kw)
  682. # load column definitions from the database if 'autoload' is defined
  683. # we do it after the table is in the singleton dictionary to support
  684. # circular foreign keys
  685. if autoload_with is not None:
  686. self._autoload(
  687. metadata,
  688. autoload_with,
  689. include_columns,
  690. _extend_on=_extend_on,
  691. _reflect_info=_reflect_info,
  692. resolve_fks=resolve_fks,
  693. )
  694. # initialize all the column, etc. objects. done after reflection to
  695. # allow user-overrides
  696. self._init_items(
  697. *args,
  698. allow_replacements=extend_existing
  699. or keep_existing
  700. or autoload_with,
  701. all_names={},
  702. )
  703. def _autoload(
  704. self,
  705. metadata: MetaData,
  706. autoload_with: Union[Engine, Connection],
  707. include_columns: Optional[Collection[str]],
  708. exclude_columns: Collection[str] = (),
  709. resolve_fks: bool = True,
  710. _extend_on: Optional[Set[Table]] = None,
  711. _reflect_info: _ReflectionInfo | None = None,
  712. ) -> None:
  713. insp = inspection.inspect(autoload_with)
  714. with insp._inspection_context() as conn_insp:
  715. conn_insp.reflect_table(
  716. self,
  717. include_columns,
  718. exclude_columns,
  719. resolve_fks,
  720. _extend_on=_extend_on,
  721. _reflect_info=_reflect_info,
  722. )
  723. @property
  724. def _sorted_constraints(self) -> List[Constraint]:
  725. """Return the set of constraints as a list, sorted by creation
  726. order.
  727. """
  728. return sorted(self.constraints, key=lambda c: c._creation_order)
  729. @property
  730. def foreign_key_constraints(self) -> Set[ForeignKeyConstraint]:
  731. """:class:`_schema.ForeignKeyConstraint` objects referred to by this
  732. :class:`_schema.Table`.
  733. This list is produced from the collection of
  734. :class:`_schema.ForeignKey`
  735. objects currently associated.
  736. .. seealso::
  737. :attr:`_schema.Table.constraints`
  738. :attr:`_schema.Table.foreign_keys`
  739. :attr:`_schema.Table.indexes`
  740. """
  741. return {
  742. fkc.constraint
  743. for fkc in self.foreign_keys
  744. if fkc.constraint is not None
  745. }
  746. def _init_existing(self, *args: Any, **kwargs: Any) -> None:
  747. autoload_with = kwargs.pop("autoload_with", None)
  748. autoload = kwargs.pop("autoload", autoload_with is not None)
  749. autoload_replace = kwargs.pop("autoload_replace", True)
  750. schema = kwargs.pop("schema", None)
  751. _extend_on = kwargs.pop("_extend_on", None)
  752. _reflect_info = kwargs.pop("_reflect_info", None)
  753. # these arguments are only used with _init()
  754. extend_existing = kwargs.pop("extend_existing", False)
  755. keep_existing = kwargs.pop("keep_existing", False)
  756. assert extend_existing
  757. assert not keep_existing
  758. if schema and schema != self.schema:
  759. raise exc.ArgumentError(
  760. f"Can't change schema of existing table "
  761. f"from '{self.schema}' to '{schema}'",
  762. )
  763. include_columns = kwargs.pop("include_columns", None)
  764. if include_columns is not None:
  765. for c in self.c:
  766. if c.name not in include_columns:
  767. self._columns.remove(c)
  768. resolve_fks = kwargs.pop("resolve_fks", True)
  769. for key in ("quote", "quote_schema"):
  770. if key in kwargs:
  771. raise exc.ArgumentError(
  772. "Can't redefine 'quote' or 'quote_schema' arguments"
  773. )
  774. # update `self` with these kwargs, if provided
  775. self.comment = kwargs.pop("comment", self.comment)
  776. self.implicit_returning = kwargs.pop(
  777. "implicit_returning", self.implicit_returning
  778. )
  779. self.info = kwargs.pop("info", self.info)
  780. exclude_columns: _typing_Sequence[str]
  781. if autoload:
  782. if not autoload_replace:
  783. # don't replace columns already present.
  784. # we'd like to do this for constraints also however we don't
  785. # have simple de-duping for unnamed constraints.
  786. exclude_columns = [c.name for c in self.c]
  787. else:
  788. exclude_columns = ()
  789. self._autoload(
  790. self.metadata,
  791. autoload_with,
  792. include_columns,
  793. exclude_columns,
  794. resolve_fks,
  795. _extend_on=_extend_on,
  796. _reflect_info=_reflect_info,
  797. )
  798. all_names = {c.name: c for c in self.c}
  799. self._extra_kwargs(**kwargs)
  800. self._init_items(*args, allow_replacements=True, all_names=all_names)
  801. def _extra_kwargs(self, **kwargs: Any) -> None:
  802. self._validate_dialect_kwargs(kwargs)
  803. def _init_collections(self) -> None:
  804. pass
  805. def _reset_exported(self) -> None:
  806. pass
  807. @util.ro_non_memoized_property
  808. def _autoincrement_column(self) -> Optional[Column[int]]:
  809. return self.primary_key._autoincrement_column
  810. @util.ro_memoized_property
  811. def _sentinel_column_characteristics(
  812. self,
  813. ) -> _SentinelColumnCharacterization:
  814. """determine a candidate column (or columns, in case of a client
  815. generated composite primary key) which can be used as an
  816. "insert sentinel" for an INSERT statement.
  817. The returned structure, :class:`_SentinelColumnCharacterization`,
  818. includes all the details needed by :class:`.Dialect` and
  819. :class:`.SQLCompiler` to determine if these column(s) can be used
  820. as an INSERT..RETURNING sentinel for a particular database
  821. dialect.
  822. .. versionadded:: 2.0.10
  823. """
  824. sentinel_is_explicit = False
  825. sentinel_is_autoinc = False
  826. the_sentinel: Optional[_typing_Sequence[Column[Any]]] = None
  827. # see if a column was explicitly marked "insert_sentinel=True".
  828. explicit_sentinel_col = self._sentinel_column
  829. if explicit_sentinel_col is not None:
  830. the_sentinel = (explicit_sentinel_col,)
  831. sentinel_is_explicit = True
  832. autoinc_col = self._autoincrement_column
  833. if sentinel_is_explicit and explicit_sentinel_col is autoinc_col:
  834. assert autoinc_col is not None
  835. sentinel_is_autoinc = True
  836. elif explicit_sentinel_col is None and autoinc_col is not None:
  837. the_sentinel = (autoinc_col,)
  838. sentinel_is_autoinc = True
  839. default_characterization = _SentinelDefaultCharacterization.UNKNOWN
  840. if the_sentinel:
  841. the_sentinel_zero = the_sentinel[0]
  842. if the_sentinel_zero.identity:
  843. if the_sentinel_zero.identity._increment_is_negative:
  844. if sentinel_is_explicit:
  845. raise exc.InvalidRequestError(
  846. "Can't use IDENTITY default with negative "
  847. "increment as an explicit sentinel column"
  848. )
  849. else:
  850. if sentinel_is_autoinc:
  851. autoinc_col = None
  852. sentinel_is_autoinc = False
  853. the_sentinel = None
  854. else:
  855. default_characterization = (
  856. _SentinelDefaultCharacterization.IDENTITY
  857. )
  858. elif (
  859. the_sentinel_zero.default is None
  860. and the_sentinel_zero.server_default is None
  861. ):
  862. if the_sentinel_zero.nullable:
  863. raise exc.InvalidRequestError(
  864. f"Column {the_sentinel_zero} has been marked as a "
  865. "sentinel "
  866. "column with no default generation function; it "
  867. "at least needs to be marked nullable=False assuming "
  868. "user-populated sentinel values will be used."
  869. )
  870. default_characterization = (
  871. _SentinelDefaultCharacterization.NONE
  872. )
  873. elif the_sentinel_zero.default is not None:
  874. if the_sentinel_zero.default.is_sentinel:
  875. default_characterization = (
  876. _SentinelDefaultCharacterization.SENTINEL_DEFAULT
  877. )
  878. elif default_is_sequence(the_sentinel_zero.default):
  879. if the_sentinel_zero.default._increment_is_negative:
  880. if sentinel_is_explicit:
  881. raise exc.InvalidRequestError(
  882. "Can't use SEQUENCE default with negative "
  883. "increment as an explicit sentinel column"
  884. )
  885. else:
  886. if sentinel_is_autoinc:
  887. autoinc_col = None
  888. sentinel_is_autoinc = False
  889. the_sentinel = None
  890. default_characterization = (
  891. _SentinelDefaultCharacterization.SEQUENCE
  892. )
  893. elif the_sentinel_zero.default.is_callable:
  894. default_characterization = (
  895. _SentinelDefaultCharacterization.CLIENTSIDE
  896. )
  897. elif the_sentinel_zero.server_default is not None:
  898. if sentinel_is_explicit:
  899. raise exc.InvalidRequestError(
  900. f"Column {the_sentinel[0]} can't be a sentinel column "
  901. "because it uses an explicit server side default "
  902. "that's not the Identity() default."
  903. )
  904. default_characterization = (
  905. _SentinelDefaultCharacterization.SERVERSIDE
  906. )
  907. if the_sentinel is None and self.primary_key:
  908. assert autoinc_col is None
  909. # determine for non-autoincrement pk if all elements are
  910. # client side
  911. for _pkc in self.primary_key:
  912. if _pkc.server_default is not None or (
  913. _pkc.default and not _pkc.default.is_callable
  914. ):
  915. break
  916. else:
  917. the_sentinel = tuple(self.primary_key)
  918. default_characterization = (
  919. _SentinelDefaultCharacterization.CLIENTSIDE
  920. )
  921. return _SentinelColumnCharacterization(
  922. the_sentinel,
  923. sentinel_is_explicit,
  924. sentinel_is_autoinc,
  925. default_characterization,
  926. )
  927. @property
  928. def autoincrement_column(self) -> Optional[Column[int]]:
  929. """Returns the :class:`.Column` object which currently represents
  930. the "auto increment" column, if any, else returns None.
  931. This is based on the rules for :class:`.Column` as defined by the
  932. :paramref:`.Column.autoincrement` parameter, which generally means the
  933. column within a single integer column primary key constraint that is
  934. not constrained by a foreign key. If the table does not have such
  935. a primary key constraint, then there's no "autoincrement" column.
  936. A :class:`.Table` may have only one column defined as the
  937. "autoincrement" column.
  938. .. versionadded:: 2.0.4
  939. .. seealso::
  940. :paramref:`.Column.autoincrement`
  941. """
  942. return self._autoincrement_column
  943. @property
  944. def key(self) -> str:
  945. """Return the 'key' for this :class:`_schema.Table`.
  946. This value is used as the dictionary key within the
  947. :attr:`_schema.MetaData.tables` collection. It is typically the same
  948. as that of :attr:`_schema.Table.name` for a table with no
  949. :attr:`_schema.Table.schema`
  950. set; otherwise it is typically of the form
  951. ``schemaname.tablename``.
  952. """
  953. return _get_table_key(self.name, self.schema)
  954. def __repr__(self) -> str:
  955. return "Table(%s)" % ", ".join(
  956. [repr(self.name)]
  957. + [repr(self.metadata)]
  958. + [repr(x) for x in self.columns]
  959. + ["%s=%s" % (k, repr(getattr(self, k))) for k in ["schema"]]
  960. )
  961. def __str__(self) -> str:
  962. return _get_table_key(self.description, self.schema)
  963. def add_is_dependent_on(self, table: Table) -> None:
  964. """Add a 'dependency' for this Table.
  965. This is another Table object which must be created
  966. first before this one can, or dropped after this one.
  967. Usually, dependencies between tables are determined via
  968. ForeignKey objects. However, for other situations that
  969. create dependencies outside of foreign keys (rules, inheriting),
  970. this method can manually establish such a link.
  971. """
  972. self._extra_dependencies.add(table)
  973. def append_column(
  974. self, column: ColumnClause[Any], replace_existing: bool = False
  975. ) -> None:
  976. """Append a :class:`_schema.Column` to this :class:`_schema.Table`.
  977. The "key" of the newly added :class:`_schema.Column`, i.e. the
  978. value of its ``.key`` attribute, will then be available
  979. in the ``.c`` collection of this :class:`_schema.Table`, and the
  980. column definition will be included in any CREATE TABLE, SELECT,
  981. UPDATE, etc. statements generated from this :class:`_schema.Table`
  982. construct.
  983. Note that this does **not** change the definition of the table
  984. as it exists within any underlying database, assuming that
  985. table has already been created in the database. Relational
  986. databases support the addition of columns to existing tables
  987. using the SQL ALTER command, which would need to be
  988. emitted for an already-existing table that doesn't contain
  989. the newly added column.
  990. :param replace_existing: When ``True``, allows replacing existing
  991. columns. When ``False``, the default, an warning will be raised
  992. if a column with the same ``.key`` already exists. A future
  993. version of sqlalchemy will instead rise a warning.
  994. .. versionadded:: 1.4.0
  995. """
  996. try:
  997. column._set_parent_with_dispatch(
  998. self,
  999. allow_replacements=replace_existing,
  1000. all_names={c.name: c for c in self.c},
  1001. )
  1002. except exc.DuplicateColumnError as de:
  1003. raise exc.DuplicateColumnError(
  1004. f"{de.args[0]} Specify replace_existing=True to "
  1005. "Table.append_column() to replace an "
  1006. "existing column."
  1007. ) from de
  1008. def append_constraint(self, constraint: Union[Index, Constraint]) -> None:
  1009. """Append a :class:`_schema.Constraint` to this
  1010. :class:`_schema.Table`.
  1011. This has the effect of the constraint being included in any
  1012. future CREATE TABLE statement, assuming specific DDL creation
  1013. events have not been associated with the given
  1014. :class:`_schema.Constraint` object.
  1015. Note that this does **not** produce the constraint within the
  1016. relational database automatically, for a table that already exists
  1017. in the database. To add a constraint to an
  1018. existing relational database table, the SQL ALTER command must
  1019. be used. SQLAlchemy also provides the
  1020. :class:`.AddConstraint` construct which can produce this SQL when
  1021. invoked as an executable clause.
  1022. """
  1023. constraint._set_parent_with_dispatch(self)
  1024. def _set_parent(self, parent: SchemaEventTarget, **kw: Any) -> None:
  1025. metadata = parent
  1026. assert isinstance(metadata, MetaData)
  1027. metadata._add_table(self.name, self.schema, self)
  1028. self.metadata = metadata
  1029. def create(self, bind: _CreateDropBind, checkfirst: bool = False) -> None:
  1030. """Issue a ``CREATE`` statement for this
  1031. :class:`_schema.Table`, using the given
  1032. :class:`.Connection` or :class:`.Engine`
  1033. for connectivity.
  1034. .. seealso::
  1035. :meth:`_schema.MetaData.create_all`.
  1036. """
  1037. bind._run_ddl_visitor(ddl.SchemaGenerator, self, checkfirst=checkfirst)
  1038. def drop(self, bind: _CreateDropBind, checkfirst: bool = False) -> None:
  1039. """Issue a ``DROP`` statement for this
  1040. :class:`_schema.Table`, using the given
  1041. :class:`.Connection` or :class:`.Engine` for connectivity.
  1042. .. seealso::
  1043. :meth:`_schema.MetaData.drop_all`.
  1044. """
  1045. bind._run_ddl_visitor(ddl.SchemaDropper, self, checkfirst=checkfirst)
  1046. @util.deprecated(
  1047. "1.4",
  1048. ":meth:`_schema.Table.tometadata` is renamed to "
  1049. ":meth:`_schema.Table.to_metadata`",
  1050. )
  1051. def tometadata(
  1052. self,
  1053. metadata: MetaData,
  1054. schema: Union[str, Literal[SchemaConst.RETAIN_SCHEMA]] = RETAIN_SCHEMA,
  1055. referred_schema_fn: Optional[
  1056. Callable[
  1057. [Table, Optional[str], ForeignKeyConstraint, Optional[str]],
  1058. Optional[str],
  1059. ]
  1060. ] = None,
  1061. name: Optional[str] = None,
  1062. ) -> Table:
  1063. """Return a copy of this :class:`_schema.Table`
  1064. associated with a different
  1065. :class:`_schema.MetaData`.
  1066. See :meth:`_schema.Table.to_metadata` for a full description.
  1067. """
  1068. return self.to_metadata(
  1069. metadata,
  1070. schema=schema,
  1071. referred_schema_fn=referred_schema_fn,
  1072. name=name,
  1073. )
  1074. def to_metadata(
  1075. self,
  1076. metadata: MetaData,
  1077. schema: Union[str, Literal[SchemaConst.RETAIN_SCHEMA]] = RETAIN_SCHEMA,
  1078. referred_schema_fn: Optional[
  1079. Callable[
  1080. [Table, Optional[str], ForeignKeyConstraint, Optional[str]],
  1081. Optional[str],
  1082. ]
  1083. ] = None,
  1084. name: Optional[str] = None,
  1085. ) -> Table:
  1086. """Return a copy of this :class:`_schema.Table` associated with a
  1087. different :class:`_schema.MetaData`.
  1088. E.g.::
  1089. m1 = MetaData()
  1090. user = Table("user", m1, Column("id", Integer, primary_key=True))
  1091. m2 = MetaData()
  1092. user_copy = user.to_metadata(m2)
  1093. .. versionchanged:: 1.4 The :meth:`_schema.Table.to_metadata` function
  1094. was renamed from :meth:`_schema.Table.tometadata`.
  1095. :param metadata: Target :class:`_schema.MetaData` object,
  1096. into which the
  1097. new :class:`_schema.Table` object will be created.
  1098. :param schema: optional string name indicating the target schema.
  1099. Defaults to the special symbol :attr:`.RETAIN_SCHEMA` which indicates
  1100. that no change to the schema name should be made in the new
  1101. :class:`_schema.Table`. If set to a string name, the new
  1102. :class:`_schema.Table`
  1103. will have this new name as the ``.schema``. If set to ``None``, the
  1104. schema will be set to that of the schema set on the target
  1105. :class:`_schema.MetaData`, which is typically ``None`` as well,
  1106. unless
  1107. set explicitly::
  1108. m2 = MetaData(schema="newschema")
  1109. # user_copy_one will have "newschema" as the schema name
  1110. user_copy_one = user.to_metadata(m2, schema=None)
  1111. m3 = MetaData() # schema defaults to None
  1112. # user_copy_two will have None as the schema name
  1113. user_copy_two = user.to_metadata(m3, schema=None)
  1114. :param referred_schema_fn: optional callable which can be supplied
  1115. in order to provide for the schema name that should be assigned
  1116. to the referenced table of a :class:`_schema.ForeignKeyConstraint`.
  1117. The callable accepts this parent :class:`_schema.Table`, the
  1118. target schema that we are changing to, the
  1119. :class:`_schema.ForeignKeyConstraint` object, and the existing
  1120. "target schema" of that constraint. The function should return the
  1121. string schema name that should be applied. To reset the schema
  1122. to "none", return the symbol :data:`.BLANK_SCHEMA`. To effect no
  1123. change, return ``None`` or :data:`.RETAIN_SCHEMA`.
  1124. .. versionchanged:: 1.4.33 The ``referred_schema_fn`` function
  1125. may return the :data:`.BLANK_SCHEMA` or :data:`.RETAIN_SCHEMA`
  1126. symbols.
  1127. E.g.::
  1128. def referred_schema_fn(table, to_schema, constraint, referred_schema):
  1129. if referred_schema == "base_tables":
  1130. return referred_schema
  1131. else:
  1132. return to_schema
  1133. new_table = table.to_metadata(
  1134. m2, schema="alt_schema", referred_schema_fn=referred_schema_fn
  1135. )
  1136. :param name: optional string name indicating the target table name.
  1137. If not specified or None, the table name is retained. This allows
  1138. a :class:`_schema.Table` to be copied to the same
  1139. :class:`_schema.MetaData` target
  1140. with a new name.
  1141. """ # noqa: E501
  1142. if name is None:
  1143. name = self.name
  1144. actual_schema: Optional[str]
  1145. if schema is RETAIN_SCHEMA:
  1146. actual_schema = self.schema
  1147. elif schema is None:
  1148. actual_schema = metadata.schema
  1149. else:
  1150. actual_schema = schema
  1151. key = _get_table_key(name, actual_schema)
  1152. if key in metadata.tables:
  1153. util.warn(
  1154. f"Table '{self.description}' already exists within the given "
  1155. "MetaData - not copying."
  1156. )
  1157. return metadata.tables[key]
  1158. args = []
  1159. for col in self.columns:
  1160. args.append(col._copy(schema=actual_schema, _to_metadata=metadata))
  1161. table = Table(
  1162. name,
  1163. metadata,
  1164. schema=actual_schema,
  1165. comment=self.comment,
  1166. *args,
  1167. **self.kwargs,
  1168. )
  1169. for const in self.constraints:
  1170. if isinstance(const, ForeignKeyConstraint):
  1171. referred_schema = const._referred_schema
  1172. if referred_schema_fn:
  1173. fk_constraint_schema = referred_schema_fn(
  1174. self, actual_schema, const, referred_schema
  1175. )
  1176. else:
  1177. fk_constraint_schema = (
  1178. actual_schema
  1179. if referred_schema == self.schema
  1180. else None
  1181. )
  1182. table.append_constraint(
  1183. const._copy(
  1184. schema=fk_constraint_schema, target_table=table
  1185. )
  1186. )
  1187. elif not const._type_bound:
  1188. # skip unique constraints that would be generated
  1189. # by the 'unique' flag on Column
  1190. if const._column_flag:
  1191. continue
  1192. table.append_constraint(
  1193. const._copy(schema=actual_schema, target_table=table)
  1194. )
  1195. for index in self.indexes:
  1196. # skip indexes that would be generated
  1197. # by the 'index' flag on Column
  1198. if index._column_flag:
  1199. continue
  1200. Index(
  1201. index.name,
  1202. unique=index.unique,
  1203. *[
  1204. _copy_expression(expr, self, table)
  1205. for expr in index._table_bound_expressions
  1206. ],
  1207. _table=table,
  1208. **index.kwargs,
  1209. )
  1210. return self._schema_item_copy(table)
  1211. class Column(DialectKWArgs, SchemaItem, ColumnClause[_T]):
  1212. """Represents a column in a database table."""
  1213. __visit_name__ = "column"
  1214. inherit_cache = True
  1215. key: str
  1216. server_default: Optional[FetchedValue]
  1217. def __init__(
  1218. self,
  1219. __name_pos: Optional[
  1220. Union[str, _TypeEngineArgument[_T], SchemaEventTarget]
  1221. ] = None,
  1222. __type_pos: Optional[
  1223. Union[_TypeEngineArgument[_T], SchemaEventTarget]
  1224. ] = None,
  1225. *args: SchemaEventTarget,
  1226. name: Optional[str] = None,
  1227. type_: Optional[_TypeEngineArgument[_T]] = None,
  1228. autoincrement: _AutoIncrementType = "auto",
  1229. default: Optional[Any] = _NoArg.NO_ARG,
  1230. insert_default: Optional[Any] = _NoArg.NO_ARG,
  1231. doc: Optional[str] = None,
  1232. key: Optional[str] = None,
  1233. index: Optional[bool] = None,
  1234. unique: Optional[bool] = None,
  1235. info: Optional[_InfoType] = None,
  1236. nullable: Optional[
  1237. Union[bool, Literal[SchemaConst.NULL_UNSPECIFIED]]
  1238. ] = SchemaConst.NULL_UNSPECIFIED,
  1239. onupdate: Optional[Any] = None,
  1240. primary_key: bool = False,
  1241. server_default: Optional[_ServerDefaultArgument] = None,
  1242. server_onupdate: Optional[_ServerOnUpdateArgument] = None,
  1243. quote: Optional[bool] = None,
  1244. system: bool = False,
  1245. comment: Optional[str] = None,
  1246. insert_sentinel: bool = False,
  1247. _omit_from_statements: bool = False,
  1248. _proxies: Optional[Any] = None,
  1249. **dialect_kwargs: Any,
  1250. ):
  1251. r"""
  1252. Construct a new ``Column`` object.
  1253. :param name: The name of this column as represented in the database.
  1254. This argument may be the first positional argument, or specified
  1255. via keyword.
  1256. Names which contain no upper case characters
  1257. will be treated as case insensitive names, and will not be quoted
  1258. unless they are a reserved word. Names with any number of upper
  1259. case characters will be quoted and sent exactly. Note that this
  1260. behavior applies even for databases which standardize upper
  1261. case names as case insensitive such as Oracle Database.
  1262. The name field may be omitted at construction time and applied
  1263. later, at any time before the Column is associated with a
  1264. :class:`_schema.Table`. This is to support convenient
  1265. usage within the :mod:`~sqlalchemy.ext.declarative` extension.
  1266. :param type\_: The column's type, indicated using an instance which
  1267. subclasses :class:`~sqlalchemy.types.TypeEngine`. If no arguments
  1268. are required for the type, the class of the type can be sent
  1269. as well, e.g.::
  1270. # use a type with arguments
  1271. Column("data", String(50))
  1272. # use no arguments
  1273. Column("level", Integer)
  1274. The ``type`` argument may be the second positional argument
  1275. or specified by keyword.
  1276. If the ``type`` is ``None`` or is omitted, it will first default to
  1277. the special type :class:`.NullType`. If and when this
  1278. :class:`_schema.Column` is made to refer to another column using
  1279. :class:`_schema.ForeignKey` and/or
  1280. :class:`_schema.ForeignKeyConstraint`, the type
  1281. of the remote-referenced column will be copied to this column as
  1282. well, at the moment that the foreign key is resolved against that
  1283. remote :class:`_schema.Column` object.
  1284. :param \*args: Additional positional arguments include various
  1285. :class:`.SchemaItem` derived constructs which will be applied
  1286. as options to the column. These include instances of
  1287. :class:`.Constraint`, :class:`_schema.ForeignKey`,
  1288. :class:`.ColumnDefault`, :class:`.Sequence`, :class:`.Computed`
  1289. :class:`.Identity`. In some cases an
  1290. equivalent keyword argument is available such as ``server_default``,
  1291. ``default`` and ``unique``.
  1292. :param autoincrement: Set up "auto increment" semantics for an
  1293. **integer primary key column with no foreign key dependencies**
  1294. (see later in this docstring for a more specific definition).
  1295. This may influence the :term:`DDL` that will be emitted for
  1296. this column during a table create, as well as how the column
  1297. will be considered when INSERT statements are compiled and
  1298. executed.
  1299. The default value is the string ``"auto"``,
  1300. which indicates that a single-column (i.e. non-composite) primary key
  1301. that is of an INTEGER type with no other client-side or server-side
  1302. default constructs indicated should receive auto increment semantics
  1303. automatically. Other values include ``True`` (force this column to
  1304. have auto-increment semantics for a :term:`composite primary key` as
  1305. well), ``False`` (this column should never have auto-increment
  1306. semantics), and the string ``"ignore_fk"`` (special-case for foreign
  1307. key columns, see below).
  1308. The term "auto increment semantics" refers both to the kind of DDL
  1309. that will be emitted for the column within a CREATE TABLE statement,
  1310. when methods such as :meth:`.MetaData.create_all` and
  1311. :meth:`.Table.create` are invoked, as well as how the column will be
  1312. considered when an INSERT statement is compiled and emitted to the
  1313. database:
  1314. * **DDL rendering** (i.e. :meth:`.MetaData.create_all`,
  1315. :meth:`.Table.create`): When used on a :class:`.Column` that has
  1316. no other
  1317. default-generating construct associated with it (such as a
  1318. :class:`.Sequence` or :class:`.Identity` construct), the parameter
  1319. will imply that database-specific keywords such as PostgreSQL
  1320. ``SERIAL``, MySQL ``AUTO_INCREMENT``, or ``IDENTITY`` on SQL Server
  1321. should also be rendered. Not every database backend has an
  1322. "implied" default generator available; for example the Oracle Database
  1323. backends alway needs an explicit construct such as
  1324. :class:`.Identity` to be included with a :class:`.Column` in order
  1325. for the DDL rendered to include auto-generating constructs to also
  1326. be produced in the database.
  1327. * **INSERT semantics** (i.e. when a :func:`_sql.insert` construct is
  1328. compiled into a SQL string and is then executed on a database using
  1329. :meth:`_engine.Connection.execute` or equivalent): A single-row
  1330. INSERT statement will be known to produce a new integer primary key
  1331. value automatically for this column, which will be accessible
  1332. after the statement is invoked via the
  1333. :attr:`.CursorResult.inserted_primary_key` attribute upon the
  1334. :class:`_result.Result` object. This also applies towards use of the
  1335. ORM when ORM-mapped objects are persisted to the database,
  1336. indicating that a new integer primary key will be available to
  1337. become part of the :term:`identity key` for that object. This
  1338. behavior takes place regardless of what DDL constructs are
  1339. associated with the :class:`_schema.Column` and is independent
  1340. of the "DDL Rendering" behavior discussed in the previous note
  1341. above.
  1342. The parameter may be set to ``True`` to indicate that a column which
  1343. is part of a composite (i.e. multi-column) primary key should
  1344. have autoincrement semantics, though note that only one column
  1345. within a primary key may have this setting. It can also
  1346. be set to ``True`` to indicate autoincrement semantics on a
  1347. column that has a client-side or server-side default configured,
  1348. however note that not all dialects can accommodate all styles
  1349. of default as an "autoincrement". It can also be
  1350. set to ``False`` on a single-column primary key that has a
  1351. datatype of INTEGER in order to disable auto increment semantics
  1352. for that column.
  1353. The setting *only* has an effect for columns which are:
  1354. * Integer derived (i.e. INT, SMALLINT, BIGINT).
  1355. * Part of the primary key
  1356. * Not referring to another column via :class:`_schema.ForeignKey`,
  1357. unless
  1358. the value is specified as ``'ignore_fk'``::
  1359. # turn on autoincrement for this column despite
  1360. # the ForeignKey()
  1361. Column(
  1362. "id",
  1363. ForeignKey("other.id"),
  1364. primary_key=True,
  1365. autoincrement="ignore_fk",
  1366. )
  1367. It is typically not desirable to have "autoincrement" enabled on a
  1368. column that refers to another via foreign key, as such a column is
  1369. required to refer to a value that originates from elsewhere.
  1370. The setting has these effects on columns that meet the
  1371. above criteria:
  1372. * DDL issued for the column, if the column does not already include
  1373. a default generating construct supported by the backend such as
  1374. :class:`.Identity`, will include database-specific
  1375. keywords intended to signify this column as an
  1376. "autoincrement" column for specific backends. Behavior for
  1377. primary SQLAlchemy dialects includes:
  1378. * AUTO INCREMENT on MySQL and MariaDB
  1379. * SERIAL on PostgreSQL
  1380. * IDENTITY on MS-SQL - this occurs even without the
  1381. :class:`.Identity` construct as the
  1382. :paramref:`.Column.autoincrement` parameter pre-dates this
  1383. construct.
  1384. * SQLite - SQLite integer primary key columns are implicitly
  1385. "auto incrementing" and no additional keywords are rendered;
  1386. to render the special SQLite keyword ``AUTOINCREMENT``
  1387. is not included as this is unnecessary and not recommended
  1388. by the database vendor. See the section
  1389. :ref:`sqlite_autoincrement` for more background.
  1390. * Oracle Database - The Oracle Database dialects have no default "autoincrement"
  1391. feature available at this time, instead the :class:`.Identity`
  1392. construct is recommended to achieve this (the :class:`.Sequence`
  1393. construct may also be used).
  1394. * Third-party dialects - consult those dialects' documentation
  1395. for details on their specific behaviors.
  1396. * When a single-row :func:`_sql.insert` construct is compiled and
  1397. executed, which does not set the :meth:`_sql.Insert.inline`
  1398. modifier, newly generated primary key values for this column
  1399. will be automatically retrieved upon statement execution
  1400. using a method specific to the database driver in use:
  1401. * MySQL, SQLite - calling upon ``cursor.lastrowid()``
  1402. (see
  1403. `https://www.python.org/dev/peps/pep-0249/#lastrowid
  1404. <https://www.python.org/dev/peps/pep-0249/#lastrowid>`_)
  1405. * PostgreSQL, SQL Server, Oracle Database - use RETURNING or an equivalent
  1406. construct when rendering an INSERT statement, and then retrieving
  1407. the newly generated primary key values after execution
  1408. * PostgreSQL, Oracle Database for :class:`_schema.Table` objects that
  1409. set :paramref:`_schema.Table.implicit_returning` to False -
  1410. for a :class:`.Sequence` only, the :class:`.Sequence` is invoked
  1411. explicitly before the INSERT statement takes place so that the
  1412. newly generated primary key value is available to the client
  1413. * SQL Server for :class:`_schema.Table` objects that
  1414. set :paramref:`_schema.Table.implicit_returning` to False -
  1415. the ``SELECT scope_identity()`` construct is used after the
  1416. INSERT statement is invoked to retrieve the newly generated
  1417. primary key value.
  1418. * Third-party dialects - consult those dialects' documentation
  1419. for details on their specific behaviors.
  1420. * For multiple-row :func:`_sql.insert` constructs invoked with
  1421. a list of parameters (i.e. "executemany" semantics), primary-key
  1422. retrieving behaviors are generally disabled, however there may
  1423. be special APIs that may be used to retrieve lists of new
  1424. primary key values for an "executemany", such as the psycopg2
  1425. "fast insertmany" feature. Such features are very new and
  1426. may not yet be well covered in documentation.
  1427. :param default: A scalar, Python callable, or
  1428. :class:`_expression.ColumnElement` expression representing the
  1429. *default value* for this column, which will be invoked upon insert
  1430. if this column is otherwise not specified in the VALUES clause of
  1431. the insert. This is a shortcut to using :class:`.ColumnDefault` as
  1432. a positional argument; see that class for full detail on the
  1433. structure of the argument.
  1434. Contrast this argument to
  1435. :paramref:`_schema.Column.server_default`
  1436. which creates a default generator on the database side.
  1437. .. seealso::
  1438. :ref:`metadata_defaults_toplevel`
  1439. :param insert_default: An alias of :paramref:`.Column.default`
  1440. for compatibility with :func:`_orm.mapped_column`.
  1441. .. versionadded: 2.0.31
  1442. :param doc: optional String that can be used by the ORM or similar
  1443. to document attributes on the Python side. This attribute does
  1444. **not** render SQL comments; use the
  1445. :paramref:`_schema.Column.comment`
  1446. parameter for this purpose.
  1447. :param key: An optional string identifier which will identify this
  1448. ``Column`` object on the :class:`_schema.Table`.
  1449. When a key is provided,
  1450. this is the only identifier referencing the ``Column`` within the
  1451. application, including ORM attribute mapping; the ``name`` field
  1452. is used only when rendering SQL.
  1453. :param index: When ``True``, indicates that a :class:`_schema.Index`
  1454. construct will be automatically generated for this
  1455. :class:`_schema.Column`, which will result in a "CREATE INDEX"
  1456. statement being emitted for the :class:`_schema.Table` when the DDL
  1457. create operation is invoked.
  1458. Using this flag is equivalent to making use of the
  1459. :class:`_schema.Index` construct explicitly at the level of the
  1460. :class:`_schema.Table` construct itself::
  1461. Table(
  1462. "some_table",
  1463. metadata,
  1464. Column("x", Integer),
  1465. Index("ix_some_table_x", "x"),
  1466. )
  1467. To add the :paramref:`_schema.Index.unique` flag to the
  1468. :class:`_schema.Index`, set both the
  1469. :paramref:`_schema.Column.unique` and
  1470. :paramref:`_schema.Column.index` flags to True simultaneously,
  1471. which will have the effect of rendering the "CREATE UNIQUE INDEX"
  1472. DDL instruction instead of "CREATE INDEX".
  1473. The name of the index is generated using the
  1474. :ref:`default naming convention <constraint_default_naming_convention>`
  1475. which for the :class:`_schema.Index` construct is of the form
  1476. ``ix_<tablename>_<columnname>``.
  1477. As this flag is intended only as a convenience for the common case
  1478. of adding a single-column, default configured index to a table
  1479. definition, explicit use of the :class:`_schema.Index` construct
  1480. should be preferred for most use cases, including composite indexes
  1481. that encompass more than one column, indexes with SQL expressions
  1482. or ordering, backend-specific index configuration options, and
  1483. indexes that use a specific name.
  1484. .. note:: the :attr:`_schema.Column.index` attribute on
  1485. :class:`_schema.Column`
  1486. **does not indicate** if this column is indexed or not, only
  1487. if this flag was explicitly set here. To view indexes on
  1488. a column, view the :attr:`_schema.Table.indexes` collection
  1489. or use :meth:`_reflection.Inspector.get_indexes`.
  1490. .. seealso::
  1491. :ref:`schema_indexes`
  1492. :ref:`constraint_naming_conventions`
  1493. :paramref:`_schema.Column.unique`
  1494. :param info: Optional data dictionary which will be populated into the
  1495. :attr:`.SchemaItem.info` attribute of this object.
  1496. :param nullable: When set to ``False``, will cause the "NOT NULL"
  1497. phrase to be added when generating DDL for the column. When
  1498. ``True``, will normally generate nothing (in SQL this defaults to
  1499. "NULL"), except in some very specific backend-specific edge cases
  1500. where "NULL" may render explicitly.
  1501. Defaults to ``True`` unless :paramref:`_schema.Column.primary_key`
  1502. is also ``True`` or the column specifies a :class:`_sql.Identity`,
  1503. in which case it defaults to ``False``.
  1504. This parameter is only used when issuing CREATE TABLE statements.
  1505. .. note::
  1506. When the column specifies a :class:`_sql.Identity` this
  1507. parameter is in general ignored by the DDL compiler. The
  1508. PostgreSQL database allows nullable identity column by
  1509. setting this parameter to ``True`` explicitly.
  1510. :param onupdate: A scalar, Python callable, or
  1511. :class:`~sqlalchemy.sql.expression.ClauseElement` representing a
  1512. default value to be applied to the column within UPDATE
  1513. statements, which will be invoked upon update if this column is not
  1514. present in the SET clause of the update. This is a shortcut to
  1515. using :class:`.ColumnDefault` as a positional argument with
  1516. ``for_update=True``.
  1517. .. seealso::
  1518. :ref:`metadata_defaults` - complete discussion of onupdate
  1519. :param primary_key: If ``True``, marks this column as a primary key
  1520. column. Multiple columns can have this flag set to specify
  1521. composite primary keys. As an alternative, the primary key of a
  1522. :class:`_schema.Table` can be specified via an explicit
  1523. :class:`.PrimaryKeyConstraint` object.
  1524. :param server_default: A :class:`.FetchedValue` instance, str, Unicode
  1525. or :func:`~sqlalchemy.sql.expression.text` construct representing
  1526. the DDL DEFAULT value for the column.
  1527. String types will be emitted as-is, surrounded by single quotes::
  1528. Column("x", Text, server_default="val")
  1529. will render:
  1530. .. sourcecode:: sql
  1531. x TEXT DEFAULT 'val'
  1532. A :func:`~sqlalchemy.sql.expression.text` expression will be
  1533. rendered as-is, without quotes::
  1534. Column("y", DateTime, server_default=text("NOW()"))
  1535. will render:
  1536. .. sourcecode:: sql
  1537. y DATETIME DEFAULT NOW()
  1538. Strings and text() will be converted into a
  1539. :class:`.DefaultClause` object upon initialization.
  1540. This parameter can also accept complex combinations of contextually
  1541. valid SQLAlchemy expressions or constructs::
  1542. from sqlalchemy import create_engine
  1543. from sqlalchemy import Table, Column, MetaData, ARRAY, Text
  1544. from sqlalchemy.dialects.postgresql import array
  1545. engine = create_engine(
  1546. "postgresql+psycopg2://scott:tiger@localhost/mydatabase"
  1547. )
  1548. metadata_obj = MetaData()
  1549. tbl = Table(
  1550. "foo",
  1551. metadata_obj,
  1552. Column(
  1553. "bar", ARRAY(Text), server_default=array(["biz", "bang", "bash"])
  1554. ),
  1555. )
  1556. metadata_obj.create_all(engine)
  1557. The above results in a table created with the following SQL:
  1558. .. sourcecode:: sql
  1559. CREATE TABLE foo (
  1560. bar TEXT[] DEFAULT ARRAY['biz', 'bang', 'bash']
  1561. )
  1562. Use :class:`.FetchedValue` to indicate that an already-existing
  1563. column will generate a default value on the database side which
  1564. will be available to SQLAlchemy for post-fetch after inserts. This
  1565. construct does not specify any DDL and the implementation is left
  1566. to the database, such as via a trigger.
  1567. .. seealso::
  1568. :ref:`server_defaults` - complete discussion of server side
  1569. defaults
  1570. :param server_onupdate: A :class:`.FetchedValue` instance
  1571. representing a database-side default generation function,
  1572. such as a trigger. This
  1573. indicates to SQLAlchemy that a newly generated value will be
  1574. available after updates. This construct does not actually
  1575. implement any kind of generation function within the database,
  1576. which instead must be specified separately.
  1577. .. warning:: This directive **does not** currently produce MySQL's
  1578. "ON UPDATE CURRENT_TIMESTAMP()" clause. See
  1579. :ref:`mysql_timestamp_onupdate` for background on how to
  1580. produce this clause.
  1581. .. seealso::
  1582. :ref:`triggered_columns`
  1583. :param quote: Force quoting of this column's name on or off,
  1584. corresponding to ``True`` or ``False``. When left at its default
  1585. of ``None``, the column identifier will be quoted according to
  1586. whether the name is case sensitive (identifiers with at least one
  1587. upper case character are treated as case sensitive), or if it's a
  1588. reserved word. This flag is only needed to force quoting of a
  1589. reserved word which is not known by the SQLAlchemy dialect.
  1590. :param unique: When ``True``, and the :paramref:`_schema.Column.index`
  1591. parameter is left at its default value of ``False``,
  1592. indicates that a :class:`_schema.UniqueConstraint`
  1593. construct will be automatically generated for this
  1594. :class:`_schema.Column`,
  1595. which will result in a "UNIQUE CONSTRAINT" clause referring
  1596. to this column being included
  1597. in the ``CREATE TABLE`` statement emitted, when the DDL create
  1598. operation for the :class:`_schema.Table` object is invoked.
  1599. When this flag is ``True`` while the
  1600. :paramref:`_schema.Column.index` parameter is simultaneously
  1601. set to ``True``, the effect instead is that a
  1602. :class:`_schema.Index` construct which includes the
  1603. :paramref:`_schema.Index.unique` parameter set to ``True``
  1604. is generated. See the documentation for
  1605. :paramref:`_schema.Column.index` for additional detail.
  1606. Using this flag is equivalent to making use of the
  1607. :class:`_schema.UniqueConstraint` construct explicitly at the
  1608. level of the :class:`_schema.Table` construct itself::
  1609. Table("some_table", metadata, Column("x", Integer), UniqueConstraint("x"))
  1610. The :paramref:`_schema.UniqueConstraint.name` parameter
  1611. of the unique constraint object is left at its default value
  1612. of ``None``; in the absence of a :ref:`naming convention <constraint_naming_conventions>`
  1613. for the enclosing :class:`_schema.MetaData`, the UNIQUE CONSTRAINT
  1614. construct will be emitted as unnamed, which typically invokes
  1615. a database-specific naming convention to take place.
  1616. As this flag is intended only as a convenience for the common case
  1617. of adding a single-column, default configured unique constraint to a table
  1618. definition, explicit use of the :class:`_schema.UniqueConstraint` construct
  1619. should be preferred for most use cases, including composite constraints
  1620. that encompass more than one column, backend-specific index configuration options, and
  1621. constraints that use a specific name.
  1622. .. note:: the :attr:`_schema.Column.unique` attribute on
  1623. :class:`_schema.Column`
  1624. **does not indicate** if this column has a unique constraint or
  1625. not, only if this flag was explicitly set here. To view
  1626. indexes and unique constraints that may involve this column,
  1627. view the
  1628. :attr:`_schema.Table.indexes` and/or
  1629. :attr:`_schema.Table.constraints` collections or use
  1630. :meth:`_reflection.Inspector.get_indexes` and/or
  1631. :meth:`_reflection.Inspector.get_unique_constraints`
  1632. .. seealso::
  1633. :ref:`schema_unique_constraint`
  1634. :ref:`constraint_naming_conventions`
  1635. :paramref:`_schema.Column.index`
  1636. :param system: When ``True``, indicates this is a "system" column,
  1637. that is a column which is automatically made available by the
  1638. database, and should not be included in the columns list for a
  1639. ``CREATE TABLE`` statement.
  1640. For more elaborate scenarios where columns should be
  1641. conditionally rendered differently on different backends,
  1642. consider custom compilation rules for :class:`.CreateColumn`.
  1643. :param comment: Optional string that will render an SQL comment on
  1644. table creation.
  1645. .. versionadded:: 1.2 Added the
  1646. :paramref:`_schema.Column.comment`
  1647. parameter to :class:`_schema.Column`.
  1648. :param insert_sentinel: Marks this :class:`_schema.Column` as an
  1649. :term:`insert sentinel` used for optimizing the performance of the
  1650. :term:`insertmanyvalues` feature for tables that don't
  1651. otherwise have qualifying primary key configurations.
  1652. .. versionadded:: 2.0.10
  1653. .. seealso::
  1654. :func:`_schema.insert_sentinel` - all in one helper for declaring
  1655. sentinel columns
  1656. :ref:`engine_insertmanyvalues`
  1657. :ref:`engine_insertmanyvalues_sentinel_columns`
  1658. """ # noqa: E501, RST201, RST202
  1659. l_args = [__name_pos, __type_pos] + list(args)
  1660. del args
  1661. if l_args:
  1662. if isinstance(l_args[0], str):
  1663. if name is not None:
  1664. raise exc.ArgumentError(
  1665. "May not pass name positionally and as a keyword."
  1666. )
  1667. name = l_args.pop(0) # type: ignore
  1668. elif l_args[0] is None:
  1669. l_args.pop(0)
  1670. if l_args:
  1671. coltype = l_args[0]
  1672. if hasattr(coltype, "_sqla_type"):
  1673. if type_ is not None:
  1674. raise exc.ArgumentError(
  1675. "May not pass type_ positionally and as a keyword."
  1676. )
  1677. type_ = l_args.pop(0) # type: ignore
  1678. elif l_args[0] is None:
  1679. l_args.pop(0)
  1680. if name is not None:
  1681. name = quoted_name(name, quote)
  1682. elif quote is not None:
  1683. raise exc.ArgumentError(
  1684. "Explicit 'name' is required when sending 'quote' argument"
  1685. )
  1686. # name = None is expected to be an interim state
  1687. # note this use case is legacy now that ORM declarative has a
  1688. # dedicated "column" construct local to the ORM
  1689. super().__init__(name, type_) # type: ignore
  1690. self.key = key if key is not None else name # type: ignore
  1691. self.primary_key = primary_key
  1692. self._insert_sentinel = insert_sentinel
  1693. self._omit_from_statements = _omit_from_statements
  1694. self._user_defined_nullable = udn = nullable
  1695. if udn is not NULL_UNSPECIFIED:
  1696. self.nullable = udn
  1697. else:
  1698. self.nullable = not primary_key
  1699. # these default to None because .index and .unique is *not*
  1700. # an informational flag about Column - there can still be an
  1701. # Index or UniqueConstraint referring to this Column.
  1702. self.index = index
  1703. self.unique = unique
  1704. self.system = system
  1705. self.doc = doc
  1706. self.autoincrement: _AutoIncrementType = autoincrement
  1707. self.constraints = set()
  1708. self.foreign_keys = set()
  1709. self.comment = comment
  1710. self.computed = None
  1711. self.identity = None
  1712. # check if this Column is proxying another column
  1713. if _proxies is not None:
  1714. self._proxies = _proxies
  1715. else:
  1716. # otherwise, add DDL-related events
  1717. self._set_type(self.type)
  1718. if insert_default is not _NoArg.NO_ARG:
  1719. resolved_default = insert_default
  1720. elif default is not _NoArg.NO_ARG:
  1721. resolved_default = default
  1722. else:
  1723. resolved_default = None
  1724. if resolved_default is not None:
  1725. if not isinstance(resolved_default, (ColumnDefault, Sequence)):
  1726. resolved_default = ColumnDefault(resolved_default)
  1727. self.default = resolved_default
  1728. l_args.append(resolved_default)
  1729. else:
  1730. self.default = None
  1731. if onupdate is not None:
  1732. if not isinstance(onupdate, (ColumnDefault, Sequence)):
  1733. onupdate = ColumnDefault(onupdate, for_update=True)
  1734. self.onupdate = onupdate
  1735. l_args.append(onupdate)
  1736. else:
  1737. self.onupdate = None
  1738. if server_default is not None:
  1739. if isinstance(server_default, FetchedValue):
  1740. server_default = server_default._as_for_update(False)
  1741. l_args.append(server_default)
  1742. else:
  1743. server_default = DefaultClause(server_default)
  1744. l_args.append(server_default)
  1745. self.server_default = server_default
  1746. if server_onupdate is not None:
  1747. if isinstance(server_onupdate, FetchedValue):
  1748. server_onupdate = server_onupdate._as_for_update(True)
  1749. l_args.append(server_onupdate)
  1750. else:
  1751. server_onupdate = DefaultClause(
  1752. server_onupdate, for_update=True
  1753. )
  1754. l_args.append(server_onupdate)
  1755. self.server_onupdate = server_onupdate
  1756. self._init_items(*cast(_typing_Sequence[SchemaItem], l_args))
  1757. util.set_creation_order(self)
  1758. if info is not None:
  1759. self.info = info
  1760. self._extra_kwargs(**dialect_kwargs)
  1761. table: Table
  1762. constraints: Set[Constraint]
  1763. foreign_keys: Set[ForeignKey]
  1764. """A collection of all :class:`_schema.ForeignKey` marker objects
  1765. associated with this :class:`_schema.Column`.
  1766. Each object is a member of a :class:`_schema.Table`-wide
  1767. :class:`_schema.ForeignKeyConstraint`.
  1768. .. seealso::
  1769. :attr:`_schema.Table.foreign_keys`
  1770. """
  1771. index: Optional[bool]
  1772. """The value of the :paramref:`_schema.Column.index` parameter.
  1773. Does not indicate if this :class:`_schema.Column` is actually indexed
  1774. or not; use :attr:`_schema.Table.indexes`.
  1775. .. seealso::
  1776. :attr:`_schema.Table.indexes`
  1777. """
  1778. unique: Optional[bool]
  1779. """The value of the :paramref:`_schema.Column.unique` parameter.
  1780. Does not indicate if this :class:`_schema.Column` is actually subject to
  1781. a unique constraint or not; use :attr:`_schema.Table.indexes` and
  1782. :attr:`_schema.Table.constraints`.
  1783. .. seealso::
  1784. :attr:`_schema.Table.indexes`
  1785. :attr:`_schema.Table.constraints`.
  1786. """
  1787. computed: Optional[Computed]
  1788. identity: Optional[Identity]
  1789. def _set_type(self, type_: TypeEngine[Any]) -> None:
  1790. assert self.type._isnull or type_ is self.type
  1791. self.type = type_
  1792. if isinstance(self.type, SchemaEventTarget):
  1793. self.type._set_parent_with_dispatch(self)
  1794. for impl in self.type._variant_mapping.values():
  1795. if isinstance(impl, SchemaEventTarget):
  1796. impl._set_parent_with_dispatch(self)
  1797. @HasMemoized.memoized_attribute
  1798. def _default_description_tuple(self) -> _DefaultDescriptionTuple:
  1799. """used by default.py -> _process_execute_defaults()"""
  1800. return _DefaultDescriptionTuple._from_column_default(self.default)
  1801. @HasMemoized.memoized_attribute
  1802. def _onupdate_description_tuple(self) -> _DefaultDescriptionTuple:
  1803. """used by default.py -> _process_execute_defaults()"""
  1804. return _DefaultDescriptionTuple._from_column_default(self.onupdate)
  1805. @util.memoized_property
  1806. def _gen_static_annotations_cache_key(self) -> bool:
  1807. """special attribute used by cache key gen, if true, we will
  1808. use a static cache key for the annotations dictionary, else we
  1809. will generate a new cache key for annotations each time.
  1810. Added for #8790
  1811. """
  1812. return self.table is not None and self.table._is_table
  1813. def _extra_kwargs(self, **kwargs: Any) -> None:
  1814. self._validate_dialect_kwargs(kwargs)
  1815. def __str__(self) -> str:
  1816. if self.name is None:
  1817. return "(no name)"
  1818. elif self.table is not None:
  1819. if self.table.named_with_column:
  1820. return self.table.description + "." + self.description
  1821. else:
  1822. return self.description
  1823. else:
  1824. return self.description
  1825. def references(self, column: Column[Any]) -> bool:
  1826. """Return True if this Column references the given column via foreign
  1827. key."""
  1828. for fk in self.foreign_keys:
  1829. if fk.column.proxy_set.intersection(column.proxy_set):
  1830. return True
  1831. else:
  1832. return False
  1833. def append_foreign_key(self, fk: ForeignKey) -> None:
  1834. fk._set_parent_with_dispatch(self)
  1835. def __repr__(self) -> str:
  1836. kwarg = []
  1837. if self.key != self.name:
  1838. kwarg.append("key")
  1839. if self.primary_key:
  1840. kwarg.append("primary_key")
  1841. if not self.nullable:
  1842. kwarg.append("nullable")
  1843. if self.onupdate:
  1844. kwarg.append("onupdate")
  1845. if self.default:
  1846. kwarg.append("default")
  1847. if self.server_default:
  1848. kwarg.append("server_default")
  1849. if self.comment:
  1850. kwarg.append("comment")
  1851. return "Column(%s)" % ", ".join(
  1852. [repr(self.name)]
  1853. + [repr(self.type)]
  1854. + [repr(x) for x in self.foreign_keys if x is not None]
  1855. + [repr(x) for x in self.constraints]
  1856. + [
  1857. (
  1858. self.table is not None
  1859. and "table=<%s>" % self.table.description
  1860. or "table=None"
  1861. )
  1862. ]
  1863. + ["%s=%s" % (k, repr(getattr(self, k))) for k in kwarg]
  1864. )
  1865. def _set_parent( # type: ignore[override]
  1866. self,
  1867. parent: SchemaEventTarget,
  1868. *,
  1869. all_names: Dict[str, Column[Any]],
  1870. allow_replacements: bool,
  1871. **kw: Any,
  1872. ) -> None:
  1873. table = parent
  1874. assert isinstance(table, Table)
  1875. if not self.name:
  1876. raise exc.ArgumentError(
  1877. "Column must be constructed with a non-blank name or "
  1878. "assign a non-blank .name before adding to a Table."
  1879. )
  1880. self._reset_memoizations()
  1881. if self.key is None:
  1882. self.key = self.name
  1883. existing = getattr(self, "table", None)
  1884. if existing is not None and existing is not table:
  1885. raise exc.ArgumentError(
  1886. f"Column object '{self.key}' already "
  1887. f"assigned to Table '{existing.description}'"
  1888. )
  1889. extra_remove = None
  1890. existing_col = None
  1891. conflicts_on = ""
  1892. if self.key in table._columns:
  1893. existing_col = table._columns[self.key]
  1894. if self.key == self.name:
  1895. conflicts_on = "name"
  1896. else:
  1897. conflicts_on = "key"
  1898. elif self.name in all_names:
  1899. existing_col = all_names[self.name]
  1900. extra_remove = {existing_col}
  1901. conflicts_on = "name"
  1902. if existing_col is not None:
  1903. if existing_col is not self:
  1904. if not allow_replacements:
  1905. raise exc.DuplicateColumnError(
  1906. f"A column with {conflicts_on} "
  1907. f"""'{
  1908. self.key if conflicts_on == 'key' else self.name
  1909. }' """
  1910. f"is already present in table '{table.name}'."
  1911. )
  1912. for fk in existing_col.foreign_keys:
  1913. table.foreign_keys.remove(fk)
  1914. if fk.constraint in table.constraints:
  1915. # this might have been removed
  1916. # already, if it's a composite constraint
  1917. # and more than one col being replaced
  1918. table.constraints.remove(fk.constraint)
  1919. if extra_remove and existing_col is not None and self.key == self.name:
  1920. util.warn(
  1921. f'Column with user-specified key "{existing_col.key}" is '
  1922. "being replaced with "
  1923. f'plain named column "{self.name}", '
  1924. f'key "{existing_col.key}" is being removed. If this is a '
  1925. "reflection operation, specify autoload_replace=False to "
  1926. "prevent this replacement."
  1927. )
  1928. table._columns.replace(self, extra_remove=extra_remove)
  1929. all_names[self.name] = self
  1930. self.table = table
  1931. if self._insert_sentinel:
  1932. if self.table._sentinel_column is not None:
  1933. raise exc.ArgumentError(
  1934. "a Table may have only one explicit sentinel column"
  1935. )
  1936. self.table._sentinel_column = self
  1937. if self.primary_key:
  1938. table.primary_key._replace(self)
  1939. elif self.key in table.primary_key:
  1940. raise exc.ArgumentError(
  1941. f"Trying to redefine primary-key column '{self.key}' as a "
  1942. f"non-primary-key column on table '{table.fullname}'"
  1943. )
  1944. if self.index:
  1945. if isinstance(self.index, str):
  1946. raise exc.ArgumentError(
  1947. "The 'index' keyword argument on Column is boolean only. "
  1948. "To create indexes with a specific name, create an "
  1949. "explicit Index object external to the Table."
  1950. )
  1951. table.append_constraint(
  1952. Index(
  1953. None, self.key, unique=bool(self.unique), _column_flag=True
  1954. )
  1955. )
  1956. elif self.unique:
  1957. if isinstance(self.unique, str):
  1958. raise exc.ArgumentError(
  1959. "The 'unique' keyword argument on Column is boolean "
  1960. "only. To create unique constraints or indexes with a "
  1961. "specific name, append an explicit UniqueConstraint to "
  1962. "the Table's list of elements, or create an explicit "
  1963. "Index object external to the Table."
  1964. )
  1965. table.append_constraint(
  1966. UniqueConstraint(self.key, _column_flag=True)
  1967. )
  1968. self._setup_on_memoized_fks(lambda fk: fk._set_remote_table(table))
  1969. if self.identity and (
  1970. isinstance(self.default, Sequence)
  1971. or isinstance(self.onupdate, Sequence)
  1972. ):
  1973. raise exc.ArgumentError(
  1974. "An column cannot specify both Identity and Sequence."
  1975. )
  1976. def _setup_on_memoized_fks(self, fn: Callable[..., Any]) -> None:
  1977. fk_keys = [
  1978. ((self.table.key, self.key), False),
  1979. ((self.table.key, self.name), True),
  1980. ]
  1981. for fk_key, link_to_name in fk_keys:
  1982. if fk_key in self.table.metadata._fk_memos:
  1983. for fk in self.table.metadata._fk_memos[fk_key]:
  1984. if fk.link_to_name is link_to_name:
  1985. fn(fk)
  1986. def _on_table_attach(self, fn: Callable[..., Any]) -> None:
  1987. if self.table is not None:
  1988. fn(self, self.table)
  1989. else:
  1990. event.listen(self, "after_parent_attach", fn)
  1991. @util.deprecated(
  1992. "1.4",
  1993. "The :meth:`_schema.Column.copy` method is deprecated "
  1994. "and will be removed in a future release.",
  1995. )
  1996. def copy(self, **kw: Any) -> Column[Any]:
  1997. return self._copy(**kw)
  1998. def _copy(self, **kw: Any) -> Column[Any]:
  1999. """Create a copy of this ``Column``, uninitialized.
  2000. This is used in :meth:`_schema.Table.to_metadata` and by the ORM.
  2001. """
  2002. # Constraint objects plus non-constraint-bound ForeignKey objects
  2003. args: List[SchemaItem] = [
  2004. c._copy(**kw) for c in self.constraints if not c._type_bound
  2005. ] + [c._copy(**kw) for c in self.foreign_keys if not c.constraint]
  2006. # ticket #5276
  2007. column_kwargs = {}
  2008. for dialect_name in self.dialect_options:
  2009. dialect_options = self.dialect_options[dialect_name]._non_defaults
  2010. for (
  2011. dialect_option_key,
  2012. dialect_option_value,
  2013. ) in dialect_options.items():
  2014. column_kwargs[dialect_name + "_" + dialect_option_key] = (
  2015. dialect_option_value
  2016. )
  2017. server_default = self.server_default
  2018. server_onupdate = self.server_onupdate
  2019. if isinstance(server_default, (Computed, Identity)):
  2020. # TODO: likely should be copied in all cases
  2021. # TODO: if a Sequence, we would need to transfer the Sequence
  2022. # .metadata as well
  2023. args.append(server_default._copy(**kw))
  2024. server_default = server_onupdate = None
  2025. type_ = self.type
  2026. if isinstance(type_, SchemaEventTarget):
  2027. type_ = type_.copy(**kw)
  2028. # TODO: DefaultGenerator is not copied here! it's just used again
  2029. # with _set_parent() pointing to the old column. see the new
  2030. # use of _copy() in the new _merge() method
  2031. c = self._constructor(
  2032. name=self.name,
  2033. type_=type_,
  2034. key=self.key,
  2035. primary_key=self.primary_key,
  2036. unique=self.unique,
  2037. system=self.system,
  2038. # quote=self.quote, # disabled 2013-08-27 (commit 031ef080)
  2039. index=self.index,
  2040. autoincrement=self.autoincrement,
  2041. default=self.default,
  2042. server_default=server_default,
  2043. onupdate=self.onupdate,
  2044. server_onupdate=server_onupdate,
  2045. doc=self.doc,
  2046. comment=self.comment,
  2047. _omit_from_statements=self._omit_from_statements,
  2048. insert_sentinel=self._insert_sentinel,
  2049. *args,
  2050. **column_kwargs,
  2051. )
  2052. # copy the state of "nullable" exactly, to accommodate for
  2053. # ORM flipping the .nullable flag directly
  2054. c.nullable = self.nullable
  2055. c._user_defined_nullable = self._user_defined_nullable
  2056. return self._schema_item_copy(c)
  2057. def _merge(self, other: Column[Any]) -> None:
  2058. """merge the elements of another column into this one.
  2059. this is used by ORM pep-593 merge and will likely need a lot
  2060. of fixes.
  2061. """
  2062. if self.primary_key:
  2063. other.primary_key = True
  2064. if self.autoincrement != "auto" and other.autoincrement == "auto":
  2065. other.autoincrement = self.autoincrement
  2066. if self.system:
  2067. other.system = self.system
  2068. if self.info:
  2069. other.info.update(self.info)
  2070. type_ = self.type
  2071. if not type_._isnull and other.type._isnull:
  2072. if isinstance(type_, SchemaEventTarget):
  2073. type_ = type_.copy()
  2074. other.type = type_
  2075. if isinstance(type_, SchemaEventTarget):
  2076. type_._set_parent_with_dispatch(other)
  2077. for impl in type_._variant_mapping.values():
  2078. if isinstance(impl, SchemaEventTarget):
  2079. impl._set_parent_with_dispatch(other)
  2080. if (
  2081. self._user_defined_nullable is not NULL_UNSPECIFIED
  2082. and other._user_defined_nullable is NULL_UNSPECIFIED
  2083. ):
  2084. other.nullable = self.nullable
  2085. other._user_defined_nullable = self._user_defined_nullable
  2086. if self.default is not None and other.default is None:
  2087. new_default = self.default._copy()
  2088. new_default._set_parent(other)
  2089. if self.server_default and other.server_default is None:
  2090. new_server_default = self.server_default
  2091. if isinstance(new_server_default, FetchedValue):
  2092. new_server_default = new_server_default._copy()
  2093. new_server_default._set_parent(other)
  2094. else:
  2095. other.server_default = new_server_default
  2096. if self.server_onupdate and other.server_onupdate is None:
  2097. new_server_onupdate = self.server_onupdate
  2098. new_server_onupdate = new_server_onupdate._copy()
  2099. new_server_onupdate._set_parent(other)
  2100. if self.onupdate and other.onupdate is None:
  2101. new_onupdate = self.onupdate._copy()
  2102. new_onupdate._set_parent(other)
  2103. if self.index in (True, False) and other.index is None:
  2104. other.index = self.index
  2105. if self.unique in (True, False) and other.unique is None:
  2106. other.unique = self.unique
  2107. if self.doc and other.doc is None:
  2108. other.doc = self.doc
  2109. if self.comment and other.comment is None:
  2110. other.comment = self.comment
  2111. for const in self.constraints:
  2112. if not const._type_bound:
  2113. new_const = const._copy()
  2114. new_const._set_parent(other)
  2115. for fk in self.foreign_keys:
  2116. if not fk.constraint:
  2117. new_fk = fk._copy()
  2118. new_fk._set_parent(other)
  2119. def _make_proxy(
  2120. self,
  2121. selectable: FromClause,
  2122. primary_key: ColumnSet,
  2123. foreign_keys: Set[KeyedColumnElement[Any]],
  2124. name: Optional[str] = None,
  2125. key: Optional[str] = None,
  2126. name_is_truncatable: bool = False,
  2127. compound_select_cols: Optional[
  2128. _typing_Sequence[ColumnElement[Any]]
  2129. ] = None,
  2130. **kw: Any,
  2131. ) -> Tuple[str, ColumnClause[_T]]:
  2132. """Create a *proxy* for this column.
  2133. This is a copy of this ``Column`` referenced by a different parent
  2134. (such as an alias or select statement). The column should
  2135. be used only in select scenarios, as its full DDL/default
  2136. information is not transferred.
  2137. """
  2138. fk = [
  2139. ForeignKey(
  2140. col if col is not None else f._colspec,
  2141. _unresolvable=col is None,
  2142. _constraint=f.constraint,
  2143. )
  2144. for f, col in [
  2145. (fk, fk._resolve_column(raiseerr=False))
  2146. for fk in self.foreign_keys
  2147. ]
  2148. ]
  2149. if name is None and self.name is None:
  2150. raise exc.InvalidRequestError(
  2151. "Cannot initialize a sub-selectable"
  2152. " with this Column object until its 'name' has "
  2153. "been assigned."
  2154. )
  2155. try:
  2156. c = self._constructor(
  2157. (
  2158. coercions.expect(
  2159. roles.TruncatedLabelRole, name if name else self.name
  2160. )
  2161. if name_is_truncatable
  2162. else (name or self.name)
  2163. ),
  2164. self.type,
  2165. # this may actually be ._proxy_key when the key is incoming
  2166. key=key if key else name if name else self.key,
  2167. primary_key=self.primary_key,
  2168. nullable=self.nullable,
  2169. _proxies=(
  2170. list(compound_select_cols)
  2171. if compound_select_cols
  2172. else [self]
  2173. ),
  2174. *fk,
  2175. )
  2176. except TypeError as err:
  2177. raise TypeError(
  2178. "Could not create a copy of this %r object. "
  2179. "Ensure the class includes a _constructor() "
  2180. "attribute or method which accepts the "
  2181. "standard Column constructor arguments, or "
  2182. "references the Column class itself." % self.__class__
  2183. ) from err
  2184. c.table = selectable
  2185. c._propagate_attrs = selectable._propagate_attrs
  2186. if selectable._is_clone_of is not None:
  2187. c._is_clone_of = selectable._is_clone_of.columns.get(c.key)
  2188. if self.primary_key:
  2189. primary_key.add(c)
  2190. if fk:
  2191. foreign_keys.update(fk) # type: ignore
  2192. return c.key, c
  2193. def insert_sentinel(
  2194. name: Optional[str] = None,
  2195. type_: Optional[_TypeEngineArgument[_T]] = None,
  2196. *,
  2197. default: Optional[Any] = None,
  2198. omit_from_statements: bool = True,
  2199. ) -> Column[Any]:
  2200. """Provides a surrogate :class:`_schema.Column` that will act as a
  2201. dedicated insert :term:`sentinel` column, allowing efficient bulk
  2202. inserts with deterministic RETURNING sorting for tables that
  2203. don't otherwise have qualifying primary key configurations.
  2204. Adding this column to a :class:`.Table` object requires that a
  2205. corresponding database table actually has this column present, so if adding
  2206. it to an existing model, existing database tables would need to be migrated
  2207. (e.g. using ALTER TABLE or similar) to include this column.
  2208. For background on how this object is used, see the section
  2209. :ref:`engine_insertmanyvalues_sentinel_columns` as part of the
  2210. section :ref:`engine_insertmanyvalues`.
  2211. The :class:`_schema.Column` returned will be a nullable integer column by
  2212. default and make use of a sentinel-specific default generator used only in
  2213. "insertmanyvalues" operations.
  2214. .. seealso::
  2215. :func:`_orm.orm_insert_sentinel`
  2216. :paramref:`_schema.Column.insert_sentinel`
  2217. :ref:`engine_insertmanyvalues`
  2218. :ref:`engine_insertmanyvalues_sentinel_columns`
  2219. .. versionadded:: 2.0.10
  2220. """
  2221. return Column(
  2222. name=name,
  2223. type_=type_api.INTEGERTYPE if type_ is None else type_,
  2224. default=(
  2225. default if default is not None else _InsertSentinelColumnDefault()
  2226. ),
  2227. _omit_from_statements=omit_from_statements,
  2228. insert_sentinel=True,
  2229. )
  2230. class ForeignKey(DialectKWArgs, SchemaItem):
  2231. """Defines a dependency between two columns.
  2232. ``ForeignKey`` is specified as an argument to a :class:`_schema.Column`
  2233. object,
  2234. e.g.::
  2235. t = Table(
  2236. "remote_table",
  2237. metadata,
  2238. Column("remote_id", ForeignKey("main_table.id")),
  2239. )
  2240. Note that ``ForeignKey`` is only a marker object that defines
  2241. a dependency between two columns. The actual constraint
  2242. is in all cases represented by the :class:`_schema.ForeignKeyConstraint`
  2243. object. This object will be generated automatically when
  2244. a ``ForeignKey`` is associated with a :class:`_schema.Column` which
  2245. in turn is associated with a :class:`_schema.Table`. Conversely,
  2246. when :class:`_schema.ForeignKeyConstraint` is applied to a
  2247. :class:`_schema.Table`,
  2248. ``ForeignKey`` markers are automatically generated to be
  2249. present on each associated :class:`_schema.Column`, which are also
  2250. associated with the constraint object.
  2251. Note that you cannot define a "composite" foreign key constraint,
  2252. that is a constraint between a grouping of multiple parent/child
  2253. columns, using ``ForeignKey`` objects. To define this grouping,
  2254. the :class:`_schema.ForeignKeyConstraint` object must be used, and applied
  2255. to the :class:`_schema.Table`. The associated ``ForeignKey`` objects
  2256. are created automatically.
  2257. The ``ForeignKey`` objects associated with an individual
  2258. :class:`_schema.Column`
  2259. object are available in the `foreign_keys` collection
  2260. of that column.
  2261. Further examples of foreign key configuration are in
  2262. :ref:`metadata_foreignkeys`.
  2263. """
  2264. __visit_name__ = "foreign_key"
  2265. parent: Column[Any]
  2266. _table_column: Optional[Column[Any]]
  2267. def __init__(
  2268. self,
  2269. column: _DDLColumnArgument,
  2270. _constraint: Optional[ForeignKeyConstraint] = None,
  2271. use_alter: bool = False,
  2272. name: _ConstraintNameArgument = None,
  2273. onupdate: Optional[str] = None,
  2274. ondelete: Optional[str] = None,
  2275. deferrable: Optional[bool] = None,
  2276. initially: Optional[str] = None,
  2277. link_to_name: bool = False,
  2278. match: Optional[str] = None,
  2279. info: Optional[_InfoType] = None,
  2280. comment: Optional[str] = None,
  2281. _unresolvable: bool = False,
  2282. **dialect_kw: Any,
  2283. ):
  2284. r"""
  2285. Construct a column-level FOREIGN KEY.
  2286. The :class:`_schema.ForeignKey` object when constructed generates a
  2287. :class:`_schema.ForeignKeyConstraint`
  2288. which is associated with the parent
  2289. :class:`_schema.Table` object's collection of constraints.
  2290. :param column: A single target column for the key relationship. A
  2291. :class:`_schema.Column` object or a column name as a string:
  2292. ``tablename.columnkey`` or ``schema.tablename.columnkey``.
  2293. ``columnkey`` is the ``key`` which has been assigned to the column
  2294. (defaults to the column name itself), unless ``link_to_name`` is
  2295. ``True`` in which case the rendered name of the column is used.
  2296. :param name: Optional string. An in-database name for the key if
  2297. `constraint` is not provided.
  2298. :param onupdate: Optional string. If set, emit ON UPDATE <value> when
  2299. issuing DDL for this constraint. Typical values include CASCADE,
  2300. DELETE and RESTRICT.
  2301. .. seealso::
  2302. :ref:`on_update_on_delete`
  2303. :param ondelete: Optional string. If set, emit ON DELETE <value> when
  2304. issuing DDL for this constraint. Typical values include CASCADE,
  2305. SET NULL and RESTRICT. Some dialects may allow for additional
  2306. syntaxes.
  2307. .. seealso::
  2308. :ref:`on_update_on_delete`
  2309. :param deferrable: Optional bool. If set, emit DEFERRABLE or NOT
  2310. DEFERRABLE when issuing DDL for this constraint.
  2311. :param initially: Optional string. If set, emit INITIALLY <value> when
  2312. issuing DDL for this constraint.
  2313. :param link_to_name: if True, the string name given in ``column`` is
  2314. the rendered name of the referenced column, not its locally
  2315. assigned ``key``.
  2316. :param use_alter: passed to the underlying
  2317. :class:`_schema.ForeignKeyConstraint`
  2318. to indicate the constraint should
  2319. be generated/dropped externally from the CREATE TABLE/ DROP TABLE
  2320. statement. See :paramref:`_schema.ForeignKeyConstraint.use_alter`
  2321. for further description.
  2322. .. seealso::
  2323. :paramref:`_schema.ForeignKeyConstraint.use_alter`
  2324. :ref:`use_alter`
  2325. :param match: Optional string. If set, emit MATCH <value> when issuing
  2326. DDL for this constraint. Typical values include SIMPLE, PARTIAL
  2327. and FULL.
  2328. :param info: Optional data dictionary which will be populated into the
  2329. :attr:`.SchemaItem.info` attribute of this object.
  2330. :param comment: Optional string that will render an SQL comment on
  2331. foreign key constraint creation.
  2332. .. versionadded:: 2.0
  2333. :param \**dialect_kw: Additional keyword arguments are dialect
  2334. specific, and passed in the form ``<dialectname>_<argname>``. The
  2335. arguments are ultimately handled by a corresponding
  2336. :class:`_schema.ForeignKeyConstraint`.
  2337. See the documentation regarding
  2338. an individual dialect at :ref:`dialect_toplevel` for detail on
  2339. documented arguments.
  2340. """
  2341. self._colspec = coercions.expect(roles.DDLReferredColumnRole, column)
  2342. self._unresolvable = _unresolvable
  2343. if isinstance(self._colspec, str):
  2344. self._table_column = None
  2345. else:
  2346. self._table_column = self._colspec
  2347. if not isinstance(
  2348. self._table_column.table, (type(None), TableClause)
  2349. ):
  2350. raise exc.ArgumentError(
  2351. "ForeignKey received Column not bound "
  2352. "to a Table, got: %r" % self._table_column.table
  2353. )
  2354. # the linked ForeignKeyConstraint.
  2355. # ForeignKey will create this when parent Column
  2356. # is attached to a Table, *or* ForeignKeyConstraint
  2357. # object passes itself in when creating ForeignKey
  2358. # markers.
  2359. self.constraint = _constraint
  2360. # .parent is not Optional under normal use
  2361. self.parent = None # type: ignore
  2362. self.use_alter = use_alter
  2363. self.name = name
  2364. self.onupdate = onupdate
  2365. self.ondelete = ondelete
  2366. self.deferrable = deferrable
  2367. self.initially = initially
  2368. self.link_to_name = link_to_name
  2369. self.match = match
  2370. self.comment = comment
  2371. if info:
  2372. self.info = info
  2373. self._unvalidated_dialect_kw = dialect_kw
  2374. def __repr__(self) -> str:
  2375. return "ForeignKey(%r)" % self._get_colspec()
  2376. @util.deprecated(
  2377. "1.4",
  2378. "The :meth:`_schema.ForeignKey.copy` method is deprecated "
  2379. "and will be removed in a future release.",
  2380. )
  2381. def copy(self, *, schema: Optional[str] = None, **kw: Any) -> ForeignKey:
  2382. return self._copy(schema=schema, **kw)
  2383. def _copy(self, *, schema: Optional[str] = None, **kw: Any) -> ForeignKey:
  2384. """Produce a copy of this :class:`_schema.ForeignKey` object.
  2385. The new :class:`_schema.ForeignKey` will not be bound
  2386. to any :class:`_schema.Column`.
  2387. This method is usually used by the internal
  2388. copy procedures of :class:`_schema.Column`, :class:`_schema.Table`,
  2389. and :class:`_schema.MetaData`.
  2390. :param schema: The returned :class:`_schema.ForeignKey` will
  2391. reference the original table and column name, qualified
  2392. by the given string schema name.
  2393. """
  2394. fk = ForeignKey(
  2395. self._get_colspec(schema=schema),
  2396. use_alter=self.use_alter,
  2397. name=self.name,
  2398. onupdate=self.onupdate,
  2399. ondelete=self.ondelete,
  2400. deferrable=self.deferrable,
  2401. initially=self.initially,
  2402. link_to_name=self.link_to_name,
  2403. match=self.match,
  2404. comment=self.comment,
  2405. **self._unvalidated_dialect_kw,
  2406. )
  2407. return self._schema_item_copy(fk)
  2408. def _get_colspec(
  2409. self,
  2410. schema: Optional[
  2411. Union[
  2412. str,
  2413. Literal[SchemaConst.RETAIN_SCHEMA, SchemaConst.BLANK_SCHEMA],
  2414. ]
  2415. ] = None,
  2416. table_name: Optional[str] = None,
  2417. _is_copy: bool = False,
  2418. ) -> str:
  2419. """Return a string based 'column specification' for this
  2420. :class:`_schema.ForeignKey`.
  2421. This is usually the equivalent of the string-based "tablename.colname"
  2422. argument first passed to the object's constructor.
  2423. """
  2424. if schema not in (None, RETAIN_SCHEMA):
  2425. _schema, tname, colname = self._column_tokens
  2426. if table_name is not None:
  2427. tname = table_name
  2428. if schema is BLANK_SCHEMA:
  2429. return "%s.%s" % (tname, colname)
  2430. else:
  2431. return "%s.%s.%s" % (schema, tname, colname)
  2432. elif table_name:
  2433. schema, tname, colname = self._column_tokens
  2434. if schema:
  2435. return "%s.%s.%s" % (schema, table_name, colname)
  2436. else:
  2437. return "%s.%s" % (table_name, colname)
  2438. elif self._table_column is not None:
  2439. if self._table_column.table is None:
  2440. if _is_copy:
  2441. raise exc.InvalidRequestError(
  2442. f"Can't copy ForeignKey object which refers to "
  2443. f"non-table bound Column {self._table_column!r}"
  2444. )
  2445. else:
  2446. return self._table_column.key
  2447. return "%s.%s" % (
  2448. self._table_column.table.fullname,
  2449. self._table_column.key,
  2450. )
  2451. else:
  2452. assert isinstance(self._colspec, str)
  2453. return self._colspec
  2454. @property
  2455. def _referred_schema(self) -> Optional[str]:
  2456. return self._column_tokens[0]
  2457. def _table_key(self) -> Any:
  2458. if self._table_column is not None:
  2459. if self._table_column.table is None:
  2460. return None
  2461. else:
  2462. return self._table_column.table.key
  2463. else:
  2464. schema, tname, colname = self._column_tokens
  2465. return _get_table_key(tname, schema)
  2466. target_fullname = property(_get_colspec)
  2467. def references(self, table: Table) -> bool:
  2468. """Return True if the given :class:`_schema.Table`
  2469. is referenced by this
  2470. :class:`_schema.ForeignKey`."""
  2471. return table.corresponding_column(self.column) is not None
  2472. def get_referent(self, table: FromClause) -> Optional[Column[Any]]:
  2473. """Return the :class:`_schema.Column` in the given
  2474. :class:`_schema.Table` (or any :class:`.FromClause`)
  2475. referenced by this :class:`_schema.ForeignKey`.
  2476. Returns None if this :class:`_schema.ForeignKey`
  2477. does not reference the given
  2478. :class:`_schema.Table`.
  2479. """
  2480. # our column is a Column, and any subquery etc. proxying us
  2481. # would be doing so via another Column, so that's what would
  2482. # be returned here
  2483. return table.columns.corresponding_column(self.column) # type: ignore
  2484. @util.memoized_property
  2485. def _column_tokens(self) -> Tuple[Optional[str], str, Optional[str]]:
  2486. """parse a string-based _colspec into its component parts."""
  2487. m = self._get_colspec().split(".")
  2488. if m is None:
  2489. raise exc.ArgumentError(
  2490. f"Invalid foreign key column specification: {self._colspec}"
  2491. )
  2492. if len(m) == 1:
  2493. tname = m.pop()
  2494. colname = None
  2495. else:
  2496. colname = m.pop()
  2497. tname = m.pop()
  2498. # A FK between column 'bar' and table 'foo' can be
  2499. # specified as 'foo', 'foo.bar', 'dbo.foo.bar',
  2500. # 'otherdb.dbo.foo.bar'. Once we have the column name and
  2501. # the table name, treat everything else as the schema
  2502. # name. Some databases (e.g. Sybase) support
  2503. # inter-database foreign keys. See tickets#1341 and --
  2504. # indirectly related -- Ticket #594. This assumes that '.'
  2505. # will never appear *within* any component of the FK.
  2506. if len(m) > 0:
  2507. schema = ".".join(m)
  2508. else:
  2509. schema = None
  2510. return schema, tname, colname
  2511. def _resolve_col_tokens(self) -> Tuple[Table, str, Optional[str]]:
  2512. if self.parent is None:
  2513. raise exc.InvalidRequestError(
  2514. "this ForeignKey object does not yet have a "
  2515. "parent Column associated with it."
  2516. )
  2517. elif self.parent.table is None:
  2518. raise exc.InvalidRequestError(
  2519. "this ForeignKey's parent column is not yet associated "
  2520. "with a Table."
  2521. )
  2522. parenttable = self.parent.table
  2523. if self._unresolvable:
  2524. schema, tname, colname = self._column_tokens
  2525. tablekey = _get_table_key(tname, schema)
  2526. return parenttable, tablekey, colname
  2527. # assertion
  2528. # basically Column._make_proxy() sends the actual
  2529. # target Column to the ForeignKey object, so the
  2530. # string resolution here is never called.
  2531. for c in self.parent.base_columns:
  2532. if isinstance(c, Column):
  2533. assert c.table is parenttable
  2534. break
  2535. else:
  2536. assert False
  2537. ######################
  2538. schema, tname, colname = self._column_tokens
  2539. if schema is None and parenttable.metadata.schema is not None:
  2540. schema = parenttable.metadata.schema
  2541. tablekey = _get_table_key(tname, schema)
  2542. return parenttable, tablekey, colname
  2543. def _link_to_col_by_colstring(
  2544. self, parenttable: Table, table: Table, colname: Optional[str]
  2545. ) -> Column[Any]:
  2546. _column = None
  2547. if colname is None:
  2548. # colname is None in the case that ForeignKey argument
  2549. # was specified as table name only, in which case we
  2550. # match the column name to the same column on the
  2551. # parent.
  2552. # this use case wasn't working in later 1.x series
  2553. # as it had no test coverage; fixed in 2.0
  2554. parent = self.parent
  2555. assert parent is not None
  2556. key = parent.key
  2557. _column = table.c.get(key, None)
  2558. elif self.link_to_name:
  2559. key = colname
  2560. for c in table.c:
  2561. if c.name == colname:
  2562. _column = c
  2563. else:
  2564. key = colname
  2565. _column = table.c.get(colname, None)
  2566. if _column is None:
  2567. raise exc.NoReferencedColumnError(
  2568. "Could not initialize target column "
  2569. f"for ForeignKey '{self._colspec}' "
  2570. f"on table '{parenttable.name}': "
  2571. f"table '{table.name}' has no column named '{key}'",
  2572. table.name,
  2573. key,
  2574. )
  2575. return _column
  2576. def _set_target_column(self, column: Column[Any]) -> None:
  2577. assert self.parent is not None
  2578. # propagate TypeEngine to parent if it didn't have one
  2579. if self.parent.type._isnull:
  2580. self.parent.type = column.type
  2581. # super-edgy case, if other FKs point to our column,
  2582. # they'd get the type propagated out also.
  2583. def set_type(fk: ForeignKey) -> None:
  2584. if fk.parent.type._isnull:
  2585. fk.parent.type = column.type
  2586. self.parent._setup_on_memoized_fks(set_type)
  2587. self.column = column # type: ignore
  2588. @util.ro_memoized_property
  2589. def column(self) -> Column[Any]:
  2590. """Return the target :class:`_schema.Column` referenced by this
  2591. :class:`_schema.ForeignKey`.
  2592. If no target column has been established, an exception
  2593. is raised.
  2594. """
  2595. return self._resolve_column()
  2596. @overload
  2597. def _resolve_column(
  2598. self, *, raiseerr: Literal[True] = ...
  2599. ) -> Column[Any]: ...
  2600. @overload
  2601. def _resolve_column(
  2602. self, *, raiseerr: bool = ...
  2603. ) -> Optional[Column[Any]]: ...
  2604. def _resolve_column(
  2605. self, *, raiseerr: bool = True
  2606. ) -> Optional[Column[Any]]:
  2607. _column: Column[Any]
  2608. if isinstance(self._colspec, str):
  2609. parenttable, tablekey, colname = self._resolve_col_tokens()
  2610. if self._unresolvable or tablekey not in parenttable.metadata:
  2611. if not raiseerr:
  2612. return None
  2613. raise exc.NoReferencedTableError(
  2614. f"Foreign key associated with column "
  2615. f"'{self.parent}' could not find "
  2616. f"table '{tablekey}' with which to generate a "
  2617. f"foreign key to target column '{colname}'",
  2618. tablekey,
  2619. )
  2620. elif parenttable.key not in parenttable.metadata:
  2621. if not raiseerr:
  2622. return None
  2623. raise exc.InvalidRequestError(
  2624. f"Table {parenttable} is no longer associated with its "
  2625. "parent MetaData"
  2626. )
  2627. else:
  2628. table = parenttable.metadata.tables[tablekey]
  2629. return self._link_to_col_by_colstring(
  2630. parenttable, table, colname
  2631. )
  2632. elif hasattr(self._colspec, "__clause_element__"):
  2633. _column = self._colspec.__clause_element__()
  2634. return _column
  2635. else:
  2636. _column = self._colspec
  2637. return _column
  2638. def _set_parent(self, parent: SchemaEventTarget, **kw: Any) -> None:
  2639. assert isinstance(parent, Column)
  2640. if self.parent is not None and self.parent is not parent:
  2641. raise exc.InvalidRequestError(
  2642. "This ForeignKey already has a parent !"
  2643. )
  2644. self.parent = parent
  2645. self.parent.foreign_keys.add(self)
  2646. self.parent._on_table_attach(self._set_table)
  2647. def _set_remote_table(self, table: Table) -> None:
  2648. parenttable, _, colname = self._resolve_col_tokens()
  2649. _column = self._link_to_col_by_colstring(parenttable, table, colname)
  2650. self._set_target_column(_column)
  2651. assert self.constraint is not None
  2652. self.constraint._validate_dest_table(table)
  2653. def _remove_from_metadata(self, metadata: MetaData) -> None:
  2654. parenttable, table_key, colname = self._resolve_col_tokens()
  2655. fk_key = (table_key, colname)
  2656. if self in metadata._fk_memos[fk_key]:
  2657. # TODO: no test coverage for self not in memos
  2658. metadata._fk_memos[fk_key].remove(self)
  2659. def _set_table(self, column: Column[Any], table: Table) -> None:
  2660. # standalone ForeignKey - create ForeignKeyConstraint
  2661. # on the hosting Table when attached to the Table.
  2662. assert isinstance(table, Table)
  2663. if self.constraint is None:
  2664. self.constraint = ForeignKeyConstraint(
  2665. [],
  2666. [],
  2667. use_alter=self.use_alter,
  2668. name=self.name,
  2669. onupdate=self.onupdate,
  2670. ondelete=self.ondelete,
  2671. deferrable=self.deferrable,
  2672. initially=self.initially,
  2673. match=self.match,
  2674. comment=self.comment,
  2675. **self._unvalidated_dialect_kw,
  2676. )
  2677. self.constraint._append_element(column, self)
  2678. self.constraint._set_parent_with_dispatch(table)
  2679. table.foreign_keys.add(self)
  2680. # set up remote ".column" attribute, or a note to pick it
  2681. # up when the other Table/Column shows up
  2682. if isinstance(self._colspec, str):
  2683. parenttable, table_key, colname = self._resolve_col_tokens()
  2684. fk_key = (table_key, colname)
  2685. if table_key in parenttable.metadata.tables:
  2686. table = parenttable.metadata.tables[table_key]
  2687. try:
  2688. _column = self._link_to_col_by_colstring(
  2689. parenttable, table, colname
  2690. )
  2691. except exc.NoReferencedColumnError:
  2692. # this is OK, we'll try later
  2693. pass
  2694. else:
  2695. self._set_target_column(_column)
  2696. parenttable.metadata._fk_memos[fk_key].append(self)
  2697. elif hasattr(self._colspec, "__clause_element__"):
  2698. _column = self._colspec.__clause_element__()
  2699. self._set_target_column(_column)
  2700. else:
  2701. _column = self._colspec
  2702. self._set_target_column(_column)
  2703. if TYPE_CHECKING:
  2704. def default_is_sequence(
  2705. obj: Optional[DefaultGenerator],
  2706. ) -> TypeGuard[Sequence]: ...
  2707. def default_is_clause_element(
  2708. obj: Optional[DefaultGenerator],
  2709. ) -> TypeGuard[ColumnElementColumnDefault]: ...
  2710. def default_is_scalar(
  2711. obj: Optional[DefaultGenerator],
  2712. ) -> TypeGuard[ScalarElementColumnDefault]: ...
  2713. else:
  2714. default_is_sequence = operator.attrgetter("is_sequence")
  2715. default_is_clause_element = operator.attrgetter("is_clause_element")
  2716. default_is_scalar = operator.attrgetter("is_scalar")
  2717. class DefaultGenerator(Executable, SchemaItem):
  2718. """Base class for column *default* values.
  2719. This object is only present on column.default or column.onupdate.
  2720. It's not valid as a server default.
  2721. """
  2722. __visit_name__ = "default_generator"
  2723. _is_default_generator = True
  2724. is_sequence = False
  2725. is_identity = False
  2726. is_server_default = False
  2727. is_clause_element = False
  2728. is_callable = False
  2729. is_scalar = False
  2730. has_arg = False
  2731. is_sentinel = False
  2732. column: Optional[Column[Any]]
  2733. def __init__(self, for_update: bool = False) -> None:
  2734. self.for_update = for_update
  2735. def _set_parent(self, parent: SchemaEventTarget, **kw: Any) -> None:
  2736. if TYPE_CHECKING:
  2737. assert isinstance(parent, Column)
  2738. self.column = parent
  2739. if self.for_update:
  2740. self.column.onupdate = self
  2741. else:
  2742. self.column.default = self
  2743. def _copy(self) -> DefaultGenerator:
  2744. raise NotImplementedError()
  2745. def _execute_on_connection(
  2746. self,
  2747. connection: Connection,
  2748. distilled_params: _CoreMultiExecuteParams,
  2749. execution_options: CoreExecuteOptionsParameter,
  2750. ) -> Any:
  2751. util.warn_deprecated(
  2752. "Using the .execute() method to invoke a "
  2753. "DefaultGenerator object is deprecated; please use "
  2754. "the .scalar() method.",
  2755. "2.0",
  2756. )
  2757. return self._execute_on_scalar(
  2758. connection, distilled_params, execution_options
  2759. )
  2760. def _execute_on_scalar(
  2761. self,
  2762. connection: Connection,
  2763. distilled_params: _CoreMultiExecuteParams,
  2764. execution_options: CoreExecuteOptionsParameter,
  2765. ) -> Any:
  2766. return connection._execute_default(
  2767. self, distilled_params, execution_options
  2768. )
  2769. class ColumnDefault(DefaultGenerator, ABC):
  2770. """A plain default value on a column.
  2771. This could correspond to a constant, a callable function,
  2772. or a SQL clause.
  2773. :class:`.ColumnDefault` is generated automatically
  2774. whenever the ``default``, ``onupdate`` arguments of
  2775. :class:`_schema.Column` are used. A :class:`.ColumnDefault`
  2776. can be passed positionally as well.
  2777. For example, the following::
  2778. Column("foo", Integer, default=50)
  2779. Is equivalent to::
  2780. Column("foo", Integer, ColumnDefault(50))
  2781. """
  2782. arg: Any
  2783. @overload
  2784. def __new__(
  2785. cls, arg: Callable[..., Any], for_update: bool = ...
  2786. ) -> CallableColumnDefault: ...
  2787. @overload
  2788. def __new__(
  2789. cls, arg: ColumnElement[Any], for_update: bool = ...
  2790. ) -> ColumnElementColumnDefault: ...
  2791. # if I return ScalarElementColumnDefault here, which is what's actually
  2792. # returned, mypy complains that
  2793. # overloads overlap w/ incompatible return types.
  2794. @overload
  2795. def __new__(cls, arg: object, for_update: bool = ...) -> ColumnDefault: ...
  2796. def __new__(
  2797. cls, arg: Any = None, for_update: bool = False
  2798. ) -> ColumnDefault:
  2799. """Construct a new :class:`.ColumnDefault`.
  2800. :param arg: argument representing the default value.
  2801. May be one of the following:
  2802. * a plain non-callable Python value, such as a
  2803. string, integer, boolean, or other simple type.
  2804. The default value will be used as is each time.
  2805. * a SQL expression, that is one which derives from
  2806. :class:`_expression.ColumnElement`. The SQL expression will
  2807. be rendered into the INSERT or UPDATE statement,
  2808. or in the case of a primary key column when
  2809. RETURNING is not used may be
  2810. pre-executed before an INSERT within a SELECT.
  2811. * A Python callable. The function will be invoked for each
  2812. new row subject to an INSERT or UPDATE.
  2813. The callable must accept exactly
  2814. zero or one positional arguments. The one-argument form
  2815. will receive an instance of the :class:`.ExecutionContext`,
  2816. which provides contextual information as to the current
  2817. :class:`_engine.Connection` in use as well as the current
  2818. statement and parameters.
  2819. """
  2820. if isinstance(arg, FetchedValue):
  2821. raise exc.ArgumentError(
  2822. "ColumnDefault may not be a server-side default type."
  2823. )
  2824. elif callable(arg):
  2825. cls = CallableColumnDefault
  2826. elif isinstance(arg, ClauseElement):
  2827. cls = ColumnElementColumnDefault
  2828. elif arg is not None:
  2829. cls = ScalarElementColumnDefault
  2830. return object.__new__(cls)
  2831. def __repr__(self) -> str:
  2832. return f"{self.__class__.__name__}({self.arg!r})"
  2833. class ScalarElementColumnDefault(ColumnDefault):
  2834. """default generator for a fixed scalar Python value
  2835. .. versionadded: 2.0
  2836. """
  2837. is_scalar = True
  2838. has_arg = True
  2839. def __init__(self, arg: Any, for_update: bool = False) -> None:
  2840. self.for_update = for_update
  2841. self.arg = arg
  2842. def _copy(self) -> ScalarElementColumnDefault:
  2843. return ScalarElementColumnDefault(
  2844. arg=self.arg, for_update=self.for_update
  2845. )
  2846. class _InsertSentinelColumnDefault(ColumnDefault):
  2847. """Default generator that's specific to the use of a "sentinel" column
  2848. when using the insertmanyvalues feature.
  2849. This default is used as part of the :func:`_schema.insert_sentinel`
  2850. construct.
  2851. """
  2852. is_sentinel = True
  2853. for_update = False
  2854. arg = None
  2855. def __new__(cls) -> _InsertSentinelColumnDefault:
  2856. return object.__new__(cls)
  2857. def __init__(self) -> None:
  2858. pass
  2859. def _set_parent(self, parent: SchemaEventTarget, **kw: Any) -> None:
  2860. col = cast("Column[Any]", parent)
  2861. if not col._insert_sentinel:
  2862. raise exc.ArgumentError(
  2863. "The _InsertSentinelColumnDefault may only be applied to a "
  2864. "Column marked as insert_sentinel=True"
  2865. )
  2866. elif not col.nullable:
  2867. raise exc.ArgumentError(
  2868. "The _InsertSentinelColumnDefault may only be applied to a "
  2869. "Column that is nullable"
  2870. )
  2871. super()._set_parent(parent, **kw)
  2872. def _copy(self) -> _InsertSentinelColumnDefault:
  2873. return _InsertSentinelColumnDefault()
  2874. _SQLExprDefault = Union["ColumnElement[Any]", "TextClause"]
  2875. class ColumnElementColumnDefault(ColumnDefault):
  2876. """default generator for a SQL expression
  2877. .. versionadded:: 2.0
  2878. """
  2879. is_clause_element = True
  2880. has_arg = True
  2881. arg: _SQLExprDefault
  2882. def __init__(
  2883. self,
  2884. arg: _SQLExprDefault,
  2885. for_update: bool = False,
  2886. ) -> None:
  2887. self.for_update = for_update
  2888. self.arg = arg
  2889. def _copy(self) -> ColumnElementColumnDefault:
  2890. return ColumnElementColumnDefault(
  2891. arg=self.arg, for_update=self.for_update
  2892. )
  2893. @util.memoized_property
  2894. @util.preload_module("sqlalchemy.sql.sqltypes")
  2895. def _arg_is_typed(self) -> bool:
  2896. sqltypes = util.preloaded.sql_sqltypes
  2897. return not isinstance(self.arg.type, sqltypes.NullType)
  2898. class _CallableColumnDefaultProtocol(Protocol):
  2899. def __call__(self, context: ExecutionContext) -> Any: ...
  2900. class CallableColumnDefault(ColumnDefault):
  2901. """default generator for a callable Python function
  2902. .. versionadded:: 2.0
  2903. """
  2904. is_callable = True
  2905. arg: _CallableColumnDefaultProtocol
  2906. has_arg = True
  2907. def __init__(
  2908. self,
  2909. arg: Union[_CallableColumnDefaultProtocol, Callable[[], Any]],
  2910. for_update: bool = False,
  2911. ) -> None:
  2912. self.for_update = for_update
  2913. self.arg = self._maybe_wrap_callable(arg)
  2914. def _copy(self) -> CallableColumnDefault:
  2915. return CallableColumnDefault(arg=self.arg, for_update=self.for_update)
  2916. def _maybe_wrap_callable(
  2917. self, fn: Union[_CallableColumnDefaultProtocol, Callable[[], Any]]
  2918. ) -> _CallableColumnDefaultProtocol:
  2919. """Wrap callables that don't accept a context.
  2920. This is to allow easy compatibility with default callables
  2921. that aren't specific to accepting of a context.
  2922. """
  2923. try:
  2924. argspec = util.get_callable_argspec(fn, no_self=True)
  2925. except TypeError:
  2926. return util.wrap_callable(lambda ctx: fn(), fn) # type: ignore
  2927. defaulted = argspec[3] is not None and len(argspec[3]) or 0
  2928. positionals = len(argspec[0]) - defaulted
  2929. if positionals == 0:
  2930. return util.wrap_callable(lambda ctx: fn(), fn) # type: ignore
  2931. elif positionals == 1:
  2932. return fn # type: ignore
  2933. else:
  2934. raise exc.ArgumentError(
  2935. "ColumnDefault Python function takes zero or one "
  2936. "positional arguments"
  2937. )
  2938. class IdentityOptions:
  2939. """Defines options for a named database sequence or an identity column.
  2940. .. versionadded:: 1.3.18
  2941. .. seealso::
  2942. :class:`.Sequence`
  2943. """
  2944. def __init__(
  2945. self,
  2946. start: Optional[int] = None,
  2947. increment: Optional[int] = None,
  2948. minvalue: Optional[int] = None,
  2949. maxvalue: Optional[int] = None,
  2950. nominvalue: Optional[bool] = None,
  2951. nomaxvalue: Optional[bool] = None,
  2952. cycle: Optional[bool] = None,
  2953. cache: Optional[int] = None,
  2954. order: Optional[bool] = None,
  2955. ) -> None:
  2956. """Construct a :class:`.IdentityOptions` object.
  2957. See the :class:`.Sequence` documentation for a complete description
  2958. of the parameters.
  2959. :param start: the starting index of the sequence.
  2960. :param increment: the increment value of the sequence.
  2961. :param minvalue: the minimum value of the sequence.
  2962. :param maxvalue: the maximum value of the sequence.
  2963. :param nominvalue: no minimum value of the sequence.
  2964. :param nomaxvalue: no maximum value of the sequence.
  2965. :param cycle: allows the sequence to wrap around when the maxvalue
  2966. or minvalue has been reached.
  2967. :param cache: optional integer value; number of future values in the
  2968. sequence which are calculated in advance.
  2969. :param order: optional boolean value; if ``True``, renders the
  2970. ORDER keyword.
  2971. """
  2972. self.start = start
  2973. self.increment = increment
  2974. self.minvalue = minvalue
  2975. self.maxvalue = maxvalue
  2976. self.nominvalue = nominvalue
  2977. self.nomaxvalue = nomaxvalue
  2978. self.cycle = cycle
  2979. self.cache = cache
  2980. self.order = order
  2981. @property
  2982. def _increment_is_negative(self) -> bool:
  2983. return self.increment is not None and self.increment < 0
  2984. class Sequence(HasSchemaAttr, IdentityOptions, DefaultGenerator):
  2985. """Represents a named database sequence.
  2986. The :class:`.Sequence` object represents the name and configurational
  2987. parameters of a database sequence. It also represents
  2988. a construct that can be "executed" by a SQLAlchemy :class:`_engine.Engine`
  2989. or :class:`_engine.Connection`,
  2990. rendering the appropriate "next value" function
  2991. for the target database and returning a result.
  2992. The :class:`.Sequence` is typically associated with a primary key column::
  2993. some_table = Table(
  2994. "some_table",
  2995. metadata,
  2996. Column(
  2997. "id",
  2998. Integer,
  2999. Sequence("some_table_seq", start=1),
  3000. primary_key=True,
  3001. ),
  3002. )
  3003. When CREATE TABLE is emitted for the above :class:`_schema.Table`, if the
  3004. target platform supports sequences, a CREATE SEQUENCE statement will
  3005. be emitted as well. For platforms that don't support sequences,
  3006. the :class:`.Sequence` construct is ignored.
  3007. .. seealso::
  3008. :ref:`defaults_sequences`
  3009. :class:`.CreateSequence`
  3010. :class:`.DropSequence`
  3011. """
  3012. __visit_name__ = "sequence"
  3013. is_sequence = True
  3014. column: Optional[Column[Any]]
  3015. data_type: Optional[TypeEngine[int]]
  3016. def __init__(
  3017. self,
  3018. name: str,
  3019. start: Optional[int] = None,
  3020. increment: Optional[int] = None,
  3021. minvalue: Optional[int] = None,
  3022. maxvalue: Optional[int] = None,
  3023. nominvalue: Optional[bool] = None,
  3024. nomaxvalue: Optional[bool] = None,
  3025. cycle: Optional[bool] = None,
  3026. schema: Optional[Union[str, Literal[SchemaConst.BLANK_SCHEMA]]] = None,
  3027. cache: Optional[int] = None,
  3028. order: Optional[bool] = None,
  3029. data_type: Optional[_TypeEngineArgument[int]] = None,
  3030. optional: bool = False,
  3031. quote: Optional[bool] = None,
  3032. metadata: Optional[MetaData] = None,
  3033. quote_schema: Optional[bool] = None,
  3034. for_update: bool = False,
  3035. ) -> None:
  3036. """Construct a :class:`.Sequence` object.
  3037. :param name: the name of the sequence.
  3038. :param start: the starting index of the sequence. This value is
  3039. used when the CREATE SEQUENCE command is emitted to the database
  3040. as the value of the "START WITH" clause. If ``None``, the
  3041. clause is omitted, which on most platforms indicates a starting
  3042. value of 1.
  3043. .. versionchanged:: 2.0 The :paramref:`.Sequence.start` parameter
  3044. is required in order to have DDL emit "START WITH". This is a
  3045. reversal of a change made in version 1.4 which would implicitly
  3046. render "START WITH 1" if the :paramref:`.Sequence.start` were
  3047. not included. See :ref:`change_7211` for more detail.
  3048. :param increment: the increment value of the sequence. This
  3049. value is used when the CREATE SEQUENCE command is emitted to
  3050. the database as the value of the "INCREMENT BY" clause. If ``None``,
  3051. the clause is omitted, which on most platforms indicates an
  3052. increment of 1.
  3053. :param minvalue: the minimum value of the sequence. This
  3054. value is used when the CREATE SEQUENCE command is emitted to
  3055. the database as the value of the "MINVALUE" clause. If ``None``,
  3056. the clause is omitted, which on most platforms indicates a
  3057. minvalue of 1 and -2^63-1 for ascending and descending sequences,
  3058. respectively.
  3059. :param maxvalue: the maximum value of the sequence. This
  3060. value is used when the CREATE SEQUENCE command is emitted to
  3061. the database as the value of the "MAXVALUE" clause. If ``None``,
  3062. the clause is omitted, which on most platforms indicates a
  3063. maxvalue of 2^63-1 and -1 for ascending and descending sequences,
  3064. respectively.
  3065. :param nominvalue: no minimum value of the sequence. This
  3066. value is used when the CREATE SEQUENCE command is emitted to
  3067. the database as the value of the "NO MINVALUE" clause. If ``None``,
  3068. the clause is omitted, which on most platforms indicates a
  3069. minvalue of 1 and -2^63-1 for ascending and descending sequences,
  3070. respectively.
  3071. :param nomaxvalue: no maximum value of the sequence. This
  3072. value is used when the CREATE SEQUENCE command is emitted to
  3073. the database as the value of the "NO MAXVALUE" clause. If ``None``,
  3074. the clause is omitted, which on most platforms indicates a
  3075. maxvalue of 2^63-1 and -1 for ascending and descending sequences,
  3076. respectively.
  3077. :param cycle: allows the sequence to wrap around when the maxvalue
  3078. or minvalue has been reached by an ascending or descending sequence
  3079. respectively. This value is used when the CREATE SEQUENCE command
  3080. is emitted to the database as the "CYCLE" clause. If the limit is
  3081. reached, the next number generated will be the minvalue or maxvalue,
  3082. respectively. If cycle=False (the default) any calls to nextval
  3083. after the sequence has reached its maximum value will return an
  3084. error.
  3085. :param schema: optional schema name for the sequence, if located
  3086. in a schema other than the default. The rules for selecting the
  3087. schema name when a :class:`_schema.MetaData`
  3088. is also present are the same
  3089. as that of :paramref:`_schema.Table.schema`.
  3090. :param cache: optional integer value; number of future values in the
  3091. sequence which are calculated in advance. Renders the CACHE keyword
  3092. understood by Oracle Database and PostgreSQL.
  3093. :param order: optional boolean value; if ``True``, renders the
  3094. ORDER keyword, understood by Oracle Database, indicating the sequence
  3095. is definitively ordered. May be necessary to provide deterministic
  3096. ordering using Oracle RAC.
  3097. :param data_type: The type to be returned by the sequence, for
  3098. dialects that allow us to choose between INTEGER, BIGINT, etc.
  3099. (e.g., mssql).
  3100. .. versionadded:: 1.4.0
  3101. :param optional: boolean value, when ``True``, indicates that this
  3102. :class:`.Sequence` object only needs to be explicitly generated
  3103. on backends that don't provide another way to generate primary
  3104. key identifiers. Currently, it essentially means, "don't create
  3105. this sequence on the PostgreSQL backend, where the SERIAL keyword
  3106. creates a sequence for us automatically".
  3107. :param quote: boolean value, when ``True`` or ``False``, explicitly
  3108. forces quoting of the :paramref:`_schema.Sequence.name` on or off.
  3109. When left at its default of ``None``, normal quoting rules based
  3110. on casing and reserved words take place.
  3111. :param quote_schema: Set the quoting preferences for the ``schema``
  3112. name.
  3113. :param metadata: optional :class:`_schema.MetaData` object which this
  3114. :class:`.Sequence` will be associated with. A :class:`.Sequence`
  3115. that is associated with a :class:`_schema.MetaData`
  3116. gains the following
  3117. capabilities:
  3118. * The :class:`.Sequence` will inherit the
  3119. :paramref:`_schema.MetaData.schema`
  3120. parameter specified to the target :class:`_schema.MetaData`, which
  3121. affects the production of CREATE / DROP DDL, if any.
  3122. * The :meth:`.Sequence.create` and :meth:`.Sequence.drop` methods
  3123. automatically use the engine bound to the :class:`_schema.MetaData`
  3124. object, if any.
  3125. * The :meth:`_schema.MetaData.create_all` and
  3126. :meth:`_schema.MetaData.drop_all`
  3127. methods will emit CREATE / DROP for this :class:`.Sequence`,
  3128. even if the :class:`.Sequence` is not associated with any
  3129. :class:`_schema.Table` / :class:`_schema.Column`
  3130. that's a member of this
  3131. :class:`_schema.MetaData`.
  3132. The above behaviors can only occur if the :class:`.Sequence` is
  3133. explicitly associated with the :class:`_schema.MetaData`
  3134. via this parameter.
  3135. .. seealso::
  3136. :ref:`sequence_metadata` - full discussion of the
  3137. :paramref:`.Sequence.metadata` parameter.
  3138. :param for_update: Indicates this :class:`.Sequence`, when associated
  3139. with a :class:`_schema.Column`,
  3140. should be invoked for UPDATE statements
  3141. on that column's table, rather than for INSERT statements, when
  3142. no value is otherwise present for that column in the statement.
  3143. """
  3144. DefaultGenerator.__init__(self, for_update=for_update)
  3145. IdentityOptions.__init__(
  3146. self,
  3147. start=start,
  3148. increment=increment,
  3149. minvalue=minvalue,
  3150. maxvalue=maxvalue,
  3151. nominvalue=nominvalue,
  3152. nomaxvalue=nomaxvalue,
  3153. cycle=cycle,
  3154. cache=cache,
  3155. order=order,
  3156. )
  3157. self.column = None
  3158. self.name = quoted_name(name, quote)
  3159. self.optional = optional
  3160. if schema is BLANK_SCHEMA:
  3161. self.schema = schema = None
  3162. elif metadata is not None and schema is None and metadata.schema:
  3163. self.schema = schema = metadata.schema
  3164. else:
  3165. self.schema = quoted_name.construct(schema, quote_schema)
  3166. self.metadata = metadata
  3167. self._key = _get_table_key(name, schema)
  3168. if metadata:
  3169. self._set_metadata(metadata)
  3170. if data_type is not None:
  3171. self.data_type = to_instance(data_type)
  3172. else:
  3173. self.data_type = None
  3174. @util.preload_module("sqlalchemy.sql.functions")
  3175. def next_value(self) -> Function[int]:
  3176. """Return a :class:`.next_value` function element
  3177. which will render the appropriate increment function
  3178. for this :class:`.Sequence` within any SQL expression.
  3179. """
  3180. return util.preloaded.sql_functions.func.next_value(self)
  3181. def _set_parent(self, parent: SchemaEventTarget, **kw: Any) -> None:
  3182. column = parent
  3183. assert isinstance(column, Column)
  3184. super()._set_parent(column)
  3185. column._on_table_attach(self._set_table)
  3186. def _copy(self) -> Sequence:
  3187. return Sequence(
  3188. name=self.name,
  3189. start=self.start,
  3190. increment=self.increment,
  3191. minvalue=self.minvalue,
  3192. maxvalue=self.maxvalue,
  3193. nominvalue=self.nominvalue,
  3194. nomaxvalue=self.nomaxvalue,
  3195. cycle=self.cycle,
  3196. schema=self.schema,
  3197. cache=self.cache,
  3198. order=self.order,
  3199. data_type=self.data_type,
  3200. optional=self.optional,
  3201. metadata=self.metadata,
  3202. for_update=self.for_update,
  3203. )
  3204. def _set_table(self, column: Column[Any], table: Table) -> None:
  3205. self._set_metadata(table.metadata)
  3206. def _set_metadata(self, metadata: MetaData) -> None:
  3207. self.metadata = metadata
  3208. self.metadata._sequences[self._key] = self
  3209. def create(self, bind: _CreateDropBind, checkfirst: bool = True) -> None:
  3210. """Creates this sequence in the database."""
  3211. bind._run_ddl_visitor(ddl.SchemaGenerator, self, checkfirst=checkfirst)
  3212. def drop(self, bind: _CreateDropBind, checkfirst: bool = True) -> None:
  3213. """Drops this sequence from the database."""
  3214. bind._run_ddl_visitor(ddl.SchemaDropper, self, checkfirst=checkfirst)
  3215. def _not_a_column_expr(self) -> NoReturn:
  3216. raise exc.InvalidRequestError(
  3217. f"This {self.__class__.__name__} cannot be used directly "
  3218. "as a column expression. Use func.next_value(sequence) "
  3219. "to produce a 'next value' function that's usable "
  3220. "as a column element."
  3221. )
  3222. @inspection._self_inspects
  3223. class FetchedValue(SchemaEventTarget):
  3224. """A marker for a transparent database-side default.
  3225. Use :class:`.FetchedValue` when the database is configured
  3226. to provide some automatic default for a column.
  3227. E.g.::
  3228. Column("foo", Integer, FetchedValue())
  3229. Would indicate that some trigger or default generator
  3230. will create a new value for the ``foo`` column during an
  3231. INSERT.
  3232. .. seealso::
  3233. :ref:`triggered_columns`
  3234. """
  3235. is_server_default = True
  3236. reflected = False
  3237. has_argument = False
  3238. is_clause_element = False
  3239. is_identity = False
  3240. column: Optional[Column[Any]]
  3241. def __init__(self, for_update: bool = False) -> None:
  3242. self.for_update = for_update
  3243. def _as_for_update(self, for_update: bool) -> FetchedValue:
  3244. if for_update == self.for_update:
  3245. return self
  3246. else:
  3247. return self._clone(for_update)
  3248. def _copy(self) -> FetchedValue:
  3249. return FetchedValue(self.for_update)
  3250. def _clone(self, for_update: bool) -> Self:
  3251. n = self.__class__.__new__(self.__class__)
  3252. n.__dict__.update(self.__dict__)
  3253. n.__dict__.pop("column", None)
  3254. n.for_update = for_update
  3255. return n
  3256. def _set_parent(self, parent: SchemaEventTarget, **kw: Any) -> None:
  3257. column = parent
  3258. assert isinstance(column, Column)
  3259. self.column = column
  3260. if self.for_update:
  3261. self.column.server_onupdate = self
  3262. else:
  3263. self.column.server_default = self
  3264. def __repr__(self) -> str:
  3265. return util.generic_repr(self)
  3266. class DefaultClause(FetchedValue):
  3267. """A DDL-specified DEFAULT column value.
  3268. :class:`.DefaultClause` is a :class:`.FetchedValue`
  3269. that also generates a "DEFAULT" clause when
  3270. "CREATE TABLE" is emitted.
  3271. :class:`.DefaultClause` is generated automatically
  3272. whenever the ``server_default``, ``server_onupdate`` arguments of
  3273. :class:`_schema.Column` are used. A :class:`.DefaultClause`
  3274. can be passed positionally as well.
  3275. For example, the following::
  3276. Column("foo", Integer, server_default="50")
  3277. Is equivalent to::
  3278. Column("foo", Integer, DefaultClause("50"))
  3279. """
  3280. has_argument = True
  3281. def __init__(
  3282. self,
  3283. arg: Union[str, ClauseElement, TextClause],
  3284. for_update: bool = False,
  3285. _reflected: bool = False,
  3286. ) -> None:
  3287. util.assert_arg_type(arg, (str, ClauseElement, TextClause), "arg")
  3288. super().__init__(for_update)
  3289. self.arg = arg
  3290. self.reflected = _reflected
  3291. def _copy(self) -> DefaultClause:
  3292. return DefaultClause(
  3293. arg=self.arg, for_update=self.for_update, _reflected=self.reflected
  3294. )
  3295. def __repr__(self) -> str:
  3296. return "DefaultClause(%r, for_update=%r)" % (self.arg, self.for_update)
  3297. class Constraint(DialectKWArgs, HasConditionalDDL, SchemaItem):
  3298. """A table-level SQL constraint.
  3299. :class:`_schema.Constraint` serves as the base class for the series of
  3300. constraint objects that can be associated with :class:`_schema.Table`
  3301. objects, including :class:`_schema.PrimaryKeyConstraint`,
  3302. :class:`_schema.ForeignKeyConstraint`
  3303. :class:`_schema.UniqueConstraint`, and
  3304. :class:`_schema.CheckConstraint`.
  3305. """
  3306. __visit_name__ = "constraint"
  3307. _creation_order: int
  3308. _column_flag: bool
  3309. def __init__(
  3310. self,
  3311. name: _ConstraintNameArgument = None,
  3312. deferrable: Optional[bool] = None,
  3313. initially: Optional[str] = None,
  3314. info: Optional[_InfoType] = None,
  3315. comment: Optional[str] = None,
  3316. _create_rule: Optional[Any] = None,
  3317. _type_bound: bool = False,
  3318. **dialect_kw: Any,
  3319. ) -> None:
  3320. r"""Create a SQL constraint.
  3321. :param name:
  3322. Optional, the in-database name of this ``Constraint``.
  3323. :param deferrable:
  3324. Optional bool. If set, emit DEFERRABLE or NOT DEFERRABLE when
  3325. issuing DDL for this constraint.
  3326. :param initially:
  3327. Optional string. If set, emit INITIALLY <value> when issuing DDL
  3328. for this constraint.
  3329. :param info: Optional data dictionary which will be populated into the
  3330. :attr:`.SchemaItem.info` attribute of this object.
  3331. :param comment: Optional string that will render an SQL comment on
  3332. foreign key constraint creation.
  3333. .. versionadded:: 2.0
  3334. :param \**dialect_kw: Additional keyword arguments are dialect
  3335. specific, and passed in the form ``<dialectname>_<argname>``. See
  3336. the documentation regarding an individual dialect at
  3337. :ref:`dialect_toplevel` for detail on documented arguments.
  3338. :param _create_rule:
  3339. used internally by some datatypes that also create constraints.
  3340. :param _type_bound:
  3341. used internally to indicate that this constraint is associated with
  3342. a specific datatype.
  3343. """
  3344. self.name = name
  3345. self.deferrable = deferrable
  3346. self.initially = initially
  3347. if info:
  3348. self.info = info
  3349. self._create_rule = _create_rule
  3350. self._type_bound = _type_bound
  3351. util.set_creation_order(self)
  3352. self._validate_dialect_kwargs(dialect_kw)
  3353. self.comment = comment
  3354. def _should_create_for_compiler(
  3355. self, compiler: DDLCompiler, **kw: Any
  3356. ) -> bool:
  3357. if self._create_rule is not None and not self._create_rule(compiler):
  3358. return False
  3359. elif self._ddl_if is not None:
  3360. return self._ddl_if._should_execute(
  3361. ddl.CreateConstraint(self), self, None, compiler=compiler, **kw
  3362. )
  3363. else:
  3364. return True
  3365. @property
  3366. def table(self) -> Table:
  3367. try:
  3368. if isinstance(self.parent, Table):
  3369. return self.parent
  3370. except AttributeError:
  3371. pass
  3372. raise exc.InvalidRequestError(
  3373. "This constraint is not bound to a table. Did you "
  3374. "mean to call table.append_constraint(constraint) ?"
  3375. )
  3376. def _set_parent(self, parent: SchemaEventTarget, **kw: Any) -> None:
  3377. assert isinstance(parent, (Table, Column))
  3378. self.parent = parent
  3379. parent.constraints.add(self)
  3380. @util.deprecated(
  3381. "1.4",
  3382. "The :meth:`_schema.Constraint.copy` method is deprecated "
  3383. "and will be removed in a future release.",
  3384. )
  3385. def copy(self, **kw: Any) -> Self:
  3386. return self._copy(**kw)
  3387. def _copy(self, **kw: Any) -> Self:
  3388. raise NotImplementedError()
  3389. class ColumnCollectionMixin:
  3390. """A :class:`_expression.ColumnCollection` of :class:`_schema.Column`
  3391. objects.
  3392. This collection represents the columns which are referred to by
  3393. this object.
  3394. """
  3395. _columns: DedupeColumnCollection[Column[Any]]
  3396. _allow_multiple_tables = False
  3397. _pending_colargs: List[Optional[Union[str, Column[Any]]]]
  3398. if TYPE_CHECKING:
  3399. def _set_parent_with_dispatch(
  3400. self, parent: SchemaEventTarget, **kw: Any
  3401. ) -> None: ...
  3402. def __init__(
  3403. self,
  3404. *columns: _DDLColumnArgument,
  3405. _autoattach: bool = True,
  3406. _column_flag: bool = False,
  3407. _gather_expressions: Optional[
  3408. List[Union[str, ColumnElement[Any]]]
  3409. ] = None,
  3410. ) -> None:
  3411. self._column_flag = _column_flag
  3412. self._columns = DedupeColumnCollection()
  3413. processed_expressions: Optional[
  3414. List[Union[ColumnElement[Any], str]]
  3415. ] = _gather_expressions
  3416. if processed_expressions is not None:
  3417. # this is expected to be an empty list
  3418. assert not processed_expressions
  3419. self._pending_colargs = []
  3420. for (
  3421. expr,
  3422. _,
  3423. _,
  3424. add_element,
  3425. ) in coercions.expect_col_expression_collection(
  3426. roles.DDLConstraintColumnRole, columns
  3427. ):
  3428. self._pending_colargs.append(add_element)
  3429. processed_expressions.append(expr)
  3430. else:
  3431. self._pending_colargs = [
  3432. coercions.expect(roles.DDLConstraintColumnRole, column)
  3433. for column in columns
  3434. ]
  3435. if _autoattach and self._pending_colargs:
  3436. self._check_attach()
  3437. def _check_attach(self, evt: bool = False) -> None:
  3438. col_objs = [c for c in self._pending_colargs if isinstance(c, Column)]
  3439. cols_w_table = [c for c in col_objs if isinstance(c.table, Table)]
  3440. cols_wo_table = set(col_objs).difference(cols_w_table)
  3441. if cols_wo_table:
  3442. # feature #3341 - place event listeners for Column objects
  3443. # such that when all those cols are attached, we autoattach.
  3444. assert not evt, "Should not reach here on event call"
  3445. # issue #3411 - don't do the per-column auto-attach if some of the
  3446. # columns are specified as strings.
  3447. has_string_cols = {
  3448. c for c in self._pending_colargs if c is not None
  3449. }.difference(col_objs)
  3450. if not has_string_cols:
  3451. def _col_attached(column: Column[Any], table: Table) -> None:
  3452. # this isinstance() corresponds with the
  3453. # isinstance() above; only want to count Table-bound
  3454. # columns
  3455. if isinstance(table, Table):
  3456. cols_wo_table.discard(column)
  3457. if not cols_wo_table:
  3458. self._check_attach(evt=True)
  3459. self._cols_wo_table = cols_wo_table
  3460. for col in cols_wo_table:
  3461. col._on_table_attach(_col_attached)
  3462. return
  3463. columns = cols_w_table
  3464. tables = {c.table for c in columns}
  3465. if len(tables) == 1:
  3466. self._set_parent_with_dispatch(tables.pop())
  3467. elif len(tables) > 1 and not self._allow_multiple_tables:
  3468. table = columns[0].table
  3469. others = [c for c in columns[1:] if c.table is not table]
  3470. if others:
  3471. # black could not format this inline
  3472. other_str = ", ".join("'%s'" % c for c in others)
  3473. raise exc.ArgumentError(
  3474. f"Column(s) {other_str} "
  3475. f"are not part of table '{table.description}'."
  3476. )
  3477. @util.ro_memoized_property
  3478. def columns(self) -> ReadOnlyColumnCollection[str, Column[Any]]:
  3479. return self._columns.as_readonly()
  3480. @util.ro_memoized_property
  3481. def c(self) -> ReadOnlyColumnCollection[str, Column[Any]]:
  3482. return self._columns.as_readonly()
  3483. def _col_expressions(
  3484. self, parent: Union[Table, Column[Any]]
  3485. ) -> List[Optional[Column[Any]]]:
  3486. if isinstance(parent, Column):
  3487. result: List[Optional[Column[Any]]] = [
  3488. c for c in self._pending_colargs if isinstance(c, Column)
  3489. ]
  3490. assert len(result) == len(self._pending_colargs)
  3491. return result
  3492. else:
  3493. try:
  3494. return [
  3495. parent.c[col] if isinstance(col, str) else col
  3496. for col in self._pending_colargs
  3497. ]
  3498. except KeyError as ke:
  3499. raise exc.ConstraintColumnNotFoundError(
  3500. f"Can't create {self.__class__.__name__} "
  3501. f"on table '{parent.description}': no column "
  3502. f"named '{ke.args[0]}' is present."
  3503. ) from ke
  3504. def _set_parent(self, parent: SchemaEventTarget, **kw: Any) -> None:
  3505. assert isinstance(parent, (Table, Column))
  3506. for col in self._col_expressions(parent):
  3507. if col is not None:
  3508. self._columns.add(col)
  3509. class ColumnCollectionConstraint(ColumnCollectionMixin, Constraint):
  3510. """A constraint that proxies a ColumnCollection."""
  3511. def __init__(
  3512. self,
  3513. *columns: _DDLColumnArgument,
  3514. name: _ConstraintNameArgument = None,
  3515. deferrable: Optional[bool] = None,
  3516. initially: Optional[str] = None,
  3517. info: Optional[_InfoType] = None,
  3518. _autoattach: bool = True,
  3519. _column_flag: bool = False,
  3520. _gather_expressions: Optional[List[_DDLColumnArgument]] = None,
  3521. **dialect_kw: Any,
  3522. ) -> None:
  3523. r"""
  3524. :param \*columns:
  3525. A sequence of column names or Column objects.
  3526. :param name:
  3527. Optional, the in-database name of this constraint.
  3528. :param deferrable:
  3529. Optional bool. If set, emit DEFERRABLE or NOT DEFERRABLE when
  3530. issuing DDL for this constraint.
  3531. :param initially:
  3532. Optional string. If set, emit INITIALLY <value> when issuing DDL
  3533. for this constraint.
  3534. :param \**dialect_kw: other keyword arguments including
  3535. dialect-specific arguments are propagated to the :class:`.Constraint`
  3536. superclass.
  3537. """
  3538. Constraint.__init__(
  3539. self,
  3540. name=name,
  3541. deferrable=deferrable,
  3542. initially=initially,
  3543. info=info,
  3544. **dialect_kw,
  3545. )
  3546. ColumnCollectionMixin.__init__(
  3547. self, *columns, _autoattach=_autoattach, _column_flag=_column_flag
  3548. )
  3549. columns: ReadOnlyColumnCollection[str, Column[Any]]
  3550. """A :class:`_expression.ColumnCollection` representing the set of columns
  3551. for this constraint.
  3552. """
  3553. def _set_parent(self, parent: SchemaEventTarget, **kw: Any) -> None:
  3554. assert isinstance(parent, (Column, Table))
  3555. Constraint._set_parent(self, parent)
  3556. ColumnCollectionMixin._set_parent(self, parent)
  3557. def __contains__(self, x: Any) -> bool:
  3558. return x in self._columns
  3559. @util.deprecated(
  3560. "1.4",
  3561. "The :meth:`_schema.ColumnCollectionConstraint.copy` method "
  3562. "is deprecated and will be removed in a future release.",
  3563. )
  3564. def copy(
  3565. self,
  3566. *,
  3567. target_table: Optional[Table] = None,
  3568. **kw: Any,
  3569. ) -> ColumnCollectionConstraint:
  3570. return self._copy(target_table=target_table, **kw)
  3571. def _copy(
  3572. self,
  3573. *,
  3574. target_table: Optional[Table] = None,
  3575. **kw: Any,
  3576. ) -> ColumnCollectionConstraint:
  3577. # ticket #5276
  3578. constraint_kwargs = {}
  3579. for dialect_name in self.dialect_options:
  3580. dialect_options = self.dialect_options[dialect_name]._non_defaults
  3581. for (
  3582. dialect_option_key,
  3583. dialect_option_value,
  3584. ) in dialect_options.items():
  3585. constraint_kwargs[dialect_name + "_" + dialect_option_key] = (
  3586. dialect_option_value
  3587. )
  3588. assert isinstance(self.parent, Table)
  3589. c = self.__class__(
  3590. name=self.name,
  3591. deferrable=self.deferrable,
  3592. initially=self.initially,
  3593. *[
  3594. _copy_expression(expr, self.parent, target_table)
  3595. for expr in self._columns
  3596. ],
  3597. comment=self.comment,
  3598. **constraint_kwargs,
  3599. )
  3600. return self._schema_item_copy(c)
  3601. def contains_column(self, col: Column[Any]) -> bool:
  3602. """Return True if this constraint contains the given column.
  3603. Note that this object also contains an attribute ``.columns``
  3604. which is a :class:`_expression.ColumnCollection` of
  3605. :class:`_schema.Column` objects.
  3606. """
  3607. return self._columns.contains_column(col)
  3608. def __iter__(self) -> Iterator[Column[Any]]:
  3609. return iter(self._columns)
  3610. def __len__(self) -> int:
  3611. return len(self._columns)
  3612. class CheckConstraint(ColumnCollectionConstraint):
  3613. """A table- or column-level CHECK constraint.
  3614. Can be included in the definition of a Table or Column.
  3615. """
  3616. _allow_multiple_tables = True
  3617. __visit_name__ = "table_or_column_check_constraint"
  3618. @_document_text_coercion(
  3619. "sqltext",
  3620. ":class:`.CheckConstraint`",
  3621. ":paramref:`.CheckConstraint.sqltext`",
  3622. )
  3623. def __init__(
  3624. self,
  3625. sqltext: _TextCoercedExpressionArgument[Any],
  3626. name: _ConstraintNameArgument = None,
  3627. deferrable: Optional[bool] = None,
  3628. initially: Optional[str] = None,
  3629. table: Optional[Table] = None,
  3630. info: Optional[_InfoType] = None,
  3631. _create_rule: Optional[Any] = None,
  3632. _autoattach: bool = True,
  3633. _type_bound: bool = False,
  3634. **dialect_kw: Any,
  3635. ) -> None:
  3636. r"""Construct a CHECK constraint.
  3637. :param sqltext:
  3638. A string containing the constraint definition, which will be used
  3639. verbatim, or a SQL expression construct. If given as a string,
  3640. the object is converted to a :func:`_expression.text` object.
  3641. If the textual
  3642. string includes a colon character, escape this using a backslash::
  3643. CheckConstraint(r"foo ~ E'a(?\:b|c)d")
  3644. :param name:
  3645. Optional, the in-database name of the constraint.
  3646. :param deferrable:
  3647. Optional bool. If set, emit DEFERRABLE or NOT DEFERRABLE when
  3648. issuing DDL for this constraint.
  3649. :param initially:
  3650. Optional string. If set, emit INITIALLY <value> when issuing DDL
  3651. for this constraint.
  3652. :param info: Optional data dictionary which will be populated into the
  3653. :attr:`.SchemaItem.info` attribute of this object.
  3654. """
  3655. self.sqltext = coercions.expect(roles.DDLExpressionRole, sqltext)
  3656. columns: List[Column[Any]] = []
  3657. visitors.traverse(self.sqltext, {}, {"column": columns.append})
  3658. super().__init__(
  3659. name=name,
  3660. deferrable=deferrable,
  3661. initially=initially,
  3662. _create_rule=_create_rule,
  3663. info=info,
  3664. _type_bound=_type_bound,
  3665. _autoattach=_autoattach,
  3666. *columns,
  3667. **dialect_kw,
  3668. )
  3669. if table is not None:
  3670. self._set_parent_with_dispatch(table)
  3671. @property
  3672. def is_column_level(self) -> bool:
  3673. return not isinstance(self.parent, Table)
  3674. @util.deprecated(
  3675. "1.4",
  3676. "The :meth:`_schema.CheckConstraint.copy` method is deprecated "
  3677. "and will be removed in a future release.",
  3678. )
  3679. def copy(
  3680. self, *, target_table: Optional[Table] = None, **kw: Any
  3681. ) -> CheckConstraint:
  3682. return self._copy(target_table=target_table, **kw)
  3683. def _copy(
  3684. self, *, target_table: Optional[Table] = None, **kw: Any
  3685. ) -> CheckConstraint:
  3686. if target_table is not None:
  3687. # note that target_table is None for the copy process of
  3688. # a column-bound CheckConstraint, so this path is not reached
  3689. # in that case.
  3690. sqltext = _copy_expression(self.sqltext, self.table, target_table)
  3691. else:
  3692. sqltext = self.sqltext
  3693. c = CheckConstraint(
  3694. sqltext,
  3695. name=self.name,
  3696. initially=self.initially,
  3697. deferrable=self.deferrable,
  3698. _create_rule=self._create_rule,
  3699. table=target_table,
  3700. comment=self.comment,
  3701. _autoattach=False,
  3702. _type_bound=self._type_bound,
  3703. )
  3704. return self._schema_item_copy(c)
  3705. class ForeignKeyConstraint(ColumnCollectionConstraint):
  3706. """A table-level FOREIGN KEY constraint.
  3707. Defines a single column or composite FOREIGN KEY ... REFERENCES
  3708. constraint. For a no-frills, single column foreign key, adding a
  3709. :class:`_schema.ForeignKey` to the definition of a :class:`_schema.Column`
  3710. is a
  3711. shorthand equivalent for an unnamed, single column
  3712. :class:`_schema.ForeignKeyConstraint`.
  3713. Examples of foreign key configuration are in :ref:`metadata_foreignkeys`.
  3714. """
  3715. __visit_name__ = "foreign_key_constraint"
  3716. def __init__(
  3717. self,
  3718. columns: _typing_Sequence[_DDLColumnArgument],
  3719. refcolumns: _typing_Sequence[_DDLColumnArgument],
  3720. name: _ConstraintNameArgument = None,
  3721. onupdate: Optional[str] = None,
  3722. ondelete: Optional[str] = None,
  3723. deferrable: Optional[bool] = None,
  3724. initially: Optional[str] = None,
  3725. use_alter: bool = False,
  3726. link_to_name: bool = False,
  3727. match: Optional[str] = None,
  3728. table: Optional[Table] = None,
  3729. info: Optional[_InfoType] = None,
  3730. comment: Optional[str] = None,
  3731. **dialect_kw: Any,
  3732. ) -> None:
  3733. r"""Construct a composite-capable FOREIGN KEY.
  3734. :param columns: A sequence of local column names. The named columns
  3735. must be defined and present in the parent Table. The names should
  3736. match the ``key`` given to each column (defaults to the name) unless
  3737. ``link_to_name`` is True.
  3738. :param refcolumns: A sequence of foreign column names or Column
  3739. objects. The columns must all be located within the same Table.
  3740. :param name: Optional, the in-database name of the key.
  3741. :param onupdate: Optional string. If set, emit ON UPDATE <value> when
  3742. issuing DDL for this constraint. Typical values include CASCADE,
  3743. DELETE and RESTRICT.
  3744. .. seealso::
  3745. :ref:`on_update_on_delete`
  3746. :param ondelete: Optional string. If set, emit ON DELETE <value> when
  3747. issuing DDL for this constraint. Typical values include CASCADE,
  3748. SET NULL and RESTRICT. Some dialects may allow for additional
  3749. syntaxes.
  3750. .. seealso::
  3751. :ref:`on_update_on_delete`
  3752. :param deferrable: Optional bool. If set, emit DEFERRABLE or NOT
  3753. DEFERRABLE when issuing DDL for this constraint.
  3754. :param initially: Optional string. If set, emit INITIALLY <value> when
  3755. issuing DDL for this constraint.
  3756. :param link_to_name: if True, the string name given in ``column`` is
  3757. the rendered name of the referenced column, not its locally assigned
  3758. ``key``.
  3759. :param use_alter: If True, do not emit the DDL for this constraint as
  3760. part of the CREATE TABLE definition. Instead, generate it via an
  3761. ALTER TABLE statement issued after the full collection of tables
  3762. have been created, and drop it via an ALTER TABLE statement before
  3763. the full collection of tables are dropped.
  3764. The use of :paramref:`_schema.ForeignKeyConstraint.use_alter` is
  3765. particularly geared towards the case where two or more tables
  3766. are established within a mutually-dependent foreign key constraint
  3767. relationship; however, the :meth:`_schema.MetaData.create_all` and
  3768. :meth:`_schema.MetaData.drop_all`
  3769. methods will perform this resolution
  3770. automatically, so the flag is normally not needed.
  3771. .. seealso::
  3772. :ref:`use_alter`
  3773. :param match: Optional string. If set, emit MATCH <value> when issuing
  3774. DDL for this constraint. Typical values include SIMPLE, PARTIAL
  3775. and FULL.
  3776. :param info: Optional data dictionary which will be populated into the
  3777. :attr:`.SchemaItem.info` attribute of this object.
  3778. :param comment: Optional string that will render an SQL comment on
  3779. foreign key constraint creation.
  3780. .. versionadded:: 2.0
  3781. :param \**dialect_kw: Additional keyword arguments are dialect
  3782. specific, and passed in the form ``<dialectname>_<argname>``. See
  3783. the documentation regarding an individual dialect at
  3784. :ref:`dialect_toplevel` for detail on documented arguments.
  3785. """
  3786. Constraint.__init__(
  3787. self,
  3788. name=name,
  3789. deferrable=deferrable,
  3790. initially=initially,
  3791. info=info,
  3792. comment=comment,
  3793. **dialect_kw,
  3794. )
  3795. self.onupdate = onupdate
  3796. self.ondelete = ondelete
  3797. self.link_to_name = link_to_name
  3798. self.use_alter = use_alter
  3799. self.match = match
  3800. if len(set(columns)) != len(refcolumns):
  3801. if len(set(columns)) != len(columns):
  3802. # e.g. FOREIGN KEY (a, a) REFERENCES r (b, c)
  3803. raise exc.ArgumentError(
  3804. "ForeignKeyConstraint with duplicate source column "
  3805. "references are not supported."
  3806. )
  3807. else:
  3808. # e.g. FOREIGN KEY (a) REFERENCES r (b, c)
  3809. # paraphrasing
  3810. # https://www.postgresql.org/docs/current/static/ddl-constraints.html
  3811. raise exc.ArgumentError(
  3812. "ForeignKeyConstraint number "
  3813. "of constrained columns must match the number of "
  3814. "referenced columns."
  3815. )
  3816. # standalone ForeignKeyConstraint - create
  3817. # associated ForeignKey objects which will be applied to hosted
  3818. # Column objects (in col.foreign_keys), either now or when attached
  3819. # to the Table for string-specified names
  3820. self.elements = [
  3821. ForeignKey(
  3822. refcol,
  3823. _constraint=self,
  3824. name=self.name,
  3825. onupdate=self.onupdate,
  3826. ondelete=self.ondelete,
  3827. use_alter=self.use_alter,
  3828. link_to_name=self.link_to_name,
  3829. match=self.match,
  3830. deferrable=self.deferrable,
  3831. initially=self.initially,
  3832. **self.dialect_kwargs,
  3833. )
  3834. for refcol in refcolumns
  3835. ]
  3836. ColumnCollectionMixin.__init__(self, *columns)
  3837. if table is not None:
  3838. if hasattr(self, "parent"):
  3839. assert table is self.parent
  3840. self._set_parent_with_dispatch(table)
  3841. def _append_element(self, column: Column[Any], fk: ForeignKey) -> None:
  3842. self._columns.add(column)
  3843. self.elements.append(fk)
  3844. columns: ReadOnlyColumnCollection[str, Column[Any]]
  3845. """A :class:`_expression.ColumnCollection` representing the set of columns
  3846. for this constraint.
  3847. """
  3848. elements: List[ForeignKey]
  3849. """A sequence of :class:`_schema.ForeignKey` objects.
  3850. Each :class:`_schema.ForeignKey`
  3851. represents a single referring column/referred
  3852. column pair.
  3853. This collection is intended to be read-only.
  3854. """
  3855. @property
  3856. def _elements(self) -> util.OrderedDict[str, ForeignKey]:
  3857. # legacy - provide a dictionary view of (column_key, fk)
  3858. return util.OrderedDict(zip(self.column_keys, self.elements))
  3859. @property
  3860. def _referred_schema(self) -> Optional[str]:
  3861. for elem in self.elements:
  3862. return elem._referred_schema
  3863. else:
  3864. return None
  3865. @property
  3866. def referred_table(self) -> Table:
  3867. """The :class:`_schema.Table` object to which this
  3868. :class:`_schema.ForeignKeyConstraint` references.
  3869. This is a dynamically calculated attribute which may not be available
  3870. if the constraint and/or parent table is not yet associated with
  3871. a metadata collection that contains the referred table.
  3872. """
  3873. return self.elements[0].column.table
  3874. def _validate_dest_table(self, table: Table) -> None:
  3875. table_keys = {elem._table_key() for elem in self.elements}
  3876. if None not in table_keys and len(table_keys) > 1:
  3877. elem0, elem1 = sorted(table_keys)[0:2]
  3878. raise exc.ArgumentError(
  3879. f"ForeignKeyConstraint on "
  3880. f"{table.fullname}({self._col_description}) refers to "
  3881. f"multiple remote tables: {elem0} and {elem1}"
  3882. )
  3883. @property
  3884. def column_keys(self) -> _typing_Sequence[str]:
  3885. """Return a list of string keys representing the local
  3886. columns in this :class:`_schema.ForeignKeyConstraint`.
  3887. This list is either the original string arguments sent
  3888. to the constructor of the :class:`_schema.ForeignKeyConstraint`,
  3889. or if the constraint has been initialized with :class:`_schema.Column`
  3890. objects, is the string ``.key`` of each element.
  3891. """
  3892. if hasattr(self, "parent"):
  3893. return self._columns.keys()
  3894. else:
  3895. return [
  3896. col.key if isinstance(col, ColumnElement) else str(col)
  3897. for col in self._pending_colargs
  3898. ]
  3899. @property
  3900. def _col_description(self) -> str:
  3901. return ", ".join(self.column_keys)
  3902. def _set_parent(self, parent: SchemaEventTarget, **kw: Any) -> None:
  3903. table = parent
  3904. assert isinstance(table, Table)
  3905. Constraint._set_parent(self, table)
  3906. ColumnCollectionConstraint._set_parent(self, table)
  3907. for col, fk in zip(self._columns, self.elements):
  3908. if not hasattr(fk, "parent") or fk.parent is not col:
  3909. fk._set_parent_with_dispatch(col)
  3910. self._validate_dest_table(table)
  3911. @util.deprecated(
  3912. "1.4",
  3913. "The :meth:`_schema.ForeignKeyConstraint.copy` method is deprecated "
  3914. "and will be removed in a future release.",
  3915. )
  3916. def copy(
  3917. self,
  3918. *,
  3919. schema: Optional[str] = None,
  3920. target_table: Optional[Table] = None,
  3921. **kw: Any,
  3922. ) -> ForeignKeyConstraint:
  3923. return self._copy(schema=schema, target_table=target_table, **kw)
  3924. def _copy(
  3925. self,
  3926. *,
  3927. schema: Optional[str] = None,
  3928. target_table: Optional[Table] = None,
  3929. **kw: Any,
  3930. ) -> ForeignKeyConstraint:
  3931. fkc = ForeignKeyConstraint(
  3932. [x.parent.key for x in self.elements],
  3933. [
  3934. x._get_colspec(
  3935. schema=schema,
  3936. table_name=(
  3937. target_table.name
  3938. if target_table is not None
  3939. and x._table_key() == x.parent.table.key
  3940. else None
  3941. ),
  3942. _is_copy=True,
  3943. )
  3944. for x in self.elements
  3945. ],
  3946. name=self.name,
  3947. onupdate=self.onupdate,
  3948. ondelete=self.ondelete,
  3949. use_alter=self.use_alter,
  3950. deferrable=self.deferrable,
  3951. initially=self.initially,
  3952. link_to_name=self.link_to_name,
  3953. match=self.match,
  3954. comment=self.comment,
  3955. )
  3956. for self_fk, other_fk in zip(self.elements, fkc.elements):
  3957. self_fk._schema_item_copy(other_fk)
  3958. return self._schema_item_copy(fkc)
  3959. class PrimaryKeyConstraint(ColumnCollectionConstraint):
  3960. """A table-level PRIMARY KEY constraint.
  3961. The :class:`.PrimaryKeyConstraint` object is present automatically
  3962. on any :class:`_schema.Table` object; it is assigned a set of
  3963. :class:`_schema.Column` objects corresponding to those marked with
  3964. the :paramref:`_schema.Column.primary_key` flag::
  3965. >>> my_table = Table(
  3966. ... "mytable",
  3967. ... metadata,
  3968. ... Column("id", Integer, primary_key=True),
  3969. ... Column("version_id", Integer, primary_key=True),
  3970. ... Column("data", String(50)),
  3971. ... )
  3972. >>> my_table.primary_key
  3973. PrimaryKeyConstraint(
  3974. Column('id', Integer(), table=<mytable>,
  3975. primary_key=True, nullable=False),
  3976. Column('version_id', Integer(), table=<mytable>,
  3977. primary_key=True, nullable=False)
  3978. )
  3979. The primary key of a :class:`_schema.Table` can also be specified by using
  3980. a :class:`.PrimaryKeyConstraint` object explicitly; in this mode of usage,
  3981. the "name" of the constraint can also be specified, as well as other
  3982. options which may be recognized by dialects::
  3983. my_table = Table(
  3984. "mytable",
  3985. metadata,
  3986. Column("id", Integer),
  3987. Column("version_id", Integer),
  3988. Column("data", String(50)),
  3989. PrimaryKeyConstraint("id", "version_id", name="mytable_pk"),
  3990. )
  3991. The two styles of column-specification should generally not be mixed.
  3992. An warning is emitted if the columns present in the
  3993. :class:`.PrimaryKeyConstraint`
  3994. don't match the columns that were marked as ``primary_key=True``, if both
  3995. are present; in this case, the columns are taken strictly from the
  3996. :class:`.PrimaryKeyConstraint` declaration, and those columns otherwise
  3997. marked as ``primary_key=True`` are ignored. This behavior is intended to
  3998. be backwards compatible with previous behavior.
  3999. For the use case where specific options are to be specified on the
  4000. :class:`.PrimaryKeyConstraint`, but the usual style of using
  4001. ``primary_key=True`` flags is still desirable, an empty
  4002. :class:`.PrimaryKeyConstraint` may be specified, which will take on the
  4003. primary key column collection from the :class:`_schema.Table` based on the
  4004. flags::
  4005. my_table = Table(
  4006. "mytable",
  4007. metadata,
  4008. Column("id", Integer, primary_key=True),
  4009. Column("version_id", Integer, primary_key=True),
  4010. Column("data", String(50)),
  4011. PrimaryKeyConstraint(name="mytable_pk", mssql_clustered=True),
  4012. )
  4013. """
  4014. __visit_name__ = "primary_key_constraint"
  4015. def __init__(
  4016. self,
  4017. *columns: _DDLColumnArgument,
  4018. name: Optional[str] = None,
  4019. deferrable: Optional[bool] = None,
  4020. initially: Optional[str] = None,
  4021. info: Optional[_InfoType] = None,
  4022. _implicit_generated: bool = False,
  4023. **dialect_kw: Any,
  4024. ) -> None:
  4025. self._implicit_generated = _implicit_generated
  4026. super().__init__(
  4027. *columns,
  4028. name=name,
  4029. deferrable=deferrable,
  4030. initially=initially,
  4031. info=info,
  4032. **dialect_kw,
  4033. )
  4034. def _set_parent(self, parent: SchemaEventTarget, **kw: Any) -> None:
  4035. table = parent
  4036. assert isinstance(table, Table)
  4037. super()._set_parent(table)
  4038. if table.primary_key is not self:
  4039. table.constraints.discard(table.primary_key)
  4040. table.primary_key = self # type: ignore
  4041. table.constraints.add(self)
  4042. table_pks = [c for c in table.c if c.primary_key]
  4043. if (
  4044. self._columns
  4045. and table_pks
  4046. and set(table_pks) != set(self._columns)
  4047. ):
  4048. # black could not format these inline
  4049. table_pk_str = ", ".join("'%s'" % c.name for c in table_pks)
  4050. col_str = ", ".join("'%s'" % c.name for c in self._columns)
  4051. util.warn(
  4052. f"Table '{table.name}' specifies columns "
  4053. f"{table_pk_str} as "
  4054. f"primary_key=True, "
  4055. f"not matching locally specified columns {col_str}; "
  4056. f"setting the "
  4057. f"current primary key columns to "
  4058. f"{col_str}. "
  4059. f"This warning "
  4060. f"may become an exception in a future release"
  4061. )
  4062. table_pks[:] = []
  4063. for c in self._columns:
  4064. c.primary_key = True
  4065. if c._user_defined_nullable is NULL_UNSPECIFIED:
  4066. c.nullable = False
  4067. if table_pks:
  4068. self._columns.extend(table_pks)
  4069. def _reload(self, columns: Iterable[Column[Any]]) -> None:
  4070. """repopulate this :class:`.PrimaryKeyConstraint` given
  4071. a set of columns.
  4072. Existing columns in the table that are marked as primary_key=True
  4073. are maintained.
  4074. Also fires a new event.
  4075. This is basically like putting a whole new
  4076. :class:`.PrimaryKeyConstraint` object on the parent
  4077. :class:`_schema.Table` object without actually replacing the object.
  4078. The ordering of the given list of columns is also maintained; these
  4079. columns will be appended to the list of columns after any which
  4080. are already present.
  4081. """
  4082. # set the primary key flag on new columns.
  4083. # note any existing PK cols on the table also have their
  4084. # flag still set.
  4085. for col in columns:
  4086. col.primary_key = True
  4087. self._columns.extend(columns)
  4088. PrimaryKeyConstraint._autoincrement_column._reset(self) # type: ignore
  4089. self._set_parent_with_dispatch(self.table)
  4090. def _replace(self, col: Column[Any]) -> None:
  4091. PrimaryKeyConstraint._autoincrement_column._reset(self) # type: ignore
  4092. self._columns.replace(col)
  4093. self.dispatch._sa_event_column_added_to_pk_constraint(self, col)
  4094. @property
  4095. def columns_autoinc_first(self) -> List[Column[Any]]:
  4096. autoinc = self._autoincrement_column
  4097. if autoinc is not None:
  4098. return [autoinc] + [c for c in self._columns if c is not autoinc]
  4099. else:
  4100. return list(self._columns)
  4101. @util.ro_memoized_property
  4102. def _autoincrement_column(self) -> Optional[Column[int]]:
  4103. def _validate_autoinc(col: Column[Any], autoinc_true: bool) -> bool:
  4104. if col.type._type_affinity is None or not issubclass(
  4105. col.type._type_affinity,
  4106. (
  4107. type_api.INTEGERTYPE._type_affinity,
  4108. type_api.NUMERICTYPE._type_affinity,
  4109. ),
  4110. ):
  4111. if autoinc_true:
  4112. raise exc.ArgumentError(
  4113. f"Column type {col.type} on column '{col}' is not "
  4114. f"compatible with autoincrement=True"
  4115. )
  4116. else:
  4117. return False
  4118. elif (
  4119. not isinstance(col.default, (type(None), Sequence))
  4120. and not autoinc_true
  4121. ):
  4122. return False
  4123. elif (
  4124. col.server_default is not None
  4125. and not isinstance(col.server_default, Identity)
  4126. and not autoinc_true
  4127. ):
  4128. return False
  4129. elif col.foreign_keys and col.autoincrement not in (
  4130. True,
  4131. "ignore_fk",
  4132. ):
  4133. return False
  4134. return True
  4135. if len(self._columns) == 1:
  4136. col = list(self._columns)[0]
  4137. if col.autoincrement is True:
  4138. _validate_autoinc(col, True)
  4139. return col
  4140. elif col.autoincrement in (
  4141. "auto",
  4142. "ignore_fk",
  4143. ) and _validate_autoinc(col, False):
  4144. return col
  4145. else:
  4146. return None
  4147. else:
  4148. autoinc = None
  4149. for col in self._columns:
  4150. if col.autoincrement is True:
  4151. _validate_autoinc(col, True)
  4152. if autoinc is not None:
  4153. raise exc.ArgumentError(
  4154. f"Only one Column may be marked "
  4155. f"autoincrement=True, found both "
  4156. f"{col.name} and {autoinc.name}."
  4157. )
  4158. else:
  4159. autoinc = col
  4160. return autoinc
  4161. class UniqueConstraint(ColumnCollectionConstraint):
  4162. """A table-level UNIQUE constraint.
  4163. Defines a single column or composite UNIQUE constraint. For a no-frills,
  4164. single column constraint, adding ``unique=True`` to the ``Column``
  4165. definition is a shorthand equivalent for an unnamed, single column
  4166. UniqueConstraint.
  4167. """
  4168. __visit_name__ = "unique_constraint"
  4169. class Index(
  4170. DialectKWArgs, ColumnCollectionMixin, HasConditionalDDL, SchemaItem
  4171. ):
  4172. """A table-level INDEX.
  4173. Defines a composite (one or more column) INDEX.
  4174. E.g.::
  4175. sometable = Table(
  4176. "sometable",
  4177. metadata,
  4178. Column("name", String(50)),
  4179. Column("address", String(100)),
  4180. )
  4181. Index("some_index", sometable.c.name)
  4182. For a no-frills, single column index, adding
  4183. :class:`_schema.Column` also supports ``index=True``::
  4184. sometable = Table(
  4185. "sometable", metadata, Column("name", String(50), index=True)
  4186. )
  4187. For a composite index, multiple columns can be specified::
  4188. Index("some_index", sometable.c.name, sometable.c.address)
  4189. Functional indexes are supported as well, typically by using the
  4190. :data:`.func` construct in conjunction with table-bound
  4191. :class:`_schema.Column` objects::
  4192. Index("some_index", func.lower(sometable.c.name))
  4193. An :class:`.Index` can also be manually associated with a
  4194. :class:`_schema.Table`,
  4195. either through inline declaration or using
  4196. :meth:`_schema.Table.append_constraint`. When this approach is used,
  4197. the names
  4198. of the indexed columns can be specified as strings::
  4199. Table(
  4200. "sometable",
  4201. metadata,
  4202. Column("name", String(50)),
  4203. Column("address", String(100)),
  4204. Index("some_index", "name", "address"),
  4205. )
  4206. To support functional or expression-based indexes in this form, the
  4207. :func:`_expression.text` construct may be used::
  4208. from sqlalchemy import text
  4209. Table(
  4210. "sometable",
  4211. metadata,
  4212. Column("name", String(50)),
  4213. Column("address", String(100)),
  4214. Index("some_index", text("lower(name)")),
  4215. )
  4216. .. seealso::
  4217. :ref:`schema_indexes` - General information on :class:`.Index`.
  4218. :ref:`postgresql_indexes` - PostgreSQL-specific options available for
  4219. the :class:`.Index` construct.
  4220. :ref:`mysql_indexes` - MySQL-specific options available for the
  4221. :class:`.Index` construct.
  4222. :ref:`mssql_indexes` - MSSQL-specific options available for the
  4223. :class:`.Index` construct.
  4224. """
  4225. __visit_name__ = "index"
  4226. table: Optional[Table]
  4227. expressions: _typing_Sequence[Union[str, ColumnElement[Any]]]
  4228. _table_bound_expressions: _typing_Sequence[ColumnElement[Any]]
  4229. def __init__(
  4230. self,
  4231. name: Optional[str],
  4232. *expressions: _DDLColumnArgument,
  4233. unique: bool = False,
  4234. quote: Optional[bool] = None,
  4235. info: Optional[_InfoType] = None,
  4236. _table: Optional[Table] = None,
  4237. _column_flag: bool = False,
  4238. **dialect_kw: Any,
  4239. ) -> None:
  4240. r"""Construct an index object.
  4241. :param name:
  4242. The name of the index
  4243. :param \*expressions:
  4244. Column expressions to include in the index. The expressions
  4245. are normally instances of :class:`_schema.Column`, but may also
  4246. be arbitrary SQL expressions which ultimately refer to a
  4247. :class:`_schema.Column`.
  4248. :param unique=False:
  4249. Keyword only argument; if True, create a unique index.
  4250. :param quote=None:
  4251. Keyword only argument; whether to apply quoting to the name of
  4252. the index. Works in the same manner as that of
  4253. :paramref:`_schema.Column.quote`.
  4254. :param info=None: Optional data dictionary which will be populated
  4255. into the :attr:`.SchemaItem.info` attribute of this object.
  4256. :param \**dialect_kw: Additional keyword arguments not mentioned above
  4257. are dialect specific, and passed in the form
  4258. ``<dialectname>_<argname>``. See the documentation regarding an
  4259. individual dialect at :ref:`dialect_toplevel` for detail on
  4260. documented arguments.
  4261. """
  4262. self.table = table = None
  4263. self.name = quoted_name.construct(name, quote)
  4264. self.unique = unique
  4265. if info is not None:
  4266. self.info = info
  4267. # TODO: consider "table" argument being public, but for
  4268. # the purpose of the fix here, it starts as private.
  4269. if _table is not None:
  4270. table = _table
  4271. self._validate_dialect_kwargs(dialect_kw)
  4272. self.expressions = []
  4273. # will call _set_parent() if table-bound column
  4274. # objects are present
  4275. ColumnCollectionMixin.__init__(
  4276. self,
  4277. *expressions,
  4278. _column_flag=_column_flag,
  4279. _gather_expressions=self.expressions,
  4280. )
  4281. if table is not None:
  4282. self._set_parent(table)
  4283. def _set_parent(self, parent: SchemaEventTarget, **kw: Any) -> None:
  4284. table = parent
  4285. assert isinstance(table, Table)
  4286. ColumnCollectionMixin._set_parent(self, table)
  4287. if self.table is not None and table is not self.table:
  4288. raise exc.ArgumentError(
  4289. f"Index '{self.name}' is against table "
  4290. f"'{self.table.description}', and "
  4291. f"cannot be associated with table '{table.description}'."
  4292. )
  4293. self.table = table
  4294. table.indexes.add(self)
  4295. expressions = self.expressions
  4296. col_expressions = self._col_expressions(table)
  4297. assert len(expressions) == len(col_expressions)
  4298. exprs = []
  4299. for expr, colexpr in zip(expressions, col_expressions):
  4300. if isinstance(expr, ClauseElement):
  4301. exprs.append(expr)
  4302. elif colexpr is not None:
  4303. exprs.append(colexpr)
  4304. else:
  4305. assert False
  4306. self.expressions = self._table_bound_expressions = exprs
  4307. def create(self, bind: _CreateDropBind, checkfirst: bool = False) -> None:
  4308. """Issue a ``CREATE`` statement for this
  4309. :class:`.Index`, using the given
  4310. :class:`.Connection` or :class:`.Engine`` for connectivity.
  4311. .. seealso::
  4312. :meth:`_schema.MetaData.create_all`.
  4313. """
  4314. bind._run_ddl_visitor(ddl.SchemaGenerator, self, checkfirst=checkfirst)
  4315. def drop(self, bind: _CreateDropBind, checkfirst: bool = False) -> None:
  4316. """Issue a ``DROP`` statement for this
  4317. :class:`.Index`, using the given
  4318. :class:`.Connection` or :class:`.Engine` for connectivity.
  4319. .. seealso::
  4320. :meth:`_schema.MetaData.drop_all`.
  4321. """
  4322. bind._run_ddl_visitor(ddl.SchemaDropper, self, checkfirst=checkfirst)
  4323. def __repr__(self) -> str:
  4324. exprs: _typing_Sequence[Any] # noqa: F842
  4325. return "Index(%s)" % (
  4326. ", ".join(
  4327. [repr(self.name)]
  4328. + [repr(e) for e in self.expressions]
  4329. + (self.unique and ["unique=True"] or [])
  4330. )
  4331. )
  4332. _NamingSchemaCallable = Callable[[Constraint, Table], str]
  4333. _NamingSchemaDirective = Union[str, _NamingSchemaCallable]
  4334. class _NamingSchemaTD(TypedDict, total=False):
  4335. fk: _NamingSchemaDirective
  4336. pk: _NamingSchemaDirective
  4337. ix: _NamingSchemaDirective
  4338. ck: _NamingSchemaDirective
  4339. uq: _NamingSchemaDirective
  4340. _NamingSchemaParameter = Union[
  4341. # it seems like the TypedDict here is useful for pylance typeahead,
  4342. # and not much else
  4343. _NamingSchemaTD,
  4344. # there is no form that allows Union[Type[Any], str] to work in all
  4345. # cases, including breaking out Mapping[] entries for each combination
  4346. # even, therefore keys must be `Any` (see #10264)
  4347. Mapping[Any, _NamingSchemaDirective],
  4348. ]
  4349. DEFAULT_NAMING_CONVENTION: _NamingSchemaParameter = util.immutabledict(
  4350. {"ix": "ix_%(column_0_label)s"}
  4351. )
  4352. class MetaData(HasSchemaAttr):
  4353. """A collection of :class:`_schema.Table`
  4354. objects and their associated schema
  4355. constructs.
  4356. Holds a collection of :class:`_schema.Table` objects as well as
  4357. an optional binding to an :class:`_engine.Engine` or
  4358. :class:`_engine.Connection`. If bound, the :class:`_schema.Table` objects
  4359. in the collection and their columns may participate in implicit SQL
  4360. execution.
  4361. The :class:`_schema.Table` objects themselves are stored in the
  4362. :attr:`_schema.MetaData.tables` dictionary.
  4363. :class:`_schema.MetaData` is a thread-safe object for read operations.
  4364. Construction of new tables within a single :class:`_schema.MetaData`
  4365. object,
  4366. either explicitly or via reflection, may not be completely thread-safe.
  4367. .. seealso::
  4368. :ref:`metadata_describing` - Introduction to database metadata
  4369. """
  4370. __visit_name__ = "metadata"
  4371. def __init__(
  4372. self,
  4373. schema: Optional[str] = None,
  4374. quote_schema: Optional[bool] = None,
  4375. naming_convention: Optional[_NamingSchemaParameter] = None,
  4376. info: Optional[_InfoType] = None,
  4377. ) -> None:
  4378. """Create a new MetaData object.
  4379. :param schema:
  4380. The default schema to use for the :class:`_schema.Table`,
  4381. :class:`.Sequence`, and potentially other objects associated with
  4382. this :class:`_schema.MetaData`. Defaults to ``None``.
  4383. .. seealso::
  4384. :ref:`schema_metadata_schema_name` - details on how the
  4385. :paramref:`_schema.MetaData.schema` parameter is used.
  4386. :paramref:`_schema.Table.schema`
  4387. :paramref:`.Sequence.schema`
  4388. :param quote_schema:
  4389. Sets the ``quote_schema`` flag for those :class:`_schema.Table`,
  4390. :class:`.Sequence`, and other objects which make usage of the
  4391. local ``schema`` name.
  4392. :param info: Optional data dictionary which will be populated into the
  4393. :attr:`.SchemaItem.info` attribute of this object.
  4394. :param naming_convention: a dictionary referring to values which
  4395. will establish default naming conventions for :class:`.Constraint`
  4396. and :class:`.Index` objects, for those objects which are not given
  4397. a name explicitly.
  4398. The keys of this dictionary may be:
  4399. * a constraint or Index class, e.g. the :class:`.UniqueConstraint`,
  4400. :class:`_schema.ForeignKeyConstraint` class, the :class:`.Index`
  4401. class
  4402. * a string mnemonic for one of the known constraint classes;
  4403. ``"fk"``, ``"pk"``, ``"ix"``, ``"ck"``, ``"uq"`` for foreign key,
  4404. primary key, index, check, and unique constraint, respectively.
  4405. * the string name of a user-defined "token" that can be used
  4406. to define new naming tokens.
  4407. The values associated with each "constraint class" or "constraint
  4408. mnemonic" key are string naming templates, such as
  4409. ``"uq_%(table_name)s_%(column_0_name)s"``,
  4410. which describe how the name should be composed. The values
  4411. associated with user-defined "token" keys should be callables of the
  4412. form ``fn(constraint, table)``, which accepts the constraint/index
  4413. object and :class:`_schema.Table` as arguments, returning a string
  4414. result.
  4415. The built-in names are as follows, some of which may only be
  4416. available for certain types of constraint:
  4417. * ``%(table_name)s`` - the name of the :class:`_schema.Table`
  4418. object
  4419. associated with the constraint.
  4420. * ``%(referred_table_name)s`` - the name of the
  4421. :class:`_schema.Table`
  4422. object associated with the referencing target of a
  4423. :class:`_schema.ForeignKeyConstraint`.
  4424. * ``%(column_0_name)s`` - the name of the :class:`_schema.Column`
  4425. at
  4426. index position "0" within the constraint.
  4427. * ``%(column_0N_name)s`` - the name of all :class:`_schema.Column`
  4428. objects in order within the constraint, joined without a
  4429. separator.
  4430. * ``%(column_0_N_name)s`` - the name of all
  4431. :class:`_schema.Column`
  4432. objects in order within the constraint, joined with an
  4433. underscore as a separator.
  4434. * ``%(column_0_label)s``, ``%(column_0N_label)s``,
  4435. ``%(column_0_N_label)s`` - the label of either the zeroth
  4436. :class:`_schema.Column` or all :class:`.Columns`, separated with
  4437. or without an underscore
  4438. * ``%(column_0_key)s``, ``%(column_0N_key)s``,
  4439. ``%(column_0_N_key)s`` - the key of either the zeroth
  4440. :class:`_schema.Column` or all :class:`.Columns`, separated with
  4441. or without an underscore
  4442. * ``%(referred_column_0_name)s``, ``%(referred_column_0N_name)s``
  4443. ``%(referred_column_0_N_name)s``, ``%(referred_column_0_key)s``,
  4444. ``%(referred_column_0N_key)s``, ... column tokens which
  4445. render the names/keys/labels of columns that are referenced
  4446. by a :class:`_schema.ForeignKeyConstraint`.
  4447. * ``%(constraint_name)s`` - a special key that refers to the
  4448. existing name given to the constraint. When this key is
  4449. present, the :class:`.Constraint` object's existing name will be
  4450. replaced with one that is composed from template string that
  4451. uses this token. When this token is present, it is required that
  4452. the :class:`.Constraint` is given an explicit name ahead of time.
  4453. * user-defined: any additional token may be implemented by passing
  4454. it along with a ``fn(constraint, table)`` callable to the
  4455. naming_convention dictionary.
  4456. .. versionadded:: 1.3.0 - added new ``%(column_0N_name)s``,
  4457. ``%(column_0_N_name)s``, and related tokens that produce
  4458. concatenations of names, keys, or labels for all columns referred
  4459. to by a given constraint.
  4460. .. seealso::
  4461. :ref:`constraint_naming_conventions` - for detailed usage
  4462. examples.
  4463. """
  4464. if schema is not None and not isinstance(schema, str):
  4465. raise exc.ArgumentError(
  4466. "expected schema argument to be a string, "
  4467. f"got {type(schema)}."
  4468. )
  4469. self.tables = util.FacadeDict()
  4470. self.schema = quoted_name.construct(schema, quote_schema)
  4471. self.naming_convention = (
  4472. naming_convention
  4473. if naming_convention
  4474. else DEFAULT_NAMING_CONVENTION
  4475. )
  4476. if info:
  4477. self.info = info
  4478. self._schemas: Set[str] = set()
  4479. self._sequences: Dict[str, Sequence] = {}
  4480. self._fk_memos: Dict[Tuple[str, Optional[str]], List[ForeignKey]] = (
  4481. collections.defaultdict(list)
  4482. )
  4483. tables: util.FacadeDict[str, Table]
  4484. """A dictionary of :class:`_schema.Table`
  4485. objects keyed to their name or "table key".
  4486. The exact key is that determined by the :attr:`_schema.Table.key`
  4487. attribute;
  4488. for a table with no :attr:`_schema.Table.schema` attribute,
  4489. this is the same
  4490. as :attr:`_schema.Table.name`. For a table with a schema,
  4491. it is typically of the
  4492. form ``schemaname.tablename``.
  4493. .. seealso::
  4494. :attr:`_schema.MetaData.sorted_tables`
  4495. """
  4496. def __repr__(self) -> str:
  4497. return "MetaData()"
  4498. def __contains__(self, table_or_key: Union[str, Table]) -> bool:
  4499. if not isinstance(table_or_key, str):
  4500. table_or_key = table_or_key.key
  4501. return table_or_key in self.tables
  4502. def _add_table(
  4503. self, name: str, schema: Optional[str], table: Table
  4504. ) -> None:
  4505. key = _get_table_key(name, schema)
  4506. self.tables._insert_item(key, table)
  4507. if schema:
  4508. self._schemas.add(schema)
  4509. def _remove_table(self, name: str, schema: Optional[str]) -> None:
  4510. key = _get_table_key(name, schema)
  4511. removed = dict.pop(self.tables, key, None)
  4512. if removed is not None:
  4513. for fk in removed.foreign_keys:
  4514. fk._remove_from_metadata(self)
  4515. if self._schemas:
  4516. self._schemas = {
  4517. t.schema for t in self.tables.values() if t.schema is not None
  4518. }
  4519. def __getstate__(self) -> Dict[str, Any]:
  4520. return {
  4521. "tables": self.tables,
  4522. "schema": self.schema,
  4523. "schemas": self._schemas,
  4524. "sequences": self._sequences,
  4525. "fk_memos": self._fk_memos,
  4526. "naming_convention": self.naming_convention,
  4527. }
  4528. def __setstate__(self, state: Dict[str, Any]) -> None:
  4529. self.tables = state["tables"]
  4530. self.schema = state["schema"]
  4531. self.naming_convention = state["naming_convention"]
  4532. self._sequences = state["sequences"]
  4533. self._schemas = state["schemas"]
  4534. self._fk_memos = state["fk_memos"]
  4535. def clear(self) -> None:
  4536. """Clear all Table objects from this MetaData."""
  4537. dict.clear(self.tables)
  4538. self._schemas.clear()
  4539. self._fk_memos.clear()
  4540. def remove(self, table: Table) -> None:
  4541. """Remove the given Table object from this MetaData."""
  4542. self._remove_table(table.name, table.schema)
  4543. @property
  4544. def sorted_tables(self) -> List[Table]:
  4545. """Returns a list of :class:`_schema.Table` objects sorted in order of
  4546. foreign key dependency.
  4547. The sorting will place :class:`_schema.Table`
  4548. objects that have dependencies
  4549. first, before the dependencies themselves, representing the
  4550. order in which they can be created. To get the order in which
  4551. the tables would be dropped, use the ``reversed()`` Python built-in.
  4552. .. warning::
  4553. The :attr:`.MetaData.sorted_tables` attribute cannot by itself
  4554. accommodate automatic resolution of dependency cycles between
  4555. tables, which are usually caused by mutually dependent foreign key
  4556. constraints. When these cycles are detected, the foreign keys
  4557. of these tables are omitted from consideration in the sort.
  4558. A warning is emitted when this condition occurs, which will be an
  4559. exception raise in a future release. Tables which are not part
  4560. of the cycle will still be returned in dependency order.
  4561. To resolve these cycles, the
  4562. :paramref:`_schema.ForeignKeyConstraint.use_alter` parameter may be
  4563. applied to those constraints which create a cycle. Alternatively,
  4564. the :func:`_schema.sort_tables_and_constraints` function will
  4565. automatically return foreign key constraints in a separate
  4566. collection when cycles are detected so that they may be applied
  4567. to a schema separately.
  4568. .. versionchanged:: 1.3.17 - a warning is emitted when
  4569. :attr:`.MetaData.sorted_tables` cannot perform a proper sort
  4570. due to cyclical dependencies. This will be an exception in a
  4571. future release. Additionally, the sort will continue to return
  4572. other tables not involved in the cycle in dependency order which
  4573. was not the case previously.
  4574. .. seealso::
  4575. :func:`_schema.sort_tables`
  4576. :func:`_schema.sort_tables_and_constraints`
  4577. :attr:`_schema.MetaData.tables`
  4578. :meth:`_reflection.Inspector.get_table_names`
  4579. :meth:`_reflection.Inspector.get_sorted_table_and_fkc_names`
  4580. """
  4581. return ddl.sort_tables(
  4582. sorted(self.tables.values(), key=lambda t: t.key) # type: ignore
  4583. )
  4584. # overload needed to work around mypy this mypy
  4585. # https://github.com/python/mypy/issues/17093
  4586. @overload
  4587. def reflect(
  4588. self,
  4589. bind: Engine,
  4590. schema: Optional[str] = ...,
  4591. views: bool = ...,
  4592. only: Union[
  4593. _typing_Sequence[str], Callable[[str, MetaData], bool], None
  4594. ] = ...,
  4595. extend_existing: bool = ...,
  4596. autoload_replace: bool = ...,
  4597. resolve_fks: bool = ...,
  4598. **dialect_kwargs: Any,
  4599. ) -> None: ...
  4600. @overload
  4601. def reflect(
  4602. self,
  4603. bind: Connection,
  4604. schema: Optional[str] = ...,
  4605. views: bool = ...,
  4606. only: Union[
  4607. _typing_Sequence[str], Callable[[str, MetaData], bool], None
  4608. ] = ...,
  4609. extend_existing: bool = ...,
  4610. autoload_replace: bool = ...,
  4611. resolve_fks: bool = ...,
  4612. **dialect_kwargs: Any,
  4613. ) -> None: ...
  4614. @util.preload_module("sqlalchemy.engine.reflection")
  4615. def reflect(
  4616. self,
  4617. bind: Union[Engine, Connection],
  4618. schema: Optional[str] = None,
  4619. views: bool = False,
  4620. only: Union[
  4621. _typing_Sequence[str], Callable[[str, MetaData], bool], None
  4622. ] = None,
  4623. extend_existing: bool = False,
  4624. autoload_replace: bool = True,
  4625. resolve_fks: bool = True,
  4626. **dialect_kwargs: Any,
  4627. ) -> None:
  4628. r"""Load all available table definitions from the database.
  4629. Automatically creates ``Table`` entries in this ``MetaData`` for any
  4630. table available in the database but not yet present in the
  4631. ``MetaData``. May be called multiple times to pick up tables recently
  4632. added to the database, however no special action is taken if a table
  4633. in this ``MetaData`` no longer exists in the database.
  4634. :param bind:
  4635. A :class:`.Connection` or :class:`.Engine` used to access the
  4636. database.
  4637. :param schema:
  4638. Optional, query and reflect tables from an alternate schema.
  4639. If None, the schema associated with this :class:`_schema.MetaData`
  4640. is used, if any.
  4641. :param views:
  4642. If True, also reflect views (materialized and plain).
  4643. :param only:
  4644. Optional. Load only a sub-set of available named tables. May be
  4645. specified as a sequence of names or a callable.
  4646. If a sequence of names is provided, only those tables will be
  4647. reflected. An error is raised if a table is requested but not
  4648. available. Named tables already present in this ``MetaData`` are
  4649. ignored.
  4650. If a callable is provided, it will be used as a boolean predicate to
  4651. filter the list of potential table names. The callable is called
  4652. with a table name and this ``MetaData`` instance as positional
  4653. arguments and should return a true value for any table to reflect.
  4654. :param extend_existing: Passed along to each :class:`_schema.Table` as
  4655. :paramref:`_schema.Table.extend_existing`.
  4656. :param autoload_replace: Passed along to each :class:`_schema.Table`
  4657. as
  4658. :paramref:`_schema.Table.autoload_replace`.
  4659. :param resolve_fks: if True, reflect :class:`_schema.Table`
  4660. objects linked
  4661. to :class:`_schema.ForeignKey` objects located in each
  4662. :class:`_schema.Table`.
  4663. For :meth:`_schema.MetaData.reflect`,
  4664. this has the effect of reflecting
  4665. related tables that might otherwise not be in the list of tables
  4666. being reflected, for example if the referenced table is in a
  4667. different schema or is omitted via the
  4668. :paramref:`.MetaData.reflect.only` parameter. When False,
  4669. :class:`_schema.ForeignKey` objects are not followed to the
  4670. :class:`_schema.Table`
  4671. in which they link, however if the related table is also part of the
  4672. list of tables that would be reflected in any case, the
  4673. :class:`_schema.ForeignKey` object will still resolve to its related
  4674. :class:`_schema.Table` after the :meth:`_schema.MetaData.reflect`
  4675. operation is
  4676. complete. Defaults to True.
  4677. .. versionadded:: 1.3.0
  4678. .. seealso::
  4679. :paramref:`_schema.Table.resolve_fks`
  4680. :param \**dialect_kwargs: Additional keyword arguments not mentioned
  4681. above are dialect specific, and passed in the form
  4682. ``<dialectname>_<argname>``. See the documentation regarding an
  4683. individual dialect at :ref:`dialect_toplevel` for detail on
  4684. documented arguments.
  4685. .. seealso::
  4686. :ref:`metadata_reflection_toplevel`
  4687. :meth:`_events.DDLEvents.column_reflect` - Event used to customize
  4688. the reflected columns. Usually used to generalize the types using
  4689. :meth:`_types.TypeEngine.as_generic`
  4690. :ref:`metadata_reflection_dbagnostic_types` - describes how to
  4691. reflect tables using general types.
  4692. """
  4693. with inspection.inspect(bind)._inspection_context() as insp:
  4694. reflect_opts: Any = {
  4695. "autoload_with": insp,
  4696. "extend_existing": extend_existing,
  4697. "autoload_replace": autoload_replace,
  4698. "resolve_fks": resolve_fks,
  4699. "_extend_on": set(),
  4700. }
  4701. reflect_opts.update(dialect_kwargs)
  4702. if schema is None:
  4703. schema = self.schema
  4704. if schema is not None:
  4705. reflect_opts["schema"] = schema
  4706. kind = util.preloaded.engine_reflection.ObjectKind.TABLE
  4707. available: util.OrderedSet[str] = util.OrderedSet(
  4708. insp.get_table_names(schema)
  4709. )
  4710. if views:
  4711. kind = util.preloaded.engine_reflection.ObjectKind.ANY
  4712. available.update(insp.get_view_names(schema))
  4713. try:
  4714. available.update(insp.get_materialized_view_names(schema))
  4715. except NotImplementedError:
  4716. pass
  4717. if schema is not None:
  4718. available_w_schema: util.OrderedSet[str] = util.OrderedSet(
  4719. [f"{schema}.{name}" for name in available]
  4720. )
  4721. else:
  4722. available_w_schema = available
  4723. current = set(self.tables)
  4724. if only is None:
  4725. load = [
  4726. name
  4727. for name, schname in zip(available, available_w_schema)
  4728. if extend_existing or schname not in current
  4729. ]
  4730. elif callable(only):
  4731. load = [
  4732. name
  4733. for name, schname in zip(available, available_w_schema)
  4734. if (extend_existing or schname not in current)
  4735. and only(name, self)
  4736. ]
  4737. else:
  4738. missing = [name for name in only if name not in available]
  4739. if missing:
  4740. s = schema and (" schema '%s'" % schema) or ""
  4741. missing_str = ", ".join(missing)
  4742. raise exc.InvalidRequestError(
  4743. f"Could not reflect: requested table(s) not available "
  4744. f"in {bind.engine!r}{s}: ({missing_str})"
  4745. )
  4746. load = [
  4747. name
  4748. for name in only
  4749. if extend_existing or name not in current
  4750. ]
  4751. # pass the available tables so the inspector can
  4752. # choose to ignore the filter_names
  4753. _reflect_info = insp._get_reflection_info(
  4754. schema=schema,
  4755. filter_names=load,
  4756. available=available,
  4757. kind=kind,
  4758. scope=util.preloaded.engine_reflection.ObjectScope.ANY,
  4759. **dialect_kwargs,
  4760. )
  4761. reflect_opts["_reflect_info"] = _reflect_info
  4762. for name in load:
  4763. try:
  4764. Table(name, self, **reflect_opts)
  4765. except exc.UnreflectableTableError as uerr:
  4766. util.warn(f"Skipping table {name}: {uerr}")
  4767. def create_all(
  4768. self,
  4769. bind: _CreateDropBind,
  4770. tables: Optional[_typing_Sequence[Table]] = None,
  4771. checkfirst: bool = True,
  4772. ) -> None:
  4773. """Create all tables stored in this metadata.
  4774. Conditional by default, will not attempt to recreate tables already
  4775. present in the target database.
  4776. :param bind:
  4777. A :class:`.Connection` or :class:`.Engine` used to access the
  4778. database.
  4779. :param tables:
  4780. Optional list of ``Table`` objects, which is a subset of the total
  4781. tables in the ``MetaData`` (others are ignored).
  4782. :param checkfirst:
  4783. Defaults to True, don't issue CREATEs for tables already present
  4784. in the target database.
  4785. """
  4786. bind._run_ddl_visitor(
  4787. ddl.SchemaGenerator, self, checkfirst=checkfirst, tables=tables
  4788. )
  4789. def drop_all(
  4790. self,
  4791. bind: _CreateDropBind,
  4792. tables: Optional[_typing_Sequence[Table]] = None,
  4793. checkfirst: bool = True,
  4794. ) -> None:
  4795. """Drop all tables stored in this metadata.
  4796. Conditional by default, will not attempt to drop tables not present in
  4797. the target database.
  4798. :param bind:
  4799. A :class:`.Connection` or :class:`.Engine` used to access the
  4800. database.
  4801. :param tables:
  4802. Optional list of ``Table`` objects, which is a subset of the
  4803. total tables in the ``MetaData`` (others are ignored).
  4804. :param checkfirst:
  4805. Defaults to True, only issue DROPs for tables confirmed to be
  4806. present in the target database.
  4807. """
  4808. bind._run_ddl_visitor(
  4809. ddl.SchemaDropper, self, checkfirst=checkfirst, tables=tables
  4810. )
  4811. class Computed(FetchedValue, SchemaItem):
  4812. """Defines a generated column, i.e. "GENERATED ALWAYS AS" syntax.
  4813. The :class:`.Computed` construct is an inline construct added to the
  4814. argument list of a :class:`_schema.Column` object::
  4815. from sqlalchemy import Computed
  4816. Table(
  4817. "square",
  4818. metadata_obj,
  4819. Column("side", Float, nullable=False),
  4820. Column("area", Float, Computed("side * side")),
  4821. )
  4822. See the linked documentation below for complete details.
  4823. .. versionadded:: 1.3.11
  4824. .. seealso::
  4825. :ref:`computed_ddl`
  4826. """
  4827. __visit_name__ = "computed_column"
  4828. column: Optional[Column[Any]]
  4829. @_document_text_coercion(
  4830. "sqltext", ":class:`.Computed`", ":paramref:`.Computed.sqltext`"
  4831. )
  4832. def __init__(
  4833. self, sqltext: _DDLColumnArgument, persisted: Optional[bool] = None
  4834. ) -> None:
  4835. """Construct a GENERATED ALWAYS AS DDL construct to accompany a
  4836. :class:`_schema.Column`.
  4837. :param sqltext:
  4838. A string containing the column generation expression, which will be
  4839. used verbatim, or a SQL expression construct, such as a
  4840. :func:`_expression.text`
  4841. object. If given as a string, the object is converted to a
  4842. :func:`_expression.text` object.
  4843. :param persisted:
  4844. Optional, controls how this column should be persisted by the
  4845. database. Possible values are:
  4846. * ``None``, the default, it will use the default persistence
  4847. defined by the database.
  4848. * ``True``, will render ``GENERATED ALWAYS AS ... STORED``, or the
  4849. equivalent for the target database if supported.
  4850. * ``False``, will render ``GENERATED ALWAYS AS ... VIRTUAL``, or
  4851. the equivalent for the target database if supported.
  4852. Specifying ``True`` or ``False`` may raise an error when the DDL
  4853. is emitted to the target database if the database does not support
  4854. that persistence option. Leaving this parameter at its default
  4855. of ``None`` is guaranteed to succeed for all databases that support
  4856. ``GENERATED ALWAYS AS``.
  4857. """
  4858. self.sqltext = coercions.expect(roles.DDLExpressionRole, sqltext)
  4859. self.persisted = persisted
  4860. self.column = None
  4861. def _set_parent(self, parent: SchemaEventTarget, **kw: Any) -> None:
  4862. assert isinstance(parent, Column)
  4863. if not isinstance(
  4864. parent.server_default, (type(None), Computed)
  4865. ) or not isinstance(parent.server_onupdate, (type(None), Computed)):
  4866. raise exc.ArgumentError(
  4867. "A generated column cannot specify a server_default or a "
  4868. "server_onupdate argument"
  4869. )
  4870. self.column = parent
  4871. parent.computed = self
  4872. self.column.server_onupdate = self
  4873. self.column.server_default = self
  4874. def _as_for_update(self, for_update: bool) -> FetchedValue:
  4875. return self
  4876. @util.deprecated(
  4877. "1.4",
  4878. "The :meth:`_schema.Computed.copy` method is deprecated "
  4879. "and will be removed in a future release.",
  4880. )
  4881. def copy(
  4882. self, *, target_table: Optional[Table] = None, **kw: Any
  4883. ) -> Computed:
  4884. return self._copy(target_table=target_table, **kw)
  4885. def _copy(
  4886. self, *, target_table: Optional[Table] = None, **kw: Any
  4887. ) -> Computed:
  4888. sqltext = _copy_expression(
  4889. self.sqltext,
  4890. self.column.table if self.column is not None else None,
  4891. target_table,
  4892. )
  4893. g = Computed(sqltext, persisted=self.persisted)
  4894. return self._schema_item_copy(g)
  4895. class Identity(IdentityOptions, FetchedValue, SchemaItem):
  4896. """Defines an identity column, i.e. "GENERATED { ALWAYS | BY DEFAULT }
  4897. AS IDENTITY" syntax.
  4898. The :class:`.Identity` construct is an inline construct added to the
  4899. argument list of a :class:`_schema.Column` object::
  4900. from sqlalchemy import Identity
  4901. Table(
  4902. "foo",
  4903. metadata_obj,
  4904. Column("id", Integer, Identity()),
  4905. Column("description", Text),
  4906. )
  4907. See the linked documentation below for complete details.
  4908. .. versionadded:: 1.4
  4909. .. seealso::
  4910. :ref:`identity_ddl`
  4911. """
  4912. __visit_name__ = "identity_column"
  4913. is_identity = True
  4914. def __init__(
  4915. self,
  4916. always: bool = False,
  4917. on_null: Optional[bool] = None,
  4918. start: Optional[int] = None,
  4919. increment: Optional[int] = None,
  4920. minvalue: Optional[int] = None,
  4921. maxvalue: Optional[int] = None,
  4922. nominvalue: Optional[bool] = None,
  4923. nomaxvalue: Optional[bool] = None,
  4924. cycle: Optional[bool] = None,
  4925. cache: Optional[int] = None,
  4926. order: Optional[bool] = None,
  4927. ) -> None:
  4928. """Construct a GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY DDL
  4929. construct to accompany a :class:`_schema.Column`.
  4930. See the :class:`.Sequence` documentation for a complete description
  4931. of most parameters.
  4932. .. note::
  4933. MSSQL supports this construct as the preferred alternative to
  4934. generate an IDENTITY on a column, but it uses non standard
  4935. syntax that only support :paramref:`_schema.Identity.start`
  4936. and :paramref:`_schema.Identity.increment`.
  4937. All other parameters are ignored.
  4938. :param always:
  4939. A boolean, that indicates the type of identity column.
  4940. If ``False`` is specified, the default, then the user-specified
  4941. value takes precedence.
  4942. If ``True`` is specified, a user-specified value is not accepted (
  4943. on some backends, like PostgreSQL, OVERRIDING SYSTEM VALUE, or
  4944. similar, may be specified in an INSERT to override the sequence
  4945. value).
  4946. Some backends also have a default value for this parameter,
  4947. ``None`` can be used to omit rendering this part in the DDL. It
  4948. will be treated as ``False`` if a backend does not have a default
  4949. value.
  4950. :param on_null:
  4951. Set to ``True`` to specify ON NULL in conjunction with a
  4952. ``always=False`` identity column. This option is only supported on
  4953. some backends, like Oracle Database.
  4954. :param start: the starting index of the sequence.
  4955. :param increment: the increment value of the sequence.
  4956. :param minvalue: the minimum value of the sequence.
  4957. :param maxvalue: the maximum value of the sequence.
  4958. :param nominvalue: no minimum value of the sequence.
  4959. :param nomaxvalue: no maximum value of the sequence.
  4960. :param cycle: allows the sequence to wrap around when the maxvalue
  4961. or minvalue has been reached.
  4962. :param cache: optional integer value; number of future values in the
  4963. sequence which are calculated in advance.
  4964. :param order: optional boolean value; if true, renders the
  4965. ORDER keyword.
  4966. """
  4967. IdentityOptions.__init__(
  4968. self,
  4969. start=start,
  4970. increment=increment,
  4971. minvalue=minvalue,
  4972. maxvalue=maxvalue,
  4973. nominvalue=nominvalue,
  4974. nomaxvalue=nomaxvalue,
  4975. cycle=cycle,
  4976. cache=cache,
  4977. order=order,
  4978. )
  4979. self.always = always
  4980. self.on_null = on_null
  4981. self.column = None
  4982. def _set_parent(self, parent: SchemaEventTarget, **kw: Any) -> None:
  4983. assert isinstance(parent, Column)
  4984. if not isinstance(
  4985. parent.server_default, (type(None), Identity)
  4986. ) or not isinstance(parent.server_onupdate, type(None)):
  4987. raise exc.ArgumentError(
  4988. "A column with an Identity object cannot specify a "
  4989. "server_default or a server_onupdate argument"
  4990. )
  4991. if parent.autoincrement is False:
  4992. raise exc.ArgumentError(
  4993. "A column with an Identity object cannot specify "
  4994. "autoincrement=False"
  4995. )
  4996. self.column = parent
  4997. parent.identity = self
  4998. if parent._user_defined_nullable is NULL_UNSPECIFIED:
  4999. parent.nullable = False
  5000. parent.server_default = self
  5001. def _as_for_update(self, for_update: bool) -> FetchedValue:
  5002. return self
  5003. @util.deprecated(
  5004. "1.4",
  5005. "The :meth:`_schema.Identity.copy` method is deprecated "
  5006. "and will be removed in a future release.",
  5007. )
  5008. def copy(self, **kw: Any) -> Identity:
  5009. return self._copy(**kw)
  5010. def _copy(self, **kw: Any) -> Identity:
  5011. i = Identity(
  5012. always=self.always,
  5013. on_null=self.on_null,
  5014. start=self.start,
  5015. increment=self.increment,
  5016. minvalue=self.minvalue,
  5017. maxvalue=self.maxvalue,
  5018. nominvalue=self.nominvalue,
  5019. nomaxvalue=self.nomaxvalue,
  5020. cycle=self.cycle,
  5021. cache=self.cache,
  5022. order=self.order,
  5023. )
  5024. return self._schema_item_copy(i)