glossary.html 109 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <title>
  7. Glossary
  8. &mdash;
  9. SQLAlchemy 1.3 Documentation
  10. </title>
  11. <!-- begin iterate through site-imported + sphinx environment css_files -->
  12. <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
  13. <link rel="stylesheet" href="_static/docs.css" type="text/css" />
  14. <link rel="stylesheet" href="_static/changelog.css" type="text/css" />
  15. <link rel="stylesheet" href="_static/sphinx_paramlinks.css" type="text/css" />
  16. <!-- end iterate through site-imported + sphinx environment css_files -->
  17. <!-- begin layout.mako headers -->
  18. <link rel="index" title="Index" href="genindex.html" />
  19. <link rel="search" title="Search" href="search.html" />
  20. <link rel="copyright" title="Copyright" href="copyright.html" />
  21. <link rel="top" title="SQLAlchemy 1.3 Documentation" href="index.html" />
  22. <!-- end layout.mako headers -->
  23. </head>
  24. <body>
  25. <div id="docs-container">
  26. <div id="docs-top-navigation-container" class="body-background">
  27. <div id="docs-header">
  28. <div id="docs-version-header">
  29. Release: <span class="version-num">1.3.17</span>
  30. | Release Date: May 13, 2020
  31. </div>
  32. <h1>SQLAlchemy 1.3 Documentation</h1>
  33. </div>
  34. </div>
  35. <div id="docs-body-container">
  36. <div id="fixed-sidebar" class="withsidebar">
  37. <div id="docs-sidebar-popout">
  38. <h3><a href="index.html">SQLAlchemy 1.3 Documentation</a></h3>
  39. <p id="sidebar-topnav">
  40. <a href="contents.html">Contents</a> |
  41. <a href="genindex.html">Index</a>
  42. </p>
  43. <div id="sidebar-search">
  44. <form class="search" action="search.html" method="get">
  45. <label>
  46. Search terms:
  47. <input type="text" placeholder="search..." name="q" size="12" />
  48. </label>
  49. <input type="hidden" name="check_keywords" value="yes" />
  50. <input type="hidden" name="area" value="default" />
  51. </form>
  52. </div>
  53. </div>
  54. <div id="docs-sidebar">
  55. <div id="sidebar-banner">
  56. </div>
  57. <div id="docs-sidebar-inner">
  58. <h3>
  59. <a href="index.html" title="SQLAlchemy 1.3 Documentation">SQLAlchemy 1.3 Documentation</a>
  60. </h3>
  61. <ul>
  62. <li><span class="link-container"><a class="reference external" href="intro.html">Overview</a></span></li>
  63. <li><span class="link-container"><a class="reference external" href="orm/index.html">SQLAlchemy ORM</a></span></li>
  64. <li><span class="link-container"><a class="reference external" href="core/index.html">SQLAlchemy Core</a></span></li>
  65. <li><span class="link-container"><a class="reference external" href="dialects/index.html">Dialects</a></span></li>
  66. <li><span class="link-container"><a class="reference external" href="faq/index.html">Frequently Asked Questions</a></span></li>
  67. <li><span class="link-container"><a class="reference external" href="errors.html">Error Messages</a></span></li>
  68. <li><span class="link-container"><a class="reference external" href="changelog/index.html">Changes and Migration</a></span></li>
  69. </ul>
  70. </div>
  71. </div>
  72. </div>
  73. <div id="docs-body" class="withsidebar" >
  74. <div class="section" id="glossary">
  75. <span id="id1"></span><h1>Glossary<a class="headerlink" href="#glossary" title="Permalink to this headline">¶</a></h1>
  76. <dl class="glossary">
  77. <dt id="term-ACID">ACID</dt><dt id="term-ACID-model">ACID model</dt><dd><p>An acronym for “Atomicity, Consistency, Isolation,
  78. Durability”; a set of properties that guarantee that
  79. database transactions are processed reliably.
  80. (via Wikipedia)</p>
  81. <div class="admonition seealso">
  82. <p class="admonition-title">See also</p>
  83. <p><a class="reference internal" href="#term-atomicity"><span class="xref std std-term">atomicity</span></a></p>
  84. <p><a class="reference internal" href="#term-consistency"><span class="xref std std-term">consistency</span></a></p>
  85. <p><a class="reference internal" href="#term-isolation"><span class="xref std std-term">isolation</span></a></p>
  86. <p><a class="reference internal" href="#term-durability"><span class="xref std std-term">durability</span></a></p>
  87. <p><a class="reference external" href="http://en.wikipedia.org/wiki/ACID_Model">ACID Model (via Wikipedia)</a></p>
  88. </div>
  89. </dd>
  90. <dt id="term-annotations">annotations</dt><dd><p>Annotations are a concept used internally by SQLAlchemy in order to store
  91. additional information along with <a class="reference internal" href="core/sqlelement.html#sqlalchemy.sql.expression.ClauseElement" title="sqlalchemy.sql.expression.ClauseElement"><code class="xref py py-class docutils literal notranslate"><span class="pre">ClauseElement</span></code></a> objects. A Python
  92. dictionary is associated with a copy of the object, which contains key/value
  93. pairs significant to various internal systems, mostly within the ORM:</p>
  94. <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">some_column</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="s1">&#39;some_column&#39;</span><span class="p">,</span> <span class="n">Integer</span><span class="p">)</span>
  95. <span class="n">some_column_annotated</span> <span class="o">=</span> <span class="n">some_column</span><span class="o">.</span><span class="n">_annotate</span><span class="p">({</span><span class="s2">&quot;entity&quot;</span><span class="p">:</span> <span class="n">User</span><span class="p">})</span></pre></div>
  96. </div>
  97. <p>The annotation system differs from the public dictionary <a class="reference internal" href="core/metadata.html#sqlalchemy.schema.Column.info" title="sqlalchemy.schema.Column.info"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Column.info</span></code></a>
  98. in that the above annotation operation creates a <em>copy</em> of the new <a class="reference internal" href="core/metadata.html#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><code class="xref py py-class docutils literal notranslate"><span class="pre">Column</span></code></a>,
  99. rather than considering all annotation values to be part of a single
  100. unit. The ORM creates copies of expression objects in order to
  101. apply annotations that are specific to their context, such as to differentiate
  102. columns that should render themselves as relative to a joined-inheritance
  103. entity versus those which should render relative to their immediate parent
  104. table alone, as well as to differentiate columns within the “join condition”
  105. of a relationship where the column in some cases needs to be expressed
  106. in terms of one particular table alias or another, based on its position
  107. within the join expression.</p>
  108. </dd>
  109. <dt id="term-association-relationship">association relationship</dt><dd><p>A two-tiered <a class="reference internal" href="#term-relationship"><span class="xref std std-term">relationship</span></a> which links two tables
  110. together using an association table in the middle. The
  111. association relationship differs from a <a class="reference internal" href="#term-many-to-many"><span class="xref std std-term">many to many</span></a>
  112. relationship in that the many-to-many table is mapped
  113. by a full class, rather than invisibly handled by the
  114. <a class="reference internal" href="orm/relationship_api.html#sqlalchemy.orm.relationship" title="sqlalchemy.orm.relationship"><code class="xref py py-func docutils literal notranslate"><span class="pre">relationship()</span></code></a> construct as in the case
  115. with many-to-many, so that additional attributes are
  116. explicitly available.</p>
  117. <p>For example, if we wanted to associate employees with
  118. projects, also storing the specific role for that employee
  119. with the project, the relational schema might look like:</p>
  120. <div class="highlight-sql notranslate"><div class="highlight"><pre><span></span><span class="k">CREATE</span> <span class="k">TABLE</span> <span class="n">employee</span> <span class="p">(</span>
  121. <span class="n">id</span> <span class="nb">INTEGER</span> <span class="k">PRIMARY</span> <span class="k">KEY</span><span class="p">,</span>
  122. <span class="n">name</span> <span class="nb">VARCHAR</span><span class="p">(</span><span class="mi">30</span><span class="p">)</span>
  123. <span class="p">)</span>
  124. <span class="k">CREATE</span> <span class="k">TABLE</span> <span class="n">project</span> <span class="p">(</span>
  125. <span class="n">id</span> <span class="nb">INTEGER</span> <span class="k">PRIMARY</span> <span class="k">KEY</span><span class="p">,</span>
  126. <span class="n">name</span> <span class="nb">VARCHAR</span><span class="p">(</span><span class="mi">30</span><span class="p">)</span>
  127. <span class="p">)</span>
  128. <span class="k">CREATE</span> <span class="k">TABLE</span> <span class="n">employee_project</span> <span class="p">(</span>
  129. <span class="n">employee_id</span> <span class="nb">INTEGER</span> <span class="k">PRIMARY</span> <span class="k">KEY</span><span class="p">,</span>
  130. <span class="n">project_id</span> <span class="nb">INTEGER</span> <span class="k">PRIMARY</span> <span class="k">KEY</span><span class="p">,</span>
  131. <span class="n">role_name</span> <span class="nb">VARCHAR</span><span class="p">(</span><span class="mi">30</span><span class="p">),</span>
  132. <span class="k">FOREIGN</span> <span class="k">KEY</span> <span class="n">employee_id</span> <span class="k">REFERENCES</span> <span class="n">employee</span><span class="p">(</span><span class="n">id</span><span class="p">),</span>
  133. <span class="k">FOREIGN</span> <span class="k">KEY</span> <span class="n">project_id</span> <span class="k">REFERENCES</span> <span class="n">project</span><span class="p">(</span><span class="n">id</span><span class="p">)</span>
  134. <span class="p">)</span></pre></div>
  135. </div>
  136. <p>A SQLAlchemy declarative mapping for the above might look like:</p>
  137. <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Employee</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
  138. <span class="n">__tablename__</span> <span class="o">=</span> <span class="s1">&#39;employee&#39;</span>
  139. <span class="nb">id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="p">)</span>
  140. <span class="n">name</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">String</span><span class="p">(</span><span class="mi">30</span><span class="p">))</span>
  141. <span class="k">class</span> <span class="nc">Project</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
  142. <span class="n">__tablename__</span> <span class="o">=</span> <span class="s1">&#39;project&#39;</span>
  143. <span class="nb">id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="p">)</span>
  144. <span class="n">name</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">String</span><span class="p">(</span><span class="mi">30</span><span class="p">))</span>
  145. <span class="k">class</span> <span class="nc">EmployeeProject</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
  146. <span class="n">__tablename__</span> <span class="o">=</span> <span class="s1">&#39;employee_project&#39;</span>
  147. <span class="n">employee_id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">ForeignKey</span><span class="p">(</span><span class="s1">&#39;employee.id&#39;</span><span class="p">),</span> <span class="n">primary_key</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
  148. <span class="n">project_id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">ForeignKey</span><span class="p">(</span><span class="s1">&#39;project.id&#39;</span><span class="p">),</span> <span class="n">primary_key</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
  149. <span class="n">role_name</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">String</span><span class="p">(</span><span class="mi">30</span><span class="p">))</span>
  150. <span class="n">project</span> <span class="o">=</span> <span class="n">relationship</span><span class="p">(</span><span class="s2">&quot;Project&quot;</span><span class="p">,</span> <span class="n">backref</span><span class="o">=</span><span class="s2">&quot;project_employees&quot;</span><span class="p">)</span>
  151. <span class="n">employee</span> <span class="o">=</span> <span class="n">relationship</span><span class="p">(</span><span class="s2">&quot;Employee&quot;</span><span class="p">,</span> <span class="n">backref</span><span class="o">=</span><span class="s2">&quot;employee_projects&quot;</span><span class="p">)</span></pre></div>
  152. </div>
  153. <p>Employees can be added to a project given a role name:</p>
  154. <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">proj</span> <span class="o">=</span> <span class="n">Project</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s2">&quot;Client A&quot;</span><span class="p">)</span>
  155. <span class="n">emp1</span> <span class="o">=</span> <span class="n">Employee</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s2">&quot;emp1&quot;</span><span class="p">)</span>
  156. <span class="n">emp2</span> <span class="o">=</span> <span class="n">Employee</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s2">&quot;emp2&quot;</span><span class="p">)</span>
  157. <span class="n">proj</span><span class="o">.</span><span class="n">project_employees</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span>
  158. <span class="n">EmployeeProject</span><span class="p">(</span><span class="n">employee</span><span class="o">=</span><span class="n">emp1</span><span class="p">,</span> <span class="n">role</span><span class="o">=</span><span class="s2">&quot;tech lead&quot;</span><span class="p">),</span>
  159. <span class="n">EmployeeProject</span><span class="p">(</span><span class="n">employee</span><span class="o">=</span><span class="n">emp2</span><span class="p">,</span> <span class="n">role</span><span class="o">=</span><span class="s2">&quot;account executive&quot;</span><span class="p">)</span>
  160. <span class="p">])</span></pre></div>
  161. </div>
  162. <div class="admonition seealso">
  163. <p class="admonition-title">See also</p>
  164. <p><a class="reference internal" href="#term-many-to-many"><span class="xref std std-term">many to many</span></a></p>
  165. </div>
  166. </dd>
  167. <dt id="term-atomicity">atomicity</dt><dd><p>Atomicity is one of the components of the <a class="reference internal" href="#term-ACID"><span class="xref std std-term">ACID</span></a> model,
  168. and requires that each transaction is “all or nothing”:
  169. if one part of the transaction fails, the entire transaction
  170. fails, and the database state is left unchanged. An atomic
  171. system must guarantee atomicity in each and every situation,
  172. including power failures, errors, and crashes.
  173. (via Wikipedia)</p>
  174. <div class="admonition seealso">
  175. <p class="admonition-title">See also</p>
  176. <p><a class="reference internal" href="#term-ACID"><span class="xref std std-term">ACID</span></a></p>
  177. <p><a class="reference external" href="http://en.wikipedia.org/wiki/Atomicity_(database_systems)">Atomicity (via Wikipedia)</a></p>
  178. </div>
  179. </dd>
  180. <dt id="term-backref">backref</dt><dt id="term-bidirectional-relationship">bidirectional relationship</dt><dd><p>An extension to the <a class="reference internal" href="#term-relationship"><span class="xref std std-term">relationship</span></a> system whereby two
  181. distinct <a class="reference internal" href="orm/relationship_api.html#sqlalchemy.orm.relationship" title="sqlalchemy.orm.relationship"><code class="xref py py-func docutils literal notranslate"><span class="pre">relationship()</span></code></a> objects can be
  182. mutually associated with each other, such that they coordinate
  183. in memory as changes occur to either side. The most common
  184. way these two relationships are constructed is by using
  185. the <a class="reference internal" href="orm/relationship_api.html#sqlalchemy.orm.relationship" title="sqlalchemy.orm.relationship"><code class="xref py py-func docutils literal notranslate"><span class="pre">relationship()</span></code></a> function explicitly
  186. for one side and specifying the <code class="docutils literal notranslate"><span class="pre">backref</span></code> keyword to it so that
  187. the other <a class="reference internal" href="orm/relationship_api.html#sqlalchemy.orm.relationship" title="sqlalchemy.orm.relationship"><code class="xref py py-func docutils literal notranslate"><span class="pre">relationship()</span></code></a> is created
  188. automatically. We can illustrate this against the example we’ve
  189. used in <a class="reference internal" href="#term-one-to-many"><span class="xref std std-term">one to many</span></a> as follows:</p>
  190. <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Department</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
  191. <span class="n">__tablename__</span> <span class="o">=</span> <span class="s1">&#39;department&#39;</span>
  192. <span class="nb">id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
  193. <span class="n">name</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">String</span><span class="p">(</span><span class="mi">30</span><span class="p">))</span>
  194. <span class="n">employees</span> <span class="o">=</span> <span class="n">relationship</span><span class="p">(</span><span class="s2">&quot;Employee&quot;</span><span class="p">,</span> <span class="n">backref</span><span class="o">=</span><span class="s2">&quot;department&quot;</span><span class="p">)</span>
  195. <span class="k">class</span> <span class="nc">Employee</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
  196. <span class="n">__tablename__</span> <span class="o">=</span> <span class="s1">&#39;employee&#39;</span>
  197. <span class="nb">id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
  198. <span class="n">name</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">String</span><span class="p">(</span><span class="mi">30</span><span class="p">))</span>
  199. <span class="n">dep_id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">ForeignKey</span><span class="p">(</span><span class="s1">&#39;department.id&#39;</span><span class="p">))</span></pre></div>
  200. </div>
  201. <p>A backref can be applied to any relationship, including one to many,
  202. many to one, and <a class="reference internal" href="#term-many-to-many"><span class="xref std std-term">many to many</span></a>.</p>
  203. <div class="admonition seealso">
  204. <p class="admonition-title">See also</p>
  205. <p><a class="reference internal" href="#term-relationship"><span class="xref std std-term">relationship</span></a></p>
  206. <p><a class="reference internal" href="#term-one-to-many"><span class="xref std std-term">one to many</span></a></p>
  207. <p><a class="reference internal" href="#term-many-to-one"><span class="xref std std-term">many to one</span></a></p>
  208. <p><a class="reference internal" href="#term-many-to-many"><span class="xref std std-term">many to many</span></a></p>
  209. </div>
  210. </dd>
  211. <dt id="term-candidate-key">candidate key</dt><dd><p>A <a class="reference internal" href="#term-relational-algebra"><span class="xref std std-term">relational algebra</span></a> term referring to an attribute or set
  212. of attributes that form a uniquely identifying key for a
  213. row. A row may have more than one candidate key, each of which
  214. is suitable for use as the primary key of that row.
  215. The primary key of a table is always a candidate key.</p>
  216. <div class="admonition seealso">
  217. <p class="admonition-title">See also</p>
  218. <p><a class="reference internal" href="#term-primary-key"><span class="xref std std-term">primary key</span></a></p>
  219. <p><a class="reference external" href="http://en.wikipedia.org/wiki/Candidate_key">Candidate key (via Wikipedia)</a></p>
  220. <p><a class="reference external" href="https://www.databasestar.com/database-keys/">https://www.databasestar.com/database-keys/</a></p>
  221. </div>
  222. </dd>
  223. <dt id="term-cascade">cascade</dt><dd><p>A term used in SQLAlchemy to describe how an ORM persistence action that
  224. takes place on a particular object would extend into other objects
  225. which are directly associated with that object. In SQLAlchemy, these
  226. object associations are configured using the <a class="reference internal" href="orm/relationship_api.html#sqlalchemy.orm.relationship" title="sqlalchemy.orm.relationship"><code class="xref py py-func docutils literal notranslate"><span class="pre">relationship()</span></code></a>
  227. construct. <a class="reference internal" href="orm/relationship_api.html#sqlalchemy.orm.relationship" title="sqlalchemy.orm.relationship"><code class="xref py py-func docutils literal notranslate"><span class="pre">relationship()</span></code></a> contains a parameter called
  228. <a class="reference internal" href="orm/relationship_api.html#sqlalchemy.orm.relationship.params.cascade" title="sqlalchemy.orm.relationship"><code class="xref py py-paramref docutils literal notranslate"><span class="pre">relationship.cascade</span></code></a> which provides options on how certain
  229. persistence operations may cascade.</p>
  230. <p>The term “cascades” as well as the general architecture of this system
  231. in SQLAlchemy was borrowed, for better or worse, from the Hibernate
  232. ORM.</p>
  233. <div class="admonition seealso">
  234. <p class="admonition-title">See also</p>
  235. <p><a class="reference internal" href="orm/cascades.html#unitofwork-cascades"><span class="std std-ref">Cascades</span></a></p>
  236. </div>
  237. </dd>
  238. <dt id="term-check-constraint">check constraint</dt><dd><p>A check constraint is a
  239. condition that defines valid data when adding or updating an
  240. entry in a table of a relational database. A check constraint
  241. is applied to each row in the table.</p>
  242. <p>(via Wikipedia)</p>
  243. <p>A check constraint can be added to a table in standard
  244. SQL using <a class="reference internal" href="#term-DDL"><span class="xref std std-term">DDL</span></a> like the following:</p>
  245. <div class="highlight-sql notranslate"><div class="highlight"><pre><span></span><span class="k">ALTER</span> <span class="k">TABLE</span> <span class="n">distributors</span> <span class="k">ADD</span> <span class="k">CONSTRAINT</span> <span class="n">zipchk</span> <span class="k">CHECK</span> <span class="p">(</span><span class="k">char_length</span><span class="p">(</span><span class="n">zipcode</span><span class="p">)</span> <span class="o">=</span> <span class="mi">5</span><span class="p">);</span></pre></div>
  246. </div>
  247. <div class="admonition seealso">
  248. <p class="admonition-title">See also</p>
  249. <p><a class="reference external" href="http://en.wikipedia.org/wiki/Check_constraint">CHECK constraint (via Wikipedia)</a></p>
  250. </div>
  251. </dd>
  252. <dt id="term-columns-clause">columns clause</dt><dd><p>The portion of the <code class="docutils literal notranslate"><span class="pre">SELECT</span></code> statement which enumerates the
  253. SQL expressions to be returned in the result set. The expressions
  254. follow the <code class="docutils literal notranslate"><span class="pre">SELECT</span></code> keyword directly and are a comma-separated
  255. list of individual expressions.</p>
  256. <p>E.g.:</p>
  257. <div class="highlight-sql notranslate"><div class="highlight"><pre><span></span><span class="k">SELECT</span> <span class="n">user_account</span><span class="p">.</span><span class="n">name</span><span class="p">,</span> <span class="n">user_account</span><span class="p">.</span><span class="n">email</span>
  258. <span class="k">FROM</span> <span class="n">user_account</span> <span class="k">WHERE</span> <span class="n">user_account</span><span class="p">.</span><span class="n">name</span> <span class="o">=</span> <span class="s1">&#39;fred&#39;</span></pre></div>
  259. </div>
  260. <p>Above, the list of columns <code class="docutils literal notranslate"><span class="pre">user_acount.name</span></code>,
  261. <code class="docutils literal notranslate"><span class="pre">user_account.email</span></code> is the columns clause of the <code class="docutils literal notranslate"><span class="pre">SELECT</span></code>.</p>
  262. </dd>
  263. <dt id="term-consistency">consistency</dt><dd><p>Consistency is one of the components of the <a class="reference internal" href="#term-ACID"><span class="xref std std-term">ACID</span></a> model,
  264. and ensures that any transaction will
  265. bring the database from one valid state to another. Any data
  266. written to the database must be valid according to all defined
  267. rules, including but not limited to <a class="reference internal" href="#term-constraints"><span class="xref std std-term">constraints</span></a>, cascades,
  268. triggers, and any combination thereof.
  269. (via Wikipedia)</p>
  270. <div class="admonition seealso">
  271. <p class="admonition-title">See also</p>
  272. <p><a class="reference internal" href="#term-ACID"><span class="xref std std-term">ACID</span></a></p>
  273. <p><a class="reference external" href="http://en.wikipedia.org/wiki/Consistency_(database_systems)">Consistency (via Wikipedia)</a></p>
  274. </div>
  275. </dd>
  276. <dt id="term-constraint">constraint</dt><dt id="term-constraints">constraints</dt><dt id="term-constrained">constrained</dt><dd><p>Rules established within a relational database that ensure
  277. the validity and consistency of data. Common forms
  278. of constraint include <a class="reference internal" href="#term-primary-key-constraint"><span class="xref std std-term">primary key constraint</span></a>,
  279. <a class="reference internal" href="#term-foreign-key-constraint"><span class="xref std std-term">foreign key constraint</span></a>, and <a class="reference internal" href="#term-check-constraint"><span class="xref std std-term">check constraint</span></a>.</p>
  280. </dd>
  281. <dt id="term-correlates">correlates</dt><dt id="term-correlated-subquery">correlated subquery</dt><dt id="term-correlated-subqueries">correlated subqueries</dt><dd><p>A <a class="reference internal" href="#term-subquery"><span class="xref std std-term">subquery</span></a> is correlated if it depends on data in the
  282. enclosing <code class="docutils literal notranslate"><span class="pre">SELECT</span></code>.</p>
  283. <p>Below, a subquery selects the aggregate value <code class="docutils literal notranslate"><span class="pre">MIN(a.id)</span></code>
  284. from the <code class="docutils literal notranslate"><span class="pre">email_address</span></code> table, such that
  285. it will be invoked for each value of <code class="docutils literal notranslate"><span class="pre">user_account.id</span></code>, correlating
  286. the value of this column against the <code class="docutils literal notranslate"><span class="pre">email_address.user_account_id</span></code>
  287. column:</p>
  288. <div class="highlight-sql notranslate"><div class="highlight"><pre><span></span><span class="k">SELECT</span> <span class="n">user_account</span><span class="p">.</span><span class="n">name</span><span class="p">,</span> <span class="n">email_address</span><span class="p">.</span><span class="n">email</span>
  289. <span class="k">FROM</span> <span class="n">user_account</span>
  290. <span class="k">JOIN</span> <span class="n">email_address</span> <span class="k">ON</span> <span class="n">user_account</span><span class="p">.</span><span class="n">id</span><span class="o">=</span><span class="n">email_address</span><span class="p">.</span><span class="n">user_account_id</span>
  291. <span class="k">WHERE</span> <span class="n">email_address</span><span class="p">.</span><span class="n">id</span> <span class="o">=</span> <span class="p">(</span>
  292. <span class="k">SELECT</span> <span class="k">MIN</span><span class="p">(</span><span class="n">a</span><span class="p">.</span><span class="n">id</span><span class="p">)</span> <span class="k">FROM</span> <span class="n">email_address</span> <span class="k">AS</span> <span class="n">a</span>
  293. <span class="k">WHERE</span> <span class="n">a</span><span class="p">.</span><span class="n">user_account_id</span><span class="o">=</span><span class="n">user_account</span><span class="p">.</span><span class="n">id</span>
  294. <span class="p">)</span></pre></div>
  295. </div>
  296. <p>The above subquery refers to the <code class="docutils literal notranslate"><span class="pre">user_account</span></code> table, which is not itself
  297. in the <code class="docutils literal notranslate"><span class="pre">FROM</span></code> clause of this nested query. Instead, the <code class="docutils literal notranslate"><span class="pre">user_account</span></code>
  298. table is received from the enclosing query, where each row selected from
  299. <code class="docutils literal notranslate"><span class="pre">user_account</span></code> results in a distinct execution of the subquery.</p>
  300. <p>A correlated subquery is in most cases present in the <a class="reference internal" href="#term-WHERE-clause"><span class="xref std std-term">WHERE clause</span></a>
  301. or <a class="reference internal" href="#term-columns-clause"><span class="xref std std-term">columns clause</span></a> of the immediately enclosing <code class="docutils literal notranslate"><span class="pre">SELECT</span></code>
  302. statement, as well as in the ORDER BY or HAVING clause.</p>
  303. <p>In less common cases, a correlated subquery may be present in the
  304. <a class="reference internal" href="#term-FROM-clause"><span class="xref std std-term">FROM clause</span></a> of an enclosing <code class="docutils literal notranslate"><span class="pre">SELECT</span></code>; in these cases the
  305. correlation is typically due to the enclosing <code class="docutils literal notranslate"><span class="pre">SELECT</span></code> itself being
  306. enclosed in the WHERE,
  307. ORDER BY, columns or HAVING clause of another <code class="docutils literal notranslate"><span class="pre">SELECT</span></code>, such as:</p>
  308. <div class="highlight-sql notranslate"><div class="highlight"><pre><span></span><span class="k">SELECT</span> <span class="n">parent</span><span class="p">.</span><span class="n">id</span> <span class="k">FROM</span> <span class="n">parent</span>
  309. <span class="k">WHERE</span> <span class="k">EXISTS</span> <span class="p">(</span>
  310. <span class="k">SELECT</span> <span class="o">*</span> <span class="k">FROM</span> <span class="p">(</span>
  311. <span class="k">SELECT</span> <span class="n">child</span><span class="p">.</span><span class="n">id</span> <span class="k">AS</span> <span class="n">id</span><span class="p">,</span> <span class="n">child</span><span class="p">.</span><span class="n">parent_id</span> <span class="k">AS</span> <span class="n">parent_id</span><span class="p">,</span> <span class="n">child</span><span class="p">.</span><span class="n">pos</span> <span class="k">AS</span> <span class="n">pos</span>
  312. <span class="k">FROM</span> <span class="n">child</span>
  313. <span class="k">WHERE</span> <span class="n">child</span><span class="p">.</span><span class="n">parent_id</span> <span class="o">=</span> <span class="n">parent</span><span class="p">.</span><span class="n">id</span> <span class="k">ORDER</span> <span class="k">BY</span> <span class="n">child</span><span class="p">.</span><span class="n">pos</span>
  314. <span class="k">LIMIT</span> <span class="mi">3</span><span class="p">)</span>
  315. <span class="k">WHERE</span> <span class="n">id</span> <span class="o">=</span> <span class="mi">7</span><span class="p">)</span></pre></div>
  316. </div>
  317. <p>Correlation from one <code class="docutils literal notranslate"><span class="pre">SELECT</span></code> directly to one which encloses the correlated
  318. query via its <code class="docutils literal notranslate"><span class="pre">FROM</span></code>
  319. clause is not possible, because the correlation can only proceed once the
  320. original source rows from the enclosing statement’s FROM clause are available.</p>
  321. </dd>
  322. <dt id="term-crud">crud</dt><dd><p>An acronym meaning “Create, Update, Delete”. The term in SQL refers to the
  323. set of operations that create, modify and delete data from the database,
  324. also known as <a class="reference internal" href="#term-DML"><span class="xref std std-term">DML</span></a>, and typically refers to the <code class="docutils literal notranslate"><span class="pre">INSERT</span></code>,
  325. <code class="docutils literal notranslate"><span class="pre">UPDATE</span></code>, and <code class="docutils literal notranslate"><span class="pre">DELETE</span></code> statements.</p>
  326. </dd>
  327. <dt id="term-DBAPI">DBAPI</dt><dd><p>DBAPI is shorthand for the phrase “Python Database API
  328. Specification”. This is a widely used specification
  329. within Python to define common usage patterns for all
  330. database connection packages. The DBAPI is a “low level”
  331. API which is typically the lowest level system used
  332. in a Python application to talk to a database. SQLAlchemy’s
  333. <a class="reference internal" href="#term-dialect"><span class="xref std std-term">dialect</span></a> system is constructed around the
  334. operation of the DBAPI, providing individual dialect
  335. classes which service a specific DBAPI on top of a
  336. specific database engine; for example, the <a class="reference internal" href="core/engines.html#sqlalchemy.create_engine" title="sqlalchemy.create_engine"><code class="xref py py-func docutils literal notranslate"><span class="pre">create_engine()</span></code></a>
  337. URL <code class="docutils literal notranslate"><span class="pre">postgresql+psycopg2://&#64;localhost/test</span></code>
  338. refers to the <a class="reference internal" href="dialects/postgresql.html#module-sqlalchemy.dialects.postgresql.psycopg2" title="sqlalchemy.dialects.postgresql.psycopg2"><code class="xref py py-mod docutils literal notranslate"><span class="pre">psycopg2</span></code></a>
  339. DBAPI/dialect combination, whereas the URL <code class="docutils literal notranslate"><span class="pre">mysql+mysqldb://&#64;localhost/test</span></code>
  340. refers to the <a class="reference internal" href="dialects/mysql.html#module-sqlalchemy.dialects.mysql.mysqldb" title="sqlalchemy.dialects.mysql.mysqldb"><code class="xref py py-mod docutils literal notranslate"><span class="pre">MySQL</span> <span class="pre">for</span> <span class="pre">Python</span></code></a>
  341. DBAPI DBAPI/dialect combination.</p>
  342. <div class="admonition seealso">
  343. <p class="admonition-title">See also</p>
  344. <p><a class="reference external" href="http://www.python.org/dev/peps/pep-0249/">PEP 249 - Python Database API Specification v2.0</a></p>
  345. </div>
  346. </dd>
  347. <dt id="term-DDL">DDL</dt><dd><p>An acronym for <strong>Data Definition Language</strong>. DDL is the subset
  348. of SQL that relational databases use to configure tables, constraints,
  349. and other permanent objects within a database schema. SQLAlchemy
  350. provides a rich API for constructing and emitting DDL expressions.</p>
  351. <div class="admonition seealso">
  352. <p class="admonition-title">See also</p>
  353. <p><a class="reference internal" href="core/metadata.html"><span class="std std-ref">Describing Databases with MetaData</span></a></p>
  354. <p><a class="reference external" href="http://en.wikipedia.org/wiki/Data_definition_language">DDL (via Wikipedia)</a></p>
  355. <p><a class="reference internal" href="#term-DML"><span class="xref std std-term">DML</span></a></p>
  356. </div>
  357. </dd>
  358. <dt id="term-deleted">deleted</dt><dd><p>This describes one of the major object states which
  359. an object can have within a <a class="reference internal" href="#term-Session"><span class="xref std std-term">session</span></a>; a deleted object
  360. is an object that was formerly persistent and has had a
  361. DELETE statement emitted to the database within a flush
  362. to delete its row. The object will move to the <a class="reference internal" href="#term-detached"><span class="xref std std-term">detached</span></a>
  363. state once the session’s transaction is committed; alternatively,
  364. if the session’s transaction is rolled back, the DELETE is
  365. reverted and the object moves back to the <a class="reference internal" href="#term-persistent"><span class="xref std std-term">persistent</span></a>
  366. state.</p>
  367. <div class="admonition seealso">
  368. <p class="admonition-title">See also</p>
  369. <p><a class="reference internal" href="orm/session_state_management.html#session-object-states"><span class="std std-ref">Quickie Intro to Object States</span></a></p>
  370. </div>
  371. </dd>
  372. <dt id="term-descriptor">descriptor</dt><dt id="term-descriptors">descriptors</dt><dd><p>In Python, a descriptor is an object attribute with “binding behavior”, one whose attribute access has been overridden by methods in the <a class="reference external" href="http://docs.python.org/howto/descriptor.html">descriptor protocol</a>.
  373. Those methods are __get__(), __set__(), and __delete__(). If any of those methods are defined
  374. for an object, it is said to be a descriptor.</p>
  375. <p>In SQLAlchemy, descriptors are used heavily in order to provide attribute behavior
  376. on mapped classes. When a class is mapped as such:</p>
  377. <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">MyClass</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
  378. <span class="n">__tablename__</span> <span class="o">=</span> <span class="s1">&#39;foo&#39;</span>
  379. <span class="nb">id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
  380. <span class="n">data</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">String</span><span class="p">)</span></pre></div>
  381. </div>
  382. <p>The <code class="docutils literal notranslate"><span class="pre">MyClass</span></code> class will be <a class="reference internal" href="#term-mapped"><span class="xref std std-term">mapped</span></a> when its definition
  383. is complete, at which point the <code class="docutils literal notranslate"><span class="pre">id</span></code> and <code class="docutils literal notranslate"><span class="pre">data</span></code> attributes,
  384. starting out as <a class="reference internal" href="core/metadata.html#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><code class="xref py py-class docutils literal notranslate"><span class="pre">Column</span></code></a> objects, will be replaced
  385. by the <a class="reference internal" href="#term-instrumentation"><span class="xref std std-term">instrumentation</span></a> system with instances
  386. of <a class="reference internal" href="orm/internals.html#sqlalchemy.orm.attributes.InstrumentedAttribute" title="sqlalchemy.orm.attributes.InstrumentedAttribute"><code class="xref py py-class docutils literal notranslate"><span class="pre">InstrumentedAttribute</span></code></a>, which are descriptors that
  387. provide the above mentioned <code class="docutils literal notranslate"><span class="pre">__get__()</span></code>, <code class="docutils literal notranslate"><span class="pre">__set__()</span></code> and
  388. <code class="docutils literal notranslate"><span class="pre">__delete__()</span></code> methods. The <a class="reference internal" href="orm/internals.html#sqlalchemy.orm.attributes.InstrumentedAttribute" title="sqlalchemy.orm.attributes.InstrumentedAttribute"><code class="xref py py-class docutils literal notranslate"><span class="pre">InstrumentedAttribute</span></code></a>
  389. will generate a SQL expression when used at the class level:</p>
  390. <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">MyClass</span><span class="o">.</span><span class="n">data</span> <span class="o">==</span> <span class="mi">5</span><span class="p">)</span>
  391. <span class="go">data = :data_1</span></pre></div>
  392. </div>
  393. <p>and at the instance level, keeps track of changes to values,
  394. and also <a class="reference internal" href="#term-lazy-loads"><span class="xref std std-term">lazy loads</span></a> unloaded attributes
  395. from the database:</p>
  396. <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">m1</span> <span class="o">=</span> <span class="n">MyClass</span><span class="p">()</span>
  397. <span class="gp">&gt;&gt;&gt; </span><span class="n">m1</span><span class="o">.</span><span class="n">id</span> <span class="o">=</span> <span class="mi">5</span>
  398. <span class="gp">&gt;&gt;&gt; </span><span class="n">m1</span><span class="o">.</span><span class="n">data</span> <span class="o">=</span> <span class="s2">&quot;some data&quot;</span>
  399. <span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sqlalchemy</span> <span class="k">import</span> <span class="n">inspect</span>
  400. <span class="gp">&gt;&gt;&gt; </span><span class="n">inspect</span><span class="p">(</span><span class="n">m1</span><span class="p">)</span><span class="o">.</span><span class="n">attrs</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">history</span><span class="o">.</span><span class="n">added</span>
  401. <span class="go">&quot;some data&quot;</span></pre></div>
  402. </div>
  403. </dd>
  404. <dt id="term-detached">detached</dt><dd><p>This describes one of the major object states which
  405. an object can have within a <a class="reference internal" href="#term-Session"><span class="xref std std-term">session</span></a>; a detached object
  406. is an object that has a database identity (i.e. a primary key)
  407. but is not associated with any session. An object that
  408. was previously <a class="reference internal" href="#term-persistent"><span class="xref std std-term">persistent</span></a> and was removed from its
  409. session either because it was expunged, or the owning
  410. session was closed, moves into the detached state.
  411. The detached state is generally used when objects are being
  412. moved between sessions or when being moved to/from an external
  413. object cache.</p>
  414. <div class="admonition seealso">
  415. <p class="admonition-title">See also</p>
  416. <p><a class="reference internal" href="orm/session_state_management.html#session-object-states"><span class="std std-ref">Quickie Intro to Object States</span></a></p>
  417. </div>
  418. </dd>
  419. <dt id="term-dialect">dialect</dt><dd><p>In SQLAlchemy, the “dialect” is a Python object that represents information
  420. and methods that allow database operations to proceed on a particular
  421. kind of database backend and a particular kind of Python driver (or
  422. :term`DBAPI`) for that database. SQLAlchemy dialects are subclasses
  423. of the <a class="reference internal" href="core/internals.html#sqlalchemy.engine.interfaces.Dialect" title="sqlalchemy.engine.interfaces.Dialect"><code class="xref py py-class docutils literal notranslate"><span class="pre">Dialect</span></code></a> class.</p>
  424. <div class="admonition seealso">
  425. <p class="admonition-title">See also</p>
  426. <p><a class="reference internal" href="core/engines.html"><span class="std std-ref">Engine Configuration</span></a></p>
  427. </div>
  428. </dd>
  429. <dt id="term-discriminator">discriminator</dt><dd><p>A result-set column which is used during <a class="reference internal" href="#term-polymorphic"><span class="xref std std-term">polymorphic</span></a> loading
  430. to determine what kind of mapped class should be applied to a particular
  431. incoming result row. In SQLAlchemy, the classes are always part
  432. of a hierarchy mapping using inheritance mapping.</p>
  433. <div class="admonition seealso">
  434. <p class="admonition-title">See also</p>
  435. <p><a class="reference internal" href="orm/inheritance.html"><span class="std std-ref">Mapping Class Inheritance Hierarchies</span></a></p>
  436. </div>
  437. </dd>
  438. <dt id="term-DML">DML</dt><dd><p>An acronym for <strong>Data Manipulation Language</strong>. DML is the subset of
  439. SQL that relational databases use to <em>modify</em> the data in tables. DML
  440. typically refers to the three widely familiar statements of INSERT,
  441. UPDATE and DELETE, otherwise known as <a class="reference internal" href="#term-crud"><span class="xref std std-term">CRUD</span></a> (acronoym for “CReate,
  442. Update, Delete”).</p>
  443. <blockquote>
  444. <div><div class="admonition seealso">
  445. <p class="admonition-title">See also</p>
  446. <p><a class="reference external" href="http://en.wikipedia.org/wiki/Data_manipulation_language">DML (via Wikipedia)</a></p>
  447. <p><a class="reference internal" href="#term-DDL"><span class="xref std std-term">DDL</span></a></p>
  448. </div>
  449. </div></blockquote>
  450. </dd>
  451. <dt id="term-domain-model">domain model</dt><dd><p>A domain model in problem solving and software engineering is a conceptual model of all the topics related to a specific problem. It describes the various entities, their attributes, roles, and relationships, plus the constraints that govern the problem domain.</p>
  452. <p>(via Wikipedia)</p>
  453. <div class="admonition seealso">
  454. <p class="admonition-title">See also</p>
  455. <p><a class="reference external" href="http://en.wikipedia.org/wiki/Domain_model">Domain Model (via Wikipedia)</a></p>
  456. </div>
  457. </dd>
  458. <dt id="term-durability">durability</dt><dd><p>Durability is a property of the <a class="reference internal" href="#term-ACID"><span class="xref std std-term">ACID</span></a> model
  459. which means that once a transaction has been committed,
  460. it will remain so, even in the event of power loss, crashes,
  461. or errors. In a relational database, for instance, once a
  462. group of SQL statements execute, the results need to be stored
  463. permanently (even if the database crashes immediately
  464. thereafter).
  465. (via Wikipedia)</p>
  466. <div class="admonition seealso">
  467. <p class="admonition-title">See also</p>
  468. <p><a class="reference internal" href="#term-ACID"><span class="xref std std-term">ACID</span></a></p>
  469. <p><a class="reference external" href="http://en.wikipedia.org/wiki/Durability_(database_systems)">Durability (via Wikipedia)</a></p>
  470. </div>
  471. </dd>
  472. <dt id="term-eager-load">eager load</dt><dt id="term-eager-loads">eager loads</dt><dt id="term-eager-loaded">eager loaded</dt><dt id="term-eager-loading">eager loading</dt><dd><p>In object relational mapping, an “eager load” refers to
  473. an attribute that is populated with its database-side value
  474. at the same time as when the object itself is loaded from the database.
  475. In SQLAlchemy, “eager loading” usually refers to related collections
  476. of objects that are mapped using the <a class="reference internal" href="orm/relationship_api.html#sqlalchemy.orm.relationship" title="sqlalchemy.orm.relationship"><code class="xref py py-func docutils literal notranslate"><span class="pre">relationship()</span></code></a> construct.
  477. Eager loading is the opposite of <a class="reference internal" href="#term-lazy-loading"><span class="xref std std-term">lazy loading</span></a>.</p>
  478. <div class="admonition seealso">
  479. <p class="admonition-title">See also</p>
  480. <p><a class="reference internal" href="orm/loading_relationships.html"><span class="doc">Relationship Loading Techniques</span></a></p>
  481. </div>
  482. </dd>
  483. <dt id="term-expire">expire</dt><dt id="term-expires">expires</dt><dt id="term-expiring">expiring</dt><dd><p>In the SQLAlchemy ORM, refers to when the data in a <a class="reference internal" href="#term-persistent"><span class="xref std std-term">persistent</span></a>
  484. or sometimes <a class="reference internal" href="#term-detached"><span class="xref std std-term">detached</span></a> object is erased, such that when
  485. the object’s attributes are next accessed, a <a class="reference internal" href="#term-lazy-load"><span class="xref std std-term">lazy load</span></a> SQL
  486. query will be emitted in order to refresh the data for this object
  487. as stored in the current ongoing transaction.</p>
  488. <div class="admonition seealso">
  489. <p class="admonition-title">See also</p>
  490. <p><a class="reference internal" href="orm/session_state_management.html#session-expire"><span class="std std-ref">Refreshing / Expiring</span></a></p>
  491. </div>
  492. </dd>
  493. <dt id="term-foreign-key-constraint">foreign key constraint</dt><dd><p>A referential constraint between two tables. A foreign key is a field or set of fields in a
  494. relational table that matches a <a class="reference internal" href="#term-candidate-key"><span class="xref std std-term">candidate key</span></a> of another table.
  495. The foreign key can be used to cross-reference tables.
  496. (via Wikipedia)</p>
  497. <p>A foreign key constraint can be added to a table in standard
  498. SQL using <a class="reference internal" href="#term-DDL"><span class="xref std std-term">DDL</span></a> like the following:</p>
  499. <div class="highlight-sql notranslate"><div class="highlight"><pre><span></span><span class="k">ALTER</span> <span class="k">TABLE</span> <span class="n">employee</span> <span class="k">ADD</span> <span class="k">CONSTRAINT</span> <span class="n">dep_id_fk</span>
  500. <span class="k">FOREIGN</span> <span class="k">KEY</span> <span class="p">(</span><span class="n">employee</span><span class="p">)</span> <span class="k">REFERENCES</span> <span class="n">department</span> <span class="p">(</span><span class="n">dep_id</span><span class="p">)</span></pre></div>
  501. </div>
  502. <div class="admonition seealso">
  503. <p class="admonition-title">See also</p>
  504. <p><a class="reference external" href="http://en.wikipedia.org/wiki/Foreign_key_constraint">Foreign Key Constraint (via Wikipedia)</a></p>
  505. </div>
  506. </dd>
  507. <dt id="term-FROM-clause">FROM clause</dt><dd><p>The portion of the <code class="docutils literal notranslate"><span class="pre">SELECT</span></code> statement which indicates the initial
  508. source of rows.</p>
  509. <p>A simple <code class="docutils literal notranslate"><span class="pre">SELECT</span></code> will feature one or more table names in its
  510. FROM clause. Multiple sources are separated by a comma:</p>
  511. <div class="highlight-sql notranslate"><div class="highlight"><pre><span></span><span class="k">SELECT</span> <span class="k">user</span><span class="p">.</span><span class="n">name</span><span class="p">,</span> <span class="n">address</span><span class="p">.</span><span class="n">email_address</span>
  512. <span class="k">FROM</span> <span class="k">user</span><span class="p">,</span> <span class="n">address</span>
  513. <span class="k">WHERE</span> <span class="k">user</span><span class="p">.</span><span class="n">id</span><span class="o">=</span><span class="n">address</span><span class="p">.</span><span class="n">user_id</span></pre></div>
  514. </div>
  515. <p>The FROM clause is also where explicit joins are specified. We can
  516. rewrite the above <code class="docutils literal notranslate"><span class="pre">SELECT</span></code> using a single <code class="docutils literal notranslate"><span class="pre">FROM</span></code> element which consists
  517. of a <code class="docutils literal notranslate"><span class="pre">JOIN</span></code> of the two tables:</p>
  518. <div class="highlight-sql notranslate"><div class="highlight"><pre><span></span><span class="k">SELECT</span> <span class="k">user</span><span class="p">.</span><span class="n">name</span><span class="p">,</span> <span class="n">address</span><span class="p">.</span><span class="n">email_address</span>
  519. <span class="k">FROM</span> <span class="k">user</span> <span class="k">JOIN</span> <span class="n">address</span> <span class="k">ON</span> <span class="k">user</span><span class="p">.</span><span class="n">id</span><span class="o">=</span><span class="n">address</span><span class="p">.</span><span class="n">user_id</span></pre></div>
  520. </div>
  521. </dd>
  522. <dt id="term-generative">generative</dt><dd><p>A term that SQLAlchemy uses to refer what’s normally known
  523. as <a class="reference internal" href="#term-method-chaining"><span class="xref std std-term">method chaining</span></a>; see that term for details.</p>
  524. </dd>
  525. <dt id="term-identity-map">identity map</dt><dd><p>A mapping between Python objects and their database identities.
  526. The identity map is a collection that’s associated with an
  527. ORM <a class="reference internal" href="#term-Session"><span class="xref std std-term">session</span></a> object, and maintains a single instance
  528. of every database object keyed to its identity. The advantage
  529. to this pattern is that all operations which occur for a particular
  530. database identity are transparently coordinated onto a single
  531. object instance. When using an identity map in conjunction with
  532. an <a class="reference internal" href="#term-isolated"><span class="xref std std-term">isolated</span></a> transaction, having a reference
  533. to an object that’s known to have a particular primary key can
  534. be considered from a practical standpoint to be a
  535. proxy to the actual database row.</p>
  536. <div class="admonition seealso">
  537. <p class="admonition-title">See also</p>
  538. <p><a class="reference external" href="http://martinfowler.com/eaaCatalog/identityMap.html">Identity Map (via Martin Fowler)</a></p>
  539. </div>
  540. </dd>
  541. <dt id="term-instrumentation">instrumentation</dt><dt id="term-instrumented">instrumented</dt><dt id="term-instrumenting">instrumenting</dt><dd><p>Instrumentation refers to the process of augmenting the functionality
  542. and attribute set of a particular class. Ideally, the
  543. behavior of the class should remain close to a regular
  544. class, except that additional behaviors and features are
  545. made available. The SQLAlchemy <a class="reference internal" href="#term-mapping"><span class="xref std std-term">mapping</span></a> process,
  546. among other things, adds database-enabled <a class="reference internal" href="#term-descriptors"><span class="xref std std-term">descriptors</span></a>
  547. to a mapped
  548. class each of which represents a particular database column
  549. or relationship to a related class.</p>
  550. </dd>
  551. <dt id="term-isolation">isolation</dt><dt id="term-isolated">isolated</dt><dt id="term-isolation-level">isolation level</dt><dd><p>The isolation property of the <a class="reference internal" href="#term-ACID"><span class="xref std std-term">ACID</span></a> model
  552. ensures that the concurrent execution
  553. of transactions results in a system state that would be
  554. obtained if transactions were executed serially, i.e. one
  555. after the other. Each transaction must execute in total
  556. isolation i.e. if T1 and T2 execute concurrently then each
  557. should remain independent of the other.
  558. (via Wikipedia)</p>
  559. <div class="admonition seealso">
  560. <p class="admonition-title">See also</p>
  561. <p><a class="reference internal" href="#term-ACID"><span class="xref std std-term">ACID</span></a></p>
  562. <p><a class="reference external" href="http://en.wikipedia.org/wiki/Isolation_(database_systems)">Isolation (via Wikipedia)</a></p>
  563. <p><a class="reference internal" href="#term-read-uncommitted"><span class="xref std std-term">read uncommitted</span></a></p>
  564. <p><a class="reference internal" href="#term-read-committed"><span class="xref std std-term">read committed</span></a></p>
  565. <p><a class="reference internal" href="#term-repeatable-read"><span class="xref std std-term">repeatable read</span></a></p>
  566. <p><a class="reference internal" href="#term-serializable"><span class="xref std std-term">serializable</span></a></p>
  567. </div>
  568. </dd>
  569. <dt id="term-lazy-initialization">lazy initialization</dt><dd><p>A tactic of delaying some initialization action, such as creating objects,
  570. populating data, or establishing connectivity to other services, until
  571. those resources are required.</p>
  572. <div class="admonition seealso">
  573. <p class="admonition-title">See also</p>
  574. <p><a class="reference external" href="https://en.wikipedia.org/wiki/Lazy_initialization">Lazy initialization (via Wikipedia)</a></p>
  575. </div>
  576. </dd>
  577. <dt id="term-lazy-load">lazy load</dt><dt id="term-lazy-loads">lazy loads</dt><dt id="term-lazy-loaded">lazy loaded</dt><dt id="term-lazy-loading">lazy loading</dt><dd><p>In object relational mapping, a “lazy load” refers to an
  578. attribute that does not contain its database-side value
  579. for some period of time, typically when the object is
  580. first loaded. Instead, the attribute receives a
  581. <em>memoization</em> that causes it to go out to the database
  582. and load its data when it’s first used. Using this pattern,
  583. the complexity and time spent within object fetches can
  584. sometimes be reduced, in that
  585. attributes for related tables don’t need to be addressed
  586. immediately. Lazy loading is the opposite of <a class="reference internal" href="#term-eager-loading"><span class="xref std std-term">eager loading</span></a>.</p>
  587. <div class="admonition seealso">
  588. <p class="admonition-title">See also</p>
  589. <p><a class="reference external" href="http://martinfowler.com/eaaCatalog/lazyLoad.html">Lazy Load (via Martin Fowler)</a></p>
  590. <p><a class="reference internal" href="#term-N-plus-one-problem"><span class="xref std std-term">N plus one problem</span></a></p>
  591. <p><a class="reference internal" href="orm/loading_relationships.html"><span class="doc">Relationship Loading Techniques</span></a></p>
  592. </div>
  593. </dd>
  594. <dt id="term-many-to-many">many to many</dt><dd><p>A style of <a class="reference internal" href="orm/relationship_api.html#sqlalchemy.orm.relationship" title="sqlalchemy.orm.relationship"><code class="xref py py-func docutils literal notranslate"><span class="pre">relationship()</span></code></a> which links two tables together
  595. via an intermediary table in the middle. Using this configuration,
  596. any number of rows on the left side may refer to any number of
  597. rows on the right, and vice versa.</p>
  598. <p>A schema where employees can be associated with projects:</p>
  599. <div class="highlight-sql notranslate"><div class="highlight"><pre><span></span><span class="k">CREATE</span> <span class="k">TABLE</span> <span class="n">employee</span> <span class="p">(</span>
  600. <span class="n">id</span> <span class="nb">INTEGER</span> <span class="k">PRIMARY</span> <span class="k">KEY</span><span class="p">,</span>
  601. <span class="n">name</span> <span class="nb">VARCHAR</span><span class="p">(</span><span class="mi">30</span><span class="p">)</span>
  602. <span class="p">)</span>
  603. <span class="k">CREATE</span> <span class="k">TABLE</span> <span class="n">project</span> <span class="p">(</span>
  604. <span class="n">id</span> <span class="nb">INTEGER</span> <span class="k">PRIMARY</span> <span class="k">KEY</span><span class="p">,</span>
  605. <span class="n">name</span> <span class="nb">VARCHAR</span><span class="p">(</span><span class="mi">30</span><span class="p">)</span>
  606. <span class="p">)</span>
  607. <span class="k">CREATE</span> <span class="k">TABLE</span> <span class="n">employee_project</span> <span class="p">(</span>
  608. <span class="n">employee_id</span> <span class="nb">INTEGER</span> <span class="k">PRIMARY</span> <span class="k">KEY</span><span class="p">,</span>
  609. <span class="n">project_id</span> <span class="nb">INTEGER</span> <span class="k">PRIMARY</span> <span class="k">KEY</span><span class="p">,</span>
  610. <span class="k">FOREIGN</span> <span class="k">KEY</span> <span class="n">employee_id</span> <span class="k">REFERENCES</span> <span class="n">employee</span><span class="p">(</span><span class="n">id</span><span class="p">),</span>
  611. <span class="k">FOREIGN</span> <span class="k">KEY</span> <span class="n">project_id</span> <span class="k">REFERENCES</span> <span class="n">project</span><span class="p">(</span><span class="n">id</span><span class="p">)</span>
  612. <span class="p">)</span></pre></div>
  613. </div>
  614. <p>Above, the <code class="docutils literal notranslate"><span class="pre">employee_project</span></code> table is the many-to-many table,
  615. which naturally forms a composite primary key consisting
  616. of the primary key from each related table.</p>
  617. <p>In SQLAlchemy, the <a class="reference internal" href="orm/relationship_api.html#sqlalchemy.orm.relationship" title="sqlalchemy.orm.relationship"><code class="xref py py-func docutils literal notranslate"><span class="pre">relationship()</span></code></a> function
  618. can represent this style of relationship in a mostly
  619. transparent fashion, where the many-to-many table is
  620. specified using plain table metadata:</p>
  621. <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Employee</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
  622. <span class="n">__tablename__</span> <span class="o">=</span> <span class="s1">&#39;employee&#39;</span>
  623. <span class="nb">id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="p">)</span>
  624. <span class="n">name</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">String</span><span class="p">(</span><span class="mi">30</span><span class="p">))</span>
  625. <span class="n">projects</span> <span class="o">=</span> <span class="n">relationship</span><span class="p">(</span>
  626. <span class="s2">&quot;Project&quot;</span><span class="p">,</span>
  627. <span class="n">secondary</span><span class="o">=</span><span class="n">Table</span><span class="p">(</span><span class="s1">&#39;employee_project&#39;</span><span class="p">,</span> <span class="n">Base</span><span class="o">.</span><span class="n">metadata</span><span class="p">,</span>
  628. <span class="n">Column</span><span class="p">(</span><span class="s2">&quot;employee_id&quot;</span><span class="p">,</span> <span class="n">Integer</span><span class="p">,</span> <span class="n">ForeignKey</span><span class="p">(</span><span class="s1">&#39;employee.id&#39;</span><span class="p">),</span>
  629. <span class="n">primary_key</span><span class="o">=</span><span class="kc">True</span><span class="p">),</span>
  630. <span class="n">Column</span><span class="p">(</span><span class="s2">&quot;project_id&quot;</span><span class="p">,</span> <span class="n">Integer</span><span class="p">,</span> <span class="n">ForeignKey</span><span class="p">(</span><span class="s1">&#39;project.id&#39;</span><span class="p">),</span>
  631. <span class="n">primary_key</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
  632. <span class="p">),</span>
  633. <span class="n">backref</span><span class="o">=</span><span class="s2">&quot;employees&quot;</span>
  634. <span class="p">)</span>
  635. <span class="k">class</span> <span class="nc">Project</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
  636. <span class="n">__tablename__</span> <span class="o">=</span> <span class="s1">&#39;project&#39;</span>
  637. <span class="nb">id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="p">)</span>
  638. <span class="n">name</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">String</span><span class="p">(</span><span class="mi">30</span><span class="p">))</span></pre></div>
  639. </div>
  640. <p>Above, the <code class="docutils literal notranslate"><span class="pre">Employee.projects</span></code> and back-referencing <code class="docutils literal notranslate"><span class="pre">Project.employees</span></code>
  641. collections are defined:</p>
  642. <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">proj</span> <span class="o">=</span> <span class="n">Project</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s2">&quot;Client A&quot;</span><span class="p">)</span>
  643. <span class="n">emp1</span> <span class="o">=</span> <span class="n">Employee</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s2">&quot;emp1&quot;</span><span class="p">)</span>
  644. <span class="n">emp2</span> <span class="o">=</span> <span class="n">Employee</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s2">&quot;emp2&quot;</span><span class="p">)</span>
  645. <span class="n">proj</span><span class="o">.</span><span class="n">employees</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="n">emp1</span><span class="p">,</span> <span class="n">emp2</span><span class="p">])</span></pre></div>
  646. </div>
  647. <div class="admonition seealso">
  648. <p class="admonition-title">See also</p>
  649. <p><a class="reference internal" href="#term-association-relationship"><span class="xref std std-term">association relationship</span></a></p>
  650. <p><a class="reference internal" href="#term-relationship"><span class="xref std std-term">relationship</span></a></p>
  651. <p><a class="reference internal" href="#term-one-to-many"><span class="xref std std-term">one to many</span></a></p>
  652. <p><a class="reference internal" href="#term-many-to-one"><span class="xref std std-term">many to one</span></a></p>
  653. </div>
  654. </dd>
  655. <dt id="term-many-to-one">many to one</dt><dd><p>A style of <a class="reference internal" href="orm/relationship_api.html#sqlalchemy.orm.relationship" title="sqlalchemy.orm.relationship"><code class="xref py py-func docutils literal notranslate"><span class="pre">relationship()</span></code></a> which links
  656. a foreign key in the parent mapper’s table to the primary
  657. key of a related table. Each parent object can
  658. then refer to exactly zero or one related object.</p>
  659. <p>The related objects in turn will have an implicit or
  660. explicit <a class="reference internal" href="#term-one-to-many"><span class="xref std std-term">one to many</span></a> relationship to any number
  661. of parent objects that refer to them.</p>
  662. <p>An example many to one schema (which, note, is identical
  663. to the <a class="reference internal" href="#term-one-to-many"><span class="xref std std-term">one to many</span></a> schema):</p>
  664. <div class="highlight-sql notranslate"><div class="highlight"><pre><span></span><span class="k">CREATE</span> <span class="k">TABLE</span> <span class="n">department</span> <span class="p">(</span>
  665. <span class="n">id</span> <span class="nb">INTEGER</span> <span class="k">PRIMARY</span> <span class="k">KEY</span><span class="p">,</span>
  666. <span class="n">name</span> <span class="nb">VARCHAR</span><span class="p">(</span><span class="mi">30</span><span class="p">)</span>
  667. <span class="p">)</span>
  668. <span class="k">CREATE</span> <span class="k">TABLE</span> <span class="n">employee</span> <span class="p">(</span>
  669. <span class="n">id</span> <span class="nb">INTEGER</span> <span class="k">PRIMARY</span> <span class="k">KEY</span><span class="p">,</span>
  670. <span class="n">name</span> <span class="nb">VARCHAR</span><span class="p">(</span><span class="mi">30</span><span class="p">),</span>
  671. <span class="n">dep_id</span> <span class="nb">INTEGER</span> <span class="k">REFERENCES</span> <span class="n">department</span><span class="p">(</span><span class="n">id</span><span class="p">)</span>
  672. <span class="p">)</span></pre></div>
  673. </div>
  674. <p>The relationship from <code class="docutils literal notranslate"><span class="pre">employee</span></code> to <code class="docutils literal notranslate"><span class="pre">department</span></code> is
  675. many to one, since many employee records can be associated with a
  676. single department. A SQLAlchemy mapping might look like:</p>
  677. <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Department</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
  678. <span class="n">__tablename__</span> <span class="o">=</span> <span class="s1">&#39;department&#39;</span>
  679. <span class="nb">id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
  680. <span class="n">name</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">String</span><span class="p">(</span><span class="mi">30</span><span class="p">))</span>
  681. <span class="k">class</span> <span class="nc">Employee</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
  682. <span class="n">__tablename__</span> <span class="o">=</span> <span class="s1">&#39;employee&#39;</span>
  683. <span class="nb">id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
  684. <span class="n">name</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">String</span><span class="p">(</span><span class="mi">30</span><span class="p">))</span>
  685. <span class="n">dep_id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">ForeignKey</span><span class="p">(</span><span class="s1">&#39;department.id&#39;</span><span class="p">))</span>
  686. <span class="n">department</span> <span class="o">=</span> <span class="n">relationship</span><span class="p">(</span><span class="s2">&quot;Department&quot;</span><span class="p">)</span></pre></div>
  687. </div>
  688. <div class="admonition seealso">
  689. <p class="admonition-title">See also</p>
  690. <p><a class="reference internal" href="#term-relationship"><span class="xref std std-term">relationship</span></a></p>
  691. <p><a class="reference internal" href="#term-one-to-many"><span class="xref std std-term">one to many</span></a></p>
  692. <p><a class="reference internal" href="#term-backref"><span class="xref std std-term">backref</span></a></p>
  693. </div>
  694. </dd>
  695. <dt id="term-mapping">mapping</dt><dt id="term-mapped">mapped</dt><dd><p>We say a class is “mapped” when it has been passed through the
  696. <a class="reference internal" href="orm/mapping_api.html#sqlalchemy.orm.mapper" title="sqlalchemy.orm.mapper"><code class="xref py py-func docutils literal notranslate"><span class="pre">mapper()</span></code></a> function. This process associates the
  697. class with a database table or other <a class="reference internal" href="#term-selectable"><span class="xref std std-term">selectable</span></a>
  698. construct, so that instances of it can be persisted
  699. using a <a class="reference internal" href="orm/session_api.html#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><code class="xref py py-class docutils literal notranslate"><span class="pre">Session</span></code></a> as well as loaded using a
  700. <a class="reference internal" href="orm/query.html#sqlalchemy.orm.query.Query" title="sqlalchemy.orm.query.Query"><code class="xref py py-class docutils literal notranslate"><span class="pre">Query</span></code></a>.</p>
  701. </dd>
  702. <dt id="term-marshalling">marshalling</dt><dt id="term-data-marshalling">data marshalling</dt><dd><p>The process of transforming the memory representation of an object to
  703. a data format suitable for storage or transmission to another part of
  704. a system, when data must be moved between different parts of a
  705. computer program or from one program to another. In terms of
  706. SQLAlchemy, we often need to “marshal” data into a format appropriate
  707. for passing into the relational database.</p>
  708. <div class="admonition seealso">
  709. <p class="admonition-title">See also</p>
  710. <p><a class="reference external" href="https://en.wikipedia.org/wiki/Marshalling_(computer_science)">Marshalling (via Wikipedia)</a></p>
  711. <p><a class="reference internal" href="core/custom_types.html#types-typedecorator"><span class="std std-ref">Augmenting Existing Types</span></a> - SQLAlchemy’s <a class="reference internal" href="core/custom_types.html#sqlalchemy.types.TypeDecorator" title="sqlalchemy.types.TypeDecorator"><code class="xref py py-class docutils literal notranslate"><span class="pre">TypeDecorator</span></code></a>
  712. is commonly used for data marshalling as data is sent into the
  713. database for INSERT and UPDATE statements, and “unmarshalling”
  714. data as it is retrieved using SELECT statements.</p>
  715. </div>
  716. </dd>
  717. <dt id="term-metadata">metadata</dt><dt id="term-table-metadata">table metadata</dt><dd><p>The term “metadata” generally refers to “data that describes data”;
  718. data that itself represents the format and/or structure of some other
  719. kind of data. In SQLAlchemy, the term “metadata” typically refers to
  720. the <a class="reference internal" href="core/metadata.html#sqlalchemy.schema.MetaData" title="sqlalchemy.schema.MetaData"><code class="xref py py-class docutils literal notranslate"><span class="pre">MetaData</span></code></a> construct, which is a collection of information
  721. about the tables, columns, constraints, and other <a class="reference internal" href="#term-DDL"><span class="xref std std-term">DDL</span></a> objects
  722. that may exist in a particular database.</p>
  723. <div class="admonition seealso">
  724. <p class="admonition-title">See also</p>
  725. <p><a class="reference external" href="https://www.martinfowler.com/eaaCatalog/metadataMapping.html">Metadata Mapping (via Martin Fowler)</a></p>
  726. </div>
  727. </dd>
  728. <dt id="term-method-chaining">method chaining</dt><dd><p>An object-oriented technique whereby the state of an object
  729. is constructed by calling methods on the object. The
  730. object features any number of methods, each of which return
  731. a new object (or in some cases the same object) with
  732. additional state added to the object.</p>
  733. <p>The two SQLAlchemy objects that make the most use of
  734. method chaining are the <a class="reference internal" href="core/selectable.html#sqlalchemy.sql.expression.Select" title="sqlalchemy.sql.expression.Select"><code class="xref py py-class docutils literal notranslate"><span class="pre">Select</span></code></a>
  735. object and the <a class="reference internal" href="orm/query.html#sqlalchemy.orm.query.Query" title="sqlalchemy.orm.query.Query"><code class="xref py py-class docutils literal notranslate"><span class="pre">Query</span></code></a> object.
  736. For example, a <a class="reference internal" href="core/selectable.html#sqlalchemy.sql.expression.Select" title="sqlalchemy.sql.expression.Select"><code class="xref py py-class docutils literal notranslate"><span class="pre">Select</span></code></a> object can
  737. be assigned two expressions to its WHERE clause as well
  738. as an ORDER BY clause by calling upon the <a class="reference internal" href="core/selectable.html#sqlalchemy.sql.expression.Select.where" title="sqlalchemy.sql.expression.Select.where"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Select.where()</span></code></a>
  739. and <a class="reference internal" href="core/selectable.html#sqlalchemy.sql.expression.Select.order_by" title="sqlalchemy.sql.expression.Select.order_by"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Select.order_by()</span></code></a> methods:</p>
  740. <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">stmt</span> <span class="o">=</span> <span class="n">select</span><span class="p">([</span><span class="n">user</span><span class="o">.</span><span class="n">c</span><span class="o">.</span><span class="n">name</span><span class="p">])</span><span class="o">.</span>\
  741. <span class="n">where</span><span class="p">(</span><span class="n">user</span><span class="o">.</span><span class="n">c</span><span class="o">.</span><span class="n">id</span> <span class="o">&gt;</span> <span class="mi">5</span><span class="p">)</span><span class="o">.</span>\
  742. <span class="n">where</span><span class="p">(</span><span class="n">user</span><span class="o">.</span><span class="n">c</span><span class="o">.</span><span class="n">name</span><span class="o">.</span><span class="n">like</span><span class="p">(</span><span class="s1">&#39;e%&#39;</span><span class="p">)</span><span class="o">.</span>\
  743. <span class="n">order_by</span><span class="p">(</span><span class="n">user</span><span class="o">.</span><span class="n">c</span><span class="o">.</span><span class="n">name</span><span class="p">)</span></pre></div>
  744. </div>
  745. <p>Each method call above returns a copy of the original
  746. <a class="reference internal" href="core/selectable.html#sqlalchemy.sql.expression.Select" title="sqlalchemy.sql.expression.Select"><code class="xref py py-class docutils literal notranslate"><span class="pre">Select</span></code></a> object with additional qualifiers
  747. added.</p>
  748. <div class="admonition seealso">
  749. <p class="admonition-title">See also</p>
  750. <p><a class="reference internal" href="#term-generative"><span class="xref std std-term">generative</span></a></p>
  751. </div>
  752. </dd>
  753. <dt id="term-N-plus-one-problem">N plus one problem</dt><dd><p>The N plus one problem is a common side effect of the
  754. <a class="reference internal" href="#term-lazy-load"><span class="xref std std-term">lazy load</span></a> pattern, whereby an application wishes
  755. to iterate through a related attribute or collection on
  756. each member of a result set of objects, where that
  757. attribute or collection is set to be loaded via the lazy
  758. load pattern. The net result is that a SELECT statement
  759. is emitted to load the initial result set of parent objects;
  760. then, as the application iterates through each member,
  761. an additional SELECT statement is emitted for each member
  762. in order to load the related attribute or collection for
  763. that member. The end result is that for a result set of
  764. N parent objects, there will be N + 1 SELECT statements emitted.</p>
  765. <p>The N plus one problem is alleviated using <a class="reference internal" href="#term-eager-loading"><span class="xref std std-term">eager loading</span></a>.</p>
  766. <div class="admonition seealso">
  767. <p class="admonition-title">See also</p>
  768. <p><a class="reference internal" href="orm/loading_relationships.html"><span class="doc">Relationship Loading Techniques</span></a></p>
  769. </div>
  770. </dd>
  771. <dt id="term-one-to-many">one to many</dt><dd><p>A style of <a class="reference internal" href="orm/relationship_api.html#sqlalchemy.orm.relationship" title="sqlalchemy.orm.relationship"><code class="xref py py-func docutils literal notranslate"><span class="pre">relationship()</span></code></a> which links
  772. the primary key of the parent mapper’s table to the foreign
  773. key of a related table. Each unique parent object can
  774. then refer to zero or more unique related objects.</p>
  775. <p>The related objects in turn will have an implicit or
  776. explicit <a class="reference internal" href="#term-many-to-one"><span class="xref std std-term">many to one</span></a> relationship to their parent
  777. object.</p>
  778. <p>An example one to many schema (which, note, is identical
  779. to the <a class="reference internal" href="#term-many-to-one"><span class="xref std std-term">many to one</span></a> schema):</p>
  780. <div class="highlight-sql notranslate"><div class="highlight"><pre><span></span><span class="k">CREATE</span> <span class="k">TABLE</span> <span class="n">department</span> <span class="p">(</span>
  781. <span class="n">id</span> <span class="nb">INTEGER</span> <span class="k">PRIMARY</span> <span class="k">KEY</span><span class="p">,</span>
  782. <span class="n">name</span> <span class="nb">VARCHAR</span><span class="p">(</span><span class="mi">30</span><span class="p">)</span>
  783. <span class="p">)</span>
  784. <span class="k">CREATE</span> <span class="k">TABLE</span> <span class="n">employee</span> <span class="p">(</span>
  785. <span class="n">id</span> <span class="nb">INTEGER</span> <span class="k">PRIMARY</span> <span class="k">KEY</span><span class="p">,</span>
  786. <span class="n">name</span> <span class="nb">VARCHAR</span><span class="p">(</span><span class="mi">30</span><span class="p">),</span>
  787. <span class="n">dep_id</span> <span class="nb">INTEGER</span> <span class="k">REFERENCES</span> <span class="n">department</span><span class="p">(</span><span class="n">id</span><span class="p">)</span>
  788. <span class="p">)</span></pre></div>
  789. </div>
  790. <p>The relationship from <code class="docutils literal notranslate"><span class="pre">department</span></code> to <code class="docutils literal notranslate"><span class="pre">employee</span></code> is
  791. one to many, since many employee records can be associated with a
  792. single department. A SQLAlchemy mapping might look like:</p>
  793. <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Department</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
  794. <span class="n">__tablename__</span> <span class="o">=</span> <span class="s1">&#39;department&#39;</span>
  795. <span class="nb">id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
  796. <span class="n">name</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">String</span><span class="p">(</span><span class="mi">30</span><span class="p">))</span>
  797. <span class="n">employees</span> <span class="o">=</span> <span class="n">relationship</span><span class="p">(</span><span class="s2">&quot;Employee&quot;</span><span class="p">)</span>
  798. <span class="k">class</span> <span class="nc">Employee</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
  799. <span class="n">__tablename__</span> <span class="o">=</span> <span class="s1">&#39;employee&#39;</span>
  800. <span class="nb">id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
  801. <span class="n">name</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">String</span><span class="p">(</span><span class="mi">30</span><span class="p">))</span>
  802. <span class="n">dep_id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">,</span> <span class="n">ForeignKey</span><span class="p">(</span><span class="s1">&#39;department.id&#39;</span><span class="p">))</span></pre></div>
  803. </div>
  804. <div class="admonition seealso">
  805. <p class="admonition-title">See also</p>
  806. <p><a class="reference internal" href="#term-relationship"><span class="xref std std-term">relationship</span></a></p>
  807. <p><a class="reference internal" href="#term-many-to-one"><span class="xref std std-term">many to one</span></a></p>
  808. <p><a class="reference internal" href="#term-backref"><span class="xref std std-term">backref</span></a></p>
  809. </div>
  810. </dd>
  811. <dt id="term-pending">pending</dt><dd><p>This describes one of the major object states which
  812. an object can have within a <a class="reference internal" href="#term-Session"><span class="xref std std-term">session</span></a>; a pending object
  813. is a new object that doesn’t have any database identity,
  814. but has been recently associated with a session. When
  815. the session emits a flush and the row is inserted, the
  816. object moves to the <a class="reference internal" href="#term-persistent"><span class="xref std std-term">persistent</span></a> state.</p>
  817. <div class="admonition seealso">
  818. <p class="admonition-title">See also</p>
  819. <p><a class="reference internal" href="orm/session_state_management.html#session-object-states"><span class="std std-ref">Quickie Intro to Object States</span></a></p>
  820. </div>
  821. </dd>
  822. <dt id="term-persistent">persistent</dt><dd><p>This describes one of the major object states which
  823. an object can have within a <a class="reference internal" href="#term-Session"><span class="xref std std-term">session</span></a>; a persistent object
  824. is an object that has a database identity (i.e. a primary key)
  825. and is currently associated with a session. Any object
  826. that was previously <a class="reference internal" href="#term-pending"><span class="xref std std-term">pending</span></a> and has now been inserted
  827. is in the persistent state, as is any object that’s
  828. been loaded by the session from the database. When a
  829. persistent object is removed from a session, it is known
  830. as <a class="reference internal" href="#term-detached"><span class="xref std std-term">detached</span></a>.</p>
  831. <div class="admonition seealso">
  832. <p class="admonition-title">See also</p>
  833. <p><a class="reference internal" href="orm/session_state_management.html#session-object-states"><span class="std std-ref">Quickie Intro to Object States</span></a></p>
  834. </div>
  835. </dd>
  836. <dt id="term-polymorphic">polymorphic</dt><dt id="term-polymorphically">polymorphically</dt><dd><p>Refers to a function that handles several types at once. In SQLAlchemy,
  837. the term is usually applied to the concept of an ORM mapped class
  838. whereby a query operation will return different subclasses
  839. based on information in the result set, typically by checking the
  840. value of a particular column in the result known as the <a class="reference internal" href="#term-discriminator"><span class="xref std std-term">discriminator</span></a>.</p>
  841. <p>Polymorphic loading in SQLAlchemy implies that a one or a
  842. combination of three different schemes are used to map a hierarchy
  843. of classes; “joined”, “single”, and “concrete”. The section
  844. <a class="reference internal" href="orm/inheritance.html"><span class="std std-ref">Mapping Class Inheritance Hierarchies</span></a> describes inheritance mapping fully.</p>
  845. </dd>
  846. <dt id="term-primary-key">primary key</dt><dt id="term-primary-key-constraint">primary key constraint</dt><dd><p>A <a class="reference internal" href="#term-constraint"><span class="xref std std-term">constraint</span></a> that uniquely defines the characteristics
  847. of each row in a table. The primary key has to consist of
  848. characteristics that cannot be duplicated by any other row.
  849. The primary key may consist of a single attribute or
  850. multiple attributes in combination.
  851. (via Wikipedia)</p>
  852. <p>The primary key of a table is typically, though not always,
  853. defined within the <code class="docutils literal notranslate"><span class="pre">CREATE</span> <span class="pre">TABLE</span></code> <a class="reference internal" href="#term-DDL"><span class="xref std std-term">DDL</span></a>:</p>
  854. <div class="highlight-sql notranslate"><div class="highlight"><pre><span></span><span class="k">CREATE</span> <span class="k">TABLE</span> <span class="n">employee</span> <span class="p">(</span>
  855. <span class="n">emp_id</span> <span class="nb">INTEGER</span><span class="p">,</span>
  856. <span class="n">emp_name</span> <span class="nb">VARCHAR</span><span class="p">(</span><span class="mi">30</span><span class="p">),</span>
  857. <span class="n">dep_id</span> <span class="nb">INTEGER</span><span class="p">,</span>
  858. <span class="k">PRIMARY</span> <span class="k">KEY</span> <span class="p">(</span><span class="n">emp_id</span><span class="p">)</span>
  859. <span class="p">)</span></pre></div>
  860. </div>
  861. <div class="admonition seealso">
  862. <p class="admonition-title">See also</p>
  863. <p><a class="reference external" href="http://en.wikipedia.org/wiki/Primary_Key">Primary key (via Wikipedia)</a></p>
  864. </div>
  865. </dd>
  866. <dt id="term-read-committed">read committed</dt><dd><p>One of the four database <a class="reference internal" href="#term-isolation"><span class="xref std std-term">isolation</span></a> levels, read committed
  867. features that the transaction will not be exposed to any data from
  868. other concurrent transactions that has not been committed yet,
  869. preventing so-called “dirty reads”. However, under read committed
  870. there can be non-repeatable reads, meaning data in a row may change
  871. when read a second time if another transaction has committed changes.</p>
  872. </dd>
  873. <dt id="term-read-uncommitted">read uncommitted</dt><dd><p>One of the four database <a class="reference internal" href="#term-isolation"><span class="xref std std-term">isolation</span></a> levels, read uncommitted
  874. features that changes made to database data within a transaction will
  875. not become permanent until the transaction is committed. However,
  876. within read uncommitted, it may be possible for data that is not
  877. committed in other transactions to be viewable within the scope of
  878. another transaction; these are known as “dirty reads”.</p>
  879. </dd>
  880. <dt id="term-registry">registry</dt><dd><p>An object, typically globally accessible, that contains long-lived
  881. information about some program state that is generally useful to many
  882. parts of a program.</p>
  883. <div class="admonition seealso">
  884. <p class="admonition-title">See also</p>
  885. <p><a class="reference external" href="https://martinfowler.com/eaaCatalog/registry.html">Registry (via Martin Fowler)</a></p>
  886. </div>
  887. </dd>
  888. <dt id="term-relational">relational</dt><dt id="term-relational-algebra">relational algebra</dt><dd><p>An algrebraic system developed by Edgar F. Codd that is used for
  889. modelling and querying the data stored in relational databases.</p>
  890. <div class="admonition seealso">
  891. <p class="admonition-title">See also</p>
  892. <p><a class="reference external" href="https://en.wikipedia.org/wiki/Relational_algebra">Relational Algebra (via Wikipedia)</a></p>
  893. </div>
  894. </dd>
  895. <dt id="term-relationship">relationship</dt><dt id="term-relationships">relationships</dt><dd><p>A connecting unit between two mapped classes, corresponding
  896. to some relationship between the two tables in the database.</p>
  897. <p>The relationship is defined using the SQLAlchemy function
  898. <a class="reference internal" href="orm/relationship_api.html#sqlalchemy.orm.relationship" title="sqlalchemy.orm.relationship"><code class="xref py py-func docutils literal notranslate"><span class="pre">relationship()</span></code></a>. Once created, SQLAlchemy
  899. inspects the arguments and underlying mappings involved
  900. in order to classify the relationship as one of three types:
  901. <a class="reference internal" href="#term-one-to-many"><span class="xref std std-term">one to many</span></a>, <a class="reference internal" href="#term-many-to-one"><span class="xref std std-term">many to one</span></a>, or <a class="reference internal" href="#term-many-to-many"><span class="xref std std-term">many to many</span></a>.
  902. With this classification, the relationship construct
  903. handles the task of persisting the appropriate linkages
  904. in the database in response to in-memory object associations,
  905. as well as the job of loading object references and collections
  906. into memory based on the current linkages in the
  907. database.</p>
  908. <div class="admonition seealso">
  909. <p class="admonition-title">See also</p>
  910. <p><a class="reference internal" href="orm/relationships.html"><span class="std std-ref">Relationship Configuration</span></a></p>
  911. </div>
  912. </dd>
  913. <dt id="term-release">release</dt><dt id="term-releases">releases</dt><dt id="term-released">released</dt><dd><p>In the context of SQLAlchemy, the term “released”
  914. refers to the process of ending the usage of a particular
  915. database connection. SQLAlchemy features the usage
  916. of connection pools, which allows configurability as to
  917. the lifespan of database connections. When using a pooled
  918. connection, the process of “closing” it, i.e. invoking
  919. a statement like <code class="docutils literal notranslate"><span class="pre">connection.close()</span></code>, may have the effect
  920. of the connection being returned to an existing pool,
  921. or it may have the effect of actually shutting down the
  922. underlying TCP/IP connection referred to by that connection -
  923. which one takes place depends on configuration as well
  924. as the current state of the pool. So we used the term
  925. <em>released</em> instead, to mean “do whatever it is you do
  926. with connections when we’re done using them”.</p>
  927. <p>The term will sometimes be used in the phrase, “release
  928. transactional resources”, to indicate more explicitly that
  929. what we are actually “releasing” is any transactional
  930. state which as accumulated upon the connection. In most
  931. situations, the process of selecting from tables, emitting
  932. updates, etc. acquires <a class="reference internal" href="#term-isolated"><span class="xref std std-term">isolated</span></a> state upon
  933. that connection as well as potential row or table locks.
  934. This state is all local to a particular transaction
  935. on the connection, and is released when we emit a rollback.
  936. An important feature of the connection pool is that when
  937. we return a connection to the pool, the <code class="docutils literal notranslate"><span class="pre">connection.rollback()</span></code>
  938. method of the DBAPI is called as well, so that as the
  939. connection is set up to be used again, it’s in a “clean”
  940. state with no references held to the previous series
  941. of operations.</p>
  942. <div class="admonition seealso">
  943. <p class="admonition-title">See also</p>
  944. <p><a class="reference internal" href="core/pooling.html"><span class="std std-ref">Connection Pooling</span></a></p>
  945. </div>
  946. </dd>
  947. <dt id="term-repeatable-read">repeatable read</dt><dd><p>One of the four database <a class="reference internal" href="#term-isolation"><span class="xref std std-term">isolation</span></a> levels, repeatable read
  948. features all of the isolation of <a class="reference internal" href="#term-read-committed"><span class="xref std std-term">read committed</span></a>, and
  949. additionally features that any particular row that is read within a
  950. transaction is guaranteed from that point to not have any subsequent
  951. external changes in value (i.e. from other concurrent UPDATE
  952. statements) for the duration of that transaction.</p>
  953. </dd>
  954. <dt id="term-RETURNING">RETURNING</dt><dd><p>This is a non-SQL standard clause provided in various forms by
  955. certain backends, which provides the service of returning a result
  956. set upon execution of an INSERT, UPDATE or DELETE statement. Any set
  957. of columns from the matched rows can be returned, as though they were
  958. produced from a SELECT statement.</p>
  959. <p>The RETURNING clause provides both a dramatic performance boost to
  960. common update/select scenarios, including retrieval of inline- or
  961. default- generated primary key values and defaults at the moment they
  962. were created, as well as a way to get at server-generated
  963. default values in an atomic way.</p>
  964. <p>An example of RETURNING, idiomatic to PostgreSQL, looks like:</p>
  965. <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">INSERT</span> <span class="n">INTO</span> <span class="n">user_account</span> <span class="p">(</span><span class="n">name</span><span class="p">)</span> <span class="n">VALUES</span> <span class="p">(</span><span class="s1">&#39;new name&#39;</span><span class="p">)</span> <span class="n">RETURNING</span> <span class="nb">id</span><span class="p">,</span> <span class="n">timestamp</span></pre></div>
  966. </div>
  967. <p>Above, the INSERT statement will provide upon execution a result set
  968. which includes the values of the columns <code class="docutils literal notranslate"><span class="pre">user_account.id</span></code> and
  969. <code class="docutils literal notranslate"><span class="pre">user_account.timestamp</span></code>, which above should have been generated as default
  970. values as they are not included otherwise (but note any series of columns
  971. or SQL expressions can be placed into RETURNING, not just default-value columns).</p>
  972. <p>The backends that currently support
  973. RETURNING or a similar construct are PostgreSQL, SQL Server, Oracle,
  974. and Firebird. The PostgreSQL and Firebird implementations are generally
  975. full featured, whereas the implementations of SQL Server and Oracle
  976. have caveats. On SQL Server, the clause is known as “OUTPUT INSERTED”
  977. for INSERT and UPDATE statements and “OUTPUT DELETED” for DELETE statements;
  978. the key caveat is that triggers are not supported in conjunction with this
  979. keyword. On Oracle, it is known as “RETURNING…INTO”, and requires that the
  980. value be placed into an OUT parameter, meaning not only is the syntax awkward,
  981. but it can also only be used for one row at a time.</p>
  982. <p>SQLAlchemy’s <a class="reference internal" href="core/dml.html#sqlalchemy.sql.expression.UpdateBase.returning" title="sqlalchemy.sql.expression.UpdateBase.returning"><code class="xref py py-meth docutils literal notranslate"><span class="pre">UpdateBase.returning()</span></code></a> system provides a layer of abstraction
  983. on top of the RETURNING systems of these backends to provide a consistent
  984. interface for returning columns. The ORM also includes many optimizations
  985. that make use of RETURNING when available.</p>
  986. </dd>
  987. <dt id="term-selectable">selectable</dt><dd><p>A term used in SQLAlchemy to describe a SQL construct that represents
  988. a collection of rows. It’s largely similar to the concept of a
  989. “relation” in <a class="reference internal" href="#term-relational-algebra"><span class="xref std std-term">relational algebra</span></a>. In SQLAlchemy, objects
  990. that subclass the <code class="xref py py-class docutils literal notranslate"><span class="pre">expression.Selectable</span></code> class are considered to be
  991. usable as “selectables” when using SQLAlchemy Core. The two most
  992. common constructs are that of the <a class="reference internal" href="core/metadata.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><code class="xref py py-class docutils literal notranslate"><span class="pre">Table</span></code></a> and that of the
  993. <a class="reference internal" href="core/selectable.html#sqlalchemy.sql.expression.Select" title="sqlalchemy.sql.expression.Select"><code class="xref py py-class docutils literal notranslate"><span class="pre">Select</span></code></a> statement.</p>
  994. </dd>
  995. <dt id="term-serializable">serializable</dt><dd><p>One of the four database <a class="reference internal" href="#term-isolation"><span class="xref std std-term">isolation</span></a> levels, serializable
  996. features all of the isolation of <a class="reference internal" href="#term-repeatable-read"><span class="xref std std-term">repeatable read</span></a>, and
  997. additionally within a lock-based approach guarantees that so-called
  998. “phantom reads” cannot occur; this means that rows which are INSERTed
  999. or DELETEd within the scope of other transactions will not be
  1000. detectable within this transaction. A row that is read within this
  1001. transaction is guaranteed to continue existing, and a row that does not
  1002. exist is guaranteed that it cannot appear of inserted from another
  1003. transaction.</p>
  1004. <p>Serializable isolation typically relies upon locking of rows or ranges
  1005. of rows in order to achieve this effect and can increase the chance of
  1006. deadlocks and degrade performance. There are also non-lock based
  1007. schemes however these necessarily rely upon rejecting transactions if
  1008. write collisions are detected.</p>
  1009. </dd>
  1010. <dt id="term-Session">Session</dt><dd><p>The container or scope for ORM database operations. Sessions
  1011. load instances from the database, track changes to mapped
  1012. instances and persist changes in a single unit of work when
  1013. flushed.</p>
  1014. <div class="admonition seealso">
  1015. <p class="admonition-title">See also</p>
  1016. <p><a class="reference internal" href="orm/session.html"><span class="doc">Using the Session</span></a></p>
  1017. </div>
  1018. </dd>
  1019. <dt id="term-subquery">subquery</dt><dd><p>Refers to a <code class="docutils literal notranslate"><span class="pre">SELECT</span></code> statement that is embedded within an enclosing
  1020. <code class="docutils literal notranslate"><span class="pre">SELECT</span></code>.</p>
  1021. <p>A subquery comes in two general flavors, one known as a “scalar select”
  1022. which specifically must return exactly one row and one column, and the
  1023. other form which acts as a “derived table” and serves as a source of
  1024. rows for the FROM clause of another select. A scalar select is eligible
  1025. to be placed in the <a class="reference internal" href="#term-WHERE-clause"><span class="xref std std-term">WHERE clause</span></a>, <a class="reference internal" href="#term-columns-clause"><span class="xref std std-term">columns clause</span></a>,
  1026. ORDER BY clause or HAVING clause of the enclosing select, whereas the
  1027. derived table form is eligible to be placed in the FROM clause of the
  1028. enclosing <code class="docutils literal notranslate"><span class="pre">SELECT</span></code>.</p>
  1029. <p>Examples:</p>
  1030. <ol class="arabic">
  1031. <li><p>a scalar subquery placed in the <a class="reference internal" href="#term-columns-clause"><span class="xref std std-term">columns clause</span></a> of an enclosing
  1032. <code class="docutils literal notranslate"><span class="pre">SELECT</span></code>. The subquery in this example is a <a class="reference internal" href="#term-correlated-subquery"><span class="xref std std-term">correlated subquery</span></a> because part
  1033. of the rows which it selects from are given via the enclosing statement.</p>
  1034. <div class="highlight-sql notranslate"><div class="highlight"><pre><span></span><span class="k">SELECT</span> <span class="n">id</span><span class="p">,</span> <span class="p">(</span><span class="k">SELECT</span> <span class="n">name</span> <span class="k">FROM</span> <span class="n">address</span> <span class="k">WHERE</span> <span class="n">address</span><span class="p">.</span><span class="n">user_id</span><span class="o">=</span><span class="k">user</span><span class="p">.</span><span class="n">id</span><span class="p">)</span>
  1035. <span class="k">FROM</span> <span class="k">user</span></pre></div>
  1036. </div>
  1037. </li>
  1038. <li><p>a scalar subquery placed in the <a class="reference internal" href="#term-WHERE-clause"><span class="xref std std-term">WHERE clause</span></a> of an enclosing
  1039. <code class="docutils literal notranslate"><span class="pre">SELECT</span></code>. This subquery in this example is not correlated as it selects a fixed result.</p>
  1040. <div class="highlight-sql notranslate"><div class="highlight"><pre><span></span><span class="k">SELECT</span> <span class="n">id</span><span class="p">,</span> <span class="n">name</span> <span class="k">FROM</span> <span class="k">user</span>
  1041. <span class="k">WHERE</span> <span class="n">status</span><span class="o">=</span><span class="p">(</span><span class="k">SELECT</span> <span class="n">status_id</span> <span class="k">FROM</span> <span class="n">status_code</span> <span class="k">WHERE</span> <span class="n">code</span><span class="o">=</span><span class="s1">&#39;C&#39;</span><span class="p">)</span></pre></div>
  1042. </div>
  1043. </li>
  1044. <li><p>a derived table subquery placed in the <a class="reference internal" href="#term-FROM-clause"><span class="xref std std-term">FROM clause</span></a> of an enclosing
  1045. <code class="docutils literal notranslate"><span class="pre">SELECT</span></code>. Such a subquery is almost always given an alias name.</p>
  1046. <div class="highlight-sql notranslate"><div class="highlight"><pre><span></span><span class="k">SELECT</span> <span class="k">user</span><span class="p">.</span><span class="n">id</span><span class="p">,</span> <span class="k">user</span><span class="p">.</span><span class="n">name</span><span class="p">,</span> <span class="n">ad_subq</span><span class="p">.</span><span class="n">email_address</span>
  1047. <span class="k">FROM</span>
  1048. <span class="k">user</span> <span class="k">JOIN</span>
  1049. <span class="p">(</span><span class="k">select</span> <span class="n">user_id</span><span class="p">,</span> <span class="n">email_address</span> <span class="k">FROM</span> <span class="n">address</span> <span class="k">WHERE</span> <span class="n">address_type</span><span class="o">=</span><span class="s1">&#39;Q&#39;</span><span class="p">)</span> <span class="k">AS</span> <span class="n">ad_subq</span>
  1050. <span class="k">ON</span> <span class="k">user</span><span class="p">.</span><span class="n">id</span> <span class="o">=</span> <span class="n">ad_subq</span><span class="p">.</span><span class="n">user_id</span></pre></div>
  1051. </div>
  1052. </li>
  1053. </ol>
  1054. </dd>
  1055. <dt id="term-transient">transient</dt><dd><p>This describes one of the major object states which
  1056. an object can have within a <a class="reference internal" href="#term-Session"><span class="xref std std-term">session</span></a>; a transient object
  1057. is a new object that doesn’t have any database identity
  1058. and has not been associated with a session yet. When the
  1059. object is added to the session, it moves to the
  1060. <a class="reference internal" href="#term-pending"><span class="xref std std-term">pending</span></a> state.</p>
  1061. <div class="admonition seealso">
  1062. <p class="admonition-title">See also</p>
  1063. <p><a class="reference internal" href="orm/session_state_management.html#session-object-states"><span class="std std-ref">Quickie Intro to Object States</span></a></p>
  1064. </div>
  1065. </dd>
  1066. <dt id="term-unique-constraint">unique constraint</dt><dt id="term-unique-key-index">unique key index</dt><dd><p>A unique key index can uniquely identify each row of data
  1067. values in a database table. A unique key index comprises a
  1068. single column or a set of columns in a single database table.
  1069. No two distinct rows or data records in a database table can
  1070. have the same data value (or combination of data values) in
  1071. those unique key index columns if NULL values are not used.
  1072. Depending on its design, a database table may have many unique
  1073. key indexes but at most one primary key index.</p>
  1074. <p>(via Wikipedia)</p>
  1075. <div class="admonition seealso">
  1076. <p class="admonition-title">See also</p>
  1077. <p><a class="reference external" href="http://en.wikipedia.org/wiki/Unique_key#Defining_unique_keys">Unique key (via Wikipedia)</a></p>
  1078. </div>
  1079. </dd>
  1080. <dt id="term-unit-of-work">unit of work</dt><dd><p>This pattern is where the system transparently keeps
  1081. track of changes to objects and periodically flushes all those
  1082. pending changes out to the database. SQLAlchemy’s Session
  1083. implements this pattern fully in a manner similar to that of
  1084. Hibernate.</p>
  1085. <div class="admonition seealso">
  1086. <p class="admonition-title">See also</p>
  1087. <p><a class="reference external" href="http://martinfowler.com/eaaCatalog/unitOfWork.html">Unit of Work (via Martin Fowler)</a></p>
  1088. <p><a class="reference internal" href="orm/session.html"><span class="doc">Using the Session</span></a></p>
  1089. </div>
  1090. </dd>
  1091. <dt id="term-version-id-column">version id column</dt><dd><p>In SQLAlchemy, this refers to the use of a particular table column that
  1092. tracks the “version” of a particular row, as the row changes values. While
  1093. there are different kinds of relational patterns that make use of a
  1094. “version id column” in different ways, SQLAlchemy’s ORM includes a particular
  1095. feature that allows for such a column to be configured as a means of
  1096. testing for stale data when a row is being UPDATEd with new information.
  1097. If the last known “version” of this column does not match that of the
  1098. row when we try to put new data into the row, we know that we are
  1099. acting on stale information.</p>
  1100. <p>There are also other ways of storing “versioned” rows in a database,
  1101. often referred to as “temporal” data. In addition to SQLAlchemy’s
  1102. versioning feature, a few more examples are also present in the
  1103. documentation, see the links below.</p>
  1104. <div class="admonition seealso">
  1105. <p class="admonition-title">See also</p>
  1106. <p><a class="reference internal" href="orm/versioning.html#mapper-version-counter"><span class="std std-ref">Configuring a Version Counter</span></a> - SQLAlchemy’s built-in version id feature.</p>
  1107. <p><a class="reference internal" href="orm/examples.html#examples-versioning"><span class="std std-ref">Versioning Objects</span></a> - other examples of mappings that version rows
  1108. temporally.</p>
  1109. </div>
  1110. </dd>
  1111. <dt id="term-WHERE-clause">WHERE clause</dt><dd><p>The portion of the <code class="docutils literal notranslate"><span class="pre">SELECT</span></code> statement which indicates criteria
  1112. by which rows should be filtered. It is a single SQL expression
  1113. which follows the keyword <code class="docutils literal notranslate"><span class="pre">WHERE</span></code>.</p>
  1114. <div class="highlight-sql notranslate"><div class="highlight"><pre><span></span><span class="k">SELECT</span> <span class="n">user_account</span><span class="p">.</span><span class="n">name</span><span class="p">,</span> <span class="n">user_account</span><span class="p">.</span><span class="n">email</span>
  1115. <span class="k">FROM</span> <span class="n">user_account</span>
  1116. <span class="k">WHERE</span> <span class="n">user_account</span><span class="p">.</span><span class="n">name</span> <span class="o">=</span> <span class="s1">&#39;fred&#39;</span> <span class="k">AND</span> <span class="n">user_account</span><span class="p">.</span><span class="n">status</span> <span class="o">=</span> <span class="s1">&#39;E&#39;</span></pre></div>
  1117. </div>
  1118. <p>Above, the phrase <code class="docutils literal notranslate"><span class="pre">WHERE</span> <span class="pre">user_account.name</span> <span class="pre">=</span> <span class="pre">'fred'</span> <span class="pre">AND</span> <span class="pre">user_account.status</span> <span class="pre">=</span> <span class="pre">'E'</span></code>
  1119. comprises the WHERE clause of the <code class="docutils literal notranslate"><span class="pre">SELECT</span></code>.</p>
  1120. </dd>
  1121. </dl>
  1122. </div>
  1123. </div>
  1124. </div>
  1125. <div id="docs-bottom-navigation" class="docs-navigation-links, withsidebar">
  1126. <div id="docs-copyright">
  1127. &copy; <a href="copyright.html">Copyright</a> 2007-2020, the SQLAlchemy authors and contributors.
  1128. Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 3.0.3.
  1129. </div>
  1130. </div>
  1131. </div>
  1132. <script type="text/javascript">
  1133. var DOCUMENTATION_OPTIONS = {
  1134. URL_ROOT: './',
  1135. VERSION: '1.3.17',
  1136. COLLAPSE_MODINDEX: false,
  1137. FILE_SUFFIX: '.html'
  1138. };
  1139. </script>
  1140. <script type="text/javascript" id="documentation_options" data-url_root="./" src="_static/documentation_options.js"></script>
  1141. <!-- begin iterate through sphinx environment script_files -->
  1142. <script type="text/javascript" src="_static/jquery.js"></script>
  1143. <script type="text/javascript" src="_static/underscore.js"></script>
  1144. <script type="text/javascript" src="_static/doctools.js"></script>
  1145. <script type="text/javascript" src="_static/language_data.js"></script>
  1146. <!-- end iterate through sphinx environment script_files -->
  1147. <script type="text/javascript" src="_static/detectmobile.js"></script>
  1148. <script type="text/javascript" src="_static/init.js"></script>
  1149. </body>
  1150. </html>