schema.py 156 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060
  1. # sql/schema.py
  2. # Copyright (C) 2005-2017 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: http://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 absolute_import
  26. from .. import exc, util, event, inspection
  27. from .base import SchemaEventTarget, DialectKWArgs
  28. import operator
  29. from . import visitors
  30. from . import type_api
  31. from .base import _bind_or_error, ColumnCollection
  32. from .elements import ClauseElement, ColumnClause, \
  33. _as_truncated, TextClause, _literal_as_text,\
  34. ColumnElement, quoted_name
  35. from .selectable import TableClause
  36. import collections
  37. import sqlalchemy
  38. from . import ddl
  39. RETAIN_SCHEMA = util.symbol('retain_schema')
  40. BLANK_SCHEMA = util.symbol(
  41. 'blank_schema',
  42. """Symbol indicating that a :class:`.Table` or :class:`.Sequence`
  43. should have 'None' for its schema, even if the parent
  44. :class:`.MetaData` has specified a schema.
  45. .. versionadded:: 1.0.14
  46. """
  47. )
  48. def _get_table_key(name, schema):
  49. if schema is None:
  50. return name
  51. else:
  52. return schema + "." + name
  53. @inspection._self_inspects
  54. class SchemaItem(SchemaEventTarget, visitors.Visitable):
  55. """Base class for items that define a database schema."""
  56. __visit_name__ = 'schema_item'
  57. def _init_items(self, *args):
  58. """Initialize the list of child items for this SchemaItem."""
  59. for item in args:
  60. if item is not None:
  61. item._set_parent_with_dispatch(self)
  62. def get_children(self, **kwargs):
  63. """used to allow SchemaVisitor access"""
  64. return []
  65. def __repr__(self):
  66. return util.generic_repr(self, omit_kwarg=['info'])
  67. @property
  68. @util.deprecated('0.9', 'Use ``<obj>.name.quote``')
  69. def quote(self):
  70. """Return the value of the ``quote`` flag passed
  71. to this schema object, for those schema items which
  72. have a ``name`` field.
  73. """
  74. return self.name.quote
  75. @util.memoized_property
  76. def info(self):
  77. """Info dictionary associated with the object, allowing user-defined
  78. data to be associated with this :class:`.SchemaItem`.
  79. The dictionary is automatically generated when first accessed.
  80. It can also be specified in the constructor of some objects,
  81. such as :class:`.Table` and :class:`.Column`.
  82. """
  83. return {}
  84. def _schema_item_copy(self, schema_item):
  85. if 'info' in self.__dict__:
  86. schema_item.info = self.info.copy()
  87. schema_item.dispatch._update(self.dispatch)
  88. return schema_item
  89. def _translate_schema(self, effective_schema, map_):
  90. return map_.get(effective_schema, effective_schema)
  91. class Table(DialectKWArgs, SchemaItem, TableClause):
  92. r"""Represent a table in a database.
  93. e.g.::
  94. mytable = Table("mytable", metadata,
  95. Column('mytable_id', Integer, primary_key=True),
  96. Column('value', String(50))
  97. )
  98. The :class:`.Table` object constructs a unique instance of itself based
  99. on its name and optional schema name within the given
  100. :class:`.MetaData` object. Calling the :class:`.Table`
  101. constructor with the same name and same :class:`.MetaData` argument
  102. a second time will return the *same* :class:`.Table` object - in this way
  103. the :class:`.Table` constructor acts as a registry function.
  104. .. seealso::
  105. :ref:`metadata_describing` - Introduction to database metadata
  106. Constructor arguments are as follows:
  107. :param name: The name of this table as represented in the database.
  108. The table name, along with the value of the ``schema`` parameter,
  109. forms a key which uniquely identifies this :class:`.Table` within
  110. the owning :class:`.MetaData` collection.
  111. Additional calls to :class:`.Table` with the same name, metadata,
  112. and schema name will return the same :class:`.Table` object.
  113. Names which contain no upper case characters
  114. will be treated as case insensitive names, and will not be quoted
  115. unless they are a reserved word or contain special characters.
  116. A name with any number of upper case characters is considered
  117. to be case sensitive, and will be sent as quoted.
  118. To enable unconditional quoting for the table name, specify the flag
  119. ``quote=True`` to the constructor, or use the :class:`.quoted_name`
  120. construct to specify the name.
  121. :param metadata: a :class:`.MetaData` object which will contain this
  122. table. The metadata is used as a point of association of this table
  123. with other tables which are referenced via foreign key. It also
  124. may be used to associate this table with a particular
  125. :class:`.Connectable`.
  126. :param \*args: Additional positional arguments are used primarily
  127. to add the list of :class:`.Column` objects contained within this
  128. table. Similar to the style of a CREATE TABLE statement, other
  129. :class:`.SchemaItem` constructs may be added here, including
  130. :class:`.PrimaryKeyConstraint`, and :class:`.ForeignKeyConstraint`.
  131. :param autoload: Defaults to False, unless :paramref:`.Table.autoload_with`
  132. is set in which case it defaults to True; :class:`.Column` objects
  133. for this table should be reflected from the database, possibly
  134. augmenting or replacing existing :class:`.Column` objects that were
  135. explicitly specified.
  136. .. versionchanged:: 1.0.0 setting the :paramref:`.Table.autoload_with`
  137. parameter implies that :paramref:`.Table.autoload` will default
  138. to True.
  139. .. seealso::
  140. :ref:`metadata_reflection_toplevel`
  141. :param autoload_replace: Defaults to ``True``; when using
  142. :paramref:`.Table.autoload`
  143. in conjunction with :paramref:`.Table.extend_existing`, indicates
  144. that :class:`.Column` objects present in the already-existing
  145. :class:`.Table` object should be replaced with columns of the same
  146. name retrieved from the autoload process. When ``False``, columns
  147. already present under existing names will be omitted from the
  148. reflection process.
  149. Note that this setting does not impact :class:`.Column` objects
  150. specified programmatically within the call to :class:`.Table` that
  151. also is autoloading; those :class:`.Column` objects will always
  152. replace existing columns of the same name when
  153. :paramref:`.Table.extend_existing` is ``True``.
  154. .. versionadded:: 0.7.5
  155. .. seealso::
  156. :paramref:`.Table.autoload`
  157. :paramref:`.Table.extend_existing`
  158. :param autoload_with: An :class:`.Engine` or :class:`.Connection` object
  159. with which this :class:`.Table` object will be reflected; when
  160. set to a non-None value, it implies that :paramref:`.Table.autoload`
  161. is ``True``. If left unset, but :paramref:`.Table.autoload` is
  162. explicitly set to ``True``, an autoload operation will attempt to
  163. proceed by locating an :class:`.Engine` or :class:`.Connection` bound
  164. to the underlying :class:`.MetaData` object.
  165. .. seealso::
  166. :paramref:`.Table.autoload`
  167. :param extend_existing: When ``True``, indicates that if this
  168. :class:`.Table` is already present in the given :class:`.MetaData`,
  169. apply further arguments within the constructor to the existing
  170. :class:`.Table`.
  171. If :paramref:`.Table.extend_existing` or
  172. :paramref:`.Table.keep_existing` are not set, and the given name
  173. of the new :class:`.Table` refers to a :class:`.Table` that is
  174. already present in the target :class:`.MetaData` collection, and
  175. this :class:`.Table` specifies additional columns or other constructs
  176. or flags that modify the table's state, an
  177. error is raised. The purpose of these two mutually-exclusive flags
  178. is to specify what action should be taken when a :class:`.Table`
  179. is specified that matches an existing :class:`.Table`, yet specifies
  180. additional constructs.
  181. :paramref:`.Table.extend_existing` will also work in conjunction
  182. with :paramref:`.Table.autoload` to run a new reflection
  183. operation against the database, even if a :class:`.Table`
  184. of the same name is already present in the target
  185. :class:`.MetaData`; newly reflected :class:`.Column` objects
  186. and other options will be added into the state of the
  187. :class:`.Table`, potentially overwriting existing columns
  188. and options of the same name.
  189. .. versionchanged:: 0.7.4 :paramref:`.Table.extend_existing` will
  190. invoke a new reflection operation when combined with
  191. :paramref:`.Table.autoload` set to True.
  192. As is always the case with :paramref:`.Table.autoload`,
  193. :class:`.Column` objects can be specified in the same :class:`.Table`
  194. constructor, which will take precedence. Below, the existing
  195. table ``mytable`` will be augmented with :class:`.Column` objects
  196. both reflected from the database, as well as the given :class:`.Column`
  197. named "y"::
  198. Table("mytable", metadata,
  199. Column('y', Integer),
  200. extend_existing=True,
  201. autoload=True,
  202. autoload_with=engine
  203. )
  204. .. seealso::
  205. :paramref:`.Table.autoload`
  206. :paramref:`.Table.autoload_replace`
  207. :paramref:`.Table.keep_existing`
  208. :param implicit_returning: True by default - indicates that
  209. RETURNING can be used by default to fetch newly inserted primary key
  210. values, for backends which support this. Note that
  211. create_engine() also provides an implicit_returning flag.
  212. :param include_columns: A list of strings indicating a subset of
  213. columns to be loaded via the ``autoload`` operation; table columns who
  214. aren't present in this list will not be represented on the resulting
  215. ``Table`` object. Defaults to ``None`` which indicates all columns
  216. should be reflected.
  217. :param info: Optional data dictionary which will be populated into the
  218. :attr:`.SchemaItem.info` attribute of this object.
  219. :param keep_existing: When ``True``, indicates that if this Table
  220. is already present in the given :class:`.MetaData`, ignore
  221. further arguments within the constructor to the existing
  222. :class:`.Table`, and return the :class:`.Table` object as
  223. originally created. This is to allow a function that wishes
  224. to define a new :class:`.Table` on first call, but on
  225. subsequent calls will return the same :class:`.Table`,
  226. without any of the declarations (particularly constraints)
  227. being applied a second time.
  228. If :paramref:`.Table.extend_existing` or
  229. :paramref:`.Table.keep_existing` are not set, and the given name
  230. of the new :class:`.Table` refers to a :class:`.Table` that is
  231. already present in the target :class:`.MetaData` collection, and
  232. this :class:`.Table` specifies additional columns or other constructs
  233. or flags that modify the table's state, an
  234. error is raised. The purpose of these two mutually-exclusive flags
  235. is to specify what action should be taken when a :class:`.Table`
  236. is specified that matches an existing :class:`.Table`, yet specifies
  237. additional constructs.
  238. .. seealso::
  239. :paramref:`.Table.extend_existing`
  240. :param listeners: A list of tuples of the form ``(<eventname>, <fn>)``
  241. which will be passed to :func:`.event.listen` upon construction.
  242. This alternate hook to :func:`.event.listen` allows the establishment
  243. of a listener function specific to this :class:`.Table` before
  244. the "autoload" process begins. Particularly useful for
  245. the :meth:`.DDLEvents.column_reflect` event::
  246. def listen_for_reflect(table, column_info):
  247. "handle the column reflection event"
  248. # ...
  249. t = Table(
  250. 'sometable',
  251. autoload=True,
  252. listeners=[
  253. ('column_reflect', listen_for_reflect)
  254. ])
  255. :param mustexist: When ``True``, indicates that this Table must already
  256. be present in the given :class:`.MetaData` collection, else
  257. an exception is raised.
  258. :param prefixes:
  259. A list of strings to insert after CREATE in the CREATE TABLE
  260. statement. They will be separated by spaces.
  261. :param quote: Force quoting of this table's name on or off, corresponding
  262. to ``True`` or ``False``. When left at its default of ``None``,
  263. the column identifier will be quoted according to whether the name is
  264. case sensitive (identifiers with at least one upper case character are
  265. treated as case sensitive), or if it's a reserved word. This flag
  266. is only needed to force quoting of a reserved word which is not known
  267. by the SQLAlchemy dialect.
  268. :param quote_schema: same as 'quote' but applies to the schema identifier.
  269. :param schema: The schema name for this table, which is required if
  270. the table resides in a schema other than the default selected schema
  271. for the engine's database connection. Defaults to ``None``.
  272. If the owning :class:`.MetaData` of this :class:`.Table` specifies
  273. its own :paramref:`.MetaData.schema` parameter, then that schema
  274. name will be applied to this :class:`.Table` if the schema parameter
  275. here is set to ``None``. To set a blank schema name on a :class:`.Table`
  276. that would otherwise use the schema set on the owning :class:`.MetaData`,
  277. specify the special symbol :attr:`.BLANK_SCHEMA`.
  278. .. versionadded:: 1.0.14 Added the :attr:`.BLANK_SCHEMA` symbol to
  279. allow a :class:`.Table` to have a blank schema name even when the
  280. parent :class:`.MetaData` specifies :paramref:`.MetaData.schema`.
  281. The quoting rules for the schema name are the same as those for the
  282. ``name`` parameter, in that quoting is applied for reserved words or
  283. case-sensitive names; to enable unconditional quoting for the
  284. schema name, specify the flag
  285. ``quote_schema=True`` to the constructor, or use the
  286. :class:`.quoted_name` construct to specify the name.
  287. :param useexisting: Deprecated. Use :paramref:`.Table.extend_existing`.
  288. :param \**kw: Additional keyword arguments not mentioned above are
  289. dialect specific, and passed in the form ``<dialectname>_<argname>``.
  290. See the documentation regarding an individual dialect at
  291. :ref:`dialect_toplevel` for detail on documented arguments.
  292. """
  293. __visit_name__ = 'table'
  294. def __new__(cls, *args, **kw):
  295. if not args:
  296. # python3k pickle seems to call this
  297. return object.__new__(cls)
  298. try:
  299. name, metadata, args = args[0], args[1], args[2:]
  300. except IndexError:
  301. raise TypeError("Table() takes at least two arguments")
  302. schema = kw.get('schema', None)
  303. if schema is None:
  304. schema = metadata.schema
  305. elif schema is BLANK_SCHEMA:
  306. schema = None
  307. keep_existing = kw.pop('keep_existing', False)
  308. extend_existing = kw.pop('extend_existing', False)
  309. if 'useexisting' in kw:
  310. msg = "useexisting is deprecated. Use extend_existing."
  311. util.warn_deprecated(msg)
  312. if extend_existing:
  313. msg = "useexisting is synonymous with extend_existing."
  314. raise exc.ArgumentError(msg)
  315. extend_existing = kw.pop('useexisting', False)
  316. if keep_existing and extend_existing:
  317. msg = "keep_existing and extend_existing are mutually exclusive."
  318. raise exc.ArgumentError(msg)
  319. mustexist = kw.pop('mustexist', False)
  320. key = _get_table_key(name, schema)
  321. if key in metadata.tables:
  322. if not keep_existing and not extend_existing and bool(args):
  323. raise exc.InvalidRequestError(
  324. "Table '%s' is already defined for this MetaData "
  325. "instance. Specify 'extend_existing=True' "
  326. "to redefine "
  327. "options and columns on an "
  328. "existing Table object." % key)
  329. table = metadata.tables[key]
  330. if extend_existing:
  331. table._init_existing(*args, **kw)
  332. return table
  333. else:
  334. if mustexist:
  335. raise exc.InvalidRequestError(
  336. "Table '%s' not defined" % (key))
  337. table = object.__new__(cls)
  338. table.dispatch.before_parent_attach(table, metadata)
  339. metadata._add_table(name, schema, table)
  340. try:
  341. table._init(name, metadata, *args, **kw)
  342. table.dispatch.after_parent_attach(table, metadata)
  343. return table
  344. except:
  345. with util.safe_reraise():
  346. metadata._remove_table(name, schema)
  347. @property
  348. @util.deprecated('0.9', 'Use ``table.schema.quote``')
  349. def quote_schema(self):
  350. """Return the value of the ``quote_schema`` flag passed
  351. to this :class:`.Table`.
  352. """
  353. return self.schema.quote
  354. def __init__(self, *args, **kw):
  355. """Constructor for :class:`~.schema.Table`.
  356. This method is a no-op. See the top-level
  357. documentation for :class:`~.schema.Table`
  358. for constructor arguments.
  359. """
  360. # __init__ is overridden to prevent __new__ from
  361. # calling the superclass constructor.
  362. def _init(self, name, metadata, *args, **kwargs):
  363. super(Table, self).__init__(
  364. quoted_name(name, kwargs.pop('quote', None)))
  365. self.metadata = metadata
  366. self.schema = kwargs.pop('schema', None)
  367. if self.schema is None:
  368. self.schema = metadata.schema
  369. elif self.schema is BLANK_SCHEMA:
  370. self.schema = None
  371. else:
  372. quote_schema = kwargs.pop('quote_schema', None)
  373. self.schema = quoted_name(self.schema, quote_schema)
  374. self.indexes = set()
  375. self.constraints = set()
  376. self._columns = ColumnCollection()
  377. PrimaryKeyConstraint(_implicit_generated=True).\
  378. _set_parent_with_dispatch(self)
  379. self.foreign_keys = set()
  380. self._extra_dependencies = set()
  381. if self.schema is not None:
  382. self.fullname = "%s.%s" % (self.schema, self.name)
  383. else:
  384. self.fullname = self.name
  385. autoload_with = kwargs.pop('autoload_with', None)
  386. autoload = kwargs.pop('autoload', autoload_with is not None)
  387. # this argument is only used with _init_existing()
  388. kwargs.pop('autoload_replace', True)
  389. _extend_on = kwargs.pop("_extend_on", None)
  390. include_columns = kwargs.pop('include_columns', None)
  391. self.implicit_returning = kwargs.pop('implicit_returning', True)
  392. if 'info' in kwargs:
  393. self.info = kwargs.pop('info')
  394. if 'listeners' in kwargs:
  395. listeners = kwargs.pop('listeners')
  396. for evt, fn in listeners:
  397. event.listen(self, evt, fn)
  398. self._prefixes = kwargs.pop('prefixes', [])
  399. self._extra_kwargs(**kwargs)
  400. # load column definitions from the database if 'autoload' is defined
  401. # we do it after the table is in the singleton dictionary to support
  402. # circular foreign keys
  403. if autoload:
  404. self._autoload(
  405. metadata, autoload_with,
  406. include_columns, _extend_on=_extend_on)
  407. # initialize all the column, etc. objects. done after reflection to
  408. # allow user-overrides
  409. self._init_items(*args)
  410. def _autoload(self, metadata, autoload_with, include_columns,
  411. exclude_columns=(), _extend_on=None):
  412. if autoload_with:
  413. autoload_with.run_callable(
  414. autoload_with.dialect.reflecttable,
  415. self, include_columns, exclude_columns,
  416. _extend_on=_extend_on
  417. )
  418. else:
  419. bind = _bind_or_error(
  420. metadata,
  421. msg="No engine is bound to this Table's MetaData. "
  422. "Pass an engine to the Table via "
  423. "autoload_with=<someengine>, "
  424. "or associate the MetaData with an engine via "
  425. "metadata.bind=<someengine>")
  426. bind.run_callable(
  427. bind.dialect.reflecttable,
  428. self, include_columns, exclude_columns,
  429. _extend_on=_extend_on
  430. )
  431. @property
  432. def _sorted_constraints(self):
  433. """Return the set of constraints as a list, sorted by creation
  434. order.
  435. """
  436. return sorted(self.constraints, key=lambda c: c._creation_order)
  437. @property
  438. def foreign_key_constraints(self):
  439. """:class:`.ForeignKeyConstraint` objects referred to by this
  440. :class:`.Table`.
  441. This list is produced from the collection of :class:`.ForeignKey`
  442. objects currently associated.
  443. .. versionadded:: 1.0.0
  444. """
  445. return set(fkc.constraint for fkc in self.foreign_keys)
  446. def _init_existing(self, *args, **kwargs):
  447. autoload_with = kwargs.pop('autoload_with', None)
  448. autoload = kwargs.pop('autoload', autoload_with is not None)
  449. autoload_replace = kwargs.pop('autoload_replace', True)
  450. schema = kwargs.pop('schema', None)
  451. _extend_on = kwargs.pop('_extend_on', None)
  452. if schema and schema != self.schema:
  453. raise exc.ArgumentError(
  454. "Can't change schema of existing table from '%s' to '%s'",
  455. (self.schema, schema))
  456. include_columns = kwargs.pop('include_columns', None)
  457. if include_columns is not None:
  458. for c in self.c:
  459. if c.name not in include_columns:
  460. self._columns.remove(c)
  461. for key in ('quote', 'quote_schema'):
  462. if key in kwargs:
  463. raise exc.ArgumentError(
  464. "Can't redefine 'quote' or 'quote_schema' arguments")
  465. if 'info' in kwargs:
  466. self.info = kwargs.pop('info')
  467. if autoload:
  468. if not autoload_replace:
  469. # don't replace columns already present.
  470. # we'd like to do this for constraints also however we don't
  471. # have simple de-duping for unnamed constraints.
  472. exclude_columns = [c.name for c in self.c]
  473. else:
  474. exclude_columns = ()
  475. self._autoload(
  476. self.metadata, autoload_with,
  477. include_columns, exclude_columns, _extend_on=_extend_on)
  478. self._extra_kwargs(**kwargs)
  479. self._init_items(*args)
  480. def _extra_kwargs(self, **kwargs):
  481. self._validate_dialect_kwargs(kwargs)
  482. def _init_collections(self):
  483. pass
  484. def _reset_exported(self):
  485. pass
  486. @property
  487. def _autoincrement_column(self):
  488. return self.primary_key._autoincrement_column
  489. @property
  490. def key(self):
  491. """Return the 'key' for this :class:`.Table`.
  492. This value is used as the dictionary key within the
  493. :attr:`.MetaData.tables` collection. It is typically the same
  494. as that of :attr:`.Table.name` for a table with no
  495. :attr:`.Table.schema` set; otherwise it is typically of the form
  496. ``schemaname.tablename``.
  497. """
  498. return _get_table_key(self.name, self.schema)
  499. def __repr__(self):
  500. return "Table(%s)" % ', '.join(
  501. [repr(self.name)] + [repr(self.metadata)] +
  502. [repr(x) for x in self.columns] +
  503. ["%s=%s" % (k, repr(getattr(self, k))) for k in ['schema']])
  504. def __str__(self):
  505. return _get_table_key(self.description, self.schema)
  506. @property
  507. def bind(self):
  508. """Return the connectable associated with this Table."""
  509. return self.metadata and self.metadata.bind or None
  510. def add_is_dependent_on(self, table):
  511. """Add a 'dependency' for this Table.
  512. This is another Table object which must be created
  513. first before this one can, or dropped after this one.
  514. Usually, dependencies between tables are determined via
  515. ForeignKey objects. However, for other situations that
  516. create dependencies outside of foreign keys (rules, inheriting),
  517. this method can manually establish such a link.
  518. """
  519. self._extra_dependencies.add(table)
  520. def append_column(self, column):
  521. """Append a :class:`~.schema.Column` to this :class:`~.schema.Table`.
  522. The "key" of the newly added :class:`~.schema.Column`, i.e. the
  523. value of its ``.key`` attribute, will then be available
  524. in the ``.c`` collection of this :class:`~.schema.Table`, and the
  525. column definition will be included in any CREATE TABLE, SELECT,
  526. UPDATE, etc. statements generated from this :class:`~.schema.Table`
  527. construct.
  528. Note that this does **not** change the definition of the table
  529. as it exists within any underlying database, assuming that
  530. table has already been created in the database. Relational
  531. databases support the addition of columns to existing tables
  532. using the SQL ALTER command, which would need to be
  533. emitted for an already-existing table that doesn't contain
  534. the newly added column.
  535. """
  536. column._set_parent_with_dispatch(self)
  537. def append_constraint(self, constraint):
  538. """Append a :class:`~.schema.Constraint` to this
  539. :class:`~.schema.Table`.
  540. This has the effect of the constraint being included in any
  541. future CREATE TABLE statement, assuming specific DDL creation
  542. events have not been associated with the given
  543. :class:`~.schema.Constraint` object.
  544. Note that this does **not** produce the constraint within the
  545. relational database automatically, for a table that already exists
  546. in the database. To add a constraint to an
  547. existing relational database table, the SQL ALTER command must
  548. be used. SQLAlchemy also provides the
  549. :class:`.AddConstraint` construct which can produce this SQL when
  550. invoked as an executable clause.
  551. """
  552. constraint._set_parent_with_dispatch(self)
  553. def append_ddl_listener(self, event_name, listener):
  554. """Append a DDL event listener to this ``Table``.
  555. .. deprecated:: 0.7
  556. See :class:`.DDLEvents`.
  557. """
  558. def adapt_listener(target, connection, **kw):
  559. listener(event_name, target, connection)
  560. event.listen(self, "" + event_name.replace('-', '_'), adapt_listener)
  561. def _set_parent(self, metadata):
  562. metadata._add_table(self.name, self.schema, self)
  563. self.metadata = metadata
  564. def get_children(self, column_collections=True,
  565. schema_visitor=False, **kw):
  566. if not schema_visitor:
  567. return TableClause.get_children(
  568. self, column_collections=column_collections, **kw)
  569. else:
  570. if column_collections:
  571. return list(self.columns)
  572. else:
  573. return []
  574. def exists(self, bind=None):
  575. """Return True if this table exists."""
  576. if bind is None:
  577. bind = _bind_or_error(self)
  578. return bind.run_callable(bind.dialect.has_table,
  579. self.name, schema=self.schema)
  580. def create(self, bind=None, checkfirst=False):
  581. """Issue a ``CREATE`` statement for this
  582. :class:`.Table`, using the given :class:`.Connectable`
  583. for connectivity.
  584. .. seealso::
  585. :meth:`.MetaData.create_all`.
  586. """
  587. if bind is None:
  588. bind = _bind_or_error(self)
  589. bind._run_visitor(ddl.SchemaGenerator,
  590. self,
  591. checkfirst=checkfirst)
  592. def drop(self, bind=None, checkfirst=False):
  593. """Issue a ``DROP`` statement for this
  594. :class:`.Table`, using the given :class:`.Connectable`
  595. for connectivity.
  596. .. seealso::
  597. :meth:`.MetaData.drop_all`.
  598. """
  599. if bind is None:
  600. bind = _bind_or_error(self)
  601. bind._run_visitor(ddl.SchemaDropper,
  602. self,
  603. checkfirst=checkfirst)
  604. def tometadata(self, metadata, schema=RETAIN_SCHEMA,
  605. referred_schema_fn=None, name=None):
  606. """Return a copy of this :class:`.Table` associated with a different
  607. :class:`.MetaData`.
  608. E.g.::
  609. m1 = MetaData()
  610. user = Table('user', m1, Column('id', Integer, priamry_key=True))
  611. m2 = MetaData()
  612. user_copy = user.tometadata(m2)
  613. :param metadata: Target :class:`.MetaData` object, into which the
  614. new :class:`.Table` object will be created.
  615. :param schema: optional string name indicating the target schema.
  616. Defaults to the special symbol :attr:`.RETAIN_SCHEMA` which indicates
  617. that no change to the schema name should be made in the new
  618. :class:`.Table`. If set to a string name, the new :class:`.Table`
  619. will have this new name as the ``.schema``. If set to ``None``, the
  620. schema will be set to that of the schema set on the target
  621. :class:`.MetaData`, which is typically ``None`` as well, unless
  622. set explicitly::
  623. m2 = MetaData(schema='newschema')
  624. # user_copy_one will have "newschema" as the schema name
  625. user_copy_one = user.tometadata(m2, schema=None)
  626. m3 = MetaData() # schema defaults to None
  627. # user_copy_two will have None as the schema name
  628. user_copy_two = user.tometadata(m3, schema=None)
  629. :param referred_schema_fn: optional callable which can be supplied
  630. in order to provide for the schema name that should be assigned
  631. to the referenced table of a :class:`.ForeignKeyConstraint`.
  632. The callable accepts this parent :class:`.Table`, the
  633. target schema that we are changing to, the
  634. :class:`.ForeignKeyConstraint` object, and the existing
  635. "target schema" of that constraint. The function should return the
  636. string schema name that should be applied.
  637. E.g.::
  638. def referred_schema_fn(table, to_schema,
  639. constraint, referred_schema):
  640. if referred_schema == 'base_tables':
  641. return referred_schema
  642. else:
  643. return to_schema
  644. new_table = table.tometadata(m2, schema="alt_schema",
  645. referred_schema_fn=referred_schema_fn)
  646. .. versionadded:: 0.9.2
  647. :param name: optional string name indicating the target table name.
  648. If not specified or None, the table name is retained. This allows
  649. a :class:`.Table` to be copied to the same :class:`.MetaData` target
  650. with a new name.
  651. .. versionadded:: 1.0.0
  652. """
  653. if name is None:
  654. name = self.name
  655. if schema is RETAIN_SCHEMA:
  656. schema = self.schema
  657. elif schema is None:
  658. schema = metadata.schema
  659. key = _get_table_key(name, schema)
  660. if key in metadata.tables:
  661. util.warn("Table '%s' already exists within the given "
  662. "MetaData - not copying." % self.description)
  663. return metadata.tables[key]
  664. args = []
  665. for c in self.columns:
  666. args.append(c.copy(schema=schema))
  667. table = Table(
  668. name, metadata, schema=schema,
  669. *args, **self.kwargs
  670. )
  671. for c in self.constraints:
  672. if isinstance(c, ForeignKeyConstraint):
  673. referred_schema = c._referred_schema
  674. if referred_schema_fn:
  675. fk_constraint_schema = referred_schema_fn(
  676. self, schema, c, referred_schema)
  677. else:
  678. fk_constraint_schema = (
  679. schema if referred_schema == self.schema else None)
  680. table.append_constraint(
  681. c.copy(schema=fk_constraint_schema, target_table=table))
  682. elif not c._type_bound:
  683. # skip unique constraints that would be generated
  684. # by the 'unique' flag on Column
  685. if isinstance(c, UniqueConstraint) and \
  686. len(c.columns) == 1 and \
  687. list(c.columns)[0].unique:
  688. continue
  689. table.append_constraint(
  690. c.copy(schema=schema, target_table=table))
  691. for index in self.indexes:
  692. # skip indexes that would be generated
  693. # by the 'index' flag on Column
  694. if len(index.columns) == 1 and \
  695. list(index.columns)[0].index:
  696. continue
  697. Index(index.name,
  698. unique=index.unique,
  699. *[table.c[col] for col in index.columns.keys()],
  700. **index.kwargs)
  701. return self._schema_item_copy(table)
  702. class Column(SchemaItem, ColumnClause):
  703. """Represents a column in a database table."""
  704. __visit_name__ = 'column'
  705. def __init__(self, *args, **kwargs):
  706. r"""
  707. Construct a new ``Column`` object.
  708. :param name: The name of this column as represented in the database.
  709. This argument may be the first positional argument, or specified
  710. via keyword.
  711. Names which contain no upper case characters
  712. will be treated as case insensitive names, and will not be quoted
  713. unless they are a reserved word. Names with any number of upper
  714. case characters will be quoted and sent exactly. Note that this
  715. behavior applies even for databases which standardize upper
  716. case names as case insensitive such as Oracle.
  717. The name field may be omitted at construction time and applied
  718. later, at any time before the Column is associated with a
  719. :class:`.Table`. This is to support convenient
  720. usage within the :mod:`~sqlalchemy.ext.declarative` extension.
  721. :param type\_: The column's type, indicated using an instance which
  722. subclasses :class:`~sqlalchemy.types.TypeEngine`. If no arguments
  723. are required for the type, the class of the type can be sent
  724. as well, e.g.::
  725. # use a type with arguments
  726. Column('data', String(50))
  727. # use no arguments
  728. Column('level', Integer)
  729. The ``type`` argument may be the second positional argument
  730. or specified by keyword.
  731. If the ``type`` is ``None`` or is omitted, it will first default to
  732. the special type :class:`.NullType`. If and when this
  733. :class:`.Column` is made to refer to another column using
  734. :class:`.ForeignKey` and/or :class:`.ForeignKeyConstraint`, the type
  735. of the remote-referenced column will be copied to this column as
  736. well, at the moment that the foreign key is resolved against that
  737. remote :class:`.Column` object.
  738. .. versionchanged:: 0.9.0
  739. Support for propagation of type to a :class:`.Column` from its
  740. :class:`.ForeignKey` object has been improved and should be
  741. more reliable and timely.
  742. :param \*args: Additional positional arguments include various
  743. :class:`.SchemaItem` derived constructs which will be applied
  744. as options to the column. These include instances of
  745. :class:`.Constraint`, :class:`.ForeignKey`, :class:`.ColumnDefault`,
  746. and :class:`.Sequence`. In some cases an equivalent keyword
  747. argument is available such as ``server_default``, ``default``
  748. and ``unique``.
  749. :param autoincrement: Set up "auto increment" semantics for an integer
  750. primary key column. The default value is the string ``"auto"``
  751. which indicates that a single-column primary key that is of
  752. an INTEGER type with no stated client-side or python-side defaults
  753. should receive auto increment semantics automatically;
  754. all other varieties of primary key columns will not. This
  755. includes that :term:`DDL` such as PostgreSQL SERIAL or MySQL
  756. AUTO_INCREMENT will be emitted for this column during a table
  757. create, as well as that the column is assumed to generate new
  758. integer primary key values when an INSERT statement invokes which
  759. will be retrieved by the dialect.
  760. The flag may be set to ``True`` to indicate that a column which
  761. is part of a composite (e.g. multi-column) primary key should
  762. have autoincrement semantics, though note that only one column
  763. within a primary key may have this setting. It can also
  764. be set to ``True`` to indicate autoincrement semantics on a
  765. column that has a client-side or server-side default configured,
  766. however note that not all dialects can accommodate all styles
  767. of default as an "autoincrement". It can also be
  768. set to ``False`` on a single-column primary key that has a
  769. datatype of INTEGER in order to disable auto increment semantics
  770. for that column.
  771. .. versionchanged:: 1.1 The autoincrement flag now defaults to
  772. ``"auto"`` which indicates autoincrement semantics by default
  773. for single-column integer primary keys only; for composite
  774. (multi-column) primary keys, autoincrement is never implicitly
  775. enabled; as always, ``autoincrement=True`` will allow for
  776. at most one of those columns to be an "autoincrement" column.
  777. ``autoincrement=True`` may also be set on a :class:`.Column`
  778. that has an explicit client-side or server-side default,
  779. subject to limitations of the backend database and dialect.
  780. The setting *only* has an effect for columns which are:
  781. * Integer derived (i.e. INT, SMALLINT, BIGINT).
  782. * Part of the primary key
  783. * Not referring to another column via :class:`.ForeignKey`, unless
  784. the value is specified as ``'ignore_fk'``::
  785. # turn on autoincrement for this column despite
  786. # the ForeignKey()
  787. Column('id', ForeignKey('other.id'),
  788. primary_key=True, autoincrement='ignore_fk')
  789. It is typically not desirable to have "autoincrement" enabled
  790. on a column that refers to another via foreign key, as such a column
  791. is required to refer to a value that originates from elsewhere.
  792. The setting has these two effects on columns that meet the
  793. above criteria:
  794. * DDL issued for the column will include database-specific
  795. keywords intended to signify this column as an
  796. "autoincrement" column, such as AUTO INCREMENT on MySQL,
  797. SERIAL on PostgreSQL, and IDENTITY on MS-SQL. It does
  798. *not* issue AUTOINCREMENT for SQLite since this is a
  799. special SQLite flag that is not required for autoincrementing
  800. behavior.
  801. .. seealso::
  802. :ref:`sqlite_autoincrement`
  803. * The column will be considered to be available using an
  804. "autoincrement" method specific to the backend database, such
  805. as calling upon ``cursor.lastrowid``, using RETURNING in an
  806. INSERT statement to get at a sequence-generated value, or using
  807. special functions such as "SELECT scope_identity()".
  808. These methods are highly specific to the DBAPIs and databases in
  809. use and vary greatly, so care should be taken when associating
  810. ``autoincrement=True`` with a custom default generation function.
  811. :param default: A scalar, Python callable, or
  812. :class:`.ColumnElement` expression representing the
  813. *default value* for this column, which will be invoked upon insert
  814. if this column is otherwise not specified in the VALUES clause of
  815. the insert. This is a shortcut to using :class:`.ColumnDefault` as
  816. a positional argument; see that class for full detail on the
  817. structure of the argument.
  818. Contrast this argument to :paramref:`.Column.server_default`
  819. which creates a default generator on the database side.
  820. .. seealso::
  821. :ref:`metadata_defaults_toplevel`
  822. :param doc: optional String that can be used by the ORM or similar
  823. to document attributes. This attribute does not render SQL
  824. comments (a future attribute 'comment' will achieve that).
  825. :param key: An optional string identifier which will identify this
  826. ``Column`` object on the :class:`.Table`. When a key is provided,
  827. this is the only identifier referencing the ``Column`` within the
  828. application, including ORM attribute mapping; the ``name`` field
  829. is used only when rendering SQL.
  830. :param index: When ``True``, indicates that the column is indexed.
  831. This is a shortcut for using a :class:`.Index` construct on the
  832. table. To specify indexes with explicit names or indexes that
  833. contain multiple columns, use the :class:`.Index` construct
  834. instead.
  835. :param info: Optional data dictionary which will be populated into the
  836. :attr:`.SchemaItem.info` attribute of this object.
  837. :param nullable: When set to ``False``, will cause the "NOT NULL"
  838. phrase to be added when generating DDL for the column. When
  839. ``True``, will normally generate nothing (in SQL this defaults to
  840. "NULL"), except in some very specific backend-specific edge cases
  841. where "NULL" may render explicitly. Defaults to ``True`` unless
  842. :paramref:`~.Column.primary_key` is also ``True``, in which case it
  843. defaults to ``False``. This parameter is only used when issuing
  844. CREATE TABLE statements.
  845. :param onupdate: A scalar, Python callable, or
  846. :class:`~sqlalchemy.sql.expression.ClauseElement` representing a
  847. default value to be applied to the column within UPDATE
  848. statements, which wil be invoked upon update if this column is not
  849. present in the SET clause of the update. This is a shortcut to
  850. using :class:`.ColumnDefault` as a positional argument with
  851. ``for_update=True``.
  852. .. seealso::
  853. :ref:`metadata_defaults` - complete discussion of onupdate
  854. :param primary_key: If ``True``, marks this column as a primary key
  855. column. Multiple columns can have this flag set to specify
  856. composite primary keys. As an alternative, the primary key of a
  857. :class:`.Table` can be specified via an explicit
  858. :class:`.PrimaryKeyConstraint` object.
  859. :param server_default: A :class:`.FetchedValue` instance, str, Unicode
  860. or :func:`~sqlalchemy.sql.expression.text` construct representing
  861. the DDL DEFAULT value for the column.
  862. String types will be emitted as-is, surrounded by single quotes::
  863. Column('x', Text, server_default="val")
  864. x TEXT DEFAULT 'val'
  865. A :func:`~sqlalchemy.sql.expression.text` expression will be
  866. rendered as-is, without quotes::
  867. Column('y', DateTime, server_default=text('NOW()'))
  868. y DATETIME DEFAULT NOW()
  869. Strings and text() will be converted into a
  870. :class:`.DefaultClause` object upon initialization.
  871. Use :class:`.FetchedValue` to indicate that an already-existing
  872. column will generate a default value on the database side which
  873. will be available to SQLAlchemy for post-fetch after inserts. This
  874. construct does not specify any DDL and the implementation is left
  875. to the database, such as via a trigger.
  876. .. seealso::
  877. :ref:`server_defaults` - complete discussion of server side
  878. defaults
  879. :param server_onupdate: A :class:`.FetchedValue` instance
  880. representing a database-side default generation function,
  881. such as a trigger. This
  882. indicates to SQLAlchemy that a newly generated value will be
  883. available after updates. This construct does not actually
  884. implement any kind of generation function within the database,
  885. which instead must be specified separately.
  886. .. seealso::
  887. :ref:`triggered_columns`
  888. :param quote: Force quoting of this column's name on or off,
  889. corresponding to ``True`` or ``False``. When left at its default
  890. of ``None``, the column identifier will be quoted according to
  891. whether the name is case sensitive (identifiers with at least one
  892. upper case character are treated as case sensitive), or if it's a
  893. reserved word. This flag is only needed to force quoting of a
  894. reserved word which is not known by the SQLAlchemy dialect.
  895. :param unique: When ``True``, indicates that this column contains a
  896. unique constraint, or if ``index`` is ``True`` as well, indicates
  897. that the :class:`.Index` should be created with the unique flag.
  898. To specify multiple columns in the constraint/index or to specify
  899. an explicit name, use the :class:`.UniqueConstraint` or
  900. :class:`.Index` constructs explicitly.
  901. :param system: When ``True``, indicates this is a "system" column,
  902. that is a column which is automatically made available by the
  903. database, and should not be included in the columns list for a
  904. ``CREATE TABLE`` statement.
  905. For more elaborate scenarios where columns should be
  906. conditionally rendered differently on different backends,
  907. consider custom compilation rules for :class:`.CreateColumn`.
  908. .. versionadded:: 0.8.3 Added the ``system=True`` parameter to
  909. :class:`.Column`.
  910. """
  911. name = kwargs.pop('name', None)
  912. type_ = kwargs.pop('type_', None)
  913. args = list(args)
  914. if args:
  915. if isinstance(args[0], util.string_types):
  916. if name is not None:
  917. raise exc.ArgumentError(
  918. "May not pass name positionally and as a keyword.")
  919. name = args.pop(0)
  920. if args:
  921. coltype = args[0]
  922. if hasattr(coltype, "_sqla_type"):
  923. if type_ is not None:
  924. raise exc.ArgumentError(
  925. "May not pass type_ positionally and as a keyword.")
  926. type_ = args.pop(0)
  927. if name is not None:
  928. name = quoted_name(name, kwargs.pop('quote', None))
  929. elif "quote" in kwargs:
  930. raise exc.ArgumentError("Explicit 'name' is required when "
  931. "sending 'quote' argument")
  932. super(Column, self).__init__(name, type_)
  933. self.key = kwargs.pop('key', name)
  934. self.primary_key = kwargs.pop('primary_key', False)
  935. self.nullable = kwargs.pop('nullable', not self.primary_key)
  936. self.default = kwargs.pop('default', None)
  937. self.server_default = kwargs.pop('server_default', None)
  938. self.server_onupdate = kwargs.pop('server_onupdate', None)
  939. # these default to None because .index and .unique is *not*
  940. # an informational flag about Column - there can still be an
  941. # Index or UniqueConstraint referring to this Column.
  942. self.index = kwargs.pop('index', None)
  943. self.unique = kwargs.pop('unique', None)
  944. self.system = kwargs.pop('system', False)
  945. self.doc = kwargs.pop('doc', None)
  946. self.onupdate = kwargs.pop('onupdate', None)
  947. self.autoincrement = kwargs.pop('autoincrement', "auto")
  948. self.constraints = set()
  949. self.foreign_keys = set()
  950. # check if this Column is proxying another column
  951. if '_proxies' in kwargs:
  952. self._proxies = kwargs.pop('_proxies')
  953. # otherwise, add DDL-related events
  954. elif isinstance(self.type, SchemaEventTarget):
  955. self.type._set_parent_with_dispatch(self)
  956. if self.default is not None:
  957. if isinstance(self.default, (ColumnDefault, Sequence)):
  958. args.append(self.default)
  959. else:
  960. if getattr(self.type, '_warn_on_bytestring', False):
  961. if isinstance(self.default, util.binary_type):
  962. util.warn(
  963. "Unicode column '%s' has non-unicode "
  964. "default value %r specified." % (
  965. self.key,
  966. self.default
  967. ))
  968. args.append(ColumnDefault(self.default))
  969. if self.server_default is not None:
  970. if isinstance(self.server_default, FetchedValue):
  971. args.append(self.server_default._as_for_update(False))
  972. else:
  973. args.append(DefaultClause(self.server_default))
  974. if self.onupdate is not None:
  975. if isinstance(self.onupdate, (ColumnDefault, Sequence)):
  976. args.append(self.onupdate)
  977. else:
  978. args.append(ColumnDefault(self.onupdate, for_update=True))
  979. if self.server_onupdate is not None:
  980. if isinstance(self.server_onupdate, FetchedValue):
  981. args.append(self.server_onupdate._as_for_update(True))
  982. else:
  983. args.append(DefaultClause(self.server_onupdate,
  984. for_update=True))
  985. self._init_items(*args)
  986. util.set_creation_order(self)
  987. if 'info' in kwargs:
  988. self.info = kwargs.pop('info')
  989. if kwargs:
  990. raise exc.ArgumentError(
  991. "Unknown arguments passed to Column: " + repr(list(kwargs)))
  992. # @property
  993. # def quote(self):
  994. # return getattr(self.name, "quote", None)
  995. def __str__(self):
  996. if self.name is None:
  997. return "(no name)"
  998. elif self.table is not None:
  999. if self.table.named_with_column:
  1000. return (self.table.description + "." + self.description)
  1001. else:
  1002. return self.description
  1003. else:
  1004. return self.description
  1005. def references(self, column):
  1006. """Return True if this Column references the given column via foreign
  1007. key."""
  1008. for fk in self.foreign_keys:
  1009. if fk.column.proxy_set.intersection(column.proxy_set):
  1010. return True
  1011. else:
  1012. return False
  1013. def append_foreign_key(self, fk):
  1014. fk._set_parent_with_dispatch(self)
  1015. def __repr__(self):
  1016. kwarg = []
  1017. if self.key != self.name:
  1018. kwarg.append('key')
  1019. if self.primary_key:
  1020. kwarg.append('primary_key')
  1021. if not self.nullable:
  1022. kwarg.append('nullable')
  1023. if self.onupdate:
  1024. kwarg.append('onupdate')
  1025. if self.default:
  1026. kwarg.append('default')
  1027. if self.server_default:
  1028. kwarg.append('server_default')
  1029. return "Column(%s)" % ', '.join(
  1030. [repr(self.name)] + [repr(self.type)] +
  1031. [repr(x) for x in self.foreign_keys if x is not None] +
  1032. [repr(x) for x in self.constraints] +
  1033. [(self.table is not None and "table=<%s>" %
  1034. self.table.description or "table=None")] +
  1035. ["%s=%s" % (k, repr(getattr(self, k))) for k in kwarg])
  1036. def _set_parent(self, table):
  1037. if not self.name:
  1038. raise exc.ArgumentError(
  1039. "Column must be constructed with a non-blank name or "
  1040. "assign a non-blank .name before adding to a Table.")
  1041. if self.key is None:
  1042. self.key = self.name
  1043. existing = getattr(self, 'table', None)
  1044. if existing is not None and existing is not table:
  1045. raise exc.ArgumentError(
  1046. "Column object '%s' already assigned to Table '%s'" % (
  1047. self.key,
  1048. existing.description
  1049. ))
  1050. if self.key in table._columns:
  1051. col = table._columns.get(self.key)
  1052. if col is not self:
  1053. for fk in col.foreign_keys:
  1054. table.foreign_keys.remove(fk)
  1055. if fk.constraint in table.constraints:
  1056. # this might have been removed
  1057. # already, if it's a composite constraint
  1058. # and more than one col being replaced
  1059. table.constraints.remove(fk.constraint)
  1060. table._columns.replace(self)
  1061. if self.primary_key:
  1062. table.primary_key._replace(self)
  1063. elif self.key in table.primary_key:
  1064. raise exc.ArgumentError(
  1065. "Trying to redefine primary-key column '%s' as a "
  1066. "non-primary-key column on table '%s'" % (
  1067. self.key, table.fullname))
  1068. self.table = table
  1069. if self.index:
  1070. if isinstance(self.index, util.string_types):
  1071. raise exc.ArgumentError(
  1072. "The 'index' keyword argument on Column is boolean only. "
  1073. "To create indexes with a specific name, create an "
  1074. "explicit Index object external to the Table.")
  1075. Index(None, self, unique=bool(self.unique))
  1076. elif self.unique:
  1077. if isinstance(self.unique, util.string_types):
  1078. raise exc.ArgumentError(
  1079. "The 'unique' keyword argument on Column is boolean "
  1080. "only. To create unique constraints or indexes with a "
  1081. "specific name, append an explicit UniqueConstraint to "
  1082. "the Table's list of elements, or create an explicit "
  1083. "Index object external to the Table.")
  1084. table.append_constraint(UniqueConstraint(self.key))
  1085. self._setup_on_memoized_fks(lambda fk: fk._set_remote_table(table))
  1086. def _setup_on_memoized_fks(self, fn):
  1087. fk_keys = [
  1088. ((self.table.key, self.key), False),
  1089. ((self.table.key, self.name), True),
  1090. ]
  1091. for fk_key, link_to_name in fk_keys:
  1092. if fk_key in self.table.metadata._fk_memos:
  1093. for fk in self.table.metadata._fk_memos[fk_key]:
  1094. if fk.link_to_name is link_to_name:
  1095. fn(fk)
  1096. def _on_table_attach(self, fn):
  1097. if self.table is not None:
  1098. fn(self, self.table)
  1099. else:
  1100. event.listen(self, 'after_parent_attach', fn)
  1101. def copy(self, **kw):
  1102. """Create a copy of this ``Column``, unitialized.
  1103. This is used in ``Table.tometadata``.
  1104. """
  1105. # Constraint objects plus non-constraint-bound ForeignKey objects
  1106. args = \
  1107. [c.copy(**kw) for c in self.constraints if not c._type_bound] + \
  1108. [c.copy(**kw) for c in self.foreign_keys if not c.constraint]
  1109. type_ = self.type
  1110. if isinstance(type_, SchemaEventTarget):
  1111. type_ = type_.copy(**kw)
  1112. c = self._constructor(
  1113. name=self.name,
  1114. type_=type_,
  1115. key=self.key,
  1116. primary_key=self.primary_key,
  1117. nullable=self.nullable,
  1118. unique=self.unique,
  1119. system=self.system,
  1120. # quote=self.quote,
  1121. index=self.index,
  1122. autoincrement=self.autoincrement,
  1123. default=self.default,
  1124. server_default=self.server_default,
  1125. onupdate=self.onupdate,
  1126. server_onupdate=self.server_onupdate,
  1127. doc=self.doc,
  1128. *args
  1129. )
  1130. return self._schema_item_copy(c)
  1131. def _make_proxy(self, selectable, name=None, key=None,
  1132. name_is_truncatable=False, **kw):
  1133. """Create a *proxy* for this column.
  1134. This is a copy of this ``Column`` referenced by a different parent
  1135. (such as an alias or select statement). The column should
  1136. be used only in select scenarios, as its full DDL/default
  1137. information is not transferred.
  1138. """
  1139. fk = [ForeignKey(f.column, _constraint=f.constraint)
  1140. for f in self.foreign_keys]
  1141. if name is None and self.name is None:
  1142. raise exc.InvalidRequestError(
  1143. "Cannot initialize a sub-selectable"
  1144. " with this Column object until its 'name' has "
  1145. "been assigned.")
  1146. try:
  1147. c = self._constructor(
  1148. _as_truncated(name or self.name) if
  1149. name_is_truncatable else (name or self.name),
  1150. self.type,
  1151. key=key if key else name if name else self.key,
  1152. primary_key=self.primary_key,
  1153. nullable=self.nullable,
  1154. _proxies=[self], *fk)
  1155. except TypeError:
  1156. util.raise_from_cause(
  1157. TypeError(
  1158. "Could not create a copy of this %r object. "
  1159. "Ensure the class includes a _constructor() "
  1160. "attribute or method which accepts the "
  1161. "standard Column constructor arguments, or "
  1162. "references the Column class itself." % self.__class__)
  1163. )
  1164. c.table = selectable
  1165. selectable._columns.add(c)
  1166. if selectable._is_clone_of is not None:
  1167. c._is_clone_of = selectable._is_clone_of.columns[c.key]
  1168. if self.primary_key:
  1169. selectable.primary_key.add(c)
  1170. c.dispatch.after_parent_attach(c, selectable)
  1171. return c
  1172. def get_children(self, schema_visitor=False, **kwargs):
  1173. if schema_visitor:
  1174. return [x for x in (self.default, self.onupdate)
  1175. if x is not None] + \
  1176. list(self.foreign_keys) + list(self.constraints)
  1177. else:
  1178. return ColumnClause.get_children(self, **kwargs)
  1179. class ForeignKey(DialectKWArgs, SchemaItem):
  1180. """Defines a dependency between two columns.
  1181. ``ForeignKey`` is specified as an argument to a :class:`.Column` object,
  1182. e.g.::
  1183. t = Table("remote_table", metadata,
  1184. Column("remote_id", ForeignKey("main_table.id"))
  1185. )
  1186. Note that ``ForeignKey`` is only a marker object that defines
  1187. a dependency between two columns. The actual constraint
  1188. is in all cases represented by the :class:`.ForeignKeyConstraint`
  1189. object. This object will be generated automatically when
  1190. a ``ForeignKey`` is associated with a :class:`.Column` which
  1191. in turn is associated with a :class:`.Table`. Conversely,
  1192. when :class:`.ForeignKeyConstraint` is applied to a :class:`.Table`,
  1193. ``ForeignKey`` markers are automatically generated to be
  1194. present on each associated :class:`.Column`, which are also
  1195. associated with the constraint object.
  1196. Note that you cannot define a "composite" foreign key constraint,
  1197. that is a constraint between a grouping of multiple parent/child
  1198. columns, using ``ForeignKey`` objects. To define this grouping,
  1199. the :class:`.ForeignKeyConstraint` object must be used, and applied
  1200. to the :class:`.Table`. The associated ``ForeignKey`` objects
  1201. are created automatically.
  1202. The ``ForeignKey`` objects associated with an individual
  1203. :class:`.Column` object are available in the `foreign_keys` collection
  1204. of that column.
  1205. Further examples of foreign key configuration are in
  1206. :ref:`metadata_foreignkeys`.
  1207. """
  1208. __visit_name__ = 'foreign_key'
  1209. def __init__(self, column, _constraint=None, use_alter=False, name=None,
  1210. onupdate=None, ondelete=None, deferrable=None,
  1211. initially=None, link_to_name=False, match=None,
  1212. info=None,
  1213. **dialect_kw):
  1214. r"""
  1215. Construct a column-level FOREIGN KEY.
  1216. The :class:`.ForeignKey` object when constructed generates a
  1217. :class:`.ForeignKeyConstraint` which is associated with the parent
  1218. :class:`.Table` object's collection of constraints.
  1219. :param column: A single target column for the key relationship. A
  1220. :class:`.Column` object or a column name as a string:
  1221. ``tablename.columnkey`` or ``schema.tablename.columnkey``.
  1222. ``columnkey`` is the ``key`` which has been assigned to the column
  1223. (defaults to the column name itself), unless ``link_to_name`` is
  1224. ``True`` in which case the rendered name of the column is used.
  1225. .. versionadded:: 0.7.4
  1226. Note that if the schema name is not included, and the
  1227. underlying :class:`.MetaData` has a "schema", that value will
  1228. be used.
  1229. :param name: Optional string. An in-database name for the key if
  1230. `constraint` is not provided.
  1231. :param onupdate: Optional string. If set, emit ON UPDATE <value> when
  1232. issuing DDL for this constraint. Typical values include CASCADE,
  1233. DELETE and RESTRICT.
  1234. :param ondelete: Optional string. If set, emit ON DELETE <value> when
  1235. issuing DDL for this constraint. Typical values include CASCADE,
  1236. DELETE and RESTRICT.
  1237. :param deferrable: Optional bool. If set, emit DEFERRABLE or NOT
  1238. DEFERRABLE when issuing DDL for this constraint.
  1239. :param initially: Optional string. If set, emit INITIALLY <value> when
  1240. issuing DDL for this constraint.
  1241. :param link_to_name: if True, the string name given in ``column`` is
  1242. the rendered name of the referenced column, not its locally
  1243. assigned ``key``.
  1244. :param use_alter: passed to the underlying
  1245. :class:`.ForeignKeyConstraint` to indicate the constraint should
  1246. be generated/dropped externally from the CREATE TABLE/ DROP TABLE
  1247. statement. See :paramref:`.ForeignKeyConstraint.use_alter`
  1248. for further description.
  1249. .. seealso::
  1250. :paramref:`.ForeignKeyConstraint.use_alter`
  1251. :ref:`use_alter`
  1252. :param match: Optional string. If set, emit MATCH <value> when issuing
  1253. DDL for this constraint. Typical values include SIMPLE, PARTIAL
  1254. and FULL.
  1255. :param info: Optional data dictionary which will be populated into the
  1256. :attr:`.SchemaItem.info` attribute of this object.
  1257. .. versionadded:: 1.0.0
  1258. :param \**dialect_kw: Additional keyword arguments are dialect
  1259. specific, and passed in the form ``<dialectname>_<argname>``. The
  1260. arguments are ultimately handled by a corresponding
  1261. :class:`.ForeignKeyConstraint`. See the documentation regarding
  1262. an individual dialect at :ref:`dialect_toplevel` for detail on
  1263. documented arguments.
  1264. .. versionadded:: 0.9.2
  1265. """
  1266. self._colspec = column
  1267. if isinstance(self._colspec, util.string_types):
  1268. self._table_column = None
  1269. else:
  1270. if hasattr(self._colspec, '__clause_element__'):
  1271. self._table_column = self._colspec.__clause_element__()
  1272. else:
  1273. self._table_column = self._colspec
  1274. if not isinstance(self._table_column, ColumnClause):
  1275. raise exc.ArgumentError(
  1276. "String, Column, or Column-bound argument "
  1277. "expected, got %r" % self._table_column)
  1278. elif not isinstance(
  1279. self._table_column.table, (util.NoneType, TableClause)):
  1280. raise exc.ArgumentError(
  1281. "ForeignKey received Column not bound "
  1282. "to a Table, got: %r" % self._table_column.table
  1283. )
  1284. # the linked ForeignKeyConstraint.
  1285. # ForeignKey will create this when parent Column
  1286. # is attached to a Table, *or* ForeignKeyConstraint
  1287. # object passes itself in when creating ForeignKey
  1288. # markers.
  1289. self.constraint = _constraint
  1290. self.parent = None
  1291. self.use_alter = use_alter
  1292. self.name = name
  1293. self.onupdate = onupdate
  1294. self.ondelete = ondelete
  1295. self.deferrable = deferrable
  1296. self.initially = initially
  1297. self.link_to_name = link_to_name
  1298. self.match = match
  1299. if info:
  1300. self.info = info
  1301. self._unvalidated_dialect_kw = dialect_kw
  1302. def __repr__(self):
  1303. return "ForeignKey(%r)" % self._get_colspec()
  1304. def copy(self, schema=None):
  1305. """Produce a copy of this :class:`.ForeignKey` object.
  1306. The new :class:`.ForeignKey` will not be bound
  1307. to any :class:`.Column`.
  1308. This method is usually used by the internal
  1309. copy procedures of :class:`.Column`, :class:`.Table`,
  1310. and :class:`.MetaData`.
  1311. :param schema: The returned :class:`.ForeignKey` will
  1312. reference the original table and column name, qualified
  1313. by the given string schema name.
  1314. """
  1315. fk = ForeignKey(
  1316. self._get_colspec(schema=schema),
  1317. use_alter=self.use_alter,
  1318. name=self.name,
  1319. onupdate=self.onupdate,
  1320. ondelete=self.ondelete,
  1321. deferrable=self.deferrable,
  1322. initially=self.initially,
  1323. link_to_name=self.link_to_name,
  1324. match=self.match,
  1325. **self._unvalidated_dialect_kw
  1326. )
  1327. return self._schema_item_copy(fk)
  1328. def _get_colspec(self, schema=None, table_name=None):
  1329. """Return a string based 'column specification' for this
  1330. :class:`.ForeignKey`.
  1331. This is usually the equivalent of the string-based "tablename.colname"
  1332. argument first passed to the object's constructor.
  1333. """
  1334. if schema:
  1335. _schema, tname, colname = self._column_tokens
  1336. if table_name is not None:
  1337. tname = table_name
  1338. return "%s.%s.%s" % (schema, tname, colname)
  1339. elif table_name:
  1340. schema, tname, colname = self._column_tokens
  1341. if schema:
  1342. return "%s.%s.%s" % (schema, table_name, colname)
  1343. else:
  1344. return "%s.%s" % (table_name, colname)
  1345. elif self._table_column is not None:
  1346. return "%s.%s" % (
  1347. self._table_column.table.fullname, self._table_column.key)
  1348. else:
  1349. return self._colspec
  1350. @property
  1351. def _referred_schema(self):
  1352. return self._column_tokens[0]
  1353. def _table_key(self):
  1354. if self._table_column is not None:
  1355. if self._table_column.table is None:
  1356. return None
  1357. else:
  1358. return self._table_column.table.key
  1359. else:
  1360. schema, tname, colname = self._column_tokens
  1361. return _get_table_key(tname, schema)
  1362. target_fullname = property(_get_colspec)
  1363. def references(self, table):
  1364. """Return True if the given :class:`.Table` is referenced by this
  1365. :class:`.ForeignKey`."""
  1366. return table.corresponding_column(self.column) is not None
  1367. def get_referent(self, table):
  1368. """Return the :class:`.Column` in the given :class:`.Table`
  1369. referenced by this :class:`.ForeignKey`.
  1370. Returns None if this :class:`.ForeignKey` does not reference the given
  1371. :class:`.Table`.
  1372. """
  1373. return table.corresponding_column(self.column)
  1374. @util.memoized_property
  1375. def _column_tokens(self):
  1376. """parse a string-based _colspec into its component parts."""
  1377. m = self._get_colspec().split('.')
  1378. if m is None:
  1379. raise exc.ArgumentError(
  1380. "Invalid foreign key column specification: %s" %
  1381. self._colspec)
  1382. if (len(m) == 1):
  1383. tname = m.pop()
  1384. colname = None
  1385. else:
  1386. colname = m.pop()
  1387. tname = m.pop()
  1388. # A FK between column 'bar' and table 'foo' can be
  1389. # specified as 'foo', 'foo.bar', 'dbo.foo.bar',
  1390. # 'otherdb.dbo.foo.bar'. Once we have the column name and
  1391. # the table name, treat everything else as the schema
  1392. # name. Some databases (e.g. Sybase) support
  1393. # inter-database foreign keys. See tickets#1341 and --
  1394. # indirectly related -- Ticket #594. This assumes that '.'
  1395. # will never appear *within* any component of the FK.
  1396. if (len(m) > 0):
  1397. schema = '.'.join(m)
  1398. else:
  1399. schema = None
  1400. return schema, tname, colname
  1401. def _resolve_col_tokens(self):
  1402. if self.parent is None:
  1403. raise exc.InvalidRequestError(
  1404. "this ForeignKey object does not yet have a "
  1405. "parent Column associated with it.")
  1406. elif self.parent.table is None:
  1407. raise exc.InvalidRequestError(
  1408. "this ForeignKey's parent column is not yet associated "
  1409. "with a Table.")
  1410. parenttable = self.parent.table
  1411. # assertion, can be commented out.
  1412. # basically Column._make_proxy() sends the actual
  1413. # target Column to the ForeignKey object, so the
  1414. # string resolution here is never called.
  1415. for c in self.parent.base_columns:
  1416. if isinstance(c, Column):
  1417. assert c.table is parenttable
  1418. break
  1419. else:
  1420. assert False
  1421. ######################
  1422. schema, tname, colname = self._column_tokens
  1423. if schema is None and parenttable.metadata.schema is not None:
  1424. schema = parenttable.metadata.schema
  1425. tablekey = _get_table_key(tname, schema)
  1426. return parenttable, tablekey, colname
  1427. def _link_to_col_by_colstring(self, parenttable, table, colname):
  1428. if not hasattr(self.constraint, '_referred_table'):
  1429. self.constraint._referred_table = table
  1430. else:
  1431. assert self.constraint._referred_table is table
  1432. _column = None
  1433. if colname is None:
  1434. # colname is None in the case that ForeignKey argument
  1435. # was specified as table name only, in which case we
  1436. # match the column name to the same column on the
  1437. # parent.
  1438. key = self.parent
  1439. _column = table.c.get(self.parent.key, None)
  1440. elif self.link_to_name:
  1441. key = colname
  1442. for c in table.c:
  1443. if c.name == colname:
  1444. _column = c
  1445. else:
  1446. key = colname
  1447. _column = table.c.get(colname, None)
  1448. if _column is None:
  1449. raise exc.NoReferencedColumnError(
  1450. "Could not initialize target column "
  1451. "for ForeignKey '%s' on table '%s': "
  1452. "table '%s' has no column named '%s'" %
  1453. (self._colspec, parenttable.name, table.name, key),
  1454. table.name, key)
  1455. self._set_target_column(_column)
  1456. def _set_target_column(self, column):
  1457. # propagate TypeEngine to parent if it didn't have one
  1458. if self.parent.type._isnull:
  1459. self.parent.type = column.type
  1460. # super-edgy case, if other FKs point to our column,
  1461. # they'd get the type propagated out also.
  1462. if isinstance(self.parent.table, Table):
  1463. def set_type(fk):
  1464. if fk.parent.type._isnull:
  1465. fk.parent.type = column.type
  1466. self.parent._setup_on_memoized_fks(set_type)
  1467. self.column = column
  1468. @util.memoized_property
  1469. def column(self):
  1470. """Return the target :class:`.Column` referenced by this
  1471. :class:`.ForeignKey`.
  1472. If no target column has been established, an exception
  1473. is raised.
  1474. .. versionchanged:: 0.9.0
  1475. Foreign key target column resolution now occurs as soon as both
  1476. the ForeignKey object and the remote Column to which it refers
  1477. are both associated with the same MetaData object.
  1478. """
  1479. if isinstance(self._colspec, util.string_types):
  1480. parenttable, tablekey, colname = self._resolve_col_tokens()
  1481. if tablekey not in parenttable.metadata:
  1482. raise exc.NoReferencedTableError(
  1483. "Foreign key associated with column '%s' could not find "
  1484. "table '%s' with which to generate a "
  1485. "foreign key to target column '%s'" %
  1486. (self.parent, tablekey, colname),
  1487. tablekey)
  1488. elif parenttable.key not in parenttable.metadata:
  1489. raise exc.InvalidRequestError(
  1490. "Table %s is no longer associated with its "
  1491. "parent MetaData" % parenttable)
  1492. else:
  1493. raise exc.NoReferencedColumnError(
  1494. "Could not initialize target column for "
  1495. "ForeignKey '%s' on table '%s': "
  1496. "table '%s' has no column named '%s'" % (
  1497. self._colspec, parenttable.name, tablekey, colname),
  1498. tablekey, colname)
  1499. elif hasattr(self._colspec, '__clause_element__'):
  1500. _column = self._colspec.__clause_element__()
  1501. return _column
  1502. else:
  1503. _column = self._colspec
  1504. return _column
  1505. def _set_parent(self, column):
  1506. if self.parent is not None and self.parent is not column:
  1507. raise exc.InvalidRequestError(
  1508. "This ForeignKey already has a parent !")
  1509. self.parent = column
  1510. self.parent.foreign_keys.add(self)
  1511. self.parent._on_table_attach(self._set_table)
  1512. def _set_remote_table(self, table):
  1513. parenttable, tablekey, colname = self._resolve_col_tokens()
  1514. self._link_to_col_by_colstring(parenttable, table, colname)
  1515. self.constraint._validate_dest_table(table)
  1516. def _remove_from_metadata(self, metadata):
  1517. parenttable, table_key, colname = self._resolve_col_tokens()
  1518. fk_key = (table_key, colname)
  1519. if self in metadata._fk_memos[fk_key]:
  1520. # TODO: no test coverage for self not in memos
  1521. metadata._fk_memos[fk_key].remove(self)
  1522. def _set_table(self, column, table):
  1523. # standalone ForeignKey - create ForeignKeyConstraint
  1524. # on the hosting Table when attached to the Table.
  1525. if self.constraint is None and isinstance(table, Table):
  1526. self.constraint = ForeignKeyConstraint(
  1527. [], [], use_alter=self.use_alter, name=self.name,
  1528. onupdate=self.onupdate, ondelete=self.ondelete,
  1529. deferrable=self.deferrable, initially=self.initially,
  1530. match=self.match,
  1531. **self._unvalidated_dialect_kw
  1532. )
  1533. self.constraint._append_element(column, self)
  1534. self.constraint._set_parent_with_dispatch(table)
  1535. table.foreign_keys.add(self)
  1536. # set up remote ".column" attribute, or a note to pick it
  1537. # up when the other Table/Column shows up
  1538. if isinstance(self._colspec, util.string_types):
  1539. parenttable, table_key, colname = self._resolve_col_tokens()
  1540. fk_key = (table_key, colname)
  1541. if table_key in parenttable.metadata.tables:
  1542. table = parenttable.metadata.tables[table_key]
  1543. try:
  1544. self._link_to_col_by_colstring(
  1545. parenttable, table, colname)
  1546. except exc.NoReferencedColumnError:
  1547. # this is OK, we'll try later
  1548. pass
  1549. parenttable.metadata._fk_memos[fk_key].append(self)
  1550. elif hasattr(self._colspec, '__clause_element__'):
  1551. _column = self._colspec.__clause_element__()
  1552. self._set_target_column(_column)
  1553. else:
  1554. _column = self._colspec
  1555. self._set_target_column(_column)
  1556. class _NotAColumnExpr(object):
  1557. def _not_a_column_expr(self):
  1558. raise exc.InvalidRequestError(
  1559. "This %s cannot be used directly "
  1560. "as a column expression." % self.__class__.__name__)
  1561. __clause_element__ = self_group = lambda self: self._not_a_column_expr()
  1562. _from_objects = property(lambda self: self._not_a_column_expr())
  1563. class DefaultGenerator(_NotAColumnExpr, SchemaItem):
  1564. """Base class for column *default* values."""
  1565. __visit_name__ = 'default_generator'
  1566. is_sequence = False
  1567. is_server_default = False
  1568. column = None
  1569. def __init__(self, for_update=False):
  1570. self.for_update = for_update
  1571. def _set_parent(self, column):
  1572. self.column = column
  1573. if self.for_update:
  1574. self.column.onupdate = self
  1575. else:
  1576. self.column.default = self
  1577. def execute(self, bind=None, **kwargs):
  1578. if bind is None:
  1579. bind = _bind_or_error(self)
  1580. return bind._execute_default(self, **kwargs)
  1581. def _execute_on_connection(self, connection, multiparams, params):
  1582. return connection._execute_default(self, multiparams, params)
  1583. @property
  1584. def bind(self):
  1585. """Return the connectable associated with this default."""
  1586. if getattr(self, 'column', None) is not None:
  1587. return self.column.table.bind
  1588. else:
  1589. return None
  1590. class ColumnDefault(DefaultGenerator):
  1591. """A plain default value on a column.
  1592. This could correspond to a constant, a callable function,
  1593. or a SQL clause.
  1594. :class:`.ColumnDefault` is generated automatically
  1595. whenever the ``default``, ``onupdate`` arguments of
  1596. :class:`.Column` are used. A :class:`.ColumnDefault`
  1597. can be passed positionally as well.
  1598. For example, the following::
  1599. Column('foo', Integer, default=50)
  1600. Is equivalent to::
  1601. Column('foo', Integer, ColumnDefault(50))
  1602. """
  1603. def __init__(self, arg, **kwargs):
  1604. """"Construct a new :class:`.ColumnDefault`.
  1605. :param arg: argument representing the default value.
  1606. May be one of the following:
  1607. * a plain non-callable Python value, such as a
  1608. string, integer, boolean, or other simple type.
  1609. The default value will be used as is each time.
  1610. * a SQL expression, that is one which derives from
  1611. :class:`.ColumnElement`. The SQL expression will
  1612. be rendered into the INSERT or UPDATE statement,
  1613. or in the case of a primary key column when
  1614. RETURNING is not used may be
  1615. pre-executed before an INSERT within a SELECT.
  1616. * A Python callable. The function will be invoked for each
  1617. new row subject to an INSERT or UPDATE.
  1618. The callable must accept exactly
  1619. zero or one positional arguments. The one-argument form
  1620. will receive an instance of the :class:`.ExecutionContext`,
  1621. which provides contextual information as to the current
  1622. :class:`.Connection` in use as well as the current
  1623. statement and parameters.
  1624. """
  1625. super(ColumnDefault, self).__init__(**kwargs)
  1626. if isinstance(arg, FetchedValue):
  1627. raise exc.ArgumentError(
  1628. "ColumnDefault may not be a server-side default type.")
  1629. if util.callable(arg):
  1630. arg = self._maybe_wrap_callable(arg)
  1631. self.arg = arg
  1632. @util.memoized_property
  1633. def is_callable(self):
  1634. return util.callable(self.arg)
  1635. @util.memoized_property
  1636. def is_clause_element(self):
  1637. return isinstance(self.arg, ClauseElement)
  1638. @util.memoized_property
  1639. def is_scalar(self):
  1640. return not self.is_callable and \
  1641. not self.is_clause_element and \
  1642. not self.is_sequence
  1643. def _maybe_wrap_callable(self, fn):
  1644. """Wrap callables that don't accept a context.
  1645. This is to allow easy compatibility with default callables
  1646. that aren't specific to accepting of a context.
  1647. """
  1648. try:
  1649. argspec = util.get_callable_argspec(fn, no_self=True)
  1650. except TypeError:
  1651. return util.wrap_callable(lambda ctx: fn(), fn)
  1652. defaulted = argspec[3] is not None and len(argspec[3]) or 0
  1653. positionals = len(argspec[0]) - defaulted
  1654. if positionals == 0:
  1655. return util.wrap_callable(lambda ctx: fn(), fn)
  1656. elif positionals == 1:
  1657. return fn
  1658. else:
  1659. raise exc.ArgumentError(
  1660. "ColumnDefault Python function takes zero or one "
  1661. "positional arguments")
  1662. def _visit_name(self):
  1663. if self.for_update:
  1664. return "column_onupdate"
  1665. else:
  1666. return "column_default"
  1667. __visit_name__ = property(_visit_name)
  1668. def __repr__(self):
  1669. return "ColumnDefault(%r)" % self.arg
  1670. class Sequence(DefaultGenerator):
  1671. """Represents a named database sequence.
  1672. The :class:`.Sequence` object represents the name and configurational
  1673. parameters of a database sequence. It also represents
  1674. a construct that can be "executed" by a SQLAlchemy :class:`.Engine`
  1675. or :class:`.Connection`, rendering the appropriate "next value" function
  1676. for the target database and returning a result.
  1677. The :class:`.Sequence` is typically associated with a primary key column::
  1678. some_table = Table(
  1679. 'some_table', metadata,
  1680. Column('id', Integer, Sequence('some_table_seq'),
  1681. primary_key=True)
  1682. )
  1683. When CREATE TABLE is emitted for the above :class:`.Table`, if the
  1684. target platform supports sequences, a CREATE SEQUENCE statement will
  1685. be emitted as well. For platforms that don't support sequences,
  1686. the :class:`.Sequence` construct is ignored.
  1687. .. seealso::
  1688. :class:`.CreateSequence`
  1689. :class:`.DropSequence`
  1690. """
  1691. __visit_name__ = 'sequence'
  1692. is_sequence = True
  1693. def __init__(self, name, start=None, increment=None, minvalue=None,
  1694. maxvalue=None, nominvalue=None, nomaxvalue=None, cycle=None,
  1695. schema=None, optional=False, quote=None, metadata=None,
  1696. quote_schema=None,
  1697. for_update=False):
  1698. """Construct a :class:`.Sequence` object.
  1699. :param name: The name of the sequence.
  1700. :param start: the starting index of the sequence. This value is
  1701. used when the CREATE SEQUENCE command is emitted to the database
  1702. as the value of the "START WITH" clause. If ``None``, the
  1703. clause is omitted, which on most platforms indicates a starting
  1704. value of 1.
  1705. :param increment: the increment value of the sequence. This
  1706. value is used when the CREATE SEQUENCE command is emitted to
  1707. the database as the value of the "INCREMENT BY" clause. If ``None``,
  1708. the clause is omitted, which on most platforms indicates an
  1709. increment of 1.
  1710. :param minvalue: the minimum value of the sequence. This
  1711. value is used when the CREATE SEQUENCE command is emitted to
  1712. the database as the value of the "MINVALUE" clause. If ``None``,
  1713. the clause is omitted, which on most platforms indicates a
  1714. minvalue of 1 and -2^63-1 for ascending and descending sequences,
  1715. respectively.
  1716. .. versionadded:: 1.0.7
  1717. :param maxvalue: the maximum value of the sequence. This
  1718. value is used when the CREATE SEQUENCE command is emitted to
  1719. the database as the value of the "MAXVALUE" clause. If ``None``,
  1720. the clause is omitted, which on most platforms indicates a
  1721. maxvalue of 2^63-1 and -1 for ascending and descending sequences,
  1722. respectively.
  1723. .. versionadded:: 1.0.7
  1724. :param nominvalue: no minimum value of the sequence. This
  1725. value is used when the CREATE SEQUENCE command is emitted to
  1726. the database as the value of the "NO MINVALUE" clause. If ``None``,
  1727. the clause is omitted, which on most platforms indicates a
  1728. minvalue of 1 and -2^63-1 for ascending and descending sequences,
  1729. respectively.
  1730. .. versionadded:: 1.0.7
  1731. :param nomaxvalue: no maximum value of the sequence. This
  1732. value is used when the CREATE SEQUENCE command is emitted to
  1733. the database as the value of the "NO MAXVALUE" clause. If ``None``,
  1734. the clause is omitted, which on most platforms indicates a
  1735. maxvalue of 2^63-1 and -1 for ascending and descending sequences,
  1736. respectively.
  1737. .. versionadded:: 1.0.7
  1738. :param cycle: allows the sequence to wrap around when the maxvalue
  1739. or minvalue has been reached by an ascending or descending sequence
  1740. respectively. This value is used when the CREATE SEQUENCE command
  1741. is emitted to the database as the "CYCLE" clause. If the limit is
  1742. reached, the next number generated will be the minvalue or maxvalue,
  1743. respectively. If cycle=False (the default) any calls to nextval
  1744. after the sequence has reached its maximum value will return an
  1745. error.
  1746. .. versionadded:: 1.0.7
  1747. :param schema: Optional schema name for the sequence, if located
  1748. in a schema other than the default. The rules for selecting the
  1749. schema name when a :class:`.MetaData` is also present are the same
  1750. as that of :paramref:`.Table.schema`.
  1751. :param optional: boolean value, when ``True``, indicates that this
  1752. :class:`.Sequence` object only needs to be explicitly generated
  1753. on backends that don't provide another way to generate primary
  1754. key identifiers. Currently, it essentially means, "don't create
  1755. this sequence on the PostgreSQL backend, where the SERIAL keyword
  1756. creates a sequence for us automatically".
  1757. :param quote: boolean value, when ``True`` or ``False``, explicitly
  1758. forces quoting of the schema name on or off. When left at its
  1759. default of ``None``, normal quoting rules based on casing and
  1760. reserved words take place.
  1761. :param quote_schema: set the quoting preferences for the ``schema``
  1762. name.
  1763. :param metadata: optional :class:`.MetaData` object which this
  1764. :class:`.Sequence` will be associated with. A :class:`.Sequence`
  1765. that is associated with a :class:`.MetaData` gains the following
  1766. capabilities:
  1767. * The :class:`.Sequence` will inherit the :paramref:`.MetaData.schema`
  1768. parameter specified to the target :class:`.MetaData`, which
  1769. affects the production of CREATE / DROP DDL, if any.
  1770. * The :meth:`.Sequence.create` and :meth:`.Sequence.drop` methods
  1771. automatically use the engine bound to the :class:`.MetaData`
  1772. object, if any.
  1773. * The :meth:`.MetaData.create_all` and :meth:`.MetaData.drop_all`
  1774. methods will emit CREATE / DROP for this :class:`.Sequence`,
  1775. even if the :class:`.Sequence` is not associated with any
  1776. :class:`.Table` / :class:`.Column` that's a member of this
  1777. :class:`.MetaData`.
  1778. The above behaviors can only occur if the :class:`.Sequence` is
  1779. explicitly associated with the :class:`.MetaData` via this parameter.
  1780. .. seealso::
  1781. :ref:`sequence_metadata` - full discussion of the
  1782. :paramref:`.Sequence.metadata` parameter.
  1783. :param for_update: Indicates this :class:`.Sequence`, when associated
  1784. with a :class:`.Column`, should be invoked for UPDATE statements
  1785. on that column's table, rather than for INSERT statements, when
  1786. no value is otherwise present for that column in the statement.
  1787. """
  1788. super(Sequence, self).__init__(for_update=for_update)
  1789. self.name = quoted_name(name, quote)
  1790. self.start = start
  1791. self.increment = increment
  1792. self.minvalue = minvalue
  1793. self.maxvalue = maxvalue
  1794. self.nominvalue = nominvalue
  1795. self.nomaxvalue = nomaxvalue
  1796. self.cycle = cycle
  1797. self.optional = optional
  1798. if schema is BLANK_SCHEMA:
  1799. self.schema = schema = None
  1800. elif metadata is not None and schema is None and metadata.schema:
  1801. self.schema = schema = metadata.schema
  1802. else:
  1803. self.schema = quoted_name(schema, quote_schema)
  1804. self.metadata = metadata
  1805. self._key = _get_table_key(name, schema)
  1806. if metadata:
  1807. self._set_metadata(metadata)
  1808. @util.memoized_property
  1809. def is_callable(self):
  1810. return False
  1811. @util.memoized_property
  1812. def is_clause_element(self):
  1813. return False
  1814. @util.dependencies("sqlalchemy.sql.functions.func")
  1815. def next_value(self, func):
  1816. """Return a :class:`.next_value` function element
  1817. which will render the appropriate increment function
  1818. for this :class:`.Sequence` within any SQL expression.
  1819. """
  1820. return func.next_value(self, bind=self.bind)
  1821. def _set_parent(self, column):
  1822. super(Sequence, self)._set_parent(column)
  1823. column._on_table_attach(self._set_table)
  1824. def _set_table(self, column, table):
  1825. self._set_metadata(table.metadata)
  1826. def _set_metadata(self, metadata):
  1827. self.metadata = metadata
  1828. self.metadata._sequences[self._key] = self
  1829. @property
  1830. def bind(self):
  1831. if self.metadata:
  1832. return self.metadata.bind
  1833. else:
  1834. return None
  1835. def create(self, bind=None, checkfirst=True):
  1836. """Creates this sequence in the database."""
  1837. if bind is None:
  1838. bind = _bind_or_error(self)
  1839. bind._run_visitor(ddl.SchemaGenerator,
  1840. self,
  1841. checkfirst=checkfirst)
  1842. def drop(self, bind=None, checkfirst=True):
  1843. """Drops this sequence from the database."""
  1844. if bind is None:
  1845. bind = _bind_or_error(self)
  1846. bind._run_visitor(ddl.SchemaDropper,
  1847. self,
  1848. checkfirst=checkfirst)
  1849. def _not_a_column_expr(self):
  1850. raise exc.InvalidRequestError(
  1851. "This %s cannot be used directly "
  1852. "as a column expression. Use func.next_value(sequence) "
  1853. "to produce a 'next value' function that's usable "
  1854. "as a column element."
  1855. % self.__class__.__name__)
  1856. @inspection._self_inspects
  1857. class FetchedValue(_NotAColumnExpr, SchemaEventTarget):
  1858. """A marker for a transparent database-side default.
  1859. Use :class:`.FetchedValue` when the database is configured
  1860. to provide some automatic default for a column.
  1861. E.g.::
  1862. Column('foo', Integer, FetchedValue())
  1863. Would indicate that some trigger or default generator
  1864. will create a new value for the ``foo`` column during an
  1865. INSERT.
  1866. .. seealso::
  1867. :ref:`triggered_columns`
  1868. """
  1869. is_server_default = True
  1870. reflected = False
  1871. has_argument = False
  1872. def __init__(self, for_update=False):
  1873. self.for_update = for_update
  1874. def _as_for_update(self, for_update):
  1875. if for_update == self.for_update:
  1876. return self
  1877. else:
  1878. return self._clone(for_update)
  1879. def _clone(self, for_update):
  1880. n = self.__class__.__new__(self.__class__)
  1881. n.__dict__.update(self.__dict__)
  1882. n.__dict__.pop('column', None)
  1883. n.for_update = for_update
  1884. return n
  1885. def _set_parent(self, column):
  1886. self.column = column
  1887. if self.for_update:
  1888. self.column.server_onupdate = self
  1889. else:
  1890. self.column.server_default = self
  1891. def __repr__(self):
  1892. return util.generic_repr(self)
  1893. class DefaultClause(FetchedValue):
  1894. """A DDL-specified DEFAULT column value.
  1895. :class:`.DefaultClause` is a :class:`.FetchedValue`
  1896. that also generates a "DEFAULT" clause when
  1897. "CREATE TABLE" is emitted.
  1898. :class:`.DefaultClause` is generated automatically
  1899. whenever the ``server_default``, ``server_onupdate`` arguments of
  1900. :class:`.Column` are used. A :class:`.DefaultClause`
  1901. can be passed positionally as well.
  1902. For example, the following::
  1903. Column('foo', Integer, server_default="50")
  1904. Is equivalent to::
  1905. Column('foo', Integer, DefaultClause("50"))
  1906. """
  1907. has_argument = True
  1908. def __init__(self, arg, for_update=False, _reflected=False):
  1909. util.assert_arg_type(arg, (util.string_types[0],
  1910. ClauseElement,
  1911. TextClause), 'arg')
  1912. super(DefaultClause, self).__init__(for_update)
  1913. self.arg = arg
  1914. self.reflected = _reflected
  1915. def __repr__(self):
  1916. return "DefaultClause(%r, for_update=%r)" % \
  1917. (self.arg, self.for_update)
  1918. class PassiveDefault(DefaultClause):
  1919. """A DDL-specified DEFAULT column value.
  1920. .. deprecated:: 0.6
  1921. :class:`.PassiveDefault` is deprecated.
  1922. Use :class:`.DefaultClause`.
  1923. """
  1924. @util.deprecated("0.6",
  1925. ":class:`.PassiveDefault` is deprecated. "
  1926. "Use :class:`.DefaultClause`.",
  1927. False)
  1928. def __init__(self, *arg, **kw):
  1929. DefaultClause.__init__(self, *arg, **kw)
  1930. class Constraint(DialectKWArgs, SchemaItem):
  1931. """A table-level SQL constraint."""
  1932. __visit_name__ = 'constraint'
  1933. def __init__(self, name=None, deferrable=None, initially=None,
  1934. _create_rule=None, info=None, _type_bound=False,
  1935. **dialect_kw):
  1936. r"""Create a SQL constraint.
  1937. :param name:
  1938. Optional, the in-database name of this ``Constraint``.
  1939. :param deferrable:
  1940. Optional bool. If set, emit DEFERRABLE or NOT DEFERRABLE when
  1941. issuing DDL for this constraint.
  1942. :param initially:
  1943. Optional string. If set, emit INITIALLY <value> when issuing DDL
  1944. for this constraint.
  1945. :param info: Optional data dictionary which will be populated into the
  1946. :attr:`.SchemaItem.info` attribute of this object.
  1947. .. versionadded:: 1.0.0
  1948. :param _create_rule:
  1949. a callable which is passed the DDLCompiler object during
  1950. compilation. Returns True or False to signal inline generation of
  1951. this Constraint.
  1952. The AddConstraint and DropConstraint DDL constructs provide
  1953. DDLElement's more comprehensive "conditional DDL" approach that is
  1954. passed a database connection when DDL is being issued. _create_rule
  1955. is instead called during any CREATE TABLE compilation, where there
  1956. may not be any transaction/connection in progress. However, it
  1957. allows conditional compilation of the constraint even for backends
  1958. which do not support addition of constraints through ALTER TABLE,
  1959. which currently includes SQLite.
  1960. _create_rule is used by some types to create constraints.
  1961. Currently, its call signature is subject to change at any time.
  1962. :param \**dialect_kw: Additional keyword arguments are dialect
  1963. specific, and passed in the form ``<dialectname>_<argname>``. See
  1964. the documentation regarding an individual dialect at
  1965. :ref:`dialect_toplevel` for detail on documented arguments.
  1966. """
  1967. self.name = name
  1968. self.deferrable = deferrable
  1969. self.initially = initially
  1970. if info:
  1971. self.info = info
  1972. self._create_rule = _create_rule
  1973. self._type_bound = _type_bound
  1974. util.set_creation_order(self)
  1975. self._validate_dialect_kwargs(dialect_kw)
  1976. @property
  1977. def table(self):
  1978. try:
  1979. if isinstance(self.parent, Table):
  1980. return self.parent
  1981. except AttributeError:
  1982. pass
  1983. raise exc.InvalidRequestError(
  1984. "This constraint is not bound to a table. Did you "
  1985. "mean to call table.append_constraint(constraint) ?")
  1986. def _set_parent(self, parent):
  1987. self.parent = parent
  1988. parent.constraints.add(self)
  1989. def copy(self, **kw):
  1990. raise NotImplementedError()
  1991. def _to_schema_column(element):
  1992. if hasattr(element, '__clause_element__'):
  1993. element = element.__clause_element__()
  1994. if not isinstance(element, Column):
  1995. raise exc.ArgumentError("schema.Column object expected")
  1996. return element
  1997. def _to_schema_column_or_string(element):
  1998. if hasattr(element, '__clause_element__'):
  1999. element = element.__clause_element__()
  2000. if not isinstance(element, util.string_types + (ColumnElement, )):
  2001. msg = "Element %r is not a string name or column element"
  2002. raise exc.ArgumentError(msg % element)
  2003. return element
  2004. class ColumnCollectionMixin(object):
  2005. columns = None
  2006. """A :class:`.ColumnCollection` of :class:`.Column` objects.
  2007. This collection represents the columns which are referred to by
  2008. this object.
  2009. """
  2010. _allow_multiple_tables = False
  2011. def __init__(self, *columns, **kw):
  2012. _autoattach = kw.pop('_autoattach', True)
  2013. self.columns = ColumnCollection()
  2014. self._pending_colargs = [_to_schema_column_or_string(c)
  2015. for c in columns]
  2016. if _autoattach and self._pending_colargs:
  2017. self._check_attach()
  2018. @classmethod
  2019. def _extract_col_expression_collection(cls, expressions):
  2020. for expr in expressions:
  2021. strname = None
  2022. column = None
  2023. if hasattr(expr, '__clause_element__'):
  2024. expr = expr.__clause_element__()
  2025. if not isinstance(expr, (ColumnElement, TextClause)):
  2026. # this assumes a string
  2027. strname = expr
  2028. else:
  2029. cols = []
  2030. visitors.traverse(expr, {}, {'column': cols.append})
  2031. if cols:
  2032. column = cols[0]
  2033. add_element = column if column is not None else strname
  2034. yield expr, column, strname, add_element
  2035. def _check_attach(self, evt=False):
  2036. col_objs = [
  2037. c for c in self._pending_colargs
  2038. if isinstance(c, Column)
  2039. ]
  2040. cols_w_table = [
  2041. c for c in col_objs if isinstance(c.table, Table)
  2042. ]
  2043. cols_wo_table = set(col_objs).difference(cols_w_table)
  2044. if cols_wo_table:
  2045. # feature #3341 - place event listeners for Column objects
  2046. # such that when all those cols are attached, we autoattach.
  2047. assert not evt, "Should not reach here on event call"
  2048. # issue #3411 - don't do the per-column auto-attach if some of the
  2049. # columns are specified as strings.
  2050. has_string_cols = set(self._pending_colargs).difference(col_objs)
  2051. if not has_string_cols:
  2052. def _col_attached(column, table):
  2053. # this isinstance() corresponds with the
  2054. # isinstance() above; only want to count Table-bound
  2055. # columns
  2056. if isinstance(table, Table):
  2057. cols_wo_table.discard(column)
  2058. if not cols_wo_table:
  2059. self._check_attach(evt=True)
  2060. self._cols_wo_table = cols_wo_table
  2061. for col in cols_wo_table:
  2062. col._on_table_attach(_col_attached)
  2063. return
  2064. columns = cols_w_table
  2065. tables = set([c.table for c in columns])
  2066. if len(tables) == 1:
  2067. self._set_parent_with_dispatch(tables.pop())
  2068. elif len(tables) > 1 and not self._allow_multiple_tables:
  2069. table = columns[0].table
  2070. others = [c for c in columns[1:] if c.table is not table]
  2071. if others:
  2072. raise exc.ArgumentError(
  2073. "Column(s) %s are not part of table '%s'." %
  2074. (", ".join("'%s'" % c for c in others),
  2075. table.description)
  2076. )
  2077. def _set_parent(self, table):
  2078. for col in self._pending_colargs:
  2079. if isinstance(col, util.string_types):
  2080. col = table.c[col]
  2081. self.columns.add(col)
  2082. class ColumnCollectionConstraint(ColumnCollectionMixin, Constraint):
  2083. """A constraint that proxies a ColumnCollection."""
  2084. def __init__(self, *columns, **kw):
  2085. r"""
  2086. :param \*columns:
  2087. A sequence of column names or Column objects.
  2088. :param name:
  2089. Optional, the in-database name of this constraint.
  2090. :param deferrable:
  2091. Optional bool. If set, emit DEFERRABLE or NOT DEFERRABLE when
  2092. issuing DDL for this constraint.
  2093. :param initially:
  2094. Optional string. If set, emit INITIALLY <value> when issuing DDL
  2095. for this constraint.
  2096. :param \**kw: other keyword arguments including dialect-specific
  2097. arguments are propagated to the :class:`.Constraint` superclass.
  2098. """
  2099. _autoattach = kw.pop('_autoattach', True)
  2100. Constraint.__init__(self, **kw)
  2101. ColumnCollectionMixin.__init__(self, *columns, _autoattach=_autoattach)
  2102. columns = None
  2103. """A :class:`.ColumnCollection` representing the set of columns
  2104. for this constraint.
  2105. """
  2106. def _set_parent(self, table):
  2107. Constraint._set_parent(self, table)
  2108. ColumnCollectionMixin._set_parent(self, table)
  2109. def __contains__(self, x):
  2110. return x in self.columns
  2111. def copy(self, **kw):
  2112. c = self.__class__(name=self.name, deferrable=self.deferrable,
  2113. initially=self.initially, *self.columns.keys())
  2114. return self._schema_item_copy(c)
  2115. def contains_column(self, col):
  2116. """Return True if this constraint contains the given column.
  2117. Note that this object also contains an attribute ``.columns``
  2118. which is a :class:`.ColumnCollection` of :class:`.Column` objects.
  2119. """
  2120. return self.columns.contains_column(col)
  2121. def __iter__(self):
  2122. # inlining of
  2123. # return iter(self.columns)
  2124. # ColumnCollection->OrderedProperties->OrderedDict
  2125. ordered_dict = self.columns._data
  2126. return (ordered_dict[key] for key in ordered_dict._list)
  2127. def __len__(self):
  2128. return len(self.columns._data)
  2129. class CheckConstraint(ColumnCollectionConstraint):
  2130. """A table- or column-level CHECK constraint.
  2131. Can be included in the definition of a Table or Column.
  2132. """
  2133. _allow_multiple_tables = True
  2134. def __init__(self, sqltext, name=None, deferrable=None,
  2135. initially=None, table=None, info=None, _create_rule=None,
  2136. _autoattach=True, _type_bound=False):
  2137. r"""Construct a CHECK constraint.
  2138. :param sqltext:
  2139. A string containing the constraint definition, which will be used
  2140. verbatim, or a SQL expression construct. If given as a string,
  2141. the object is converted to a :class:`.Text` object. If the textual
  2142. string includes a colon character, escape this using a backslash::
  2143. CheckConstraint(r"foo ~ E'a(?\:b|c)d")
  2144. :param name:
  2145. Optional, the in-database name of the constraint.
  2146. :param deferrable:
  2147. Optional bool. If set, emit DEFERRABLE or NOT DEFERRABLE when
  2148. issuing DDL for this constraint.
  2149. :param initially:
  2150. Optional string. If set, emit INITIALLY <value> when issuing DDL
  2151. for this constraint.
  2152. :param info: Optional data dictionary which will be populated into the
  2153. :attr:`.SchemaItem.info` attribute of this object.
  2154. .. versionadded:: 1.0.0
  2155. """
  2156. self.sqltext = _literal_as_text(sqltext, warn=False)
  2157. columns = []
  2158. visitors.traverse(self.sqltext, {}, {'column': columns.append})
  2159. super(CheckConstraint, self).\
  2160. __init__(
  2161. name=name, deferrable=deferrable,
  2162. initially=initially, _create_rule=_create_rule, info=info,
  2163. _type_bound=_type_bound, _autoattach=_autoattach,
  2164. *columns)
  2165. if table is not None:
  2166. self._set_parent_with_dispatch(table)
  2167. def __visit_name__(self):
  2168. if isinstance(self.parent, Table):
  2169. return "check_constraint"
  2170. else:
  2171. return "column_check_constraint"
  2172. __visit_name__ = property(__visit_name__)
  2173. def copy(self, target_table=None, **kw):
  2174. if target_table is not None:
  2175. def replace(col):
  2176. if self.table.c.contains_column(col):
  2177. return target_table.c[col.key]
  2178. else:
  2179. return None
  2180. sqltext = visitors.replacement_traverse(self.sqltext, {}, replace)
  2181. else:
  2182. sqltext = self.sqltext
  2183. c = CheckConstraint(sqltext,
  2184. name=self.name,
  2185. initially=self.initially,
  2186. deferrable=self.deferrable,
  2187. _create_rule=self._create_rule,
  2188. table=target_table,
  2189. _autoattach=False,
  2190. _type_bound=self._type_bound)
  2191. return self._schema_item_copy(c)
  2192. class ForeignKeyConstraint(ColumnCollectionConstraint):
  2193. """A table-level FOREIGN KEY constraint.
  2194. Defines a single column or composite FOREIGN KEY ... REFERENCES
  2195. constraint. For a no-frills, single column foreign key, adding a
  2196. :class:`.ForeignKey` to the definition of a :class:`.Column` is a
  2197. shorthand equivalent for an unnamed, single column
  2198. :class:`.ForeignKeyConstraint`.
  2199. Examples of foreign key configuration are in :ref:`metadata_foreignkeys`.
  2200. """
  2201. __visit_name__ = 'foreign_key_constraint'
  2202. def __init__(self, columns, refcolumns, name=None, onupdate=None,
  2203. ondelete=None, deferrable=None, initially=None,
  2204. use_alter=False, link_to_name=False, match=None,
  2205. table=None, info=None, **dialect_kw):
  2206. r"""Construct a composite-capable FOREIGN KEY.
  2207. :param columns: A sequence of local column names. The named columns
  2208. must be defined and present in the parent Table. The names should
  2209. match the ``key`` given to each column (defaults to the name) unless
  2210. ``link_to_name`` is True.
  2211. :param refcolumns: A sequence of foreign column names or Column
  2212. objects. The columns must all be located within the same Table.
  2213. :param name: Optional, the in-database name of the key.
  2214. :param onupdate: Optional string. If set, emit ON UPDATE <value> when
  2215. issuing DDL for this constraint. Typical values include CASCADE,
  2216. DELETE and RESTRICT.
  2217. :param ondelete: Optional string. If set, emit ON DELETE <value> when
  2218. issuing DDL for this constraint. Typical values include CASCADE,
  2219. DELETE and RESTRICT.
  2220. :param deferrable: Optional bool. If set, emit DEFERRABLE or NOT
  2221. DEFERRABLE when issuing DDL for this constraint.
  2222. :param initially: Optional string. If set, emit INITIALLY <value> when
  2223. issuing DDL for this constraint.
  2224. :param link_to_name: if True, the string name given in ``column`` is
  2225. the rendered name of the referenced column, not its locally assigned
  2226. ``key``.
  2227. :param use_alter: If True, do not emit the DDL for this constraint as
  2228. part of the CREATE TABLE definition. Instead, generate it via an
  2229. ALTER TABLE statement issued after the full collection of tables
  2230. have been created, and drop it via an ALTER TABLE statement before
  2231. the full collection of tables are dropped.
  2232. The use of :paramref:`.ForeignKeyConstraint.use_alter` is
  2233. particularly geared towards the case where two or more tables
  2234. are established within a mutually-dependent foreign key constraint
  2235. relationship; however, the :meth:`.MetaData.create_all` and
  2236. :meth:`.MetaData.drop_all` methods will perform this resolution
  2237. automatically, so the flag is normally not needed.
  2238. .. versionchanged:: 1.0.0 Automatic resolution of foreign key
  2239. cycles has been added, removing the need to use the
  2240. :paramref:`.ForeignKeyConstraint.use_alter` in typical use
  2241. cases.
  2242. .. seealso::
  2243. :ref:`use_alter`
  2244. :param match: Optional string. If set, emit MATCH <value> when issuing
  2245. DDL for this constraint. Typical values include SIMPLE, PARTIAL
  2246. and FULL.
  2247. :param info: Optional data dictionary which will be populated into the
  2248. :attr:`.SchemaItem.info` attribute of this object.
  2249. .. versionadded:: 1.0.0
  2250. :param \**dialect_kw: Additional keyword arguments are dialect
  2251. specific, and passed in the form ``<dialectname>_<argname>``. See
  2252. the documentation regarding an individual dialect at
  2253. :ref:`dialect_toplevel` for detail on documented arguments.
  2254. .. versionadded:: 0.9.2
  2255. """
  2256. Constraint.__init__(
  2257. self, name=name, deferrable=deferrable, initially=initially,
  2258. info=info, **dialect_kw)
  2259. self.onupdate = onupdate
  2260. self.ondelete = ondelete
  2261. self.link_to_name = link_to_name
  2262. self.use_alter = use_alter
  2263. self.match = match
  2264. # standalone ForeignKeyConstraint - create
  2265. # associated ForeignKey objects which will be applied to hosted
  2266. # Column objects (in col.foreign_keys), either now or when attached
  2267. # to the Table for string-specified names
  2268. self.elements = [
  2269. ForeignKey(
  2270. refcol,
  2271. _constraint=self,
  2272. name=self.name,
  2273. onupdate=self.onupdate,
  2274. ondelete=self.ondelete,
  2275. use_alter=self.use_alter,
  2276. link_to_name=self.link_to_name,
  2277. match=self.match,
  2278. deferrable=self.deferrable,
  2279. initially=self.initially,
  2280. **self.dialect_kwargs
  2281. ) for refcol in refcolumns
  2282. ]
  2283. ColumnCollectionMixin.__init__(self, *columns)
  2284. if table is not None:
  2285. if hasattr(self, "parent"):
  2286. assert table is self.parent
  2287. self._set_parent_with_dispatch(table)
  2288. def _append_element(self, column, fk):
  2289. self.columns.add(column)
  2290. self.elements.append(fk)
  2291. columns = None
  2292. """A :class:`.ColumnCollection` representing the set of columns
  2293. for this constraint.
  2294. """
  2295. elements = None
  2296. """A sequence of :class:`.ForeignKey` objects.
  2297. Each :class:`.ForeignKey` represents a single referring column/referred
  2298. column pair.
  2299. This collection is intended to be read-only.
  2300. """
  2301. @property
  2302. def _elements(self):
  2303. # legacy - provide a dictionary view of (column_key, fk)
  2304. return util.OrderedDict(
  2305. zip(self.column_keys, self.elements)
  2306. )
  2307. @property
  2308. def _referred_schema(self):
  2309. for elem in self.elements:
  2310. return elem._referred_schema
  2311. else:
  2312. return None
  2313. @property
  2314. def referred_table(self):
  2315. """The :class:`.Table` object to which this
  2316. :class:`.ForeignKeyConstraint` references.
  2317. This is a dynamically calculated attribute which may not be available
  2318. if the constraint and/or parent table is not yet associated with
  2319. a metadata collection that contains the referred table.
  2320. .. versionadded:: 1.0.0
  2321. """
  2322. return self.elements[0].column.table
  2323. def _validate_dest_table(self, table):
  2324. table_keys = set([elem._table_key()
  2325. for elem in self.elements])
  2326. if None not in table_keys and len(table_keys) > 1:
  2327. elem0, elem1 = sorted(table_keys)[0:2]
  2328. raise exc.ArgumentError(
  2329. 'ForeignKeyConstraint on %s(%s) refers to '
  2330. 'multiple remote tables: %s and %s' % (
  2331. table.fullname,
  2332. self._col_description,
  2333. elem0,
  2334. elem1
  2335. ))
  2336. @property
  2337. def column_keys(self):
  2338. """Return a list of string keys representing the local
  2339. columns in this :class:`.ForeignKeyConstraint`.
  2340. This list is either the original string arguments sent
  2341. to the constructor of the :class:`.ForeignKeyConstraint`,
  2342. or if the constraint has been initialized with :class:`.Column`
  2343. objects, is the string .key of each element.
  2344. .. versionadded:: 1.0.0
  2345. """
  2346. if hasattr(self, "parent"):
  2347. return self.columns.keys()
  2348. else:
  2349. return [
  2350. col.key if isinstance(col, ColumnElement)
  2351. else str(col) for col in self._pending_colargs
  2352. ]
  2353. @property
  2354. def _col_description(self):
  2355. return ", ".join(self.column_keys)
  2356. def _set_parent(self, table):
  2357. Constraint._set_parent(self, table)
  2358. try:
  2359. ColumnCollectionConstraint._set_parent(self, table)
  2360. except KeyError as ke:
  2361. raise exc.ArgumentError(
  2362. "Can't create ForeignKeyConstraint "
  2363. "on table '%s': no column "
  2364. "named '%s' is present." % (table.description, ke.args[0]))
  2365. for col, fk in zip(self.columns, self.elements):
  2366. if not hasattr(fk, 'parent') or \
  2367. fk.parent is not col:
  2368. fk._set_parent_with_dispatch(col)
  2369. self._validate_dest_table(table)
  2370. def copy(self, schema=None, target_table=None, **kw):
  2371. fkc = ForeignKeyConstraint(
  2372. [x.parent.key for x in self.elements],
  2373. [x._get_colspec(
  2374. schema=schema,
  2375. table_name=target_table.name
  2376. if target_table is not None
  2377. and x._table_key() == x.parent.table.key
  2378. else None)
  2379. for x in self.elements],
  2380. name=self.name,
  2381. onupdate=self.onupdate,
  2382. ondelete=self.ondelete,
  2383. use_alter=self.use_alter,
  2384. deferrable=self.deferrable,
  2385. initially=self.initially,
  2386. link_to_name=self.link_to_name,
  2387. match=self.match
  2388. )
  2389. for self_fk, other_fk in zip(
  2390. self.elements,
  2391. fkc.elements):
  2392. self_fk._schema_item_copy(other_fk)
  2393. return self._schema_item_copy(fkc)
  2394. class PrimaryKeyConstraint(ColumnCollectionConstraint):
  2395. """A table-level PRIMARY KEY constraint.
  2396. The :class:`.PrimaryKeyConstraint` object is present automatically
  2397. on any :class:`.Table` object; it is assigned a set of
  2398. :class:`.Column` objects corresponding to those marked with
  2399. the :paramref:`.Column.primary_key` flag::
  2400. >>> my_table = Table('mytable', metadata,
  2401. ... Column('id', Integer, primary_key=True),
  2402. ... Column('version_id', Integer, primary_key=True),
  2403. ... Column('data', String(50))
  2404. ... )
  2405. >>> my_table.primary_key
  2406. PrimaryKeyConstraint(
  2407. Column('id', Integer(), table=<mytable>,
  2408. primary_key=True, nullable=False),
  2409. Column('version_id', Integer(), table=<mytable>,
  2410. primary_key=True, nullable=False)
  2411. )
  2412. The primary key of a :class:`.Table` can also be specified by using
  2413. a :class:`.PrimaryKeyConstraint` object explicitly; in this mode of usage,
  2414. the "name" of the constraint can also be specified, as well as other
  2415. options which may be recognized by dialects::
  2416. my_table = Table('mytable', metadata,
  2417. Column('id', Integer),
  2418. Column('version_id', Integer),
  2419. Column('data', String(50)),
  2420. PrimaryKeyConstraint('id', 'version_id',
  2421. name='mytable_pk')
  2422. )
  2423. The two styles of column-specification should generally not be mixed.
  2424. An warning is emitted if the columns present in the
  2425. :class:`.PrimaryKeyConstraint`
  2426. don't match the columns that were marked as ``primary_key=True``, if both
  2427. are present; in this case, the columns are taken strictly from the
  2428. :class:`.PrimaryKeyConstraint` declaration, and those columns otherwise
  2429. marked as ``primary_key=True`` are ignored. This behavior is intended to
  2430. be backwards compatible with previous behavior.
  2431. .. versionchanged:: 0.9.2 Using a mixture of columns within a
  2432. :class:`.PrimaryKeyConstraint` in addition to columns marked as
  2433. ``primary_key=True`` now emits a warning if the lists don't match.
  2434. The ultimate behavior of ignoring those columns marked with the flag
  2435. only is currently maintained for backwards compatibility; this warning
  2436. may raise an exception in a future release.
  2437. For the use case where specific options are to be specified on the
  2438. :class:`.PrimaryKeyConstraint`, but the usual style of using
  2439. ``primary_key=True`` flags is still desirable, an empty
  2440. :class:`.PrimaryKeyConstraint` may be specified, which will take on the
  2441. primary key column collection from the :class:`.Table` based on the
  2442. flags::
  2443. my_table = Table('mytable', metadata,
  2444. Column('id', Integer, primary_key=True),
  2445. Column('version_id', Integer, primary_key=True),
  2446. Column('data', String(50)),
  2447. PrimaryKeyConstraint(name='mytable_pk',
  2448. mssql_clustered=True)
  2449. )
  2450. .. versionadded:: 0.9.2 an empty :class:`.PrimaryKeyConstraint` may now
  2451. be specified for the purposes of establishing keyword arguments with
  2452. the constraint, independently of the specification of "primary key"
  2453. columns within the :class:`.Table` itself; columns marked as
  2454. ``primary_key=True`` will be gathered into the empty constraint's
  2455. column collection.
  2456. """
  2457. __visit_name__ = 'primary_key_constraint'
  2458. def __init__(self, *columns, **kw):
  2459. self._implicit_generated = kw.pop('_implicit_generated', False)
  2460. super(PrimaryKeyConstraint, self).__init__(*columns, **kw)
  2461. def _set_parent(self, table):
  2462. super(PrimaryKeyConstraint, self)._set_parent(table)
  2463. if table.primary_key is not self:
  2464. table.constraints.discard(table.primary_key)
  2465. table.primary_key = self
  2466. table.constraints.add(self)
  2467. table_pks = [c for c in table.c if c.primary_key]
  2468. if self.columns and table_pks and \
  2469. set(table_pks) != set(self.columns.values()):
  2470. util.warn(
  2471. "Table '%s' specifies columns %s as primary_key=True, "
  2472. "not matching locally specified columns %s; setting the "
  2473. "current primary key columns to %s. This warning "
  2474. "may become an exception in a future release" %
  2475. (
  2476. table.name,
  2477. ", ".join("'%s'" % c.name for c in table_pks),
  2478. ", ".join("'%s'" % c.name for c in self.columns),
  2479. ", ".join("'%s'" % c.name for c in self.columns)
  2480. )
  2481. )
  2482. table_pks[:] = []
  2483. for c in self.columns:
  2484. c.primary_key = True
  2485. c.nullable = False
  2486. self.columns.extend(table_pks)
  2487. def _reload(self, columns):
  2488. """repopulate this :class:`.PrimaryKeyConstraint` given
  2489. a set of columns.
  2490. Existing columns in the table that are marked as primary_key=True
  2491. are maintained.
  2492. Also fires a new event.
  2493. This is basically like putting a whole new
  2494. :class:`.PrimaryKeyConstraint` object on the parent
  2495. :class:`.Table` object without actually replacing the object.
  2496. The ordering of the given list of columns is also maintained; these
  2497. columns will be appended to the list of columns after any which
  2498. are already present.
  2499. """
  2500. # set the primary key flag on new columns.
  2501. # note any existing PK cols on the table also have their
  2502. # flag still set.
  2503. for col in columns:
  2504. col.primary_key = True
  2505. self.columns.extend(columns)
  2506. PrimaryKeyConstraint._autoincrement_column._reset(self)
  2507. self._set_parent_with_dispatch(self.table)
  2508. def _replace(self, col):
  2509. PrimaryKeyConstraint._autoincrement_column._reset(self)
  2510. self.columns.replace(col)
  2511. @property
  2512. def columns_autoinc_first(self):
  2513. autoinc = self._autoincrement_column
  2514. if autoinc is not None:
  2515. return [autoinc] + [c for c in self.columns if c is not autoinc]
  2516. else:
  2517. return list(self.columns)
  2518. @util.memoized_property
  2519. def _autoincrement_column(self):
  2520. def _validate_autoinc(col, autoinc_true):
  2521. if col.type._type_affinity is None or not issubclass(
  2522. col.type._type_affinity,
  2523. type_api.INTEGERTYPE._type_affinity):
  2524. if autoinc_true:
  2525. raise exc.ArgumentError(
  2526. "Column type %s on column '%s' is not "
  2527. "compatible with autoincrement=True" % (
  2528. col.type,
  2529. col
  2530. ))
  2531. else:
  2532. return False
  2533. elif not isinstance(col.default, (type(None), Sequence)) and \
  2534. not autoinc_true:
  2535. return False
  2536. elif col.server_default is not None and not autoinc_true:
  2537. return False
  2538. elif (
  2539. col.foreign_keys and col.autoincrement
  2540. not in (True, 'ignore_fk')):
  2541. return False
  2542. return True
  2543. if len(self.columns) == 1:
  2544. col = list(self.columns)[0]
  2545. if col.autoincrement is True:
  2546. _validate_autoinc(col, True)
  2547. return col
  2548. elif (
  2549. col.autoincrement in ('auto', 'ignore_fk') and
  2550. _validate_autoinc(col, False)
  2551. ):
  2552. return col
  2553. else:
  2554. autoinc = None
  2555. for col in self.columns:
  2556. if col.autoincrement is True:
  2557. _validate_autoinc(col, True)
  2558. if autoinc is not None:
  2559. raise exc.ArgumentError(
  2560. "Only one Column may be marked "
  2561. "autoincrement=True, found both %s and %s." %
  2562. (col.name, autoinc.name)
  2563. )
  2564. else:
  2565. autoinc = col
  2566. return autoinc
  2567. class UniqueConstraint(ColumnCollectionConstraint):
  2568. """A table-level UNIQUE constraint.
  2569. Defines a single column or composite UNIQUE constraint. For a no-frills,
  2570. single column constraint, adding ``unique=True`` to the ``Column``
  2571. definition is a shorthand equivalent for an unnamed, single column
  2572. UniqueConstraint.
  2573. """
  2574. __visit_name__ = 'unique_constraint'
  2575. class Index(DialectKWArgs, ColumnCollectionMixin, SchemaItem):
  2576. """A table-level INDEX.
  2577. Defines a composite (one or more column) INDEX.
  2578. E.g.::
  2579. sometable = Table("sometable", metadata,
  2580. Column("name", String(50)),
  2581. Column("address", String(100))
  2582. )
  2583. Index("some_index", sometable.c.name)
  2584. For a no-frills, single column index, adding
  2585. :class:`.Column` also supports ``index=True``::
  2586. sometable = Table("sometable", metadata,
  2587. Column("name", String(50), index=True)
  2588. )
  2589. For a composite index, multiple columns can be specified::
  2590. Index("some_index", sometable.c.name, sometable.c.address)
  2591. Functional indexes are supported as well, typically by using the
  2592. :data:`.func` construct in conjunction with table-bound
  2593. :class:`.Column` objects::
  2594. Index("some_index", func.lower(sometable.c.name))
  2595. .. versionadded:: 0.8 support for functional and expression-based indexes.
  2596. An :class:`.Index` can also be manually associated with a :class:`.Table`,
  2597. either through inline declaration or using
  2598. :meth:`.Table.append_constraint`. When this approach is used, the names
  2599. of the indexed columns can be specified as strings::
  2600. Table("sometable", metadata,
  2601. Column("name", String(50)),
  2602. Column("address", String(100)),
  2603. Index("some_index", "name", "address")
  2604. )
  2605. To support functional or expression-based indexes in this form, the
  2606. :func:`.text` construct may be used::
  2607. from sqlalchemy import text
  2608. Table("sometable", metadata,
  2609. Column("name", String(50)),
  2610. Column("address", String(100)),
  2611. Index("some_index", text("lower(name)"))
  2612. )
  2613. .. versionadded:: 0.9.5 the :func:`.text` construct may be used to
  2614. specify :class:`.Index` expressions, provided the :class:`.Index`
  2615. is explicitly associated with the :class:`.Table`.
  2616. .. seealso::
  2617. :ref:`schema_indexes` - General information on :class:`.Index`.
  2618. :ref:`postgresql_indexes` - PostgreSQL-specific options available for
  2619. the :class:`.Index` construct.
  2620. :ref:`mysql_indexes` - MySQL-specific options available for the
  2621. :class:`.Index` construct.
  2622. :ref:`mssql_indexes` - MSSQL-specific options available for the
  2623. :class:`.Index` construct.
  2624. """
  2625. __visit_name__ = 'index'
  2626. def __init__(self, name, *expressions, **kw):
  2627. r"""Construct an index object.
  2628. :param name:
  2629. The name of the index
  2630. :param \*expressions:
  2631. Column expressions to include in the index. The expressions
  2632. are normally instances of :class:`.Column`, but may also
  2633. be arbitrary SQL expressions which ultimately refer to a
  2634. :class:`.Column`.
  2635. :param unique=False:
  2636. Keyword only argument; if True, create a unique index.
  2637. :param quote=None:
  2638. Keyword only argument; whether to apply quoting to the name of
  2639. the index. Works in the same manner as that of
  2640. :paramref:`.Column.quote`.
  2641. :param info=None: Optional data dictionary which will be populated
  2642. into the :attr:`.SchemaItem.info` attribute of this object.
  2643. .. versionadded:: 1.0.0
  2644. :param \**kw: Additional keyword arguments not mentioned above are
  2645. dialect specific, and passed in the form
  2646. ``<dialectname>_<argname>``. See the documentation regarding an
  2647. individual dialect at :ref:`dialect_toplevel` for detail on
  2648. documented arguments.
  2649. """
  2650. self.table = None
  2651. columns = []
  2652. processed_expressions = []
  2653. for expr, column, strname, add_element in self.\
  2654. _extract_col_expression_collection(expressions):
  2655. if add_element is not None:
  2656. columns.append(add_element)
  2657. processed_expressions.append(expr)
  2658. self.expressions = processed_expressions
  2659. self.name = quoted_name(name, kw.pop("quote", None))
  2660. self.unique = kw.pop('unique', False)
  2661. if 'info' in kw:
  2662. self.info = kw.pop('info')
  2663. self._validate_dialect_kwargs(kw)
  2664. # will call _set_parent() if table-bound column
  2665. # objects are present
  2666. ColumnCollectionMixin.__init__(self, *columns)
  2667. def _set_parent(self, table):
  2668. ColumnCollectionMixin._set_parent(self, table)
  2669. if self.table is not None and table is not self.table:
  2670. raise exc.ArgumentError(
  2671. "Index '%s' is against table '%s', and "
  2672. "cannot be associated with table '%s'." % (
  2673. self.name,
  2674. self.table.description,
  2675. table.description
  2676. )
  2677. )
  2678. self.table = table
  2679. table.indexes.add(self)
  2680. self.expressions = [
  2681. expr if isinstance(expr, ClauseElement)
  2682. else colexpr
  2683. for expr, colexpr in util.zip_longest(self.expressions,
  2684. self.columns)
  2685. ]
  2686. @property
  2687. def bind(self):
  2688. """Return the connectable associated with this Index."""
  2689. return self.table.bind
  2690. def create(self, bind=None):
  2691. """Issue a ``CREATE`` statement for this
  2692. :class:`.Index`, using the given :class:`.Connectable`
  2693. for connectivity.
  2694. .. seealso::
  2695. :meth:`.MetaData.create_all`.
  2696. """
  2697. if bind is None:
  2698. bind = _bind_or_error(self)
  2699. bind._run_visitor(ddl.SchemaGenerator, self)
  2700. return self
  2701. def drop(self, bind=None):
  2702. """Issue a ``DROP`` statement for this
  2703. :class:`.Index`, using the given :class:`.Connectable`
  2704. for connectivity.
  2705. .. seealso::
  2706. :meth:`.MetaData.drop_all`.
  2707. """
  2708. if bind is None:
  2709. bind = _bind_or_error(self)
  2710. bind._run_visitor(ddl.SchemaDropper, self)
  2711. def __repr__(self):
  2712. return 'Index(%s)' % (
  2713. ", ".join(
  2714. [repr(self.name)] +
  2715. [repr(e) for e in self.expressions] +
  2716. (self.unique and ["unique=True"] or [])
  2717. ))
  2718. DEFAULT_NAMING_CONVENTION = util.immutabledict({
  2719. "ix": 'ix_%(column_0_label)s'
  2720. })
  2721. class MetaData(SchemaItem):
  2722. """A collection of :class:`.Table` objects and their associated schema
  2723. constructs.
  2724. Holds a collection of :class:`.Table` objects as well as
  2725. an optional binding to an :class:`.Engine` or
  2726. :class:`.Connection`. If bound, the :class:`.Table` objects
  2727. in the collection and their columns may participate in implicit SQL
  2728. execution.
  2729. The :class:`.Table` objects themselves are stored in the
  2730. :attr:`.MetaData.tables` dictionary.
  2731. :class:`.MetaData` is a thread-safe object for read operations.
  2732. Construction of new tables within a single :class:`.MetaData` object,
  2733. either explicitly or via reflection, may not be completely thread-safe.
  2734. .. seealso::
  2735. :ref:`metadata_describing` - Introduction to database metadata
  2736. """
  2737. __visit_name__ = 'metadata'
  2738. def __init__(self, bind=None, reflect=False, schema=None,
  2739. quote_schema=None,
  2740. naming_convention=DEFAULT_NAMING_CONVENTION,
  2741. info=None
  2742. ):
  2743. """Create a new MetaData object.
  2744. :param bind:
  2745. An Engine or Connection to bind to. May also be a string or URL
  2746. instance, these are passed to create_engine() and this MetaData will
  2747. be bound to the resulting engine.
  2748. :param reflect:
  2749. Optional, automatically load all tables from the bound database.
  2750. Defaults to False. ``bind`` is required when this option is set.
  2751. .. deprecated:: 0.8
  2752. Please use the :meth:`.MetaData.reflect` method.
  2753. :param schema:
  2754. The default schema to use for the :class:`.Table`,
  2755. :class:`.Sequence`, and potentially other objects associated with
  2756. this :class:`.MetaData`. Defaults to ``None``.
  2757. When this value is set, any :class:`.Table` or :class:`.Sequence`
  2758. which specifies ``None`` for the schema parameter will instead
  2759. have this schema name defined. To build a :class:`.Table`
  2760. or :class:`.Sequence` that still has ``None`` for the schema
  2761. even when this parameter is present, use the :attr:`.BLANK_SCHEMA`
  2762. symbol.
  2763. .. note::
  2764. As refered above, the :paramref:`.MetaData.schema` parameter
  2765. only refers to the **default value** that will be applied to
  2766. the :paramref:`.Table.schema` parameter of an incoming
  2767. :class:`.Table` object. It does not refer to how the
  2768. :class:`.Table` is catalogued within the :class:`.MetaData`,
  2769. which remains consistent vs. a :class:`.MetaData` collection
  2770. that does not define this parameter. The :class:`.Table`
  2771. within the :class:`.MetaData` will still be keyed based on its
  2772. schema-qualified name, e.g.
  2773. ``my_metadata.tables["some_schema.my_table"]``.
  2774. The current behavior of the :class:`.ForeignKey` object is to
  2775. circumvent this restriction, where it can locate a table given
  2776. the table name alone, where the schema will be assumed to be
  2777. present from this value as specified on the owning
  2778. :class:`.MetaData` collection. However, this implies that a
  2779. table qualified with BLANK_SCHEMA cannot currently be referred
  2780. to by string name from :class:`.ForeignKey`. Other parts of
  2781. SQLAlchemy such as Declarative may not have similar behaviors
  2782. built in, however may do so in a future release, along with a
  2783. consistent method of referring to a table in BLANK_SCHEMA.
  2784. .. seealso::
  2785. :paramref:`.Table.schema`
  2786. :paramref:`.Sequence.schema`
  2787. :param quote_schema:
  2788. Sets the ``quote_schema`` flag for those :class:`.Table`,
  2789. :class:`.Sequence`, and other objects which make usage of the
  2790. local ``schema`` name.
  2791. :param info: Optional data dictionary which will be populated into the
  2792. :attr:`.SchemaItem.info` attribute of this object.
  2793. .. versionadded:: 1.0.0
  2794. :param naming_convention: a dictionary referring to values which
  2795. will establish default naming conventions for :class:`.Constraint`
  2796. and :class:`.Index` objects, for those objects which are not given
  2797. a name explicitly.
  2798. The keys of this dictionary may be:
  2799. * a constraint or Index class, e.g. the :class:`.UniqueConstraint`,
  2800. :class:`.ForeignKeyConstraint` class, the :class:`.Index` class
  2801. * a string mnemonic for one of the known constraint classes;
  2802. ``"fk"``, ``"pk"``, ``"ix"``, ``"ck"``, ``"uq"`` for foreign key,
  2803. primary key, index, check, and unique constraint, respectively.
  2804. * the string name of a user-defined "token" that can be used
  2805. to define new naming tokens.
  2806. The values associated with each "constraint class" or "constraint
  2807. mnemonic" key are string naming templates, such as
  2808. ``"uq_%(table_name)s_%(column_0_name)s"``,
  2809. which describe how the name should be composed. The values
  2810. associated with user-defined "token" keys should be callables of the
  2811. form ``fn(constraint, table)``, which accepts the constraint/index
  2812. object and :class:`.Table` as arguments, returning a string
  2813. result.
  2814. The built-in names are as follows, some of which may only be
  2815. available for certain types of constraint:
  2816. * ``%(table_name)s`` - the name of the :class:`.Table` object
  2817. associated with the constraint.
  2818. * ``%(referred_table_name)s`` - the name of the :class:`.Table`
  2819. object associated with the referencing target of a
  2820. :class:`.ForeignKeyConstraint`.
  2821. * ``%(column_0_name)s`` - the name of the :class:`.Column` at
  2822. index position "0" within the constraint.
  2823. * ``%(column_0_label)s`` - the label of the :class:`.Column` at
  2824. index position "0", e.g. :attr:`.Column.label`
  2825. * ``%(column_0_key)s`` - the key of the :class:`.Column` at
  2826. index position "0", e.g. :attr:`.Column.key`
  2827. * ``%(referred_column_0_name)s`` - the name of a :class:`.Column`
  2828. at index position "0" referenced by a
  2829. :class:`.ForeignKeyConstraint`.
  2830. * ``%(constraint_name)s`` - a special key that refers to the
  2831. existing name given to the constraint. When this key is
  2832. present, the :class:`.Constraint` object's existing name will be
  2833. replaced with one that is composed from template string that
  2834. uses this token. When this token is present, it is required that
  2835. the :class:`.Constraint` is given an explicit name ahead of time.
  2836. * user-defined: any additional token may be implemented by passing
  2837. it along with a ``fn(constraint, table)`` callable to the
  2838. naming_convention dictionary.
  2839. .. versionadded:: 0.9.2
  2840. .. seealso::
  2841. :ref:`constraint_naming_conventions` - for detailed usage
  2842. examples.
  2843. """
  2844. self.tables = util.immutabledict()
  2845. self.schema = quoted_name(schema, quote_schema)
  2846. self.naming_convention = naming_convention
  2847. if info:
  2848. self.info = info
  2849. self._schemas = set()
  2850. self._sequences = {}
  2851. self._fk_memos = collections.defaultdict(list)
  2852. self.bind = bind
  2853. if reflect:
  2854. util.warn_deprecated("reflect=True is deprecate; please "
  2855. "use the reflect() method.")
  2856. if not bind:
  2857. raise exc.ArgumentError(
  2858. "A bind must be supplied in conjunction "
  2859. "with reflect=True")
  2860. self.reflect()
  2861. tables = None
  2862. """A dictionary of :class:`.Table` objects keyed to their name or "table key".
  2863. The exact key is that determined by the :attr:`.Table.key` attribute;
  2864. for a table with no :attr:`.Table.schema` attribute, this is the same
  2865. as :attr:`.Table.name`. For a table with a schema, it is typically of the
  2866. form ``schemaname.tablename``.
  2867. .. seealso::
  2868. :attr:`.MetaData.sorted_tables`
  2869. """
  2870. def __repr__(self):
  2871. return 'MetaData(bind=%r)' % self.bind
  2872. def __contains__(self, table_or_key):
  2873. if not isinstance(table_or_key, util.string_types):
  2874. table_or_key = table_or_key.key
  2875. return table_or_key in self.tables
  2876. def _add_table(self, name, schema, table):
  2877. key = _get_table_key(name, schema)
  2878. dict.__setitem__(self.tables, key, table)
  2879. if schema:
  2880. self._schemas.add(schema)
  2881. def _remove_table(self, name, schema):
  2882. key = _get_table_key(name, schema)
  2883. removed = dict.pop(self.tables, key, None)
  2884. if removed is not None:
  2885. for fk in removed.foreign_keys:
  2886. fk._remove_from_metadata(self)
  2887. if self._schemas:
  2888. self._schemas = set([t.schema
  2889. for t in self.tables.values()
  2890. if t.schema is not None])
  2891. def __getstate__(self):
  2892. return {'tables': self.tables,
  2893. 'schema': self.schema,
  2894. 'schemas': self._schemas,
  2895. 'sequences': self._sequences,
  2896. 'fk_memos': self._fk_memos,
  2897. 'naming_convention': self.naming_convention
  2898. }
  2899. def __setstate__(self, state):
  2900. self.tables = state['tables']
  2901. self.schema = state['schema']
  2902. self.naming_convention = state['naming_convention']
  2903. self._bind = None
  2904. self._sequences = state['sequences']
  2905. self._schemas = state['schemas']
  2906. self._fk_memos = state['fk_memos']
  2907. def is_bound(self):
  2908. """True if this MetaData is bound to an Engine or Connection."""
  2909. return self._bind is not None
  2910. def bind(self):
  2911. """An :class:`.Engine` or :class:`.Connection` to which this
  2912. :class:`.MetaData` is bound.
  2913. Typically, a :class:`.Engine` is assigned to this attribute
  2914. so that "implicit execution" may be used, or alternatively
  2915. as a means of providing engine binding information to an
  2916. ORM :class:`.Session` object::
  2917. engine = create_engine("someurl://")
  2918. metadata.bind = engine
  2919. .. seealso::
  2920. :ref:`dbengine_implicit` - background on "bound metadata"
  2921. """
  2922. return self._bind
  2923. @util.dependencies("sqlalchemy.engine.url")
  2924. def _bind_to(self, url, bind):
  2925. """Bind this MetaData to an Engine, Connection, string or URL."""
  2926. if isinstance(bind, util.string_types + (url.URL, )):
  2927. self._bind = sqlalchemy.create_engine(bind)
  2928. else:
  2929. self._bind = bind
  2930. bind = property(bind, _bind_to)
  2931. def clear(self):
  2932. """Clear all Table objects from this MetaData."""
  2933. dict.clear(self.tables)
  2934. self._schemas.clear()
  2935. self._fk_memos.clear()
  2936. def remove(self, table):
  2937. """Remove the given Table object from this MetaData."""
  2938. self._remove_table(table.name, table.schema)
  2939. @property
  2940. def sorted_tables(self):
  2941. """Returns a list of :class:`.Table` objects sorted in order of
  2942. foreign key dependency.
  2943. The sorting will place :class:`.Table` objects that have dependencies
  2944. first, before the dependencies themselves, representing the
  2945. order in which they can be created. To get the order in which
  2946. the tables would be dropped, use the ``reversed()`` Python built-in.
  2947. .. warning::
  2948. The :attr:`.sorted_tables` accessor cannot by itself accommodate
  2949. automatic resolution of dependency cycles between tables, which
  2950. are usually caused by mutually dependent foreign key constraints.
  2951. To resolve these cycles, either the
  2952. :paramref:`.ForeignKeyConstraint.use_alter` parameter may be appled
  2953. to those constraints, or use the
  2954. :func:`.schema.sort_tables_and_constraints` function which will break
  2955. out foreign key constraints involved in cycles separately.
  2956. .. seealso::
  2957. :func:`.schema.sort_tables`
  2958. :func:`.schema.sort_tables_and_constraints`
  2959. :attr:`.MetaData.tables`
  2960. :meth:`.Inspector.get_table_names`
  2961. :meth:`.Inspector.get_sorted_table_and_fkc_names`
  2962. """
  2963. return ddl.sort_tables(sorted(self.tables.values(), key=lambda t: t.key))
  2964. def reflect(self, bind=None, schema=None, views=False, only=None,
  2965. extend_existing=False,
  2966. autoload_replace=True,
  2967. **dialect_kwargs):
  2968. r"""Load all available table definitions from the database.
  2969. Automatically creates ``Table`` entries in this ``MetaData`` for any
  2970. table available in the database but not yet present in the
  2971. ``MetaData``. May be called multiple times to pick up tables recently
  2972. added to the database, however no special action is taken if a table
  2973. in this ``MetaData`` no longer exists in the database.
  2974. :param bind:
  2975. A :class:`.Connectable` used to access the database; if None, uses
  2976. the existing bind on this ``MetaData``, if any.
  2977. :param schema:
  2978. Optional, query and reflect tables from an alterate schema.
  2979. If None, the schema associated with this :class:`.MetaData`
  2980. is used, if any.
  2981. :param views:
  2982. If True, also reflect views.
  2983. :param only:
  2984. Optional. Load only a sub-set of available named tables. May be
  2985. specified as a sequence of names or a callable.
  2986. If a sequence of names is provided, only those tables will be
  2987. reflected. An error is raised if a table is requested but not
  2988. available. Named tables already present in this ``MetaData`` are
  2989. ignored.
  2990. If a callable is provided, it will be used as a boolean predicate to
  2991. filter the list of potential table names. The callable is called
  2992. with a table name and this ``MetaData`` instance as positional
  2993. arguments and should return a true value for any table to reflect.
  2994. :param extend_existing: Passed along to each :class:`.Table` as
  2995. :paramref:`.Table.extend_existing`.
  2996. .. versionadded:: 0.9.1
  2997. :param autoload_replace: Passed along to each :class:`.Table` as
  2998. :paramref:`.Table.autoload_replace`.
  2999. .. versionadded:: 0.9.1
  3000. :param \**dialect_kwargs: Additional keyword arguments not mentioned
  3001. above are dialect specific, and passed in the form
  3002. ``<dialectname>_<argname>``. See the documentation regarding an
  3003. individual dialect at :ref:`dialect_toplevel` for detail on
  3004. documented arguments.
  3005. .. versionadded:: 0.9.2 - Added
  3006. :paramref:`.MetaData.reflect.**dialect_kwargs` to support
  3007. dialect-level reflection options for all :class:`.Table`
  3008. objects reflected.
  3009. """
  3010. if bind is None:
  3011. bind = _bind_or_error(self)
  3012. with bind.connect() as conn:
  3013. reflect_opts = {
  3014. 'autoload': True,
  3015. 'autoload_with': conn,
  3016. 'extend_existing': extend_existing,
  3017. 'autoload_replace': autoload_replace,
  3018. '_extend_on': set()
  3019. }
  3020. reflect_opts.update(dialect_kwargs)
  3021. if schema is None:
  3022. schema = self.schema
  3023. if schema is not None:
  3024. reflect_opts['schema'] = schema
  3025. available = util.OrderedSet(
  3026. bind.engine.table_names(schema, connection=conn))
  3027. if views:
  3028. available.update(
  3029. bind.dialect.get_view_names(conn, schema)
  3030. )
  3031. if schema is not None:
  3032. available_w_schema = util.OrderedSet(["%s.%s" % (schema, name)
  3033. for name in available])
  3034. else:
  3035. available_w_schema = available
  3036. current = set(self.tables)
  3037. if only is None:
  3038. load = [name for name, schname in
  3039. zip(available, available_w_schema)
  3040. if extend_existing or schname not in current]
  3041. elif util.callable(only):
  3042. load = [name for name, schname in
  3043. zip(available, available_w_schema)
  3044. if (extend_existing or schname not in current)
  3045. and only(name, self)]
  3046. else:
  3047. missing = [name for name in only if name not in available]
  3048. if missing:
  3049. s = schema and (" schema '%s'" % schema) or ''
  3050. raise exc.InvalidRequestError(
  3051. 'Could not reflect: requested table(s) not available '
  3052. 'in %r%s: (%s)' %
  3053. (bind.engine, s, ', '.join(missing)))
  3054. load = [name for name in only if extend_existing or
  3055. name not in current]
  3056. for name in load:
  3057. Table(name, self, **reflect_opts)
  3058. def append_ddl_listener(self, event_name, listener):
  3059. """Append a DDL event listener to this ``MetaData``.
  3060. .. deprecated:: 0.7
  3061. See :class:`.DDLEvents`.
  3062. """
  3063. def adapt_listener(target, connection, **kw):
  3064. tables = kw['tables']
  3065. listener(event, target, connection, tables=tables)
  3066. event.listen(self, "" + event_name.replace('-', '_'), adapt_listener)
  3067. def create_all(self, bind=None, tables=None, checkfirst=True):
  3068. """Create all tables stored in this metadata.
  3069. Conditional by default, will not attempt to recreate tables already
  3070. present in the target database.
  3071. :param bind:
  3072. A :class:`.Connectable` used to access the
  3073. database; if None, uses the existing bind on this ``MetaData``, if
  3074. any.
  3075. :param tables:
  3076. Optional list of ``Table`` objects, which is a subset of the total
  3077. tables in the ``MetaData`` (others are ignored).
  3078. :param checkfirst:
  3079. Defaults to True, don't issue CREATEs for tables already present
  3080. in the target database.
  3081. """
  3082. if bind is None:
  3083. bind = _bind_or_error(self)
  3084. bind._run_visitor(ddl.SchemaGenerator,
  3085. self,
  3086. checkfirst=checkfirst,
  3087. tables=tables)
  3088. def drop_all(self, bind=None, tables=None, checkfirst=True):
  3089. """Drop all tables stored in this metadata.
  3090. Conditional by default, will not attempt to drop tables not present in
  3091. the target database.
  3092. :param bind:
  3093. A :class:`.Connectable` used to access the
  3094. database; if None, uses the existing bind on this ``MetaData``, if
  3095. any.
  3096. :param tables:
  3097. Optional list of ``Table`` objects, which is a subset of the
  3098. total tables in the ``MetaData`` (others are ignored).
  3099. :param checkfirst:
  3100. Defaults to True, only issue DROPs for tables confirmed to be
  3101. present in the target database.
  3102. """
  3103. if bind is None:
  3104. bind = _bind_or_error(self)
  3105. bind._run_visitor(ddl.SchemaDropper,
  3106. self,
  3107. checkfirst=checkfirst,
  3108. tables=tables)
  3109. class ThreadLocalMetaData(MetaData):
  3110. """A MetaData variant that presents a different ``bind`` in every thread.
  3111. Makes the ``bind`` property of the MetaData a thread-local value, allowing
  3112. this collection of tables to be bound to different ``Engine``
  3113. implementations or connections in each thread.
  3114. The ThreadLocalMetaData starts off bound to None in each thread. Binds
  3115. must be made explicitly by assigning to the ``bind`` property or using
  3116. ``connect()``. You can also re-bind dynamically multiple times per
  3117. thread, just like a regular ``MetaData``.
  3118. """
  3119. __visit_name__ = 'metadata'
  3120. def __init__(self):
  3121. """Construct a ThreadLocalMetaData."""
  3122. self.context = util.threading.local()
  3123. self.__engines = {}
  3124. super(ThreadLocalMetaData, self).__init__()
  3125. def bind(self):
  3126. """The bound Engine or Connection for this thread.
  3127. This property may be assigned an Engine or Connection, or assigned a
  3128. string or URL to automatically create a basic Engine for this bind
  3129. with ``create_engine()``."""
  3130. return getattr(self.context, '_engine', None)
  3131. @util.dependencies("sqlalchemy.engine.url")
  3132. def _bind_to(self, url, bind):
  3133. """Bind to a Connectable in the caller's thread."""
  3134. if isinstance(bind, util.string_types + (url.URL, )):
  3135. try:
  3136. self.context._engine = self.__engines[bind]
  3137. except KeyError:
  3138. e = sqlalchemy.create_engine(bind)
  3139. self.__engines[bind] = e
  3140. self.context._engine = e
  3141. else:
  3142. # TODO: this is squirrely. we shouldn't have to hold onto engines
  3143. # in a case like this
  3144. if bind not in self.__engines:
  3145. self.__engines[bind] = bind
  3146. self.context._engine = bind
  3147. bind = property(bind, _bind_to)
  3148. def is_bound(self):
  3149. """True if there is a bind for this thread."""
  3150. return (hasattr(self.context, '_engine') and
  3151. self.context._engine is not None)
  3152. def dispose(self):
  3153. """Dispose all bound engines, in all thread contexts."""
  3154. for e in self.__engines.values():
  3155. if hasattr(e, 'dispose'):
  3156. e.dispose()
  3157. class _SchemaTranslateMap(object):
  3158. """Provide translation of schema names based on a mapping.
  3159. Also provides helpers for producing cache keys and optimized
  3160. access when no mapping is present.
  3161. Used by the :paramref:`.Connection.execution_options.schema_translate_map`
  3162. feature.
  3163. .. versionadded:: 1.1
  3164. """
  3165. __slots__ = 'map_', '__call__', 'hash_key', 'is_default'
  3166. _default_schema_getter = operator.attrgetter("schema")
  3167. def __init__(self, map_):
  3168. self.map_ = map_
  3169. if map_ is not None:
  3170. def schema_for_object(obj):
  3171. effective_schema = self._default_schema_getter(obj)
  3172. effective_schema = obj._translate_schema(
  3173. effective_schema, map_)
  3174. return effective_schema
  3175. self.__call__ = schema_for_object
  3176. self.hash_key = ";".join(
  3177. "%s=%s" % (k, map_[k])
  3178. for k in sorted(map_, key=str)
  3179. )
  3180. self.is_default = False
  3181. else:
  3182. self.hash_key = 0
  3183. self.__call__ = self._default_schema_getter
  3184. self.is_default = True
  3185. @classmethod
  3186. def _schema_getter(cls, map_):
  3187. if map_ is None:
  3188. return _default_schema_map
  3189. elif isinstance(map_, _SchemaTranslateMap):
  3190. return map_
  3191. else:
  3192. return _SchemaTranslateMap(map_)
  3193. _default_schema_map = _SchemaTranslateMap(None)
  3194. _schema_getter = _SchemaTranslateMap._schema_getter