1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
<html xmlns="http://www.w3.org/1999/xhtml">
6
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
11
Defining Constraints and Indexes
13
SQLAlchemy 0.8 Documentation
17
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
18
<link rel="stylesheet" href="../_static/docs.css" type="text/css" />
20
<script type="text/javascript">
21
var DOCUMENTATION_OPTIONS = {
24
COLLAPSE_MODINDEX: false,
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" />
53
<div id="docs-container">
57
<div id="docs-header">
58
<h1>SQLAlchemy 0.8 Documentation</h1>
60
<div id="docs-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" />
69
<div id="docs-version-header">
70
Release: <span class="version-num">0.8.3</span> | Release Date: October 26, 2013
77
<div id="docs-top-navigation">
78
<div id="docs-top-page-control" class="docs-navigation-links">
81
<a href="defaults.html" title="previous chapter">Column Insert/Update Defaults</a>
84
<a href="ddl.html" title="next chapter">Customizing DDL</a>
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
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>
100
Defining Constraints and Indexes
105
Defining Constraints and Indexes
112
<div id="docs-body-container">
114
<div id="docs-sidebar">
115
<h3><a href="../index.html">Table of Contents</a></h3>
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>
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>
131
<li><a class="reference internal" href="#index-api">Index API</a></li>
137
<h4>Previous Topic</h4>
139
<a href="defaults.html" title="previous chapter">Column Insert/Update Defaults</a>
143
<a href="ddl.html" title="next chapter">Customizing DDL</a>
147
<h4>Quick Search</h4>
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" />
158
<div id="docs-body" class="withsidebar" >
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 “joint” 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
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">'user_preference'</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">'pref_id'</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">'user_id'</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">"user.user_id"</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">'pref_name'</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">'pref_value'</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>
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’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><tablename>.<columnname></em>, or for a table in a remote
194
schema or “owner” of the form <em><schemaname>.<tablename>.<columnname></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’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>
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">'invoice'</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">'invoice_id'</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">'ref_num'</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">'description'</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>
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">'invoice_item'</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">'item_id'</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">'item_name'</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">'invoice_id'</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">'ref_num'</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">'invoice_id'</span><span class="p">,</span> <span class="s">'ref_num'</span><span class="p">],</span> <span class="p">[</span><span class="s">'invoice.invoice_id'</span><span class="p">,</span> <span class="s">'invoice.ref_num'</span><span class="p">])</span>
223
<span class="p">)</span></pre></div>
225
<p>It’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 “REFERENCES” keyword to be added inline to a column definition
237
within a “CREATE TABLE” 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 “CONSTRAINT”
240
keyword inline with “CREATE TABLE”. 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 “ALTER TABLE <tablename> ADD CONSTRAINT <name>
248
...”. Since a name is required, the <tt class="docutils literal"><span class="pre">name</span></tt> attribute must also be specified.
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">'node'</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">'node_id'</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">'primary_element'</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">'element.element_id'</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">'fk_node_element_id'</span><span class="p">)</span>
254
<span class="p">)</span>
255
<span class="p">)</span>
257
<span class="n">element</span> <span class="o">=</span> <span class="n">Table</span><span class="p">(</span><span class="s">'element'</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">'element_id'</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">'parent_node_id'</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">'parent_node_id'</span><span class="p">],</span>
262
<span class="p">[</span><span class="s">'node.node_id'</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">'fk_element_parent_node_id'</span>
265
<span class="p">)</span>
266
<span class="p">)</span></pre></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 “ON UPDATE
275
CASCADE”, “ON DELETE CASCADE”, and “ON DELETE SET NULL”, corresponding to
276
foreign key constraints. The phrase after “ON UPDATE” or “ON DELETE” 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
“ON UPDATE” or “ON DELETE” 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">'child'</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">'id'</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">'parent.id'</span><span class="p">,</span> <span class="n">onupdate</span><span class="o">=</span><span class="s">"CASCADE"</span><span class="p">,</span> <span class="n">ondelete</span><span class="o">=</span><span class="s">"CASCADE"</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>
290
<span class="n">composite</span> <span class="o">=</span> <span class="n">Table</span><span class="p">(</span><span class="s">'composite'</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">'id'</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">'rev_id'</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">'note_id'</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">'rev_id'</span><span class="p">,</span> <span class="s">'note_id'</span><span class="p">],</span>
296
<span class="p">[</span><span class="s">'revisions.id'</span><span class="p">,</span> <span class="s">'revisions.note_id'</span><span class="p">],</span>
297
<span class="n">onupdate</span><span class="o">=</span><span class="s">"CASCADE"</span><span class="p">,</span> <span class="n">ondelete</span><span class="o">=</span><span class="s">"SET NULL"</span>
298
<span class="p">)</span>
299
<span class="p">)</span></pre></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
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">'mytable'</span><span class="p">,</span> <span class="n">meta</span><span class="p">,</span>
315
<span class="c"># per-column anonymous unique constraint</span>
316
<span class="n">Column</span><span class="p">(</span><span class="s">'col1'</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>
318
<span class="n">Column</span><span class="p">(</span><span class="s">'col2'</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">'col3'</span><span class="p">,</span> <span class="n">Integer</span><span class="p">),</span>
321
<span class="c"># explicit/composite unique constraint. 'name' is optional.</span>
322
<span class="n">UniqueConstraint</span><span class="p">(</span><span class="s">'col2'</span><span class="p">,</span> <span class="s">'col3'</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s">'uix_1'</span><span class="p">)</span>
323
<span class="p">)</span></pre></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 “database independent” 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
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">'mytable'</span><span class="p">,</span> <span class="n">meta</span><span class="p">,</span>
339
<span class="c"># per-column CHECK constraint</span>
340
<span class="n">Column</span><span class="p">(</span><span class="s">'col1'</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">'col1>5'</span><span class="p">)),</span>
342
<span class="n">Column</span><span class="p">(</span><span class="s">'col2'</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">'col3'</span><span class="p">,</span> <span class="n">Integer</span><span class="p">),</span>
345
<span class="c"># table level CHECK constraint. 'name' is optional.</span>
346
<span class="n">CheckConstraint</span><span class="p">(</span><span class="s">'col2 > col3 + 5'</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s">'check1'</span><span class="p">)</span>
347
<span class="p">)</span>
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),
354
CONSTRAINT check1 CHECK (col2 > col3 + 5)
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>
369
<div class="section" id="constraints-api">
370
<h2>Constraints API<a class="headerlink" href="#constraints-api" title="Permalink to this headline">¶</a></h2>
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>
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>
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>
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,
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">"remote_table"</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">"remote_id"</span><span class="p">,</span> <span class="n">ForeignKey</span><span class="p">(</span><span class="s">"main_table.id"</span><span class="p">))</span>
402
<span class="p">)</span></pre></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 “composite” 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
423
<p>Further examples of foreign key configuration are in
424
<em class="xref std std-ref">metadata_foreignkeys</em>.</p>
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’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" />
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> – <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 “schema”, that value will
449
<li><strong>name</strong> – Optional string. An in-database name for the key if
450
<cite>constraint</cite> is not provided.</li>
451
<li><strong>onupdate</strong> – Optional string. If set, emit ON UPDATE <value> when
452
issuing DDL for this constraint. Typical values include CASCADE,
453
DELETE and RESTRICT.</li>
454
<li><strong>ondelete</strong> – Optional string. If set, emit ON DELETE <value> when
455
issuing DDL for this constraint. Typical values include CASCADE,
456
DELETE and RESTRICT.</li>
457
<li><strong>deferrable</strong> – Optional bool. If set, emit DEFERRABLE or NOT
458
DEFERRABLE when issuing DDL for this constraint.</li>
459
<li><strong>initially</strong> – Optional string. If set, emit INITIALLY <value> when
460
issuing DDL for this constraint.</li>
461
<li><strong>link_to_name</strong> – 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> – 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’ constructor for details.</li>
468
<li><strong>match</strong> – Optional string. If set, emit MATCH <value> when issuing
469
DDL for this constraint. Typical values include SIMPLE, PARTIAL
471
<li><strong>schema</strong> – Deprecated; this flag does nothing and will be removed
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’t
492
yet present, an error is raised.</p>
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" />
508
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>schema</strong> – 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>
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>
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>
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 ‘column specification’ 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 “tablename.colname”
538
argument first passed to the object’s constructor.</p>
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>
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" />
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> – 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> – 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> – Optional, the in-database name of the key.</li>
569
<li><strong>onupdate</strong> – Optional string. If set, emit ON UPDATE <value> when
570
issuing DDL for this constraint. Typical values include CASCADE,
571
DELETE and RESTRICT.</li>
572
<li><strong>ondelete</strong> – Optional string. If set, emit ON DELETE <value> when
573
issuing DDL for this constraint. Typical values include CASCADE,
574
DELETE and RESTRICT.</li>
575
<li><strong>deferrable</strong> – Optional bool. If set, emit DEFERRABLE or NOT
576
DEFERRABLE when issuing DDL for this constraint.</li>
577
<li><strong>initially</strong> – Optional string. If set, emit INITIALLY <value> when
578
issuing DDL for this constraint.</li>
579
<li><strong>link_to_name</strong> – 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> – 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 “after-create” and “before-drop” 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> – Optional string. If set, emit MATCH <value> when issuing
592
DDL for this constraint. Typical values include SIMPLE, PARTIAL
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>
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>
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_<column</span>
629
<span class="pre">label></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 “CREATE
637
INDEX” 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">'mytable'</span><span class="p">,</span> <span class="n">meta</span><span class="p">,</span>
640
<span class="c"># an indexed column, with index "ix_mytable_col1"</span>
641
<span class="n">Column</span><span class="p">(</span><span class="s">'col1'</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>
643
<span class="c"># a uniquely indexed column with index "ix_mytable_col2"</span>
644
<span class="n">Column</span><span class="p">(</span><span class="s">'col2'</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>
646
<span class="n">Column</span><span class="p">(</span><span class="s">'col3'</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">'col4'</span><span class="p">,</span> <span class="n">Integer</span><span class="p">),</span>
649
<span class="n">Column</span><span class="p">(</span><span class="s">'col5'</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">'col6'</span><span class="p">,</span> <span class="n">Integer</span><span class="p">),</span>
651
<span class="p">)</span>
653
<span class="c"># place an index on col3, col4</span>
654
<span class="n">Index</span><span class="p">(</span><span class="s">'idx_col34'</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>
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">'myindex'</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>
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 (
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>
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
“inline” 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">'mytable'</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">'col1'</span><span class="p">,</span> <span class="n">Integer</span><span class="p">),</span>
682
<span class="n">Column</span><span class="p">(</span><span class="s">'col2'</span><span class="p">,</span> <span class="n">Integer</span><span class="p">),</span>
684
<span class="n">Column</span><span class="p">(</span><span class="s">'col3'</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">'col4'</span><span class="p">,</span> <span class="n">Integer</span><span class="p">),</span>
687
<span class="c"># place an index on col1, col2</span>
688
<span class="n">Index</span><span class="p">(</span><span class="s">'idx_col12'</span><span class="p">,</span> <span class="s">'col1'</span><span class="p">,</span> <span class="s">'col2'</span><span class="p">),</span>
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">'idx_col34'</span><span class="p">,</span> <span class="s">'col3'</span><span class="p">,</span> <span class="s">'col4'</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>
694
<div class="versionadded">
695
<p><span>New in version 0.7: </span>Support of “inline” 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>
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">'someindex'</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>
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>
710
<span class="n">Index</span><span class="p">(</span><span class="s">'someindex'</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>
712
<p>Or with a backend that supports functional indexes such as Postgresql,
713
a “case insensitive” 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>
716
<span class="n">Index</span><span class="p">(</span><span class="s">'someindex'</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>
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>
724
<div class="section" id="index-api">
725
<h2>Index API<a class="headerlink" href="#index-api" title="Permalink to this headline">¶</a></h2>
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>
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">"sometable"</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">"name"</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">"address"</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>
738
<span class="n">Index</span><span class="p">(</span><span class="s">"some_index"</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>
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">"sometable"</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">"name"</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>
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">"some_index"</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>
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">"some_index"</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>
753
<div class="versionadded">
754
<p><span>New in version 0.8: </span>support for functional and expression-based indexes.</p>
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>
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" />
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> – The name of the index</li>
776
<li><strong>*expressions</strong> – Column or SQL expressions.</li>
777
<li><strong>unique</strong> – Defaults to False: create a unique index.</li>
778
<li><strong>**kw</strong> – Other keyword arguments may be interpreted by specific dialects.</li>
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>
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>
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>
825
<div id="docs-bottom-navigation" class="docs-navigation-links">
827
<a href="defaults.html" title="previous chapter">Column Insert/Update Defaults</a>
829
<a href="ddl.html" title="next chapter">Customizing DDL</a>
831
<div id="docs-copyright">
832
© <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.