Sophie

Sophie

distrib > Fedora > 20 > i386 > by-pkgid > 05d9cf179789e23021b5913b4cd77ca6 > files > 576

python3-sqlalchemy-0.8.7-1.fc20.i686.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        
        <title>
            
    
                Defining Constraints and Indexes
             &mdash;
    SQLAlchemy 0.8 Documentation

        </title>

        
            <!-- begin iterate through SQLA + sphinx environment css_files -->
                <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
                <link rel="stylesheet" href="../_static/docs.css" type="text/css" />
                <link rel="stylesheet" href="../_static/changelog.css" type="text/css" />
                <link rel="stylesheet" href="../_static/sphinx_paramlinks.css" type="text/css" />
            <!-- end iterate through SQLA + sphinx environment css_files -->
        

        

    

    <!-- begin layout.mako headers -->

    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
          URL_ROOT:    '../',
          VERSION:     '0.8.7',
          COLLAPSE_MODINDEX: false,
          FILE_SUFFIX: '.html'
      };
    </script>

    <!-- begin iterate through sphinx environment script_files -->
        <script type="text/javascript" src="../_static/jquery.js"></script>
        <script type="text/javascript" src="../_static/underscore.js"></script>
        <script type="text/javascript" src="../_static/doctools.js"></script>
    <!-- end iterate through sphinx environment script_files -->

    <script type="text/javascript" src="../_static/detectmobile.js"></script>
    <script type="text/javascript" src="../_static/init.js"></script>
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
        <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="SQLAlchemy 0.8 Documentation" href="../index.html" />
        <link rel="up" title="Schema Definition Language" href="schema.html" />
        <link rel="next" title="Customizing DDL" href="ddl.html" />
        <link rel="prev" title="Column Insert/Update Defaults" href="defaults.html" />
    <!-- end layout.mako headers -->


    </head>
    <body>
        












<div id="docs-container">





<div id="docs-top-navigation-container" class="body-background">
<div id="docs-header">
    <div id="docs-version-header">
        Release: <span class="version-num">0.8.7</span> | Release Date: July 22, 2014
    </div>

    <h1>SQLAlchemy 0.8 Documentation</h1>

</div>
</div>

<div id="docs-body-container">

    <div id="fixed-sidebar" class="withsidebar">


        <div id="docs-sidebar-popout">
            <h3><a href="../index.html">SQLAlchemy 0.8 Documentation</a></h3>

            <p id="sidebar-paginate">
                    <a href="schema.html" title="Schema Definition Language">Up</a> |

                    <a href="defaults.html" title="Column Insert/Update Defaults">Prev</a> |
                    <a href="ddl.html" title="Customizing DDL">Next</a>
            </p>

            <p id="sidebar-topnav">
                <a href="../index.html">Contents</a> |
                <a href="../genindex.html">Index</a>
            </p>

            <div id="sidebar-search">
                <form class="search" action="../search.html" method="get">
                  <input type="text" name="q" size="12" /> <input type="submit" value="Search" />
                  <input type="hidden" name="check_keywords" value="yes" />
                  <input type="hidden" name="area" value="default" />
                </form>
            </div>

        </div>

        <div id="docs-sidebar">

        <h3><a href="#">            
                Defining Constraints and Indexes
            
        </a></h3>
        <ul>
<li><a class="reference internal" href="#">Defining Constraints and Indexes</a><ul>
<li><a class="reference internal" href="#defining-foreign-keys">Defining Foreign Keys</a><ul>
<li><a class="reference internal" href="#creating-dropping-foreign-key-constraints-via-alter">Creating/Dropping Foreign Key Constraints via ALTER</a></li>
<li><a class="reference internal" href="#on-update-and-on-delete">ON UPDATE and ON DELETE</a></li>
</ul>
</li>
<li><a class="reference internal" href="#unique-constraint">UNIQUE Constraint</a></li>
<li><a class="reference internal" href="#check-constraint">CHECK Constraint</a></li>
<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>
<li><a class="reference internal" href="#constraints-api">Constraints API</a></li>
<li><a class="reference internal" href="#indexes">Indexes</a><ul>
<li><a class="reference internal" href="#functional-indexes">Functional Indexes</a></li>
</ul>
</li>
<li><a class="reference internal" href="#index-api">Index API</a></li>
</ul>
</li>
</ul>




        </div>

    </div>

    

    <div id="docs-body" class="withsidebar" >
        
<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">
<h1>Defining Constraints and Indexes<a class="headerlink" href="#defining-constraints-and-indexes" title="Permalink to this headline">¶</a></h1>
<p id="metadata-foreignkeys">This section will discuss SQL <a class="reference internal" href="../glossary.html#term-constraints"><em class="xref std std-term">constraints</em></a> and indexes.  In SQLAlchemy
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>
<div class="section" id="defining-foreign-keys">
<h2>Defining Foreign Keys<a class="headerlink" href="#defining-foreign-keys" title="Permalink to this headline">¶</a></h2>
<p>A <em>foreign key</em> in SQL is a table-level construct that constrains one or more
columns in that table to only allow values that are present in a different set
of columns, typically but not always located on a different table. We call the
columns which are constrained the <em>foreign key</em> columns and the columns which
they are constrained towards the <em>referenced</em> columns. The referenced columns
almost always define the primary key for their owning table, though there are
exceptions to this. The foreign key is the &#8220;joint&#8221; that connects together
pairs of rows which have a relationship with each other, and SQLAlchemy
assigns very deep importance to this concept in virtually every area of its
operation.</p>
<p>In SQLAlchemy as well as in DDL, foreign key constraints can be defined as
additional attributes within the table clause, or for single-column foreign
keys they may optionally be specified within the definition of a single
column. The single column foreign key is more common, and at the column level
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
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>
<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>
    <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>
    <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>
    <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>
    <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>
<span class="p">)</span></pre></div>
</div>
<p>Above, we define a new table <tt class="docutils literal"><span class="pre">user_preference</span></tt> for which each row must
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>
table&#8217;s <tt class="docutils literal"><span class="pre">user_id</span></tt> column.</p>
<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
string of the form <em>&lt;tablename&gt;.&lt;columnname&gt;</em>, or for a table in a remote
schema or &#8220;owner&#8221; of the form <em>&lt;schemaname&gt;.&lt;tablename&gt;.&lt;columnname&gt;</em>. It may
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
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>
object via its <tt class="docutils literal"><span class="pre">c</span></tt> collection:</p>
<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>
</div>
<p>The advantage to using a string is that the in-python linkage between <tt class="docutils literal"><span class="pre">user</span></tt>
and <tt class="docutils literal"><span class="pre">user_preference</span></tt> is resolved only when first needed, so that table
objects can be easily spread across multiple modules and defined in any order.</p>
<p>Foreign keys may also be defined at the table level, using 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. This object can
describe a single- or multi-column foreign key. A multi-column foreign key is
known as a <em>composite</em> foreign key, and almost always references a table that
has a composite primary key. Below we define a table <tt class="docutils literal"><span class="pre">invoice</span></tt> which has a
composite primary key:</p>
<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>
    <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>
    <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>
    <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>
<span class="p">)</span></pre></div>
</div>
<p>And then a table <tt class="docutils literal"><span class="pre">invoice_item</span></tt> with a composite foreign key referencing
<tt class="docutils literal"><span class="pre">invoice</span></tt>:</p>
<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>
    <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>
    <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>
    <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>
    <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>
    <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>
<span class="p">)</span></pre></div>
</div>
<p>It&#8217;s important to note that 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> is the only way to define a
composite foreign key. While we could also have placed individual
<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
<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
would not be aware that these two values should be paired together - it would
be two individual foreign key constraints instead of a single composite
foreign key referencing two columns.</p>
<div class="section" id="creating-dropping-foreign-key-constraints-via-alter">
<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>
<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
causes the &#8220;REFERENCES&#8221; keyword to be added inline to a column definition
within a &#8220;CREATE TABLE&#8221; statement when
<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
<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;
keyword inline with &#8220;CREATE TABLE&#8221;. There are some cases where this is
undesireable, particularly when two tables reference each other mutually, each
with a foreign key referencing the other. In such a situation at least one of
the foreign key constraints must be generated after both tables have been
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
<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
<tt class="docutils literal"><span class="pre">use_alter=True</span></tt>. When using this flag, the constraint will be generated
using a definition similar to &#8220;ALTER TABLE &lt;tablename&gt; ADD CONSTRAINT &lt;name&gt;
...&#8221;. Since a name is required, the <tt class="docutils literal"><span class="pre">name</span></tt> attribute must also be specified.
For example:</p>
<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>
    <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>
    <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>
        <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>
    <span class="p">)</span>
<span class="p">)</span>

<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>
    <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>
    <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>
    <span class="n">ForeignKeyConstraint</span><span class="p">(</span>
        <span class="p">[</span><span class="s">&#39;parent_node_id&#39;</span><span class="p">],</span>
        <span class="p">[</span><span class="s">&#39;node.node_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_element_parent_node_id&#39;</span>
    <span class="p">)</span>
<span class="p">)</span></pre></div>
</div>
</div>
<div class="section" id="on-update-and-on-delete">
<span id="on-update-on-delete"></span><h3>ON UPDATE and ON DELETE<a class="headerlink" href="#on-update-and-on-delete" title="Permalink to this headline">¶</a></h3>
<p>Most databases support <em>cascading</em> of foreign key values, that is the when a
parent row is updated the new value is placed in child rows, or when the
parent row is deleted all corresponding child rows are set to null or deleted.
In data definition language these are specified using phrases like &#8220;ON UPDATE
CASCADE&#8221;, &#8220;ON DELETE CASCADE&#8221;, and &#8220;ON DELETE SET NULL&#8221;, corresponding to
foreign key constraints. The phrase after &#8220;ON UPDATE&#8221; or &#8220;ON DELETE&#8221; may also
other allow other phrases that are specific to the database in use. 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> and
<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
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
arguments. The value is any string which will be output after the appropriate
&#8220;ON UPDATE&#8221; or &#8220;ON DELETE&#8221; phrase:</p>
<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>
    <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">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>
            <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span>
    <span class="p">)</span>
<span class="p">)</span>

<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>
    <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>
    <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>
    <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>
    <span class="n">ForeignKeyConstraint</span><span class="p">(</span>
                <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>
                <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>
                <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>
    <span class="p">)</span>
<span class="p">)</span></pre></div>
</div>
<p>Note that these clauses are not supported on SQLite, and require <tt class="docutils literal"><span class="pre">InnoDB</span></tt>
tables when used with MySQL. They may also not be supported on other
databases.</p>
</div>
</div>
<div class="section" id="unique-constraint">
<h2>UNIQUE Constraint<a class="headerlink" href="#unique-constraint" title="Permalink to this headline">¶</a></h2>
<p>Unique constraints can be created anonymously on a single column using the
<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
unique constraints and/or those with multiple columns are created via the
<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>
<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>
<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>

    <span class="c"># per-column anonymous unique constraint</span>
    <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>

    <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">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>

    <span class="c"># explicit/composite unique constraint.  &#39;name&#39; is optional.</span>
    <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>
    <span class="p">)</span></pre></div>
</div>
</div>
<div class="section" id="check-constraint">
<h2>CHECK Constraint<a class="headerlink" href="#check-constraint" title="Permalink to this headline">¶</a></h2>
<p>Check constraints can be named or unnamed and can be created at the Column or
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.
The text of the check constraint is passed directly through to the database,
so there is limited &#8220;database independent&#8221; behavior. Column level check
constraints generally should only refer to the column to which they are
placed, while table level constraints can refer to any columns in the table.</p>
<p>Note that some databases do not actively support check constraints such as
MySQL.</p>
<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>
<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>

    <span class="c"># per-column CHECK constraint</span>
    <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>

    <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">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>

    <span class="c"># table level CHECK constraint.  &#39;name&#39; is optional.</span>
    <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>
    <span class="p">)</span>

<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>
<div class='popup_sql'>CREATE TABLE mytable (
    col1 INTEGER  CHECK (col1>5),
    col2 INTEGER,
    col3 INTEGER,
    CONSTRAINT check1  CHECK (col2 > col3 + 5)
)</div></pre></div>
</div>
</div>
<div class="section" id="setting-up-constraints-when-using-the-declarative-orm-extension">
<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>
<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
table metadata, which among other things can be used by the SQLAlchemy ORM
as a target to map a class.  The <a class="reference internal" href="../orm/extensions/declarative.html"><em>Declarative</em></a>
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
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>
<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>
to a table defined using Declarative, use the <tt class="docutils literal"><span class="pre">__table_args__</span></tt> attribute,
described at <a class="reference internal" href="../orm/extensions/declarative.html#declarative-table-args"><em>Table Configuration</em></a>.</p>
</div>
<div class="section" id="constraints-api">
<h2>Constraints API<a class="headerlink" href="#constraints-api" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="sqlalchemy.schema.Constraint">
<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>
<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>
<p>A table-level SQL constraint.</p>
</dd></dl>

<dl class="class">
<dt id="sqlalchemy.schema.CheckConstraint">
<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>
<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>
<p>A table- or column-level CHECK constraint.</p>
<p>Can be included in the definition of a Table or Column.</p>
</dd></dl>

<dl class="class">
<dt id="sqlalchemy.schema.ColumnCollectionConstraint">
<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>
<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>
<p>A constraint that proxies a ColumnCollection.</p>
</dd></dl>

<dl class="class">
<dt id="sqlalchemy.schema.ForeignKey">
<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>
<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>
<p>Defines a dependency between two columns.</p>
<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,
e.g.:</p>
<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>
    <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>
<span class="p">)</span></pre></div>
</div>
<p>Note that <tt class="docutils literal"><span class="pre">ForeignKey</span></tt> is only a marker object that defines
a dependency between two columns.   The actual constraint
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>
object.   This object will be generated automatically when
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
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,
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>,
<tt class="docutils literal"><span class="pre">ForeignKey</span></tt> markers are automatically generated to be
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
associated with the constraint object.</p>
<p>Note that you cannot define a &#8220;composite&#8221; foreign key constraint,
that is a constraint between a grouping of multiple parent/child
columns, using <tt class="docutils literal"><span class="pre">ForeignKey</span></tt> objects.   To define this grouping,
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
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
are created automatically.</p>
<p>The <tt class="docutils literal"><span class="pre">ForeignKey</span></tt> objects associated with an individual
<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
of that column.</p>
<p>Further examples of foreign key configuration are in
<em class="xref std std-ref">metadata_foreignkeys</em>.</p>
<dl class="method">
<dt id="sqlalchemy.schema.ForeignKey.__init__">
<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>
<dd><p>Construct a column-level FOREIGN KEY.</p>
<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
<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
<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>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><span class="target" id="sqlalchemy.schema.ForeignKey.params.column"></span><strong>column</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.schema.ForeignKey.params.column">¶</a> &#8211; <p>A single target column for the key relationship. 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 or a column name as a string:
<tt class="docutils literal"><span class="pre">tablename.columnkey</span></tt> or <tt class="docutils literal"><span class="pre">schema.tablename.columnkey</span></tt>.
<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
(defaults to the column name itself), unless <tt class="docutils literal"><span class="pre">link_to_name</span></tt> is
<tt class="docutils literal"><span class="pre">True</span></tt> in which case the rendered name of the column is used.</p>
<div class="versionadded">
<p><span>New in version 0.7.4: </span>Note that if the schema name is not included, and the
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
be used.</p>
</div>
</li>
<li><span class="target" id="sqlalchemy.schema.ForeignKey.params.name"></span><strong>name</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.schema.ForeignKey.params.name">¶</a> &#8211; Optional string. An in-database name for the key if
<cite>constraint</cite> is not provided.</li>
<li><span class="target" id="sqlalchemy.schema.ForeignKey.params.onupdate"></span><strong>onupdate</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.schema.ForeignKey.params.onupdate">¶</a> &#8211; Optional string. If set, emit ON UPDATE &lt;value&gt; when
issuing DDL for this constraint. Typical values include CASCADE,
DELETE and RESTRICT.</li>
<li><span class="target" id="sqlalchemy.schema.ForeignKey.params.ondelete"></span><strong>ondelete</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.schema.ForeignKey.params.ondelete">¶</a> &#8211; Optional string. If set, emit ON DELETE &lt;value&gt; when
issuing DDL for this constraint. Typical values include CASCADE,
DELETE and RESTRICT.</li>
<li><span class="target" id="sqlalchemy.schema.ForeignKey.params.deferrable"></span><strong>deferrable</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.schema.ForeignKey.params.deferrable">¶</a> &#8211; Optional bool. If set, emit DEFERRABLE or NOT
DEFERRABLE when issuing DDL for this constraint.</li>
<li><span class="target" id="sqlalchemy.schema.ForeignKey.params.initially"></span><strong>initially</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.schema.ForeignKey.params.initially">¶</a> &#8211; Optional string. If set, emit INITIALLY &lt;value&gt; when
issuing DDL for this constraint.</li>
<li><span class="target" id="sqlalchemy.schema.ForeignKey.params.link_to_name"></span><strong>link_to_name</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.schema.ForeignKey.params.link_to_name">¶</a> &#8211; if True, the string name given in <tt class="docutils literal"><span class="pre">column</span></tt> is
the rendered name of the referenced column, not its locally
assigned <tt class="docutils literal"><span class="pre">key</span></tt>.</li>
<li><span class="target" id="sqlalchemy.schema.ForeignKey.params.use_alter"></span><strong>use_alter</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.schema.ForeignKey.params.use_alter">¶</a> &#8211; passed to the underlying
<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
generated/dropped externally from the CREATE TABLE/ DROP TABLE
statement. See that classes&#8217; constructor for details.</li>
<li><span class="target" id="sqlalchemy.schema.ForeignKey.params.match"></span><strong>match</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.schema.ForeignKey.params.match">¶</a> &#8211; Optional string. If set, emit MATCH &lt;value&gt; when issuing
DDL for this constraint. Typical values include SIMPLE, PARTIAL
and FULL.</li>
<li><span class="target" id="sqlalchemy.schema.ForeignKey.params.schema"></span><strong>schema</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.schema.ForeignKey.params.schema">¶</a> &#8211; Deprecated; this flag does nothing and will be removed
in 0.9.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="attribute">
<dt id="sqlalchemy.schema.ForeignKey.column">
<tt class="descname">column</tt><a class="headerlink" href="#sqlalchemy.schema.ForeignKey.column" title="Permalink to this definition">¶</a></dt>
<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
<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>
<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
string-based target column specification, this
attribute will on first access initiate a resolution
process to locate the referenced remote
<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
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
<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
yet present, an error is raised.</p>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.schema.ForeignKey.copy">
<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>
<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>
<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
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>
<p>This method is usually used by the internal
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>,
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>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><span class="target" id="sqlalchemy.schema.ForeignKey.copy.params.schema"></span><strong>schema</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.schema.ForeignKey.copy.params.schema">¶</a> &#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
reference the original table and column name, qualified
by the given string schema name.</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.schema.ForeignKey.get_referent">
<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>
<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>
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>
<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
<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>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.schema.ForeignKey.references">
<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>
<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
<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>
</dd></dl>

<dl class="attribute">
<dt id="sqlalchemy.schema.ForeignKey.target_fullname">
<tt class="descname">target_fullname</tt><a class="headerlink" href="#sqlalchemy.schema.ForeignKey.target_fullname" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a string based &#8216;column specification&#8217; for 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>
<p>This is usually the equivalent of the string-based &#8220;tablename.colname&#8221;
argument first passed to the object&#8217;s constructor.</p>
</dd></dl>

</dd></dl>

<dl class="class">
<dt id="sqlalchemy.schema.ForeignKeyConstraint">
<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>
<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>
<p>A table-level FOREIGN KEY constraint.</p>
<p>Defines a single column or composite FOREIGN KEY ... REFERENCES
constraint. For a no-frills, single column foreign key, adding 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> 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
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>
<p>Examples of foreign key configuration are in <em class="xref std std-ref">metadata_foreignkeys</em>.</p>
<dl class="method">
<dt id="sqlalchemy.schema.ForeignKeyConstraint.__init__">
<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>
<dd><p>Construct a composite-capable FOREIGN KEY.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><span class="target" id="sqlalchemy.schema.ForeignKeyConstraint.params.columns"></span><strong>columns</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.schema.ForeignKeyConstraint.params.columns">¶</a> &#8211; A sequence of local column names. The named columns
must be defined and present in the parent Table. The names should
match the <tt class="docutils literal"><span class="pre">key</span></tt> given to each column (defaults to the name) unless
<tt class="docutils literal"><span class="pre">link_to_name</span></tt> is True.</li>
<li><span class="target" id="sqlalchemy.schema.ForeignKeyConstraint.params.refcolumns"></span><strong>refcolumns</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.schema.ForeignKeyConstraint.params.refcolumns">¶</a> &#8211; A sequence of foreign column names or Column
objects. The columns must all be located within the same Table.</li>
<li><span class="target" id="sqlalchemy.schema.ForeignKeyConstraint.params.name"></span><strong>name</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.schema.ForeignKeyConstraint.params.name">¶</a> &#8211; Optional, the in-database name of the key.</li>
<li><span class="target" id="sqlalchemy.schema.ForeignKeyConstraint.params.onupdate"></span><strong>onupdate</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.schema.ForeignKeyConstraint.params.onupdate">¶</a> &#8211; Optional string. If set, emit ON UPDATE &lt;value&gt; when
issuing DDL for this constraint. Typical values include CASCADE,
DELETE and RESTRICT.</li>
<li><span class="target" id="sqlalchemy.schema.ForeignKeyConstraint.params.ondelete"></span><strong>ondelete</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.schema.ForeignKeyConstraint.params.ondelete">¶</a> &#8211; Optional string. If set, emit ON DELETE &lt;value&gt; when
issuing DDL for this constraint. Typical values include CASCADE,
DELETE and RESTRICT.</li>
<li><span class="target" id="sqlalchemy.schema.ForeignKeyConstraint.params.deferrable"></span><strong>deferrable</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.schema.ForeignKeyConstraint.params.deferrable">¶</a> &#8211; Optional bool. If set, emit DEFERRABLE or NOT
DEFERRABLE when issuing DDL for this constraint.</li>
<li><span class="target" id="sqlalchemy.schema.ForeignKeyConstraint.params.initially"></span><strong>initially</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.schema.ForeignKeyConstraint.params.initially">¶</a> &#8211; Optional string. If set, emit INITIALLY &lt;value&gt; when
issuing DDL for this constraint.</li>
<li><span class="target" id="sqlalchemy.schema.ForeignKeyConstraint.params.link_to_name"></span><strong>link_to_name</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.schema.ForeignKeyConstraint.params.link_to_name">¶</a> &#8211; if True, the string name given in <tt class="docutils literal"><span class="pre">column</span></tt> is
the rendered name of the referenced column, not its locally assigned
<tt class="docutils literal"><span class="pre">key</span></tt>.</li>
<li><span class="target" id="sqlalchemy.schema.ForeignKeyConstraint.params.use_alter"></span><strong>use_alter</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.schema.ForeignKeyConstraint.params.use_alter">¶</a> &#8211; If True, do not emit the DDL for this constraint as
part of the CREATE TABLE definition. Instead, generate it via an
ALTER TABLE statement issued after the full collection of tables
have been created, and drop it via an ALTER TABLE statement before
the full collection of tables are dropped. This is shorthand for the
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
as &#8220;after-create&#8221; and &#8220;before-drop&#8221; events on the MetaData object.
This is normally used to generate/drop constraints on objects that
are mutually dependent on each other.</li>
<li><span class="target" id="sqlalchemy.schema.ForeignKeyConstraint.params.match"></span><strong>match</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.schema.ForeignKeyConstraint.params.match">¶</a> &#8211; Optional string. If set, emit MATCH &lt;value&gt; when issuing
DDL for this constraint. Typical values include SIMPLE, PARTIAL
and FULL.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

</dd></dl>

<dl class="class">
<dt id="sqlalchemy.schema.PrimaryKeyConstraint">
<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>
<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>
<p>A table-level PRIMARY KEY constraint.</p>
<p>Defines a single column or composite PRIMARY KEY constraint. For a
no-frills primary key, adding <tt class="docutils literal"><span class="pre">primary_key=True</span></tt> to one or more
<tt class="docutils literal"><span class="pre">Column</span></tt> definitions is a shorthand equivalent for an unnamed single- or
multiple-column PrimaryKeyConstraint.</p>
</dd></dl>

<dl class="class">
<dt id="sqlalchemy.schema.UniqueConstraint">
<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>
<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>
<p>A table-level UNIQUE constraint.</p>
<p>Defines a single column or composite UNIQUE constraint. For a no-frills,
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>
definition is a shorthand equivalent for an unnamed, single column
UniqueConstraint.</p>
</dd></dl>

</div>
<div class="section" id="indexes">
<span id="schema-indexes"></span><h2>Indexes<a class="headerlink" href="#indexes" title="Permalink to this headline">¶</a></h2>
<p>Indexes can be created anonymously (using an auto-generated name <tt class="docutils literal"><span class="pre">ix_&lt;column</span>
<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
<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
<tt class="docutils literal"><span class="pre">unique</span></tt> to apply the uniqueness to the index itself, instead of adding a
separate UNIQUE constraint. For indexes with specific names or which encompass
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,
which requires a name.</p>
<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
<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
INDEX&#8221; is issued right after the create statements for the table:</p>
<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>
<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>
    <span class="c"># an indexed column, with index &quot;ix_mytable_col1&quot;</span>
    <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>

    <span class="c"># a uniquely indexed column with index &quot;ix_mytable_col2&quot;</span>
    <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>

    <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>
    <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>

    <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>
    <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>
    <span class="p">)</span>

<span class="c"># place an index on col3, col4</span>
<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>

<span class="c"># place a unique index on col5, col6</span>
<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>

<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>
<div class='popup_sql'>CREATE TABLE mytable (
    col1 INTEGER,
    col2 INTEGER,
    col3 INTEGER,
    col4 INTEGER,
    col5 INTEGER,
    col6 INTEGER
)
CREATE INDEX ix_mytable_col1 ON mytable (col1)
CREATE UNIQUE INDEX ix_mytable_col2 ON mytable (col2)
CREATE UNIQUE INDEX myindex ON mytable (col5, col6)
CREATE INDEX idx_col34 ON mytable (col3, col4)</div></pre></div>
</div>
<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
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>
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
&#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
identify columns:</p>
<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>
<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>
    <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">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">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>
    <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>

    <span class="c"># place an index on col1, col2</span>
    <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>

    <span class="c"># place a unique index on col3, col4</span>
    <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>
<span class="p">)</span></pre></div>
</div>
<div class="versionadded">
<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>
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>
</div>
<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>
<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>
<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>
<div class='popup_sql'>CREATE INDEX someindex ON mytable (col5)</div></pre></div>
</div>
<div class="section" id="functional-indexes">
<span id="schema-indexes-functional"></span><h3>Functional Indexes<a class="headerlink" href="#functional-indexes" title="Permalink to this headline">¶</a></h3>
<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
target backend.  To create an index against a column using a descending
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>
<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>

<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>
</div>
<p>Or with a backend that supports functional indexes such as Postgresql,
a &#8220;case insensitive&#8221; index can be created using the <tt class="docutils literal"><span class="pre">lower()</span></tt> function:</p>
<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>

<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>
</div>
<div class="versionadded">
<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
as well as plain columns.</p>
</div>
</div>
</div>
<div class="section" id="index-api">
<h2>Index API<a class="headerlink" href="#index-api" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="sqlalchemy.schema.Index">
<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>
<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>
<p>A table-level INDEX.</p>
<p>Defines a composite (one or more column) INDEX.</p>
<p>E.g.:</p>
<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>
                <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">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>
            <span class="p">)</span>

<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>
</div>
<p>For a no-frills, single column index, adding
<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>
<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>
                <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>
            <span class="p">)</span></pre></div>
</div>
<p>For a composite index, multiple columns can be specified:</p>
<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>
</div>
<p>Functional indexes are supported as well, keeping in mind that at least
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>
<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>
</div>
<div class="versionadded">
<p><span>New in version 0.8: </span>support for functional and expression-based indexes.</p>
</div>
<div class="admonition seealso">
<p class="first admonition-title">See also</p>
<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>
<p><a class="reference internal" href="../dialects/postgresql.html#postgresql-indexes"><em>Postgresql-Specific Index Options</em></a> - PostgreSQL-specific options available for 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.</p>
<p><a class="reference internal" href="../dialects/mysql.html#mysql-indexes"><em>MySQL Specific Index Options</em></a> - MySQL-specific options available for 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.</p>
<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
<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>
</div>
<dl class="method">
<dt id="sqlalchemy.schema.Index.__init__">
<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>
<dd><p>Construct an index object.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><span class="target" id="sqlalchemy.schema.Index.params.name"></span><strong>name</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.schema.Index.params.name">¶</a> &#8211; The name of the index</li>
<li><span class="target" id="sqlalchemy.schema.Index.params.*expressions"></span><strong>*expressions</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.schema.Index.params.*expressions">¶</a> &#8211; Column or SQL expressions.</li>
<li><span class="target" id="sqlalchemy.schema.Index.params.unique"></span><strong>unique</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.schema.Index.params.unique">¶</a> &#8211; Defaults to False: create a unique index.</li>
<li><span class="target" id="sqlalchemy.schema.Index.params.**kw"></span><strong>**kw</strong><a class="paramlink headerlink reference internal" href="#sqlalchemy.schema.Index.params.**kw">¶</a> &#8211; Other keyword arguments may be interpreted by specific dialects.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="attribute">
<dt id="sqlalchemy.schema.Index.bind">
<tt class="descname">bind</tt><a class="headerlink" href="#sqlalchemy.schema.Index.bind" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the connectable associated with this Index.</p>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.schema.Index.create">
<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>
<dd><p>Issue a <tt class="docutils literal"><span class="pre">CREATE</span></tt> statement for this
<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>
for connectivity.</p>
<div class="admonition seealso">
<p class="first admonition-title">See also</p>
<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>
</div>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.schema.Index.drop">
<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>
<dd><p>Issue a <tt class="docutils literal"><span class="pre">DROP</span></tt> statement for this
<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>
for connectivity.</p>
<div class="admonition seealso">
<p class="first admonition-title">See also</p>
<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>
</div>
</dd></dl>

</dd></dl>

</div>
</div>

    </div>

</div>

<div id="docs-bottom-navigation" class="docs-navigation-links">
        Previous:
        <a href="defaults.html" title="previous chapter">Column Insert/Update Defaults</a>
        Next:
        <a href="ddl.html" title="next chapter">Customizing DDL</a>

    <div id="docs-copyright">
        &copy; <a href="../copyright.html">Copyright</a> 2007-2014, the SQLAlchemy authors and contributors.
        Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.2b1.
    </div>
</div>

</div>

        
    </body>
</html>