~ubuntu-branches/debian/jessie/sqlalchemy/jessie

« back to all changes in this revision

Viewing changes to doc/core/constraints.html

  • Committer: Package Import Robot
  • Author(s): Piotr Ożarowski
  • Date: 2013-10-28 22:29:40 UTC
  • mfrom: (1.4.24)
  • Revision ID: package-import@ubuntu.com-20131028222940-wvyqffl4g617caun
Tags: 0.8.3-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 
2
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
3
 
 
4
<html xmlns="http://www.w3.org/1999/xhtml">
 
5
    <head>
 
6
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 
7
        
 
8
        <title>
 
9
            
 
10
    
 
11
                Defining Constraints and Indexes
 
12
             &mdash; 
 
13
    SQLAlchemy 0.8 Documentation
 
14
 
 
15
        </title>
 
16
        
 
17
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
 
18
    <link rel="stylesheet" href="../_static/docs.css" type="text/css" />
 
19
 
 
20
    <script type="text/javascript">
 
21
      var DOCUMENTATION_OPTIONS = {
 
22
          URL_ROOT:    '../',
 
23
          VERSION:     '0.8.3',
 
24
          COLLAPSE_MODINDEX: false,
 
25
          FILE_SUFFIX: '.html'
 
26
      };
 
27
    </script>
 
28
        <script type="text/javascript" src="../_static/jquery.js"></script>
 
29
        <script type="text/javascript" src="../_static/underscore.js"></script>
 
30
        <script type="text/javascript" src="../_static/doctools.js"></script>
 
31
    <script type="text/javascript" src="../_static/init.js"></script>
 
32
    <link rel="index" title="Index" href="../genindex.html" />
 
33
    <link rel="search" title="Search" href="../search.html" />
 
34
        <link rel="copyright" title="Copyright" href="../copyright.html" />
 
35
    <link rel="top" title="SQLAlchemy 0.8 Documentation" href="../index.html" />
 
36
        <link rel="up" title="Schema Definition Language" href="schema.html" />
 
37
        <link rel="next" title="Customizing DDL" href="ddl.html" />
 
38
        <link rel="prev" title="Column Insert/Update Defaults" href="defaults.html" />
 
39
 
 
40
    </head>
 
41
    <body>
 
42
        
 
43
 
 
44
 
 
45
 
 
46
 
 
47
 
 
48
 
 
49
 
 
50
 
 
51
 
 
52
 
 
53
<div id="docs-container">
 
54
 
 
55
 
 
56
 
 
57
<div id="docs-header">
 
58
    <h1>SQLAlchemy 0.8 Documentation</h1>
 
59
 
 
60
    <div id="docs-search">
 
61
    Search:
 
62
    <form class="search" action="../search.html" method="get">
 
63
      <input type="text" name="q" size="18" /> <input type="submit" value="Search" />
 
64
      <input type="hidden" name="check_keywords" value="yes" />
 
65
      <input type="hidden" name="area" value="default" />
 
66
    </form>
 
67
    </div>
 
68
 
 
69
    <div id="docs-version-header">
 
70
        Release: <span class="version-num">0.8.3</span> | Release Date: October 26, 2013
 
71
 
 
72
 
 
73
    </div>
 
74
 
 
75
</div>
 
76
 
 
77
<div id="docs-top-navigation">
 
78
    <div id="docs-top-page-control" class="docs-navigation-links">
 
79
        <ul>
 
80
            <li>Prev:
 
81
            <a href="defaults.html" title="previous chapter">Column Insert/Update Defaults</a>
 
82
            </li>
 
83
            <li>Next:
 
84
            <a href="ddl.html" title="next chapter">Customizing DDL</a>
 
85
            </li>
 
86
 
 
87
        <li>
 
88
            <a href="../contents.html">Table of Contents</a> |
 
89
            <a href="../genindex.html">Index</a>
 
90
            | <a href="../_sources/core/constraints.txt">view source
 
91
        </li>
 
92
        </ul>
 
93
    </div>
 
94
 
 
95
    <div id="docs-navigation-banner">
 
96
        <a href="../index.html">SQLAlchemy 0.8 Documentation</a>
 
97
                » <a href="index.html" title="SQLAlchemy Core">SQLAlchemy Core</a>
 
98
                » <a href="schema.html" title="Schema Definition Language">Schema Definition Language</a>
 
99
        » 
 
100
                Defining Constraints and Indexes
 
101
             
 
102
 
 
103
        <h2>
 
104
            
 
105
                Defining Constraints and Indexes
 
106
            
 
107
        </h2>
 
108
    </div>
 
109
 
 
110
</div>
 
111
 
 
112
<div id="docs-body-container">
 
113
 
 
114
    <div id="docs-sidebar">
 
115
    <h3><a href="../index.html">Table of Contents</a></h3>
 
116
    <ul>
 
117
<li><a class="reference internal" href="#">Defining Constraints and Indexes</a><ul>
 
118
<li><a class="reference internal" href="#defining-foreign-keys">Defining Foreign Keys</a><ul>
 
119
<li><a class="reference internal" href="#creating-dropping-foreign-key-constraints-via-alter">Creating/Dropping Foreign Key Constraints via ALTER</a></li>
 
120
<li><a class="reference internal" href="#on-update-and-on-delete">ON UPDATE and ON DELETE</a></li>
 
121
</ul>
 
122
</li>
 
123
<li><a class="reference internal" href="#unique-constraint">UNIQUE Constraint</a></li>
 
124
<li><a class="reference internal" href="#check-constraint">CHECK Constraint</a></li>
 
125
<li><a class="reference internal" href="#setting-up-constraints-when-using-the-declarative-orm-extension">Setting up Constraints when using the Declarative ORM Extension</a></li>
 
126
<li><a class="reference internal" href="#constraints-api">Constraints API</a></li>
 
127
<li><a class="reference internal" href="#indexes">Indexes</a><ul>
 
128
<li><a class="reference internal" href="#functional-indexes">Functional Indexes</a></li>
 
129
</ul>
 
130
</li>
 
131
<li><a class="reference internal" href="#index-api">Index API</a></li>
 
132
</ul>
 
133
</li>
 
134
</ul>
 
135
 
 
136
 
 
137
    <h4>Previous Topic</h4>
 
138
    <p>
 
139
    <a href="defaults.html" title="previous chapter">Column Insert/Update Defaults</a>
 
140
    </p>
 
141
    <h4>Next Topic</h4>
 
142
    <p>
 
143
    <a href="ddl.html" title="next chapter">Customizing DDL</a>
 
144
    </p>
 
145
 
 
146
 
 
147
    <h4>Quick Search</h4>
 
148
    <p>
 
149
    <form class="search" action="../search.html" method="get">
 
150
      <input type="text" name="q" size="18" /> <input type="submit" value="Search" />
 
151
      <input type="hidden" name="check_keywords" value="yes" />
 
152
      <input type="hidden" name="area" value="default" />
 
153
    </form>
 
154
    </p>
 
155
 
 
156
    </div>
 
157
 
 
158
    <div id="docs-body" class="withsidebar" >
 
159
        
 
160
<span class="target" id="module-sqlalchemy.schema"><span id="metadata-constraints"></span><span id="metadata-constraints-toplevel"></span></span><div class="section" id="defining-constraints-and-indexes">
 
161
<h1>Defining Constraints and Indexes<a class="headerlink" href="#defining-constraints-and-indexes" title="Permalink to this headline">¶</a></h1>
 
162
<p id="metadata-foreignkeys">This section will discuss SQL <em class="xref std std-term">constraints</em> and indexes.  In SQLAlchemy
 
163
the key classes include <a class="reference internal" href="#sqlalchemy.schema.ForeignKeyConstraint" title="sqlalchemy.schema.ForeignKeyConstraint"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKeyConstraint</span></tt></a> and <a class="reference internal" href="#sqlalchemy.schema.Index" title="sqlalchemy.schema.Index"><tt class="xref py py-class docutils literal"><span class="pre">Index</span></tt></a>.</p>
 
164
<div class="section" id="defining-foreign-keys">
 
165
<h2>Defining Foreign Keys<a class="headerlink" href="#defining-foreign-keys" title="Permalink to this headline">¶</a></h2>
 
166
<p>A <em>foreign key</em> in SQL is a table-level construct that constrains one or more
 
167
columns in that table to only allow values that are present in a different set
 
168
of columns, typically but not always located on a different table. We call the
 
169
columns which are constrained the <em>foreign key</em> columns and the columns which
 
170
they are constrained towards the <em>referenced</em> columns. The referenced columns
 
171
almost always define the primary key for their owning table, though there are
 
172
exceptions to this. The foreign key is the &#8220;joint&#8221; that connects together
 
173
pairs of rows which have a relationship with each other, and SQLAlchemy
 
174
assigns very deep importance to this concept in virtually every area of its
 
175
operation.</p>
 
176
<p>In SQLAlchemy as well as in DDL, foreign key constraints can be defined as
 
177
additional attributes within the table clause, or for single-column foreign
 
178
keys they may optionally be specified within the definition of a single
 
179
column. The single column foreign key is more common, and at the column level
 
180
is specified by constructing a <a class="reference internal" href="#sqlalchemy.schema.ForeignKey" title="sqlalchemy.schema.ForeignKey"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></tt></a> object
 
181
as an argument to a <a class="reference internal" href="metadata.html#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a> object:</p>
 
182
<div class="highlight-python"><div class="highlight"><pre><span class="n">user_preference</span> <span class="o">=</span> <span class="n">Table</span><span class="p">(</span><span class="s">&#39;user_preference&#39;</span><span class="p">,</span> <span class="n">metadata</span><span class="p">,</span>
 
183
    <span class="n">Column</span><span class="p">(</span><span class="s">&#39;pref_id&#39;</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="bp">True</span><span class="p">),</span>
 
184
    <span class="n">Column</span><span class="p">(</span><span class="s">&#39;user_id&#39;</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="s">&quot;user.user_id&quot;</span><span class="p">),</span> <span class="n">nullable</span><span class="o">=</span><span class="bp">False</span><span class="p">),</span>
 
185
    <span class="n">Column</span><span class="p">(</span><span class="s">&#39;pref_name&#39;</span><span class="p">,</span> <span class="n">String</span><span class="p">(</span><span class="mi">40</span><span class="p">),</span> <span class="n">nullable</span><span class="o">=</span><span class="bp">False</span><span class="p">),</span>
 
186
    <span class="n">Column</span><span class="p">(</span><span class="s">&#39;pref_value&#39;</span><span class="p">,</span> <span class="n">String</span><span class="p">(</span><span class="mi">100</span><span class="p">))</span>
 
187
<span class="p">)</span></pre></div>
 
188
</div>
 
189
<p>Above, we define a new table <tt class="docutils literal"><span class="pre">user_preference</span></tt> for which each row must
 
190
contain a value in the <tt class="docutils literal"><span class="pre">user_id</span></tt> column that also exists in the <tt class="docutils literal"><span class="pre">user</span></tt>
 
191
table&#8217;s <tt class="docutils literal"><span class="pre">user_id</span></tt> column.</p>
 
192
<p>The argument to <a class="reference internal" href="#sqlalchemy.schema.ForeignKey" title="sqlalchemy.schema.ForeignKey"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></tt></a> is most commonly a
 
193
string of the form <em>&lt;tablename&gt;.&lt;columnname&gt;</em>, or for a table in a remote
 
194
schema or &#8220;owner&#8221; of the form <em>&lt;schemaname&gt;.&lt;tablename&gt;.&lt;columnname&gt;</em>. It may
 
195
also be an actual <a class="reference internal" href="metadata.html#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a> object, which as we&#8217;ll
 
196
see later is accessed from an existing <a class="reference internal" href="metadata.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a>
 
197
object via its <tt class="docutils literal"><span class="pre">c</span></tt> collection:</p>
 
198
<div class="highlight-python"><div class="highlight"><pre><span class="n">ForeignKey</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">user_id</span><span class="p">)</span></pre></div>
 
199
</div>
 
200
<p>The advantage to using a string is that the in-python linkage between <tt class="docutils literal"><span class="pre">user</span></tt>
 
201
and <tt class="docutils literal"><span class="pre">user_preference</span></tt> is resolved only when first needed, so that table
 
202
objects can be easily spread across multiple modules and defined in any order.</p>
 
203
<p>Foreign keys may also be defined at the table level, using the
 
204
<a class="reference internal" href="#sqlalchemy.schema.ForeignKeyConstraint" title="sqlalchemy.schema.ForeignKeyConstraint"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKeyConstraint</span></tt></a> object. This object can
 
205
describe a single- or multi-column foreign key. A multi-column foreign key is
 
206
known as a <em>composite</em> foreign key, and almost always references a table that
 
207
has a composite primary key. Below we define a table <tt class="docutils literal"><span class="pre">invoice</span></tt> which has a
 
208
composite primary key:</p>
 
209
<div class="highlight-python"><div class="highlight"><pre><span class="n">invoice</span> <span class="o">=</span> <span class="n">Table</span><span class="p">(</span><span class="s">&#39;invoice&#39;</span><span class="p">,</span> <span class="n">metadata</span><span class="p">,</span>
 
210
    <span class="n">Column</span><span class="p">(</span><span class="s">&#39;invoice_id&#39;</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="bp">True</span><span class="p">),</span>
 
211
    <span class="n">Column</span><span class="p">(</span><span class="s">&#39;ref_num&#39;</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="bp">True</span><span class="p">),</span>
 
212
    <span class="n">Column</span><span class="p">(</span><span class="s">&#39;description&#39;</span><span class="p">,</span> <span class="n">String</span><span class="p">(</span><span class="mi">60</span><span class="p">),</span> <span class="n">nullable</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
 
213
<span class="p">)</span></pre></div>
 
214
</div>
 
215
<p>And then a table <tt class="docutils literal"><span class="pre">invoice_item</span></tt> with a composite foreign key referencing
 
216
<tt class="docutils literal"><span class="pre">invoice</span></tt>:</p>
 
217
<div class="highlight-python"><div class="highlight"><pre><span class="n">invoice_item</span> <span class="o">=</span> <span class="n">Table</span><span class="p">(</span><span class="s">&#39;invoice_item&#39;</span><span class="p">,</span> <span class="n">metadata</span><span class="p">,</span>
 
218
    <span class="n">Column</span><span class="p">(</span><span class="s">&#39;item_id&#39;</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="bp">True</span><span class="p">),</span>
 
219
    <span class="n">Column</span><span class="p">(</span><span class="s">&#39;item_name&#39;</span><span class="p">,</span> <span class="n">String</span><span class="p">(</span><span class="mi">60</span><span class="p">),</span> <span class="n">nullable</span><span class="o">=</span><span class="bp">False</span><span class="p">),</span>
 
220
    <span class="n">Column</span><span class="p">(</span><span class="s">&#39;invoice_id&#39;</span><span class="p">,</span> <span class="n">Integer</span><span class="p">,</span> <span class="n">nullable</span><span class="o">=</span><span class="bp">False</span><span class="p">),</span>
 
221
    <span class="n">Column</span><span class="p">(</span><span class="s">&#39;ref_num&#39;</span><span class="p">,</span> <span class="n">Integer</span><span class="p">,</span> <span class="n">nullable</span><span class="o">=</span><span class="bp">False</span><span class="p">),</span>
 
222
    <span class="n">ForeignKeyConstraint</span><span class="p">([</span><span class="s">&#39;invoice_id&#39;</span><span class="p">,</span> <span class="s">&#39;ref_num&#39;</span><span class="p">],</span> <span class="p">[</span><span class="s">&#39;invoice.invoice_id&#39;</span><span class="p">,</span> <span class="s">&#39;invoice.ref_num&#39;</span><span class="p">])</span>
 
223
<span class="p">)</span></pre></div>
 
224
</div>
 
225
<p>It&#8217;s important to note that the
 
226
<a class="reference internal" href="#sqlalchemy.schema.ForeignKeyConstraint" title="sqlalchemy.schema.ForeignKeyConstraint"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKeyConstraint</span></tt></a> is the only way to define a
 
227
composite foreign key. While we could also have placed individual
 
228
<a class="reference internal" href="#sqlalchemy.schema.ForeignKey" title="sqlalchemy.schema.ForeignKey"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></tt></a> objects on both the
 
229
<tt class="docutils literal"><span class="pre">invoice_item.invoice_id</span></tt> and <tt class="docutils literal"><span class="pre">invoice_item.ref_num</span></tt> columns, SQLAlchemy
 
230
would not be aware that these two values should be paired together - it would
 
231
be two individual foreign key constraints instead of a single composite
 
232
foreign key referencing two columns.</p>
 
233
<div class="section" id="creating-dropping-foreign-key-constraints-via-alter">
 
234
<span id="use-alter"></span><h3>Creating/Dropping Foreign Key Constraints via ALTER<a class="headerlink" href="#creating-dropping-foreign-key-constraints-via-alter" title="Permalink to this headline">¶</a></h3>
 
235
<p>In all the above examples, the <a class="reference internal" href="#sqlalchemy.schema.ForeignKey" title="sqlalchemy.schema.ForeignKey"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></tt></a> object
 
236
causes the &#8220;REFERENCES&#8221; keyword to be added inline to a column definition
 
237
within a &#8220;CREATE TABLE&#8221; statement when
 
238
<a class="reference internal" href="metadata.html#sqlalchemy.schema.MetaData.create_all" title="sqlalchemy.schema.MetaData.create_all"><tt class="xref py py-func docutils literal"><span class="pre">create_all()</span></tt></a> is issued, and
 
239
<a class="reference internal" href="#sqlalchemy.schema.ForeignKeyConstraint" title="sqlalchemy.schema.ForeignKeyConstraint"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKeyConstraint</span></tt></a> invokes the &#8220;CONSTRAINT&#8221;
 
240
keyword inline with &#8220;CREATE TABLE&#8221;. There are some cases where this is
 
241
undesireable, particularly when two tables reference each other mutually, each
 
242
with a foreign key referencing the other. In such a situation at least one of
 
243
the foreign key constraints must be generated after both tables have been
 
244
built. To support such a scheme, <a class="reference internal" href="#sqlalchemy.schema.ForeignKey" title="sqlalchemy.schema.ForeignKey"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></tt></a> and
 
245
<a class="reference internal" href="#sqlalchemy.schema.ForeignKeyConstraint" title="sqlalchemy.schema.ForeignKeyConstraint"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKeyConstraint</span></tt></a> offer the flag
 
246
<tt class="docutils literal"><span class="pre">use_alter=True</span></tt>. When using this flag, the constraint will be generated
 
247
using a definition similar to &#8220;ALTER TABLE &lt;tablename&gt; ADD CONSTRAINT &lt;name&gt;
 
248
...&#8221;. Since a name is required, the <tt class="docutils literal"><span class="pre">name</span></tt> attribute must also be specified.
 
249
For example:</p>
 
250
<div class="highlight-python"><div class="highlight"><pre><span class="n">node</span> <span class="o">=</span> <span class="n">Table</span><span class="p">(</span><span class="s">&#39;node&#39;</span><span class="p">,</span> <span class="n">meta</span><span class="p">,</span>
 
251
    <span class="n">Column</span><span class="p">(</span><span class="s">&#39;node_id&#39;</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="bp">True</span><span class="p">),</span>
 
252
    <span class="n">Column</span><span class="p">(</span><span class="s">&#39;primary_element&#39;</span><span class="p">,</span> <span class="n">Integer</span><span class="p">,</span>
 
253
        <span class="n">ForeignKey</span><span class="p">(</span><span class="s">&#39;element.element_id&#39;</span><span class="p">,</span> <span class="n">use_alter</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s">&#39;fk_node_element_id&#39;</span><span class="p">)</span>
 
254
    <span class="p">)</span>
 
255
<span class="p">)</span>
 
256
 
 
257
<span class="n">element</span> <span class="o">=</span> <span class="n">Table</span><span class="p">(</span><span class="s">&#39;element&#39;</span><span class="p">,</span> <span class="n">meta</span><span class="p">,</span>
 
258
    <span class="n">Column</span><span class="p">(</span><span class="s">&#39;element_id&#39;</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="bp">True</span><span class="p">),</span>
 
259
    <span class="n">Column</span><span class="p">(</span><span class="s">&#39;parent_node_id&#39;</span><span class="p">,</span> <span class="n">Integer</span><span class="p">),</span>
 
260
    <span class="n">ForeignKeyConstraint</span><span class="p">(</span>
 
261
        <span class="p">[</span><span class="s">&#39;parent_node_id&#39;</span><span class="p">],</span>
 
262
        <span class="p">[</span><span class="s">&#39;node.node_id&#39;</span><span class="p">],</span>
 
263
        <span class="n">use_alter</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span>
 
264
        <span class="n">name</span><span class="o">=</span><span class="s">&#39;fk_element_parent_node_id&#39;</span>
 
265
    <span class="p">)</span>
 
266
<span class="p">)</span></pre></div>
 
267
</div>
 
268
</div>
 
269
<div class="section" id="on-update-and-on-delete">
 
270
<h3>ON UPDATE and ON DELETE<a class="headerlink" href="#on-update-and-on-delete" title="Permalink to this headline">¶</a></h3>
 
271
<p>Most databases support <em>cascading</em> of foreign key values, that is the when a
 
272
parent row is updated the new value is placed in child rows, or when the
 
273
parent row is deleted all corresponding child rows are set to null or deleted.
 
274
In data definition language these are specified using phrases like &#8220;ON UPDATE
 
275
CASCADE&#8221;, &#8220;ON DELETE CASCADE&#8221;, and &#8220;ON DELETE SET NULL&#8221;, corresponding to
 
276
foreign key constraints. The phrase after &#8220;ON UPDATE&#8221; or &#8220;ON DELETE&#8221; may also
 
277
other allow other phrases that are specific to the database in use. The
 
278
<a class="reference internal" href="#sqlalchemy.schema.ForeignKey" title="sqlalchemy.schema.ForeignKey"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></tt></a> and
 
279
<a class="reference internal" href="#sqlalchemy.schema.ForeignKeyConstraint" title="sqlalchemy.schema.ForeignKeyConstraint"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKeyConstraint</span></tt></a> objects support the
 
280
generation of this clause via the <tt class="docutils literal"><span class="pre">onupdate</span></tt> and <tt class="docutils literal"><span class="pre">ondelete</span></tt> keyword
 
281
arguments. The value is any string which will be output after the appropriate
 
282
&#8220;ON UPDATE&#8221; or &#8220;ON DELETE&#8221; phrase:</p>
 
283
<div class="highlight-python"><div class="highlight"><pre><span class="n">child</span> <span class="o">=</span> <span class="n">Table</span><span class="p">(</span><span class="s">&#39;child&#39;</span><span class="p">,</span> <span class="n">meta</span><span class="p">,</span>
 
284
    <span class="n">Column</span><span class="p">(</span><span class="s">&#39;id&#39;</span><span class="p">,</span> <span class="n">Integer</span><span class="p">,</span>
 
285
            <span class="n">ForeignKey</span><span class="p">(</span><span class="s">&#39;parent.id&#39;</span><span class="p">,</span> <span class="n">onupdate</span><span class="o">=</span><span class="s">&quot;CASCADE&quot;</span><span class="p">,</span> <span class="n">ondelete</span><span class="o">=</span><span class="s">&quot;CASCADE&quot;</span><span class="p">),</span>
 
286
            <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span>
 
287
    <span class="p">)</span>
 
288
<span class="p">)</span>
 
289
 
 
290
<span class="n">composite</span> <span class="o">=</span> <span class="n">Table</span><span class="p">(</span><span class="s">&#39;composite&#39;</span><span class="p">,</span> <span class="n">meta</span><span class="p">,</span>
 
291
    <span class="n">Column</span><span class="p">(</span><span class="s">&#39;id&#39;</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="bp">True</span><span class="p">),</span>
 
292
    <span class="n">Column</span><span class="p">(</span><span class="s">&#39;rev_id&#39;</span><span class="p">,</span> <span class="n">Integer</span><span class="p">),</span>
 
293
    <span class="n">Column</span><span class="p">(</span><span class="s">&#39;note_id&#39;</span><span class="p">,</span> <span class="n">Integer</span><span class="p">),</span>
 
294
    <span class="n">ForeignKeyConstraint</span><span class="p">(</span>
 
295
                <span class="p">[</span><span class="s">&#39;rev_id&#39;</span><span class="p">,</span> <span class="s">&#39;note_id&#39;</span><span class="p">],</span>
 
296
                <span class="p">[</span><span class="s">&#39;revisions.id&#39;</span><span class="p">,</span> <span class="s">&#39;revisions.note_id&#39;</span><span class="p">],</span>
 
297
                <span class="n">onupdate</span><span class="o">=</span><span class="s">&quot;CASCADE&quot;</span><span class="p">,</span> <span class="n">ondelete</span><span class="o">=</span><span class="s">&quot;SET NULL&quot;</span>
 
298
    <span class="p">)</span>
 
299
<span class="p">)</span></pre></div>
 
300
</div>
 
301
<p>Note that these clauses are not supported on SQLite, and require <tt class="docutils literal"><span class="pre">InnoDB</span></tt>
 
302
tables when used with MySQL. They may also not be supported on other
 
303
databases.</p>
 
304
</div>
 
305
</div>
 
306
<div class="section" id="unique-constraint">
 
307
<h2>UNIQUE Constraint<a class="headerlink" href="#unique-constraint" title="Permalink to this headline">¶</a></h2>
 
308
<p>Unique constraints can be created anonymously on a single column using the
 
309
<tt class="docutils literal"><span class="pre">unique</span></tt> keyword on <a class="reference internal" href="metadata.html#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a>. Explicitly named
 
310
unique constraints and/or those with multiple columns are created via the
 
311
<a class="reference internal" href="#sqlalchemy.schema.UniqueConstraint" title="sqlalchemy.schema.UniqueConstraint"><tt class="xref py py-class docutils literal"><span class="pre">UniqueConstraint</span></tt></a> table-level construct.</p>
 
312
<div class="highlight-python+sql"><div class="highlight"><pre><span class="n">meta</span> <span class="o">=</span> <span class="n">MetaData</span><span class="p">()</span>
 
313
<span class="n">mytable</span> <span class="o">=</span> <span class="n">Table</span><span class="p">(</span><span class="s">&#39;mytable&#39;</span><span class="p">,</span> <span class="n">meta</span><span class="p">,</span>
 
314
 
 
315
    <span class="c"># per-column anonymous unique constraint</span>
 
316
    <span class="n">Column</span><span class="p">(</span><span class="s">&#39;col1&#39;</span><span class="p">,</span> <span class="n">Integer</span><span class="p">,</span> <span class="n">unique</span><span class="o">=</span><span class="bp">True</span><span class="p">),</span>
 
317
 
 
318
    <span class="n">Column</span><span class="p">(</span><span class="s">&#39;col2&#39;</span><span class="p">,</span> <span class="n">Integer</span><span class="p">),</span>
 
319
    <span class="n">Column</span><span class="p">(</span><span class="s">&#39;col3&#39;</span><span class="p">,</span> <span class="n">Integer</span><span class="p">),</span>
 
320
 
 
321
    <span class="c"># explicit/composite unique constraint.  &#39;name&#39; is optional.</span>
 
322
    <span class="n">UniqueConstraint</span><span class="p">(</span><span class="s">&#39;col2&#39;</span><span class="p">,</span> <span class="s">&#39;col3&#39;</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s">&#39;uix_1&#39;</span><span class="p">)</span>
 
323
    <span class="p">)</span></pre></div>
 
324
</div>
 
325
</div>
 
326
<div class="section" id="check-constraint">
 
327
<h2>CHECK Constraint<a class="headerlink" href="#check-constraint" title="Permalink to this headline">¶</a></h2>
 
328
<p>Check constraints can be named or unnamed and can be created at the Column or
 
329
Table level, using the <a class="reference internal" href="#sqlalchemy.schema.CheckConstraint" title="sqlalchemy.schema.CheckConstraint"><tt class="xref py py-class docutils literal"><span class="pre">CheckConstraint</span></tt></a> construct.
 
330
The text of the check constraint is passed directly through to the database,
 
331
so there is limited &#8220;database independent&#8221; behavior. Column level check
 
332
constraints generally should only refer to the column to which they are
 
333
placed, while table level constraints can refer to any columns in the table.</p>
 
334
<p>Note that some databases do not actively support check constraints such as
 
335
MySQL.</p>
 
336
<div class="highlight-python+sql"><div class="highlight"><pre><span class="n">meta</span> <span class="o">=</span> <span class="n">MetaData</span><span class="p">()</span>
 
337
<span class="n">mytable</span> <span class="o">=</span> <span class="n">Table</span><span class="p">(</span><span class="s">&#39;mytable&#39;</span><span class="p">,</span> <span class="n">meta</span><span class="p">,</span>
 
338
 
 
339
    <span class="c"># per-column CHECK constraint</span>
 
340
    <span class="n">Column</span><span class="p">(</span><span class="s">&#39;col1&#39;</span><span class="p">,</span> <span class="n">Integer</span><span class="p">,</span> <span class="n">CheckConstraint</span><span class="p">(</span><span class="s">&#39;col1&gt;5&#39;</span><span class="p">)),</span>
 
341
 
 
342
    <span class="n">Column</span><span class="p">(</span><span class="s">&#39;col2&#39;</span><span class="p">,</span> <span class="n">Integer</span><span class="p">),</span>
 
343
    <span class="n">Column</span><span class="p">(</span><span class="s">&#39;col3&#39;</span><span class="p">,</span> <span class="n">Integer</span><span class="p">),</span>
 
344
 
 
345
    <span class="c"># table level CHECK constraint.  &#39;name&#39; is optional.</span>
 
346
    <span class="n">CheckConstraint</span><span class="p">(</span><span class="s">&#39;col2 &gt; col3 + 5&#39;</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s">&#39;check1&#39;</span><span class="p">)</span>
 
347
    <span class="p">)</span>
 
348
 
 
349
<a href='#' class='sql_link'>sql</a><span class="n">mytable</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">engine</span><span class="p">)</span>
 
350
<div class='popup_sql'>CREATE TABLE mytable (
 
351
    col1 INTEGER  CHECK (col1>5),
 
352
    col2 INTEGER,
 
353
    col3 INTEGER,
 
354
    CONSTRAINT check1  CHECK (col2 > col3 + 5)
 
355
)</div></pre></div>
 
356
</div>
 
357
</div>
 
358
<div class="section" id="setting-up-constraints-when-using-the-declarative-orm-extension">
 
359
<h2>Setting up Constraints when using the Declarative ORM Extension<a class="headerlink" href="#setting-up-constraints-when-using-the-declarative-orm-extension" title="Permalink to this headline">¶</a></h2>
 
360
<p>The <a class="reference internal" href="metadata.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> is the SQLAlchemy Core construct that allows one to define
 
361
table metadata, which among other things can be used by the SQLAlchemy ORM
 
362
as a target to map a class.  The <a class="reference internal" href="../orm/extensions/declarative.html"><em>Declarative</em></a>
 
363
extension allows the <a class="reference internal" href="metadata.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> object to be created automatically, given
 
364
the contents of the table primarily as a mapping of <a class="reference internal" href="metadata.html#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a> objects.</p>
 
365
<p>To apply table-level constraint objects such as <a class="reference internal" href="#sqlalchemy.schema.ForeignKeyConstraint" title="sqlalchemy.schema.ForeignKeyConstraint"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKeyConstraint</span></tt></a>
 
366
to a table defined using Declarative, use the <tt class="docutils literal"><span class="pre">__table_args__</span></tt> attribute,
 
367
described at <a class="reference internal" href="../orm/extensions/declarative.html#declarative-table-args"><em>Table Configuration</em></a>.</p>
 
368
</div>
 
369
<div class="section" id="constraints-api">
 
370
<h2>Constraints API<a class="headerlink" href="#constraints-api" title="Permalink to this headline">¶</a></h2>
 
371
<dl class="class">
 
372
<dt id="sqlalchemy.schema.Constraint">
 
373
<em class="property">class </em><tt class="descclassname">sqlalchemy.schema.</tt><tt class="descname">Constraint</tt><big>(</big><em>name=None</em>, <em>deferrable=None</em>, <em>initially=None</em>, <em>_create_rule=None</em>, <em>**kw</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Constraint" title="Permalink to this definition">¶</a></dt>
 
374
<dd><p>Bases: <a class="reference internal" href="metadata.html#sqlalchemy.schema.SchemaItem" title="sqlalchemy.schema.SchemaItem"><tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.schema.SchemaItem</span></tt></a></p>
 
375
<p>A table-level SQL constraint.</p>
 
376
</dd></dl>
 
377
 
 
378
<dl class="class">
 
379
<dt id="sqlalchemy.schema.CheckConstraint">
 
380
<em class="property">class </em><tt class="descclassname">sqlalchemy.schema.</tt><tt class="descname">CheckConstraint</tt><big>(</big><em>sqltext</em>, <em>name=None</em>, <em>deferrable=None</em>, <em>initially=None</em>, <em>table=None</em>, <em>_create_rule=None</em>, <em>_autoattach=True</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.CheckConstraint" title="Permalink to this definition">¶</a></dt>
 
381
<dd><p>Bases: <a class="reference internal" href="#sqlalchemy.schema.Constraint" title="sqlalchemy.schema.Constraint"><tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.schema.Constraint</span></tt></a></p>
 
382
<p>A table- or column-level CHECK constraint.</p>
 
383
<p>Can be included in the definition of a Table or Column.</p>
 
384
</dd></dl>
 
385
 
 
386
<dl class="class">
 
387
<dt id="sqlalchemy.schema.ColumnCollectionConstraint">
 
388
<em class="property">class </em><tt class="descclassname">sqlalchemy.schema.</tt><tt class="descname">ColumnCollectionConstraint</tt><big>(</big><em>*columns</em>, <em>**kw</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.ColumnCollectionConstraint" title="Permalink to this definition">¶</a></dt>
 
389
<dd><p>Bases: <tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.schema.ColumnCollectionMixin</span></tt>, <a class="reference internal" href="#sqlalchemy.schema.Constraint" title="sqlalchemy.schema.Constraint"><tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.schema.Constraint</span></tt></a></p>
 
390
<p>A constraint that proxies a ColumnCollection.</p>
 
391
</dd></dl>
 
392
 
 
393
<dl class="class">
 
394
<dt id="sqlalchemy.schema.ForeignKey">
 
395
<em class="property">class </em><tt class="descclassname">sqlalchemy.schema.</tt><tt class="descname">ForeignKey</tt><big>(</big><em>column</em>, <em>_constraint=None</em>, <em>use_alter=False</em>, <em>name=None</em>, <em>onupdate=None</em>, <em>ondelete=None</em>, <em>deferrable=None</em>, <em>schema=None</em>, <em>initially=None</em>, <em>link_to_name=False</em>, <em>match=None</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.ForeignKey" title="Permalink to this definition">¶</a></dt>
 
396
<dd><p>Bases: <a class="reference internal" href="metadata.html#sqlalchemy.schema.SchemaItem" title="sqlalchemy.schema.SchemaItem"><tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.schema.SchemaItem</span></tt></a></p>
 
397
<p>Defines a dependency between two columns.</p>
 
398
<p><tt class="docutils literal"><span class="pre">ForeignKey</span></tt> is specified as an argument to a <a class="reference internal" href="metadata.html#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a> object,
 
399
e.g.:</p>
 
400
<div class="highlight-python"><div class="highlight"><pre><span class="n">t</span> <span class="o">=</span> <span class="n">Table</span><span class="p">(</span><span class="s">&quot;remote_table&quot;</span><span class="p">,</span> <span class="n">metadata</span><span class="p">,</span>
 
401
    <span class="n">Column</span><span class="p">(</span><span class="s">&quot;remote_id&quot;</span><span class="p">,</span> <span class="n">ForeignKey</span><span class="p">(</span><span class="s">&quot;main_table.id&quot;</span><span class="p">))</span>
 
402
<span class="p">)</span></pre></div>
 
403
</div>
 
404
<p>Note that <tt class="docutils literal"><span class="pre">ForeignKey</span></tt> is only a marker object that defines
 
405
a dependency between two columns.   The actual constraint
 
406
is in all cases represented by the <a class="reference internal" href="#sqlalchemy.schema.ForeignKeyConstraint" title="sqlalchemy.schema.ForeignKeyConstraint"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKeyConstraint</span></tt></a>
 
407
object.   This object will be generated automatically when
 
408
a <tt class="docutils literal"><span class="pre">ForeignKey</span></tt> is associated with a <a class="reference internal" href="metadata.html#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a> which
 
409
in turn is associated with a <a class="reference internal" href="metadata.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a>.   Conversely,
 
410
when <a class="reference internal" href="#sqlalchemy.schema.ForeignKeyConstraint" title="sqlalchemy.schema.ForeignKeyConstraint"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKeyConstraint</span></tt></a> is applied to a <a class="reference internal" href="metadata.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a>,
 
411
<tt class="docutils literal"><span class="pre">ForeignKey</span></tt> markers are automatically generated to be
 
412
present on each associated <a class="reference internal" href="metadata.html#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a>, which are also
 
413
associated with the constraint object.</p>
 
414
<p>Note that you cannot define a &#8220;composite&#8221; foreign key constraint,
 
415
that is a constraint between a grouping of multiple parent/child
 
416
columns, using <tt class="docutils literal"><span class="pre">ForeignKey</span></tt> objects.   To define this grouping,
 
417
the <a class="reference internal" href="#sqlalchemy.schema.ForeignKeyConstraint" title="sqlalchemy.schema.ForeignKeyConstraint"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKeyConstraint</span></tt></a> object must be used, and applied
 
418
to the <a class="reference internal" href="metadata.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a>.   The associated <tt class="docutils literal"><span class="pre">ForeignKey</span></tt> objects
 
419
are created automatically.</p>
 
420
<p>The <tt class="docutils literal"><span class="pre">ForeignKey</span></tt> objects associated with an individual
 
421
<a class="reference internal" href="metadata.html#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a> object are available in the <cite>foreign_keys</cite> collection
 
422
of that column.</p>
 
423
<p>Further examples of foreign key configuration are in
 
424
<em class="xref std std-ref">metadata_foreignkeys</em>.</p>
 
425
<dl class="method">
 
426
<dt id="sqlalchemy.schema.ForeignKey.__init__">
 
427
<tt class="descname">__init__</tt><big>(</big><em>column</em>, <em>_constraint=None</em>, <em>use_alter=False</em>, <em>name=None</em>, <em>onupdate=None</em>, <em>ondelete=None</em>, <em>deferrable=None</em>, <em>schema=None</em>, <em>initially=None</em>, <em>link_to_name=False</em>, <em>match=None</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.ForeignKey.__init__" title="Permalink to this definition">¶</a></dt>
 
428
<dd><p>Construct a column-level FOREIGN KEY.</p>
 
429
<p>The <a class="reference internal" href="#sqlalchemy.schema.ForeignKey" title="sqlalchemy.schema.ForeignKey"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></tt></a> object when constructed generates a
 
430
<a class="reference internal" href="#sqlalchemy.schema.ForeignKeyConstraint" title="sqlalchemy.schema.ForeignKeyConstraint"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKeyConstraint</span></tt></a> which is associated with the parent
 
431
<a class="reference internal" href="metadata.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> object&#8217;s collection of constraints.</p>
 
432
<table class="docutils field-list" frame="void" rules="none">
 
433
<col class="field-name" />
 
434
<col class="field-body" />
 
435
<tbody valign="top">
 
436
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
 
437
<li><strong>column</strong> &#8211; <p>A single target column for the key relationship. A
 
438
<a class="reference internal" href="metadata.html#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a> object or a column name as a string:
 
439
<tt class="docutils literal"><span class="pre">tablename.columnkey</span></tt> or <tt class="docutils literal"><span class="pre">schema.tablename.columnkey</span></tt>.
 
440
<tt class="docutils literal"><span class="pre">columnkey</span></tt> is the <tt class="docutils literal"><span class="pre">key</span></tt> which has been assigned to the column
 
441
(defaults to the column name itself), unless <tt class="docutils literal"><span class="pre">link_to_name</span></tt> is
 
442
<tt class="docutils literal"><span class="pre">True</span></tt> in which case the rendered name of the column is used.</p>
 
443
<div class="versionadded">
 
444
<p><span>New in version 0.7.4: </span>Note that if the schema name is not included, and the
 
445
underlying <a class="reference internal" href="metadata.html#sqlalchemy.schema.MetaData" title="sqlalchemy.schema.MetaData"><tt class="xref py py-class docutils literal"><span class="pre">MetaData</span></tt></a> has a &#8220;schema&#8221;, that value will
 
446
be used.</p>
 
447
</div>
 
448
</li>
 
449
<li><strong>name</strong> &#8211; Optional string. An in-database name for the key if
 
450
<cite>constraint</cite> is not provided.</li>
 
451
<li><strong>onupdate</strong> &#8211; Optional string. If set, emit ON UPDATE &lt;value&gt; when
 
452
issuing DDL for this constraint. Typical values include CASCADE,
 
453
DELETE and RESTRICT.</li>
 
454
<li><strong>ondelete</strong> &#8211; Optional string. If set, emit ON DELETE &lt;value&gt; when
 
455
issuing DDL for this constraint. Typical values include CASCADE,
 
456
DELETE and RESTRICT.</li>
 
457
<li><strong>deferrable</strong> &#8211; Optional bool. If set, emit DEFERRABLE or NOT
 
458
DEFERRABLE when issuing DDL for this constraint.</li>
 
459
<li><strong>initially</strong> &#8211; Optional string. If set, emit INITIALLY &lt;value&gt; when
 
460
issuing DDL for this constraint.</li>
 
461
<li><strong>link_to_name</strong> &#8211; if True, the string name given in <tt class="docutils literal"><span class="pre">column</span></tt> is
 
462
the rendered name of the referenced column, not its locally
 
463
assigned <tt class="docutils literal"><span class="pre">key</span></tt>.</li>
 
464
<li><strong>use_alter</strong> &#8211; passed to the underlying
 
465
<a class="reference internal" href="#sqlalchemy.schema.ForeignKeyConstraint" title="sqlalchemy.schema.ForeignKeyConstraint"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKeyConstraint</span></tt></a> to indicate the constraint should be
 
466
generated/dropped externally from the CREATE TABLE/ DROP TABLE
 
467
statement. See that classes&#8217; constructor for details.</li>
 
468
<li><strong>match</strong> &#8211; Optional string. If set, emit MATCH &lt;value&gt; when issuing
 
469
DDL for this constraint. Typical values include SIMPLE, PARTIAL
 
470
and FULL.</li>
 
471
<li><strong>schema</strong> &#8211; Deprecated; this flag does nothing and will be removed
 
472
in 0.9.</li>
 
473
</ul>
 
474
</td>
 
475
</tr>
 
476
</tbody>
 
477
</table>
 
478
</dd></dl>
 
479
 
 
480
<dl class="attribute">
 
481
<dt id="sqlalchemy.schema.ForeignKey.column">
 
482
<tt class="descname">column</tt><a class="headerlink" href="#sqlalchemy.schema.ForeignKey.column" title="Permalink to this definition">¶</a></dt>
 
483
<dd><p>Return the target <a class="reference internal" href="metadata.html#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a> referenced by this
 
484
<a class="reference internal" href="#sqlalchemy.schema.ForeignKey" title="sqlalchemy.schema.ForeignKey"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></tt></a>.</p>
 
485
<p>If this <a class="reference internal" href="#sqlalchemy.schema.ForeignKey" title="sqlalchemy.schema.ForeignKey"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></tt></a> was created using a
 
486
string-based target column specification, this
 
487
attribute will on first access initiate a resolution
 
488
process to locate the referenced remote
 
489
<a class="reference internal" href="metadata.html#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a>.  The resolution process traverses
 
490
to the parent <a class="reference internal" href="metadata.html#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a>, <a class="reference internal" href="metadata.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a>, and
 
491
<a class="reference internal" href="metadata.html#sqlalchemy.schema.MetaData" title="sqlalchemy.schema.MetaData"><tt class="xref py py-class docutils literal"><span class="pre">MetaData</span></tt></a> to proceed - if any of these aren&#8217;t
 
492
yet present, an error is raised.</p>
 
493
</dd></dl>
 
494
 
 
495
<dl class="method">
 
496
<dt id="sqlalchemy.schema.ForeignKey.copy">
 
497
<tt class="descname">copy</tt><big>(</big><em>schema=None</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.ForeignKey.copy" title="Permalink to this definition">¶</a></dt>
 
498
<dd><p>Produce a copy of this <a class="reference internal" href="#sqlalchemy.schema.ForeignKey" title="sqlalchemy.schema.ForeignKey"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></tt></a> object.</p>
 
499
<p>The new <a class="reference internal" href="#sqlalchemy.schema.ForeignKey" title="sqlalchemy.schema.ForeignKey"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></tt></a> will not be bound
 
500
to any <a class="reference internal" href="metadata.html#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a>.</p>
 
501
<p>This method is usually used by the internal
 
502
copy procedures of <a class="reference internal" href="metadata.html#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a>, <a class="reference internal" href="metadata.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a>,
 
503
and <a class="reference internal" href="metadata.html#sqlalchemy.schema.MetaData" title="sqlalchemy.schema.MetaData"><tt class="xref py py-class docutils literal"><span class="pre">MetaData</span></tt></a>.</p>
 
504
<table class="docutils field-list" frame="void" rules="none">
 
505
<col class="field-name" />
 
506
<col class="field-body" />
 
507
<tbody valign="top">
 
508
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>schema</strong> &#8211; The returned <a class="reference internal" href="#sqlalchemy.schema.ForeignKey" title="sqlalchemy.schema.ForeignKey"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></tt></a> will
 
509
reference the original table and column name, qualified
 
510
by the given string schema name.</td>
 
511
</tr>
 
512
</tbody>
 
513
</table>
 
514
</dd></dl>
 
515
 
 
516
<dl class="method">
 
517
<dt id="sqlalchemy.schema.ForeignKey.get_referent">
 
518
<tt class="descname">get_referent</tt><big>(</big><em>table</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.ForeignKey.get_referent" title="Permalink to this definition">¶</a></dt>
 
519
<dd><p>Return the <a class="reference internal" href="metadata.html#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a> in the given <a class="reference internal" href="metadata.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a>
 
520
referenced by this <a class="reference internal" href="#sqlalchemy.schema.ForeignKey" title="sqlalchemy.schema.ForeignKey"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></tt></a>.</p>
 
521
<p>Returns None if this <a class="reference internal" href="#sqlalchemy.schema.ForeignKey" title="sqlalchemy.schema.ForeignKey"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></tt></a> does not reference the given
 
522
<a class="reference internal" href="metadata.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a>.</p>
 
523
</dd></dl>
 
524
 
 
525
<dl class="method">
 
526
<dt id="sqlalchemy.schema.ForeignKey.references">
 
527
<tt class="descname">references</tt><big>(</big><em>table</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.ForeignKey.references" title="Permalink to this definition">¶</a></dt>
 
528
<dd><p>Return True if the given <a class="reference internal" href="metadata.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> is referenced by this
 
529
<a class="reference internal" href="#sqlalchemy.schema.ForeignKey" title="sqlalchemy.schema.ForeignKey"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></tt></a>.</p>
 
530
</dd></dl>
 
531
 
 
532
<dl class="attribute">
 
533
<dt id="sqlalchemy.schema.ForeignKey.target_fullname">
 
534
<tt class="descname">target_fullname</tt><a class="headerlink" href="#sqlalchemy.schema.ForeignKey.target_fullname" title="Permalink to this definition">¶</a></dt>
 
535
<dd><p>Return a string based &#8216;column specification&#8217; for this
 
536
<a class="reference internal" href="#sqlalchemy.schema.ForeignKey" title="sqlalchemy.schema.ForeignKey"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></tt></a>.</p>
 
537
<p>This is usually the equivalent of the string-based &#8220;tablename.colname&#8221;
 
538
argument first passed to the object&#8217;s constructor.</p>
 
539
</dd></dl>
 
540
 
 
541
</dd></dl>
 
542
 
 
543
<dl class="class">
 
544
<dt id="sqlalchemy.schema.ForeignKeyConstraint">
 
545
<em class="property">class </em><tt class="descclassname">sqlalchemy.schema.</tt><tt class="descname">ForeignKeyConstraint</tt><big>(</big><em>columns</em>, <em>refcolumns</em>, <em>name=None</em>, <em>onupdate=None</em>, <em>ondelete=None</em>, <em>deferrable=None</em>, <em>initially=None</em>, <em>use_alter=False</em>, <em>link_to_name=False</em>, <em>match=None</em>, <em>table=None</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.ForeignKeyConstraint" title="Permalink to this definition">¶</a></dt>
 
546
<dd><p>Bases: <a class="reference internal" href="#sqlalchemy.schema.Constraint" title="sqlalchemy.schema.Constraint"><tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.schema.Constraint</span></tt></a></p>
 
547
<p>A table-level FOREIGN KEY constraint.</p>
 
548
<p>Defines a single column or composite FOREIGN KEY ... REFERENCES
 
549
constraint. For a no-frills, single column foreign key, adding a
 
550
<a class="reference internal" href="#sqlalchemy.schema.ForeignKey" title="sqlalchemy.schema.ForeignKey"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></tt></a> to the definition of a <a class="reference internal" href="metadata.html#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a> is a shorthand
 
551
equivalent for an unnamed, single column <a class="reference internal" href="#sqlalchemy.schema.ForeignKeyConstraint" title="sqlalchemy.schema.ForeignKeyConstraint"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKeyConstraint</span></tt></a>.</p>
 
552
<p>Examples of foreign key configuration are in <em class="xref std std-ref">metadata_foreignkeys</em>.</p>
 
553
<dl class="method">
 
554
<dt id="sqlalchemy.schema.ForeignKeyConstraint.__init__">
 
555
<tt class="descname">__init__</tt><big>(</big><em>columns</em>, <em>refcolumns</em>, <em>name=None</em>, <em>onupdate=None</em>, <em>ondelete=None</em>, <em>deferrable=None</em>, <em>initially=None</em>, <em>use_alter=False</em>, <em>link_to_name=False</em>, <em>match=None</em>, <em>table=None</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.ForeignKeyConstraint.__init__" title="Permalink to this definition">¶</a></dt>
 
556
<dd><p>Construct a composite-capable FOREIGN KEY.</p>
 
557
<table class="docutils field-list" frame="void" rules="none">
 
558
<col class="field-name" />
 
559
<col class="field-body" />
 
560
<tbody valign="top">
 
561
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
 
562
<li><strong>columns</strong> &#8211; A sequence of local column names. The named columns
 
563
must be defined and present in the parent Table. The names should
 
564
match the <tt class="docutils literal"><span class="pre">key</span></tt> given to each column (defaults to the name) unless
 
565
<tt class="docutils literal"><span class="pre">link_to_name</span></tt> is True.</li>
 
566
<li><strong>refcolumns</strong> &#8211; A sequence of foreign column names or Column
 
567
objects. The columns must all be located within the same Table.</li>
 
568
<li><strong>name</strong> &#8211; Optional, the in-database name of the key.</li>
 
569
<li><strong>onupdate</strong> &#8211; Optional string. If set, emit ON UPDATE &lt;value&gt; when
 
570
issuing DDL for this constraint. Typical values include CASCADE,
 
571
DELETE and RESTRICT.</li>
 
572
<li><strong>ondelete</strong> &#8211; Optional string. If set, emit ON DELETE &lt;value&gt; when
 
573
issuing DDL for this constraint. Typical values include CASCADE,
 
574
DELETE and RESTRICT.</li>
 
575
<li><strong>deferrable</strong> &#8211; Optional bool. If set, emit DEFERRABLE or NOT
 
576
DEFERRABLE when issuing DDL for this constraint.</li>
 
577
<li><strong>initially</strong> &#8211; Optional string. If set, emit INITIALLY &lt;value&gt; when
 
578
issuing DDL for this constraint.</li>
 
579
<li><strong>link_to_name</strong> &#8211; if True, the string name given in <tt class="docutils literal"><span class="pre">column</span></tt> is
 
580
the rendered name of the referenced column, not its locally assigned
 
581
<tt class="docutils literal"><span class="pre">key</span></tt>.</li>
 
582
<li><strong>use_alter</strong> &#8211; If True, do not emit the DDL for this constraint as
 
583
part of the CREATE TABLE definition. Instead, generate it via an
 
584
ALTER TABLE statement issued after the full collection of tables
 
585
have been created, and drop it via an ALTER TABLE statement before
 
586
the full collection of tables are dropped. This is shorthand for the
 
587
usage of <a class="reference internal" href="ddl.html#sqlalchemy.schema.AddConstraint" title="sqlalchemy.schema.AddConstraint"><tt class="xref py py-class docutils literal"><span class="pre">AddConstraint</span></tt></a> and <a class="reference internal" href="ddl.html#sqlalchemy.schema.DropConstraint" title="sqlalchemy.schema.DropConstraint"><tt class="xref py py-class docutils literal"><span class="pre">DropConstraint</span></tt></a> applied
 
588
as &#8220;after-create&#8221; and &#8220;before-drop&#8221; events on the MetaData object.
 
589
This is normally used to generate/drop constraints on objects that
 
590
are mutually dependent on each other.</li>
 
591
<li><strong>match</strong> &#8211; Optional string. If set, emit MATCH &lt;value&gt; when issuing
 
592
DDL for this constraint. Typical values include SIMPLE, PARTIAL
 
593
and FULL.</li>
 
594
</ul>
 
595
</td>
 
596
</tr>
 
597
</tbody>
 
598
</table>
 
599
</dd></dl>
 
600
 
 
601
</dd></dl>
 
602
 
 
603
<dl class="class">
 
604
<dt id="sqlalchemy.schema.PrimaryKeyConstraint">
 
605
<em class="property">class </em><tt class="descclassname">sqlalchemy.schema.</tt><tt class="descname">PrimaryKeyConstraint</tt><big>(</big><em>*columns</em>, <em>**kw</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.PrimaryKeyConstraint" title="Permalink to this definition">¶</a></dt>
 
606
<dd><p>Bases: <a class="reference internal" href="#sqlalchemy.schema.ColumnCollectionConstraint" title="sqlalchemy.schema.ColumnCollectionConstraint"><tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.schema.ColumnCollectionConstraint</span></tt></a></p>
 
607
<p>A table-level PRIMARY KEY constraint.</p>
 
608
<p>Defines a single column or composite PRIMARY KEY constraint. For a
 
609
no-frills primary key, adding <tt class="docutils literal"><span class="pre">primary_key=True</span></tt> to one or more
 
610
<tt class="docutils literal"><span class="pre">Column</span></tt> definitions is a shorthand equivalent for an unnamed single- or
 
611
multiple-column PrimaryKeyConstraint.</p>
 
612
</dd></dl>
 
613
 
 
614
<dl class="class">
 
615
<dt id="sqlalchemy.schema.UniqueConstraint">
 
616
<em class="property">class </em><tt class="descclassname">sqlalchemy.schema.</tt><tt class="descname">UniqueConstraint</tt><big>(</big><em>*columns</em>, <em>**kw</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.UniqueConstraint" title="Permalink to this definition">¶</a></dt>
 
617
<dd><p>Bases: <a class="reference internal" href="#sqlalchemy.schema.ColumnCollectionConstraint" title="sqlalchemy.schema.ColumnCollectionConstraint"><tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.schema.ColumnCollectionConstraint</span></tt></a></p>
 
618
<p>A table-level UNIQUE constraint.</p>
 
619
<p>Defines a single column or composite UNIQUE constraint. For a no-frills,
 
620
single column constraint, adding <tt class="docutils literal"><span class="pre">unique=True</span></tt> to the <tt class="docutils literal"><span class="pre">Column</span></tt>
 
621
definition is a shorthand equivalent for an unnamed, single column
 
622
UniqueConstraint.</p>
 
623
</dd></dl>
 
624
 
 
625
</div>
 
626
<div class="section" id="indexes">
 
627
<span id="schema-indexes"></span><h2>Indexes<a class="headerlink" href="#indexes" title="Permalink to this headline">¶</a></h2>
 
628
<p>Indexes can be created anonymously (using an auto-generated name <tt class="docutils literal"><span class="pre">ix_&lt;column</span>
 
629
<span class="pre">label&gt;</span></tt>) for a single column using the inline <tt class="docutils literal"><span class="pre">index</span></tt> keyword on
 
630
<a class="reference internal" href="metadata.html#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a>, which also modifies the usage of
 
631
<tt class="docutils literal"><span class="pre">unique</span></tt> to apply the uniqueness to the index itself, instead of adding a
 
632
separate UNIQUE constraint. For indexes with specific names or which encompass
 
633
more than one column, use the <a class="reference internal" href="#sqlalchemy.schema.Index" title="sqlalchemy.schema.Index"><tt class="xref py py-class docutils literal"><span class="pre">Index</span></tt></a> construct,
 
634
which requires a name.</p>
 
635
<p>Below we illustrate a <a class="reference internal" href="metadata.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> with several
 
636
<a class="reference internal" href="#sqlalchemy.schema.Index" title="sqlalchemy.schema.Index"><tt class="xref py py-class docutils literal"><span class="pre">Index</span></tt></a> objects associated. The DDL for &#8220;CREATE
 
637
INDEX&#8221; is issued right after the create statements for the table:</p>
 
638
<div class="highlight-python+sql"><div class="highlight"><pre><span class="n">meta</span> <span class="o">=</span> <span class="n">MetaData</span><span class="p">()</span>
 
639
<span class="n">mytable</span> <span class="o">=</span> <span class="n">Table</span><span class="p">(</span><span class="s">&#39;mytable&#39;</span><span class="p">,</span> <span class="n">meta</span><span class="p">,</span>
 
640
    <span class="c"># an indexed column, with index &quot;ix_mytable_col1&quot;</span>
 
641
    <span class="n">Column</span><span class="p">(</span><span class="s">&#39;col1&#39;</span><span class="p">,</span> <span class="n">Integer</span><span class="p">,</span> <span class="n">index</span><span class="o">=</span><span class="bp">True</span><span class="p">),</span>
 
642
 
 
643
    <span class="c"># a uniquely indexed column with index &quot;ix_mytable_col2&quot;</span>
 
644
    <span class="n">Column</span><span class="p">(</span><span class="s">&#39;col2&#39;</span><span class="p">,</span> <span class="n">Integer</span><span class="p">,</span> <span class="n">index</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">unique</span><span class="o">=</span><span class="bp">True</span><span class="p">),</span>
 
645
 
 
646
    <span class="n">Column</span><span class="p">(</span><span class="s">&#39;col3&#39;</span><span class="p">,</span> <span class="n">Integer</span><span class="p">),</span>
 
647
    <span class="n">Column</span><span class="p">(</span><span class="s">&#39;col4&#39;</span><span class="p">,</span> <span class="n">Integer</span><span class="p">),</span>
 
648
 
 
649
    <span class="n">Column</span><span class="p">(</span><span class="s">&#39;col5&#39;</span><span class="p">,</span> <span class="n">Integer</span><span class="p">),</span>
 
650
    <span class="n">Column</span><span class="p">(</span><span class="s">&#39;col6&#39;</span><span class="p">,</span> <span class="n">Integer</span><span class="p">),</span>
 
651
    <span class="p">)</span>
 
652
 
 
653
<span class="c"># place an index on col3, col4</span>
 
654
<span class="n">Index</span><span class="p">(</span><span class="s">&#39;idx_col34&#39;</span><span class="p">,</span> <span class="n">mytable</span><span class="o">.</span><span class="n">c</span><span class="o">.</span><span class="n">col3</span><span class="p">,</span> <span class="n">mytable</span><span class="o">.</span><span class="n">c</span><span class="o">.</span><span class="n">col4</span><span class="p">)</span>
 
655
 
 
656
<span class="c"># place a unique index on col5, col6</span>
 
657
<span class="n">Index</span><span class="p">(</span><span class="s">&#39;myindex&#39;</span><span class="p">,</span> <span class="n">mytable</span><span class="o">.</span><span class="n">c</span><span class="o">.</span><span class="n">col5</span><span class="p">,</span> <span class="n">mytable</span><span class="o">.</span><span class="n">c</span><span class="o">.</span><span class="n">col6</span><span class="p">,</span> <span class="n">unique</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
 
658
 
 
659
<a href='#' class='sql_link'>sql</a><span class="n">mytable</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">engine</span><span class="p">)</span>
 
660
<div class='popup_sql'>CREATE TABLE mytable (
 
661
    col1 INTEGER,
 
662
    col2 INTEGER,
 
663
    col3 INTEGER,
 
664
    col4 INTEGER,
 
665
    col5 INTEGER,
 
666
    col6 INTEGER
 
667
)
 
668
CREATE INDEX ix_mytable_col1 ON mytable (col1)
 
669
CREATE UNIQUE INDEX ix_mytable_col2 ON mytable (col2)
 
670
CREATE UNIQUE INDEX myindex ON mytable (col5, col6)
 
671
CREATE INDEX idx_col34 ON mytable (col3, col4)</div></pre></div>
 
672
</div>
 
673
<p>Note in the example above, the <a class="reference internal" href="#sqlalchemy.schema.Index" title="sqlalchemy.schema.Index"><tt class="xref py py-class docutils literal"><span class="pre">Index</span></tt></a> construct is created
 
674
externally to the table which it corresponds, using <a class="reference internal" href="metadata.html#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a>
 
675
objects directly.  <a class="reference internal" href="#sqlalchemy.schema.Index" title="sqlalchemy.schema.Index"><tt class="xref py py-class docutils literal"><span class="pre">Index</span></tt></a> also supports
 
676
&#8220;inline&#8221; definition inside the <a class="reference internal" href="metadata.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a>, using string names to
 
677
identify columns:</p>
 
678
<div class="highlight-python"><div class="highlight"><pre><span class="n">meta</span> <span class="o">=</span> <span class="n">MetaData</span><span class="p">()</span>
 
679
<span class="n">mytable</span> <span class="o">=</span> <span class="n">Table</span><span class="p">(</span><span class="s">&#39;mytable&#39;</span><span class="p">,</span> <span class="n">meta</span><span class="p">,</span>
 
680
    <span class="n">Column</span><span class="p">(</span><span class="s">&#39;col1&#39;</span><span class="p">,</span> <span class="n">Integer</span><span class="p">),</span>
 
681
 
 
682
    <span class="n">Column</span><span class="p">(</span><span class="s">&#39;col2&#39;</span><span class="p">,</span> <span class="n">Integer</span><span class="p">),</span>
 
683
 
 
684
    <span class="n">Column</span><span class="p">(</span><span class="s">&#39;col3&#39;</span><span class="p">,</span> <span class="n">Integer</span><span class="p">),</span>
 
685
    <span class="n">Column</span><span class="p">(</span><span class="s">&#39;col4&#39;</span><span class="p">,</span> <span class="n">Integer</span><span class="p">),</span>
 
686
 
 
687
    <span class="c"># place an index on col1, col2</span>
 
688
    <span class="n">Index</span><span class="p">(</span><span class="s">&#39;idx_col12&#39;</span><span class="p">,</span> <span class="s">&#39;col1&#39;</span><span class="p">,</span> <span class="s">&#39;col2&#39;</span><span class="p">),</span>
 
689
 
 
690
    <span class="c"># place a unique index on col3, col4</span>
 
691
    <span class="n">Index</span><span class="p">(</span><span class="s">&#39;idx_col34&#39;</span><span class="p">,</span> <span class="s">&#39;col3&#39;</span><span class="p">,</span> <span class="s">&#39;col4&#39;</span><span class="p">,</span> <span class="n">unique</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
 
692
<span class="p">)</span></pre></div>
 
693
</div>
 
694
<div class="versionadded">
 
695
<p><span>New in version 0.7: </span>Support of &#8220;inline&#8221; definition inside the <a class="reference internal" href="metadata.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a>
 
696
for <a class="reference internal" href="#sqlalchemy.schema.Index" title="sqlalchemy.schema.Index"><tt class="xref py py-class docutils literal"><span class="pre">Index</span></tt></a>.</p>
 
697
</div>
 
698
<p>The <a class="reference internal" href="#sqlalchemy.schema.Index" title="sqlalchemy.schema.Index"><tt class="xref py py-class docutils literal"><span class="pre">Index</span></tt></a> object also supports its own <tt class="docutils literal"><span class="pre">create()</span></tt> method:</p>
 
699
<div class="highlight-python+sql"><div class="highlight"><pre><span class="n">i</span> <span class="o">=</span> <span class="n">Index</span><span class="p">(</span><span class="s">&#39;someindex&#39;</span><span class="p">,</span> <span class="n">mytable</span><span class="o">.</span><span class="n">c</span><span class="o">.</span><span class="n">col5</span><span class="p">)</span>
 
700
<a href='#' class='sql_link'>sql</a><span class="n">i</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">engine</span><span class="p">)</span>
 
701
<div class='popup_sql'>CREATE INDEX someindex ON mytable (col5)</div></pre></div>
 
702
</div>
 
703
<div class="section" id="functional-indexes">
 
704
<span id="schema-indexes-functional"></span><h3>Functional Indexes<a class="headerlink" href="#functional-indexes" title="Permalink to this headline">¶</a></h3>
 
705
<p><a class="reference internal" href="#sqlalchemy.schema.Index" title="sqlalchemy.schema.Index"><tt class="xref py py-class docutils literal"><span class="pre">Index</span></tt></a> supports SQL and function expressions, as supported by the
 
706
target backend.  To create an index against a column using a descending
 
707
value, the <a class="reference internal" href="sqlelement.html#sqlalchemy.sql.expression.ColumnElement.desc" title="sqlalchemy.sql.expression.ColumnElement.desc"><tt class="xref py py-meth docutils literal"><span class="pre">ColumnElement.desc()</span></tt></a> modifier may be used:</p>
 
708
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">sqlalchemy</span> <span class="kn">import</span> <span class="n">Index</span>
 
709
 
 
710
<span class="n">Index</span><span class="p">(</span><span class="s">&#39;someindex&#39;</span><span class="p">,</span> <span class="n">mytable</span><span class="o">.</span><span class="n">c</span><span class="o">.</span><span class="n">somecol</span><span class="o">.</span><span class="n">desc</span><span class="p">())</span></pre></div>
 
711
</div>
 
712
<p>Or with a backend that supports functional indexes such as Postgresql,
 
713
a &#8220;case insensitive&#8221; index can be created using the <tt class="docutils literal"><span class="pre">lower()</span></tt> function:</p>
 
714
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">sqlalchemy</span> <span class="kn">import</span> <span class="n">func</span><span class="p">,</span> <span class="n">Index</span>
 
715
 
 
716
<span class="n">Index</span><span class="p">(</span><span class="s">&#39;someindex&#39;</span><span class="p">,</span> <span class="n">func</span><span class="o">.</span><span class="n">lower</span><span class="p">(</span><span class="n">mytable</span><span class="o">.</span><span class="n">c</span><span class="o">.</span><span class="n">somecol</span><span class="p">))</span></pre></div>
 
717
</div>
 
718
<div class="versionadded">
 
719
<p><span>New in version 0.8: </span><a class="reference internal" href="#sqlalchemy.schema.Index" title="sqlalchemy.schema.Index"><tt class="xref py py-class docutils literal"><span class="pre">Index</span></tt></a> supports SQL expressions and functions
 
720
as well as plain columns.</p>
 
721
</div>
 
722
</div>
 
723
</div>
 
724
<div class="section" id="index-api">
 
725
<h2>Index API<a class="headerlink" href="#index-api" title="Permalink to this headline">¶</a></h2>
 
726
<dl class="class">
 
727
<dt id="sqlalchemy.schema.Index">
 
728
<em class="property">class </em><tt class="descclassname">sqlalchemy.schema.</tt><tt class="descname">Index</tt><big>(</big><em>name</em>, <em>*expressions</em>, <em>**kw</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Index" title="Permalink to this definition">¶</a></dt>
 
729
<dd><p>Bases: <tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.schema.ColumnCollectionMixin</span></tt>, <a class="reference internal" href="metadata.html#sqlalchemy.schema.SchemaItem" title="sqlalchemy.schema.SchemaItem"><tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.schema.SchemaItem</span></tt></a></p>
 
730
<p>A table-level INDEX.</p>
 
731
<p>Defines a composite (one or more column) INDEX.</p>
 
732
<p>E.g.:</p>
 
733
<div class="highlight-python"><div class="highlight"><pre><span class="n">sometable</span> <span class="o">=</span> <span class="n">Table</span><span class="p">(</span><span class="s">&quot;sometable&quot;</span><span class="p">,</span> <span class="n">metadata</span><span class="p">,</span>
 
734
                <span class="n">Column</span><span class="p">(</span><span class="s">&quot;name&quot;</span><span class="p">,</span> <span class="n">String</span><span class="p">(</span><span class="mi">50</span><span class="p">)),</span>
 
735
                <span class="n">Column</span><span class="p">(</span><span class="s">&quot;address&quot;</span><span class="p">,</span> <span class="n">String</span><span class="p">(</span><span class="mi">100</span><span class="p">))</span>
 
736
            <span class="p">)</span>
 
737
 
 
738
<span class="n">Index</span><span class="p">(</span><span class="s">&quot;some_index&quot;</span><span class="p">,</span> <span class="n">sometable</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>
 
739
</div>
 
740
<p>For a no-frills, single column index, adding
 
741
<a class="reference internal" href="metadata.html#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a> also supports <tt class="docutils literal"><span class="pre">index=True</span></tt>:</p>
 
742
<div class="highlight-python"><div class="highlight"><pre><span class="n">sometable</span> <span class="o">=</span> <span class="n">Table</span><span class="p">(</span><span class="s">&quot;sometable&quot;</span><span class="p">,</span> <span class="n">metadata</span><span class="p">,</span>
 
743
                <span class="n">Column</span><span class="p">(</span><span class="s">&quot;name&quot;</span><span class="p">,</span> <span class="n">String</span><span class="p">(</span><span class="mi">50</span><span class="p">),</span> <span class="n">index</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
 
744
            <span class="p">)</span></pre></div>
 
745
</div>
 
746
<p>For a composite index, multiple columns can be specified:</p>
 
747
<div class="highlight-python"><div class="highlight"><pre><span class="n">Index</span><span class="p">(</span><span class="s">&quot;some_index&quot;</span><span class="p">,</span> <span class="n">sometable</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="n">sometable</span><span class="o">.</span><span class="n">c</span><span class="o">.</span><span class="n">address</span><span class="p">)</span></pre></div>
 
748
</div>
 
749
<p>Functional indexes are supported as well, keeping in mind that at least
 
750
one <a class="reference internal" href="metadata.html#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a> must be present:</p>
 
751
<div class="highlight-python"><div class="highlight"><pre><span class="n">Index</span><span class="p">(</span><span class="s">&quot;some_index&quot;</span><span class="p">,</span> <span class="n">func</span><span class="o">.</span><span class="n">lower</span><span class="p">(</span><span class="n">sometable</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>
 
752
</div>
 
753
<div class="versionadded">
 
754
<p><span>New in version 0.8: </span>support for functional and expression-based indexes.</p>
 
755
</div>
 
756
<div class="admonition seealso">
 
757
<p class="first admonition-title">See also</p>
 
758
<p><a class="reference internal" href="#schema-indexes"><em>Indexes</em></a> - General information on <a class="reference internal" href="#sqlalchemy.schema.Index" title="sqlalchemy.schema.Index"><tt class="xref py py-class docutils literal"><span class="pre">Index</span></tt></a>.</p>
 
759
<p><a class="reference internal" href="../dialects/postgresql.html#postgresql-indexes"><em>Postgresql-Specific Index Options</em></a> - PostgreSQL-specific options available for the
 
760
<a class="reference internal" href="#sqlalchemy.schema.Index" title="sqlalchemy.schema.Index"><tt class="xref py py-class docutils literal"><span class="pre">Index</span></tt></a> construct.</p>
 
761
<p><a class="reference internal" href="../dialects/mysql.html#mysql-indexes"><em>MySQL Specific Index Options</em></a> - MySQL-specific options available for the
 
762
<a class="reference internal" href="#sqlalchemy.schema.Index" title="sqlalchemy.schema.Index"><tt class="xref py py-class docutils literal"><span class="pre">Index</span></tt></a> construct.</p>
 
763
<p class="last"><a class="reference internal" href="../dialects/mssql.html#mssql-indexes"><em>MSSQL-Specific Index Options</em></a> - MSSQL-specific options available for the
 
764
<a class="reference internal" href="#sqlalchemy.schema.Index" title="sqlalchemy.schema.Index"><tt class="xref py py-class docutils literal"><span class="pre">Index</span></tt></a> construct.</p>
 
765
</div>
 
766
<dl class="method">
 
767
<dt id="sqlalchemy.schema.Index.__init__">
 
768
<tt class="descname">__init__</tt><big>(</big><em>name</em>, <em>*expressions</em>, <em>**kw</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Index.__init__" title="Permalink to this definition">¶</a></dt>
 
769
<dd><p>Construct an index object.</p>
 
770
<table class="docutils field-list" frame="void" rules="none">
 
771
<col class="field-name" />
 
772
<col class="field-body" />
 
773
<tbody valign="top">
 
774
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
 
775
<li><strong>name</strong> &#8211; The name of the index</li>
 
776
<li><strong>*expressions</strong> &#8211; Column or SQL expressions.</li>
 
777
<li><strong>unique</strong> &#8211; Defaults to False: create a unique index.</li>
 
778
<li><strong>**kw</strong> &#8211; Other keyword arguments may be interpreted by specific dialects.</li>
 
779
</ul>
 
780
</td>
 
781
</tr>
 
782
</tbody>
 
783
</table>
 
784
</dd></dl>
 
785
 
 
786
<dl class="attribute">
 
787
<dt id="sqlalchemy.schema.Index.bind">
 
788
<tt class="descname">bind</tt><a class="headerlink" href="#sqlalchemy.schema.Index.bind" title="Permalink to this definition">¶</a></dt>
 
789
<dd><p>Return the connectable associated with this Index.</p>
 
790
</dd></dl>
 
791
 
 
792
<dl class="method">
 
793
<dt id="sqlalchemy.schema.Index.create">
 
794
<tt class="descname">create</tt><big>(</big><em>bind=None</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Index.create" title="Permalink to this definition">¶</a></dt>
 
795
<dd><p>Issue a <tt class="docutils literal"><span class="pre">CREATE</span></tt> statement for this
 
796
<a class="reference internal" href="#sqlalchemy.schema.Index" title="sqlalchemy.schema.Index"><tt class="xref py py-class docutils literal"><span class="pre">Index</span></tt></a>, using the given <a class="reference internal" href="connections.html#sqlalchemy.engine.Connectable" title="sqlalchemy.engine.Connectable"><tt class="xref py py-class docutils literal"><span class="pre">Connectable</span></tt></a>
 
797
for connectivity.</p>
 
798
<div class="admonition seealso">
 
799
<p class="first admonition-title">See also</p>
 
800
<p class="last"><a class="reference internal" href="metadata.html#sqlalchemy.schema.MetaData.create_all" title="sqlalchemy.schema.MetaData.create_all"><tt class="xref py py-meth docutils literal"><span class="pre">MetaData.create_all()</span></tt></a>.</p>
 
801
</div>
 
802
</dd></dl>
 
803
 
 
804
<dl class="method">
 
805
<dt id="sqlalchemy.schema.Index.drop">
 
806
<tt class="descname">drop</tt><big>(</big><em>bind=None</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Index.drop" title="Permalink to this definition">¶</a></dt>
 
807
<dd><p>Issue a <tt class="docutils literal"><span class="pre">DROP</span></tt> statement for this
 
808
<a class="reference internal" href="#sqlalchemy.schema.Index" title="sqlalchemy.schema.Index"><tt class="xref py py-class docutils literal"><span class="pre">Index</span></tt></a>, using the given <a class="reference internal" href="connections.html#sqlalchemy.engine.Connectable" title="sqlalchemy.engine.Connectable"><tt class="xref py py-class docutils literal"><span class="pre">Connectable</span></tt></a>
 
809
for connectivity.</p>
 
810
<div class="admonition seealso">
 
811
<p class="first admonition-title">See also</p>
 
812
<p class="last"><a class="reference internal" href="metadata.html#sqlalchemy.schema.MetaData.drop_all" title="sqlalchemy.schema.MetaData.drop_all"><tt class="xref py py-meth docutils literal"><span class="pre">MetaData.drop_all()</span></tt></a>.</p>
 
813
</div>
 
814
</dd></dl>
 
815
 
 
816
</dd></dl>
 
817
 
 
818
</div>
 
819
</div>
 
820
 
 
821
    </div>
 
822
 
 
823
</div>
 
824
 
 
825
<div id="docs-bottom-navigation" class="docs-navigation-links">
 
826
        Previous:
 
827
        <a href="defaults.html" title="previous chapter">Column Insert/Update Defaults</a>
 
828
        Next:
 
829
        <a href="ddl.html" title="next chapter">Customizing DDL</a>
 
830
 
 
831
    <div id="docs-copyright">
 
832
        &copy; <a href="../copyright.html">Copyright</a> 2007-2013, the SQLAlchemy authors and contributors.
 
833
        Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.2b1.
 
834
    </div>
 
835
</div>
 
836
 
 
837
</div>
 
838
 
 
839
        
 
840
    </body>
 
841
</html>
 
842
 
 
843