Sophie

Sophie

distrib > Mageia > 1 > i586 > by-pkgid > 06f6da806447a4fcfe8c011dd17414c2 > files > 423

python-sqlalchemy-0.6.8-1.mga1.i586.rpm

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

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        
        <title>
                Schema Definition Language
             &mdash; SQLAlchemy 0.6.8 Documentation</title>
        
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <link rel="stylesheet" href="../_static/docs.css" type="text/css" />

    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
          URL_ROOT:    '../',
          VERSION:     '0.6.8',
          COLLAPSE_MODINDEX: false,
          FILE_SUFFIX: '.html'
      };
    </script>
        <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>
    <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.6.8 Documentation" href="../index.html" />
        <link rel="up" title="SQLAlchemy Core" href="index.html" />
        <link rel="next" title="Column and Data Types" href="types.html" />
        <link rel="prev" title="Connection Pooling" href="pooling.html" />

    </head>
    <body>
        



<h1>SQLAlchemy 0.6.8 Documentation</h1>

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

<div class="versionheader">
    Version: <span class="versionnum">0.6.8</span> Last Updated: 06/05/2011 13:10:26
</div>
<div class="clearboth"></div>

<div id="topnav">
    <div id="pagecontrol">
        <ul>
            <li>Prev:
            <a href="pooling.html" title="previous chapter">Connection Pooling</a>
            </li>
            <li>Next:
            <a href="types.html" title="next chapter">Column and Data Types</a>
            </li>

        <li>
            <a href="../contents.html">Table of Contents</a> |
            <a href="../genindex.html">Index</a>
            | <a href="../_sources/core/schema.txt">view source
        </li>
        </ul>
    </div>
    <div id="navbanner">
        <a class="totoc" href="../index.html">SQLAlchemy 0.6.8 Documentation</a>
                » <a href="index.html" title="SQLAlchemy Core">SQLAlchemy Core</a>
        » 
                Schema Definition Language
             

        <h2>
            
                Schema Definition Language
            
        </h2>
        <ul>
<li><a class="reference internal" href="#">Schema Definition Language</a><ul>
<li><a class="reference internal" href="#describing-databases-with-metadata">Describing Databases with MetaData</a><ul>
<li><a class="reference internal" href="#accessing-tables-and-columns">Accessing Tables and Columns</a></li>
<li><a class="reference internal" href="#creating-and-dropping-database-tables">Creating and Dropping Database Tables</a></li>
<li><a class="reference internal" href="#binding-metadata-to-an-engine-or-connection">Binding MetaData to an Engine or Connection</a></li>
<li><a class="reference internal" href="#specifying-the-schema-name">Specifying the Schema Name</a></li>
<li><a class="reference internal" href="#backend-specific-options">Backend-Specific Options</a></li>
<li><a class="reference internal" href="#schema-api-constructs">Schema API Constructs</a></li>
</ul>
</li>
<li><a class="reference internal" href="#reflecting-database-objects">Reflecting Database Objects</a><ul>
<li><a class="reference internal" href="#overriding-reflected-columns">Overriding Reflected Columns</a></li>
<li><a class="reference internal" href="#reflecting-views">Reflecting Views</a></li>
<li><a class="reference internal" href="#reflecting-all-tables-at-once">Reflecting All Tables at Once</a></li>
<li><a class="reference internal" href="#fine-grained-reflection-with-inspector">Fine Grained Reflection with Inspector</a></li>
</ul>
</li>
<li><a class="reference internal" href="#column-insert-update-defaults">Column Insert/Update Defaults</a><ul>
<li><a class="reference internal" href="#scalar-defaults">Scalar Defaults</a></li>
<li><a class="reference internal" href="#python-executed-functions">Python-Executed Functions</a><ul>
<li><a class="reference internal" href="#context-sensitive-default-functions">Context-Sensitive Default Functions</a></li>
</ul>
</li>
<li><a class="reference internal" href="#sql-expressions">SQL Expressions</a></li>
<li><a class="reference internal" href="#server-side-defaults">Server Side Defaults</a></li>
<li><a class="reference internal" href="#triggered-columns">Triggered Columns</a></li>
<li><a class="reference internal" href="#defining-sequences">Defining Sequences</a></li>
<li><a class="reference internal" href="#default-geneation-api-constructs">Default Geneation API Constructs</a></li>
</ul>
</li>
<li><a class="reference internal" href="#defining-constraints-and-indexes">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>
<li><a class="reference internal" href="#foreign-key-api-constructs">Foreign Key API Constructs</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="#other-constraint-classes">Other Constraint Classes</a></li>
<li><a class="reference internal" href="#indexes">Indexes</a></li>
</ul>
</li>
<li><a class="reference internal" href="#customizing-ddl">Customizing DDL</a><ul>
<li><a class="reference internal" href="#controlling-ddl-sequences">Controlling DDL Sequences</a></li>
<li><a class="reference internal" href="#custom-ddl">Custom DDL</a></li>
<li><a class="reference internal" href="#ddl-api">DDL API</a></li>
</ul>
</li>
</ul>
</li>
</ul>

    </div>
    <div class="clearboth"></div>
</div>

<div class="document">
    <div class="body">
        
<div class="section" id="module-sqlalchemy.schema">
<span id="schema-definition-language"></span><span id="metadata-toplevel"></span><h1>Schema Definition Language<a class="headerlink" href="#module-sqlalchemy.schema" title="Permalink to this headline">¶</a></h1>
<div class="section" id="describing-databases-with-metadata">
<span id="metadata-describing"></span><h2>Describing Databases with MetaData<a class="headerlink" href="#describing-databases-with-metadata" title="Permalink to this headline">¶</a></h2>
<p>The core of SQLAlchemy&#8217;s query and object mapping operations are supported by
<em>database metadata</em>, which is comprised of Python objects that describe tables
and other schema-level objects. These objects are at the core of three major
types of operations - issuing CREATE and DROP statements (known as <em>DDL</em>),
constructing SQL queries, and expressing information about structures that
already exist within the database.</p>
<p>Database metadata can be expressed by explicitly naming the various components
and their properties, using constructs such as
<a class="reference internal" href="#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a>, <a class="reference internal" href="#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="#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.Sequence" title="sqlalchemy.schema.Sequence"><tt class="xref py py-class docutils literal"><span class="pre">Sequence</span></tt></a>, all of which are imported from the
<tt class="docutils literal"><span class="pre">sqlalchemy.schema</span></tt> package. It can also be generated by SQLAlchemy using a
process called <em>reflection</em>, which means you start with a single object such
as <a class="reference internal" href="#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a>, assign it a name, and then instruct
SQLAlchemy to load all the additional information related to that name from a
particular engine source.</p>
<p>A key feature of SQLAlchemy&#8217;s database metadata constructs is that they are
designed to be used in a <em>declarative</em> style which closely resembles that of
real DDL. They are therefore most intuitive to those who have some background
in creating real schema generation scripts.</p>
<p>A collection of metadata entities is stored in an object aptly named
<a class="reference internal" href="#sqlalchemy.schema.MetaData" title="sqlalchemy.schema.MetaData"><tt class="xref py py-class docutils literal"><span class="pre">MetaData</span></tt></a>:</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="o">*</span>

<span class="n">metadata</span> <span class="o">=</span> <span class="n">MetaData</span><span class="p">()</span></pre></div>
</div>
<p><a class="reference internal" href="#sqlalchemy.schema.MetaData" title="sqlalchemy.schema.MetaData"><tt class="xref py py-class docutils literal"><span class="pre">MetaData</span></tt></a> is a container object that keeps together
many different features of a database (or multiple databases) being described.</p>
<p>To represent a table, use the <a class="reference internal" href="#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> class. Its two
primary arguments are the table name, then the
<a class="reference internal" href="#sqlalchemy.schema.MetaData" title="sqlalchemy.schema.MetaData"><tt class="xref py py-class docutils literal"><span class="pre">MetaData</span></tt></a> object which it will be associated with.
The remaining positional arguments are mostly
<a class="reference internal" href="#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a> objects describing each column:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">user</span> <span class="o">=</span> <span class="n">Table</span><span class="p">(</span><span class="s">&#39;user&#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;user_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_name&#39;</span><span class="p">,</span> <span class="n">String</span><span class="p">(</span><span class="mi">16</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;email_address&#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">Column</span><span class="p">(</span><span class="s">&#39;password&#39;</span><span class="p">,</span> <span class="n">String</span><span class="p">(</span><span class="mi">20</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>Above, a table called <tt class="docutils literal"><span class="pre">user</span></tt> is described, which contains four columns. The
primary key of the table consists of the <tt class="docutils literal"><span class="pre">user_id</span></tt> column. Multiple columns
may be assigned the <tt class="docutils literal"><span class="pre">primary_key=True</span></tt> flag which denotes a multi-column
primary key, known as a <em>composite</em> primary key.</p>
<p>Note also that each column describes its datatype using objects corresponding
to genericized types, such as <a class="reference internal" href="types.html#sqlalchemy.types.Integer" title="sqlalchemy.types.Integer"><tt class="xref py py-class docutils literal"><span class="pre">Integer</span></tt></a> and
<a class="reference internal" href="types.html#sqlalchemy.types.String" title="sqlalchemy.types.String"><tt class="xref py py-class docutils literal"><span class="pre">String</span></tt></a>. SQLAlchemy features dozens of types of
varying levels of specificity as well as the ability to create custom types.
Documentation on the type system can be found at <em class="xref std std-ref">types</em>.</p>
<div class="section" id="accessing-tables-and-columns">
<h3>Accessing Tables and Columns<a class="headerlink" href="#accessing-tables-and-columns" title="Permalink to this headline">¶</a></h3>
<p>The <a class="reference internal" href="#sqlalchemy.schema.MetaData" title="sqlalchemy.schema.MetaData"><tt class="xref py py-class docutils literal"><span class="pre">MetaData</span></tt></a> object contains all of the schema
constructs we&#8217;ve associated with it. It supports a few methods of accessing
these table objects, such as the <tt class="docutils literal"><span class="pre">sorted_tables</span></tt> accessor which returns a
list of each <a class="reference internal" href="#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> object in order of foreign key
dependency (that is, each table is preceded by all tables which it
references):</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="n">metadata</span><span class="o">.</span><span class="n">sorted_tables</span><span class="p">:</span>
<span class="gp">... </span>   <span class="k">print</span> <span class="n">t</span><span class="o">.</span><span class="n">name</span>
<span class="go">user</span>
<span class="go">user_preference</span>
<span class="go">invoice</span>
<span class="go">invoice_item</span></pre></div>
</div>
<p>In most cases, individual <a class="reference internal" href="#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> objects have been
explicitly declared, and these objects are typically accessed directly as
module-level variables in an application. Once a
<a class="reference internal" href="#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> has been defined, it has a full set of
accessors which allow inspection of its properties. Given the following
<a class="reference internal" href="#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> definition:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">employees</span> <span class="o">=</span> <span class="n">Table</span><span class="p">(</span><span class="s">&#39;employees&#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;employee_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;employee_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;employee_dept&#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;departments.department_id&quot;</span><span class="p">))</span>
<span class="p">)</span></pre></div>
</div>
<p>Note 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 used in this table -
this construct defines a reference to a remote table, and is fully described
in <a class="reference internal" href="#metadata-foreignkeys"><em>Defining Foreign Keys</em></a>. Methods of accessing information about this
table include:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># access the column &quot;EMPLOYEE_ID&quot;:</span>
<span class="n">employees</span><span class="o">.</span><span class="n">columns</span><span class="o">.</span><span class="n">employee_id</span>

<span class="c"># or just</span>
<span class="n">employees</span><span class="o">.</span><span class="n">c</span><span class="o">.</span><span class="n">employee_id</span>

<span class="c"># via string</span>
<span class="n">employees</span><span class="o">.</span><span class="n">c</span><span class="p">[</span><span class="s">&#39;employee_id&#39;</span><span class="p">]</span>

<span class="c"># iterate through all columns</span>
<span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">employees</span><span class="o">.</span><span class="n">c</span><span class="p">:</span>
    <span class="k">print</span> <span class="n">c</span>

<span class="c"># get the table&#39;s primary key columns</span>
<span class="k">for</span> <span class="n">primary_key</span> <span class="ow">in</span> <span class="n">employees</span><span class="o">.</span><span class="n">primary_key</span><span class="p">:</span>
    <span class="k">print</span> <span class="n">primary_key</span>

<span class="c"># get the table&#39;s foreign key objects:</span>
<span class="k">for</span> <span class="n">fkey</span> <span class="ow">in</span> <span class="n">employees</span><span class="o">.</span><span class="n">foreign_keys</span><span class="p">:</span>
    <span class="k">print</span> <span class="n">fkey</span>

<span class="c"># access the table&#39;s MetaData:</span>
<span class="n">employees</span><span class="o">.</span><span class="n">metadata</span>

<span class="c"># access the table&#39;s bound Engine or Connection, if its MetaData is bound:</span>
<span class="n">employees</span><span class="o">.</span><span class="n">bind</span>

<span class="c"># access a column&#39;s name, type, nullable, primary key, foreign key</span>
<span class="n">employees</span><span class="o">.</span><span class="n">c</span><span class="o">.</span><span class="n">employee_id</span><span class="o">.</span><span class="n">name</span>
<span class="n">employees</span><span class="o">.</span><span class="n">c</span><span class="o">.</span><span class="n">employee_id</span><span class="o">.</span><span class="n">type</span>
<span class="n">employees</span><span class="o">.</span><span class="n">c</span><span class="o">.</span><span class="n">employee_id</span><span class="o">.</span><span class="n">nullable</span>
<span class="n">employees</span><span class="o">.</span><span class="n">c</span><span class="o">.</span><span class="n">employee_id</span><span class="o">.</span><span class="n">primary_key</span>
<span class="n">employees</span><span class="o">.</span><span class="n">c</span><span class="o">.</span><span class="n">employee_dept</span><span class="o">.</span><span class="n">foreign_keys</span>

<span class="c"># get the &quot;key&quot; of a column, which defaults to its name, but can</span>
<span class="c"># be any user-defined string:</span>
<span class="n">employees</span><span class="o">.</span><span class="n">c</span><span class="o">.</span><span class="n">employee_name</span><span class="o">.</span><span class="n">key</span>

<span class="c"># access a column&#39;s table:</span>
<span class="n">employees</span><span class="o">.</span><span class="n">c</span><span class="o">.</span><span class="n">employee_id</span><span class="o">.</span><span class="n">table</span> <span class="ow">is</span> <span class="n">employees</span>

<span class="c"># get the table related by a foreign key</span>
<span class="nb">list</span><span class="p">(</span><span class="n">employees</span><span class="o">.</span><span class="n">c</span><span class="o">.</span><span class="n">employee_dept</span><span class="o">.</span><span class="n">foreign_keys</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">column</span><span class="o">.</span><span class="n">table</span></pre></div>
</div>
</div>
<div class="section" id="creating-and-dropping-database-tables">
<span id="metadata-binding"></span><h3>Creating and Dropping Database Tables<a class="headerlink" href="#creating-and-dropping-database-tables" title="Permalink to this headline">¶</a></h3>
<p>Once you&#8217;ve defined some <a class="reference internal" href="#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> objects, assuming
you&#8217;re working with a brand new database one thing you might want to do is
issue CREATE statements for those tables and their related constructs (as an
aside, it&#8217;s also quite possible that you <em>don&#8217;t</em> want to do this, if you
already have some preferred methodology such as tools included with your
database or an existing scripting system - if that&#8217;s the case, feel free to
skip this section - SQLAlchemy has no requirement that it be used to create
your tables).</p>
<p>The usual way to issue CREATE is to use
<a class="reference internal" href="#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> on the
<a class="reference internal" href="#sqlalchemy.schema.MetaData" title="sqlalchemy.schema.MetaData"><tt class="xref py py-class docutils literal"><span class="pre">MetaData</span></tt></a> object. This method will issue queries
that first check for the existence of each individual table, and if not found
will issue the CREATE statements:</p>
<blockquote>
<div><div class="highlight-python+sql"><div class="highlight"><pre><span class="n">engine</span> <span class="o">=</span> <span class="n">create_engine</span><span class="p">(</span><span class="s">&#39;sqlite:///:memory:&#39;</span><span class="p">)</span>

<span class="n">metadata</span> <span class="o">=</span> <span class="n">MetaData</span><span class="p">()</span>

<span class="n">user</span> <span class="o">=</span> <span class="n">Table</span><span class="p">(</span><span class="s">&#39;user&#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;user_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_name&#39;</span><span class="p">,</span> <span class="n">String</span><span class="p">(</span><span class="mi">16</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;email_address&#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">key</span><span class="o">=</span><span class="s">&#39;email&#39;</span><span class="p">),</span>
    <span class="n">Column</span><span class="p">(</span><span class="s">&#39;password&#39;</span><span class="p">,</span> <span class="n">String</span><span class="p">(</span><span class="mi">20</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>

<span class="n">user_prefs</span> <span class="o">=</span> <span class="n">Table</span><span class="p">(</span><span class="s">&#39;user_prefs&#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>

<a href='#' class='sql_link'>sql</a><span class="n">metadata</span><span class="o">.</span><span class="n">create_all</span><span class="p">(</span><span class="n">engine</span><span class="p">)</span>
<div class='popup_sql'>PRAGMA table_info(user){}
CREATE TABLE user(
        user_id INTEGER NOT NULL PRIMARY KEY,
        user_name VARCHAR(16) NOT NULL,
        email_address VARCHAR(60),
        password VARCHAR(20) NOT NULL
)
PRAGMA table_info(user_prefs){}
CREATE TABLE user_prefs(
        pref_id INTEGER NOT NULL PRIMARY KEY,
        user_id INTEGER NOT NULL REFERENCES user(user_id),
        pref_name VARCHAR(40) NOT NULL,
        pref_value VARCHAR(100)
)</div></pre></div>
</div>
</div></blockquote>
<p><a class="reference internal" href="#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> creates foreign key constraints
between tables usually inline with the table definition itself, and for this
reason it also generates the tables in order of their dependency. There are
options to change this behavior such that <tt class="docutils literal"><span class="pre">ALTER</span> <span class="pre">TABLE</span></tt> is used instead.</p>
<p>Dropping all tables is similarly achieved using the
<a class="reference internal" href="#sqlalchemy.schema.MetaData.drop_all" title="sqlalchemy.schema.MetaData.drop_all"><tt class="xref py py-func docutils literal"><span class="pre">drop_all()</span></tt></a> method. This method does the
exact opposite of <a class="reference internal" href="#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> - the
presence of each table is checked first, and tables are dropped in reverse
order of dependency.</p>
<p>Creating and dropping individual tables can be done via the <tt class="docutils literal"><span class="pre">create()</span></tt> and
<tt class="docutils literal"><span class="pre">drop()</span></tt> methods of <a class="reference internal" href="#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a>. These methods by
default issue the CREATE or DROP regardless of the table being present:</p>
<div class="highlight-python+sql"><div class="highlight"><pre><span class="n">engine</span> <span class="o">=</span> <span class="n">create_engine</span><span class="p">(</span><span class="s">&#39;sqlite:///:memory:&#39;</span><span class="p">)</span>

<span class="n">meta</span> <span class="o">=</span> <span class="n">MetaData</span><span class="p">()</span>

<span class="n">employees</span> <span class="o">=</span> <span class="n">Table</span><span class="p">(</span><span class="s">&#39;employees&#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;employee_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;employee_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">key</span><span class="o">=</span><span class="s">&#39;name&#39;</span><span class="p">),</span>
    <span class="n">Column</span><span class="p">(</span><span class="s">&#39;employee_dept&#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;departments.department_id&quot;</span><span class="p">))</span>
<span class="p">)</span>
<a href='#' class='sql_link'>sql</a><span class="n">employees</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 employees(
employee_id SERIAL NOT NULL PRIMARY KEY,
employee_name VARCHAR(60) NOT NULL,
employee_dept INTEGER REFERENCES departments(department_id)
)</div></pre></div>
</div>
<p><tt class="docutils literal"><span class="pre">drop()</span></tt> method:</p>
<div class="highlight-python+sql"><div class="highlight"><pre><a href='#' class='sql_link'>sql</a><span class="n">employees</span><span class="o">.</span><span class="n">drop</span><span class="p">(</span><span class="n">engine</span><span class="p">)</span>
<div class='popup_sql'>DROP TABLE employee</div></pre></div>
</div>
<p>To enable the &#8220;check first for the table existing&#8221; logic, add the
<tt class="docutils literal"><span class="pre">checkfirst=True</span></tt> argument to <tt class="docutils literal"><span class="pre">create()</span></tt> or <tt class="docutils literal"><span class="pre">drop()</span></tt>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">employees</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="n">engine</span><span class="p">,</span> <span class="n">checkfirst</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="n">employees</span><span class="o">.</span><span class="n">drop</span><span class="p">(</span><span class="n">engine</span><span class="p">,</span> <span class="n">checkfirst</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span></pre></div>
</div>
</div>
<div class="section" id="binding-metadata-to-an-engine-or-connection">
<h3>Binding MetaData to an Engine or Connection<a class="headerlink" href="#binding-metadata-to-an-engine-or-connection" title="Permalink to this headline">¶</a></h3>
<p>Notice in the previous section the creator/dropper methods accept an argument
for the database engine in use. When a schema construct is combined with an
<a class="reference internal" href="connections.html#sqlalchemy.engine.base.Engine" title="sqlalchemy.engine.base.Engine"><tt class="xref py py-class docutils literal"><span class="pre">Engine</span></tt></a> object, or an individual
<a class="reference internal" href="connections.html#sqlalchemy.engine.base.Connection" title="sqlalchemy.engine.base.Connection"><tt class="xref py py-class docutils literal"><span class="pre">Connection</span></tt></a> object, we call this the <em>bind</em>.
In the above examples the bind is associated with the schema construct only
for the duration of the operation. However, the option exists to persistently
associate a bind with a set of schema constructs via the
<a class="reference internal" href="#sqlalchemy.schema.MetaData" title="sqlalchemy.schema.MetaData"><tt class="xref py py-class docutils literal"><span class="pre">MetaData</span></tt></a> object&#8217;s <tt class="docutils literal"><span class="pre">bind</span></tt> attribute:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">engine</span> <span class="o">=</span> <span class="n">create_engine</span><span class="p">(</span><span class="s">&#39;sqlite://&#39;</span><span class="p">)</span>

<span class="c"># create MetaData</span>
<span class="n">meta</span> <span class="o">=</span> <span class="n">MetaData</span><span class="p">()</span>

<span class="c"># bind to an engine</span>
<span class="n">meta</span><span class="o">.</span><span class="n">bind</span> <span class="o">=</span> <span class="n">engine</span></pre></div>
</div>
<p>We can now call methods like <a class="reference internal" href="#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>
without needing to pass the <a class="reference internal" href="connections.html#sqlalchemy.engine.base.Engine" title="sqlalchemy.engine.base.Engine"><tt class="xref py py-class docutils literal"><span class="pre">Engine</span></tt></a>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">meta</span><span class="o">.</span><span class="n">create_all</span><span class="p">()</span></pre></div>
</div>
<p>The MetaData&#8217;s bind is used for anything that requires an active connection,
such as loading the definition of a table from the database automatically
(called <em>reflection</em>):</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># describe a table called &#39;users&#39;, query the database for its columns</span>
<span class="n">users_table</span> <span class="o">=</span> <span class="n">Table</span><span class="p">(</span><span class="s">&#39;users&#39;</span><span class="p">,</span> <span class="n">meta</span><span class="p">,</span> <span class="n">autoload</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span></pre></div>
</div>
<p>As well as for executing SQL constructs that are derived from that MetaData&#8217;s table objects:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># generate a SELECT statement and execute</span>
<span class="n">result</span> <span class="o">=</span> <span class="n">users_table</span><span class="o">.</span><span class="n">select</span><span class="p">()</span><span class="o">.</span><span class="n">execute</span><span class="p">()</span></pre></div>
</div>
<p>Binding the MetaData to the Engine is a <strong>completely optional</strong> feature. The
above operations can be achieved without the persistent bind using
parameters:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># describe a table called &#39;users&#39;, query the database for its columns</span>
<span class="n">users_table</span> <span class="o">=</span> <span class="n">Table</span><span class="p">(</span><span class="s">&#39;users&#39;</span><span class="p">,</span> <span class="n">meta</span><span class="p">,</span> <span class="n">autoload</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">autoload_with</span><span class="o">=</span><span class="n">engine</span><span class="p">)</span>

<span class="c"># generate a SELECT statement and execute</span>
<span class="n">result</span> <span class="o">=</span> <span class="n">engine</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="n">users_table</span><span class="o">.</span><span class="n">select</span><span class="p">())</span></pre></div>
</div>
<p>Should you use bind ? It&#8217;s probably best to start without it, and wait for a
specific need to arise. Bind is useful if:</p>
<ul class="simple">
<li>You aren&#8217;t using the ORM, are usually using &#8220;connectionless&#8221; execution, and
find yourself constantly needing to specify the same
<a class="reference internal" href="connections.html#sqlalchemy.engine.base.Engine" title="sqlalchemy.engine.base.Engine"><tt class="xref py py-class docutils literal"><span class="pre">Engine</span></tt></a> object throughout the entire
application. Bind can be used here to provide &#8220;implicit&#8221; execution.</li>
<li>Your application has multiple schemas that correspond to different engines.
Using one <a class="reference internal" href="#sqlalchemy.schema.MetaData" title="sqlalchemy.schema.MetaData"><tt class="xref py py-class docutils literal"><span class="pre">MetaData</span></tt></a> for each schema, bound to
each engine, provides a decent place to delineate between the schemas. The
ORM will also integrate with this approach, where the <a class="reference internal" href="../orm/session.html#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><tt class="xref py py-class docutils literal"><span class="pre">Session</span></tt></a> will
naturally use the engine that is bound to each table via its metadata
(provided the <a class="reference internal" href="../orm/session.html#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><tt class="xref py py-class docutils literal"><span class="pre">Session</span></tt></a> itself has no <tt class="docutils literal"><span class="pre">bind</span></tt> configured.).</li>
</ul>
<p>Alternatively, the <tt class="docutils literal"><span class="pre">bind</span></tt> attribute of <a class="reference internal" href="#sqlalchemy.schema.MetaData" title="sqlalchemy.schema.MetaData"><tt class="xref py py-class docutils literal"><span class="pre">MetaData</span></tt></a>
is <em>confusing</em> if:</p>
<ul class="simple">
<li>Your application talks to multiple database engines at different times,
which use the <em>same</em> set of <a class="reference internal" href="#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> objects. It&#8217;s usually confusing
and unnecessary to begin to create &#8220;copies&#8221; of <a class="reference internal" href="#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> objects just
so that different engines can be used for different operations. An example
is an application that writes data to a &#8220;master&#8221; database while performing
read-only operations from a &#8220;read slave&#8221;. A global
<a class="reference internal" href="#sqlalchemy.schema.MetaData" title="sqlalchemy.schema.MetaData"><tt class="xref py py-class docutils literal"><span class="pre">MetaData</span></tt></a> object is <em>not</em> appropriate for
per-request switching like this, although a
<a class="reference internal" href="#sqlalchemy.schema.ThreadLocalMetaData" title="sqlalchemy.schema.ThreadLocalMetaData"><tt class="xref py py-class docutils literal"><span class="pre">ThreadLocalMetaData</span></tt></a> object is.</li>
<li>You are using the ORM <a class="reference internal" href="../orm/session.html#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><tt class="xref py py-class docutils literal"><span class="pre">Session</span></tt></a> to handle which class/table is bound
to which engine, or you are using the <a class="reference internal" href="../orm/session.html#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><tt class="xref py py-class docutils literal"><span class="pre">Session</span></tt></a> to manage switching
between engines. Its a good idea to keep the &#8220;binding of tables to engines&#8221;
in one place - either using <a class="reference internal" href="#sqlalchemy.schema.MetaData" title="sqlalchemy.schema.MetaData"><tt class="xref py py-class docutils literal"><span class="pre">MetaData</span></tt></a> only (the
<a class="reference internal" href="../orm/session.html#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><tt class="xref py py-class docutils literal"><span class="pre">Session</span></tt></a> can of course be present, it just has no <tt class="docutils literal"><span class="pre">bind</span></tt>
configured), or using <a class="reference internal" href="../orm/session.html#sqlalchemy.orm.session.Session" title="sqlalchemy.orm.session.Session"><tt class="xref py py-class docutils literal"><span class="pre">Session</span></tt></a> only (the <tt class="docutils literal"><span class="pre">bind</span></tt> attribute of
<a class="reference internal" href="#sqlalchemy.schema.MetaData" title="sqlalchemy.schema.MetaData"><tt class="xref py py-class docutils literal"><span class="pre">MetaData</span></tt></a> is left empty).</li>
</ul>
</div>
<div class="section" id="specifying-the-schema-name">
<h3>Specifying the Schema Name<a class="headerlink" href="#specifying-the-schema-name" title="Permalink to this headline">¶</a></h3>
<p>Some databases support the concept of multiple schemas.  A <a class="reference internal" href="#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> can reference this by specifying the <tt class="docutils literal"><span class="pre">schema</span></tt> keyword argument:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">financial_info</span> <span class="o">=</span> <span class="n">Table</span><span class="p">(</span><span class="s">&#39;financial_info&#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;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="n">nullable</span><span class="o">=</span><span class="bp">False</span><span class="p">),</span>
    <span class="n">schema</span><span class="o">=</span><span class="s">&#39;remote_banks&#39;</span>
<span class="p">)</span></pre></div>
</div>
<p>Within the <a class="reference internal" href="#sqlalchemy.schema.MetaData" title="sqlalchemy.schema.MetaData"><tt class="xref py py-class docutils literal"><span class="pre">MetaData</span></tt></a> collection, this table will be identified by the combination of <tt class="docutils literal"><span class="pre">financial_info</span></tt> and <tt class="docutils literal"><span class="pre">remote_banks</span></tt>.  If another table called <tt class="docutils literal"><span class="pre">financial_info</span></tt> is referenced without the <tt class="docutils literal"><span class="pre">remote_banks</span></tt> schema, it will refer to a different <a class="reference internal" href="#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></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> objects can specify references to columns in this table using the form <tt class="docutils literal"><span class="pre">remote_banks.financial_info.id</span></tt>.</p>
<p>The <tt class="docutils literal"><span class="pre">schema</span></tt> argument should be used for any name qualifiers required, including Oracle&#8217;s &#8220;owner&#8221; attribute and similar.  It also can accommodate a dotted name for longer schemes:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">schema</span><span class="o">=</span><span class="s">&quot;dbo.scott&quot;</span></pre></div>
</div>
</div>
<div class="section" id="backend-specific-options">
<h3>Backend-Specific Options<a class="headerlink" href="#backend-specific-options" title="Permalink to this headline">¶</a></h3>
<p><a class="reference internal" href="#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> supports database-specific options.   For example, MySQL has different table backend types, including &#8220;MyISAM&#8221; and &#8220;InnoDB&#8221;.   This can be expressed with <a class="reference internal" href="#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> using <tt class="docutils literal"><span class="pre">mysql_engine</span></tt>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">addresses</span> <span class="o">=</span> <span class="n">Table</span><span class="p">(</span><span class="s">&#39;engine_email_addresses&#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;address_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;remote_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="n">users</span><span class="o">.</span><span class="n">c</span><span class="o">.</span><span class="n">user_id</span><span class="p">)),</span>
    <span class="n">Column</span><span class="p">(</span><span class="s">&#39;email_address&#39;</span><span class="p">,</span> <span class="n">String</span><span class="p">(</span><span class="mi">20</span><span class="p">)),</span>
    <span class="n">mysql_engine</span><span class="o">=</span><span class="s">&#39;InnoDB&#39;</span>
<span class="p">)</span></pre></div>
</div>
<p>Other backends may support table-level options as well - these would be described in the individual documentation sections for each dialect.</p>
</div>
<div class="section" id="schema-api-constructs">
<h3>Schema API Constructs<a class="headerlink" href="#schema-api-constructs" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="sqlalchemy.schema.Column">
<em class="property">class </em><tt class="descclassname">sqlalchemy.schema.</tt><tt class="descname">Column</tt><big>(</big><em>*args</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Column" title="Permalink to this definition">¶</a></dt>
<dd><p>Bases: <tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.schema.SchemaItem</span></tt>, <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.ColumnClause" title="sqlalchemy.sql.expression.ColumnClause"><tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.sql.expression.ColumnClause</span></tt></a></p>
<p>Represents a column in a database table.</p>
<dl class="method">
<dt id="sqlalchemy.schema.Column.__init__">
<tt class="descname">__init__</tt><big>(</big><em>*args</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Column.__init__" title="Permalink to this definition">¶</a></dt>
<dd><p>Construct a new <tt class="docutils literal"><span class="pre">Column</span></tt> 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"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>name</strong> &#8211; <p>The name of this column as represented in the database. 
This argument may be the first positional argument, or specified
via keyword.</p>
<p>Names which contain no upper case characters
will be treated as case insensitive names, and will not be quoted
unless they are a reserved word.  Names with any number of upper
case characters will be quoted and sent exactly.  Note that this
behavior applies even for databases which standardize upper 
case names as case insensitive such as Oracle.</p>
<p>The name field may be omitted at construction time and applied
later, at any time before the Column is associated with a 
<a class="reference internal" href="#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a>.  This is to support convenient
usage within the <a class="reference internal" href="../orm/extensions/declarative.html#module-sqlalchemy.ext.declarative" title="sqlalchemy.ext.declarative"><tt class="xref py py-mod docutils literal"><span class="pre">declarative</span></tt></a> extension.</p>
</li>
<li><strong>type_</strong> &#8211; <p>The column&#8217;s type, indicated using an instance which 
subclasses <a class="reference internal" href="types.html#sqlalchemy.types.AbstractType" title="sqlalchemy.types.AbstractType"><tt class="xref py py-class docutils literal"><span class="pre">AbstractType</span></tt></a>.  If no arguments
are required for the type, the class of the type can be sent
as well, e.g.:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># use a type with arguments</span>
<span class="n">Column</span><span class="p">(</span><span class="s">&#39;data&#39;</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="c"># use no arguments</span>
<span class="n">Column</span><span class="p">(</span><span class="s">&#39;level&#39;</span><span class="p">,</span> <span class="n">Integer</span><span class="p">)</span></pre></div>
</div>
<p>The <tt class="docutils literal"><span class="pre">type</span></tt> argument may be the second positional argument
or specified by keyword.</p>
<p>There is partial support for automatic detection of the 
type based on that of 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> associated 
with this column, if the type is specified as <tt class="xref docutils literal"><span class="pre">None</span></tt>. 
However, this feature is not fully implemented and 
may not function in all cases.</p>
</li>
<li><strong>*args</strong> &#8211; Additional positional arguments include various 
<tt class="xref py py-class docutils literal"><span class="pre">SchemaItem</span></tt> derived constructs which will be applied 
as options to the column.  These include instances of 
<a class="reference internal" href="#sqlalchemy.schema.Constraint" title="sqlalchemy.schema.Constraint"><tt class="xref py py-class docutils literal"><span class="pre">Constraint</span></tt></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>, <a class="reference internal" href="#sqlalchemy.schema.ColumnDefault" title="sqlalchemy.schema.ColumnDefault"><tt class="xref py py-class docutils literal"><span class="pre">ColumnDefault</span></tt></a>, 
and <a class="reference internal" href="#sqlalchemy.schema.Sequence" title="sqlalchemy.schema.Sequence"><tt class="xref py py-class docutils literal"><span class="pre">Sequence</span></tt></a>.  In some cases an equivalent keyword 
argument is available such as <tt class="docutils literal"><span class="pre">server_default</span></tt>, <tt class="docutils literal"><span class="pre">default</span></tt>
and <tt class="docutils literal"><span class="pre">unique</span></tt>.</li>
<li><strong>autoincrement</strong> &#8211; <p>This flag may be set to <tt class="xref docutils literal"><span class="pre">False</span></tt> to 
indicate an integer primary key column that should not be
considered to be the &#8220;autoincrement&#8221; column, that is
the integer primary key column which generates values 
implicitly upon INSERT and whose value is usually returned
via the DBAPI cursor.lastrowid attribute.   It defaults
to <tt class="xref docutils literal"><span class="pre">True</span></tt> to satisfy the common use case of a table
with a single integer primary key column.  If the table
has a composite primary key consisting of more than one
integer column, set this flag to True only on the 
column that should be considered &#8220;autoincrement&#8221;.</p>
<p>The setting <em>only</em> has an effect for columns which are:</p>
<ul>
<li>Integer derived (i.e. INT, SMALLINT, BIGINT)</li>
<li>Part of the primary key</li>
<li>Are not referenced by any foreign keys</li>
<li>have no server side or client side defaults (with the exception
of Postgresql SERIAL).</li>
</ul>
<p>The setting has these two effects on columns that meet the
above criteria:</p>
<ul>
<li>DDL issued for the column will include database-specific
keywords intended to signify this column as an
&#8220;autoincrement&#8221; column, such as AUTO INCREMENT on MySQL,
SERIAL on Postgresql, and IDENTITY on MS-SQL.  It does 
<em>not</em> issue AUTOINCREMENT for SQLite since this is a
special SQLite flag that is not required for autoincrementing
behavior.  See the SQLite dialect documentation for
information on SQLite&#8217;s AUTOINCREMENT.</li>
<li>The column will be considered to be available as 
cursor.lastrowid or equivalent, for those dialects which
&#8220;post fetch&#8221; newly inserted identifiers after a row has
been inserted (SQLite, MySQL, MS-SQL).  It does not have 
any effect in this regard for databases that use sequences 
to generate primary key identifiers (i.e. Firebird, Postgresql, 
Oracle).</li>
</ul>
</li>
<li><strong>default</strong> &#8211; <p>A scalar, Python callable, or
<a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.ClauseElement" title="sqlalchemy.sql.expression.ClauseElement"><tt class="xref py py-class docutils literal"><span class="pre">ClauseElement</span></tt></a> representing the
<em>default value</em> for this column, which will be invoked upon insert
if this column is otherwise not specified in the VALUES clause of
the insert. This is a shortcut to using <a class="reference internal" href="#sqlalchemy.schema.ColumnDefault" title="sqlalchemy.schema.ColumnDefault"><tt class="xref py py-class docutils literal"><span class="pre">ColumnDefault</span></tt></a> as
a positional argument.</p>
<p>Contrast this argument to <tt class="docutils literal"><span class="pre">server_default</span></tt> which creates a 
default generator on the database side.</p>
</li>
<li><strong>doc</strong> &#8211; optional String that can be used by the ORM or similar
to document attributes.   This attribute does not render SQL
comments (a future attribute &#8216;comment&#8217; will achieve that).</li>
<li><strong>key</strong> &#8211; An optional string identifier which will identify this
<tt class="docutils literal"><span class="pre">Column</span></tt> object on the <a class="reference internal" href="#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a>. When a key is provided,
this is the only identifier referencing the <tt class="docutils literal"><span class="pre">Column</span></tt> within the
application, including ORM attribute mapping; the <tt class="docutils literal"><span class="pre">name</span></tt> field
is used only when rendering SQL.</li>
<li><strong>index</strong> &#8211; When <tt class="xref docutils literal"><span class="pre">True</span></tt>, indicates that the column is indexed.
This is a shortcut for using a <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 on the
table. To specify indexes with explicit names or indexes that
contain multiple columns, 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
instead.</li>
<li><strong>info</strong> &#8211; A dictionary which defaults to <tt class="docutils literal"><span class="pre">{}</span></tt>. A space to store
application specific data. This must be a dictionary.</li>
<li><strong>nullable</strong> &#8211; If set to the default of <tt class="xref docutils literal"><span class="pre">True</span></tt>, indicates the 
column will be rendered as allowing NULL, else it&#8217;s rendered as
NOT NULL. This parameter is only used when issuing CREATE TABLE
statements.</li>
<li><strong>onupdate</strong> &#8211; A scalar, Python callable, or
<a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.ClauseElement" title="sqlalchemy.sql.expression.ClauseElement"><tt class="xref py py-class docutils literal"><span class="pre">ClauseElement</span></tt></a> representing a
default value to be applied to the column within UPDATE
statements, which wil be invoked upon update if this column is not
present in the SET clause of the update. This is a shortcut to
using <a class="reference internal" href="#sqlalchemy.schema.ColumnDefault" title="sqlalchemy.schema.ColumnDefault"><tt class="xref py py-class docutils literal"><span class="pre">ColumnDefault</span></tt></a> as a positional argument with
<tt class="docutils literal"><span class="pre">for_update=True</span></tt>.</li>
<li><strong>primary_key</strong> &#8211; If <tt class="xref docutils literal"><span class="pre">True</span></tt>, marks this column as a primary key
column. Multiple columns can have this flag set to specify
composite primary keys. As an alternative, the primary key of a
<a class="reference internal" href="#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> can be specified via an explicit
<a class="reference internal" href="#sqlalchemy.schema.PrimaryKeyConstraint" title="sqlalchemy.schema.PrimaryKeyConstraint"><tt class="xref py py-class docutils literal"><span class="pre">PrimaryKeyConstraint</span></tt></a> object.</li>
<li><strong>server_default</strong> &#8211; <p>A <a class="reference internal" href="#sqlalchemy.schema.FetchedValue" title="sqlalchemy.schema.FetchedValue"><tt class="xref py py-class docutils literal"><span class="pre">FetchedValue</span></tt></a> instance, str, Unicode
or <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.text" title="sqlalchemy.sql.expression.text"><tt class="xref py py-func docutils literal"><span class="pre">text()</span></tt></a> construct representing
the DDL DEFAULT value for the column.</p>
<p>String types will be emitted as-is, surrounded by single quotes:</p>
<div class="highlight-python"><pre>Column('x', Text, server_default="val")

x TEXT DEFAULT 'val'</pre>
</div>
<p>A <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.text" title="sqlalchemy.sql.expression.text"><tt class="xref py py-func docutils literal"><span class="pre">text()</span></tt></a> expression will be
rendered as-is, without quotes:</p>
<div class="highlight-python"><pre>Column('y', DateTime, server_default=text('NOW()'))0

y DATETIME DEFAULT NOW()</pre>
</div>
<p>Strings and text() will be converted into a <a class="reference internal" href="#sqlalchemy.schema.DefaultClause" title="sqlalchemy.schema.DefaultClause"><tt class="xref py py-class docutils literal"><span class="pre">DefaultClause</span></tt></a>
object upon initialization.</p>
<p>Use <a class="reference internal" href="#sqlalchemy.schema.FetchedValue" title="sqlalchemy.schema.FetchedValue"><tt class="xref py py-class docutils literal"><span class="pre">FetchedValue</span></tt></a> to indicate that an already-existing
column will generate a default value on the database side which
will be available to SQLAlchemy for post-fetch after inserts. This
construct does not specify any DDL and the implementation is left
to the database, such as via a trigger.</p>
</li>
<li><strong>server_onupdate</strong> &#8211; A <a class="reference internal" href="#sqlalchemy.schema.FetchedValue" title="sqlalchemy.schema.FetchedValue"><tt class="xref py py-class docutils literal"><span class="pre">FetchedValue</span></tt></a> instance
representing a database-side default generation function. This
indicates to SQLAlchemy that a newly generated value will be
available after updates. This construct does not specify any DDL
and the implementation is left to the database, such as via a
trigger.</li>
<li><strong>quote</strong> &#8211; Force quoting of this column&#8217;s name on or off,
corresponding to <tt class="xref docutils literal"><span class="pre">True</span></tt> or <tt class="xref docutils literal"><span class="pre">False</span></tt>. When left at its default
of <tt class="xref docutils literal"><span class="pre">None</span></tt>, the column identifier will be quoted according to
whether the name is case sensitive (identifiers with at least one
upper case character are treated as case sensitive), or if it&#8217;s a
reserved word. This flag is only needed to force quoting of a
reserved word which is not known by the SQLAlchemy dialect.</li>
<li><strong>unique</strong> &#8211; When <tt class="xref docutils literal"><span class="pre">True</span></tt>, indicates that this column contains a
unique constraint, or if <tt class="docutils literal"><span class="pre">index</span></tt> is <tt class="xref docutils literal"><span class="pre">True</span></tt> as well, indicates
that 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> should be created with the unique flag.
To specify multiple columns in the constraint/index or to specify
an explicit name, use 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> or
<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> constructs explicitly.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.schema.Column.append_foreign_key">
<tt class="descname">append_foreign_key</tt><big>(</big><em>fk</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Column.append_foreign_key" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<dl class="method">
<dt id="sqlalchemy.schema.Column.copy">
<tt class="descname">copy</tt><big>(</big><em>**kw</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Column.copy" title="Permalink to this definition">¶</a></dt>
<dd><p>Create a copy of this <tt class="docutils literal"><span class="pre">Column</span></tt>, unitialized.</p>
<p>This is used in <tt class="docutils literal"><span class="pre">Table.tometadata</span></tt>.</p>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.schema.Column.get_children">
<tt class="descname">get_children</tt><big>(</big><em>schema_visitor=False</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Column.get_children" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<dl class="method">
<dt id="sqlalchemy.schema.Column.references">
<tt class="descname">references</tt><big>(</big><em>column</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Column.references" title="Permalink to this definition">¶</a></dt>
<dd><p>Return True if this Column references the given column via foreign
key.</p>
</dd></dl>

</dd></dl>

<dl class="class">
<dt id="sqlalchemy.schema.MetaData">
<em class="property">class </em><tt class="descclassname">sqlalchemy.schema.</tt><tt class="descname">MetaData</tt><big>(</big><em>bind=None</em>, <em>reflect=False</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.MetaData" title="Permalink to this definition">¶</a></dt>
<dd><p>Bases: <tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.schema.SchemaItem</span></tt></p>
<p>A collection of Tables and their associated schema constructs.</p>
<p>Holds a collection of Tables and an optional binding to an <tt class="docutils literal"><span class="pre">Engine</span></tt> or
<tt class="docutils literal"><span class="pre">Connection</span></tt>.  If bound, the <a class="reference internal" href="#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> objects
in the collection and their columns may participate in implicit SQL
execution.</p>
<p>The <cite>Table</cite> objects themselves are stored in the <cite>metadata.tables</cite>
dictionary.</p>
<p>The <tt class="docutils literal"><span class="pre">bind</span></tt> property may be assigned to dynamically.  A common pattern is
to start unbound and then bind later when an engine is available:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">metadata</span> <span class="o">=</span> <span class="n">MetaData</span><span class="p">()</span>
<span class="c"># define tables</span>
<span class="n">Table</span><span class="p">(</span><span class="s">&#39;mytable&#39;</span><span class="p">,</span> <span class="n">metadata</span><span class="p">,</span> <span class="o">...</span><span class="p">)</span>
<span class="c"># connect to an engine later, perhaps after loading a URL from a</span>
<span class="c"># configuration file</span>
<span class="n">metadata</span><span class="o">.</span><span class="n">bind</span> <span class="o">=</span> <span class="n">an_engine</span></pre></div>
</div>
<p>MetaData is a thread-safe object after tables have been explicitly defined
or loaded via reflection.</p>
<span class="target" id="index-0"></span><dl class="method">
<dt id="sqlalchemy.schema.MetaData.__init__">
<tt class="descname">__init__</tt><big>(</big><em>bind=None</em>, <em>reflect=False</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.MetaData.__init__" title="Permalink to this definition">¶</a></dt>
<dd><p>Create a new MetaData 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"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>bind</strong> &#8211; An Engine or Connection to bind to.  May also be a string or URL
instance, these are passed to create_engine() and this MetaData will
be bound to the resulting engine.</li>
<li><strong>reflect</strong> &#8211; Optional, automatically load all tables from the bound database.
Defaults to False. <tt class="docutils literal"><span class="pre">bind</span></tt> is required when this option is set.
For finer control over loaded tables, use the <tt class="docutils literal"><span class="pre">reflect</span></tt> method of
<tt class="docutils literal"><span class="pre">MetaData</span></tt>.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.schema.MetaData.append_ddl_listener">
<tt class="descname">append_ddl_listener</tt><big>(</big><em>event</em>, <em>listener</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.MetaData.append_ddl_listener" title="Permalink to this definition">¶</a></dt>
<dd><p>Append a DDL event listener to this <tt class="docutils literal"><span class="pre">MetaData</span></tt>.</p>
<p>The <tt class="docutils literal"><span class="pre">listener</span></tt> callable will be triggered when this <tt class="docutils literal"><span class="pre">MetaData</span></tt> is
involved in DDL creates or drops, and will be invoked either before
all Table-related actions or after.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>event</strong> &#8211; One of <tt class="docutils literal"><span class="pre">MetaData.ddl_events</span></tt>; &#8216;before-create&#8217;, &#8216;after-create&#8217;,
&#8216;before-drop&#8217; or &#8216;after-drop&#8217;.</li>
<li><strong>listener</strong> &#8211; <p>A callable, invoked with three positional arguments:</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">event:</th><td class="field-body">The event currently being handled</td>
</tr>
<tr class="field"><th class="field-name">target:</th><td class="field-body">The <tt class="docutils literal"><span class="pre">MetaData</span></tt> object being operated upon</td>
</tr>
<tr class="field"><th class="field-name">bind:</th><td class="field-body">The <tt class="docutils literal"><span class="pre">Connection</span></tt> bueing used for DDL execution.</td>
</tr>
</tbody>
</table>
</li>
</ul>
</td>
</tr>
</tbody>
</table>
<p>Listeners are added to the MetaData&#8217;s <tt class="docutils literal"><span class="pre">ddl_listeners</span></tt> attribute.</p>
<p>Note: MetaData listeners are invoked even when <tt class="docutils literal"><span class="pre">Tables</span></tt> are created
in isolation.  This may change in a future release. I.e.:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># triggers all MetaData and Table listeners:</span>
<span class="n">metadata</span><span class="o">.</span><span class="n">create_all</span><span class="p">()</span>

<span class="c"># triggers MetaData listeners too:</span>
<span class="n">some</span><span class="o">.</span><span class="n">table</span><span class="o">.</span><span class="n">create</span><span class="p">()</span></pre></div>
</div>
</dd></dl>

<dl class="attribute">
<dt id="sqlalchemy.schema.MetaData.bind">
<tt class="descname">bind</tt><a class="headerlink" href="#sqlalchemy.schema.MetaData.bind" title="Permalink to this definition">¶</a></dt>
<dd><p>An Engine or Connection to which this MetaData is bound.</p>
<p>This property may be assigned an <tt class="docutils literal"><span class="pre">Engine</span></tt> or <tt class="docutils literal"><span class="pre">Connection</span></tt>, or
assigned a string or URL to automatically create a basic <tt class="docutils literal"><span class="pre">Engine</span></tt>
for this bind with <tt class="docutils literal"><span class="pre">create_engine()</span></tt>.</p>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.schema.MetaData.clear">
<tt class="descname">clear</tt><big>(</big><big>)</big><a class="headerlink" href="#sqlalchemy.schema.MetaData.clear" title="Permalink to this definition">¶</a></dt>
<dd><p>Clear all Table objects from this MetaData.</p>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.schema.MetaData.create_all">
<tt class="descname">create_all</tt><big>(</big><em>bind=None</em>, <em>tables=None</em>, <em>checkfirst=True</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.MetaData.create_all" title="Permalink to this definition">¶</a></dt>
<dd><p>Create all tables stored in this metadata.</p>
<p>Conditional by default, will not attempt to recreate tables already
present in the target database.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>bind</strong> &#8211; A <a class="reference internal" href="connections.html#sqlalchemy.engine.base.Connectable" title="sqlalchemy.engine.base.Connectable"><tt class="xref py py-class docutils literal"><span class="pre">Connectable</span></tt></a> used to access the
database; if None, uses the existing bind on this <tt class="docutils literal"><span class="pre">MetaData</span></tt>, if
any.</li>
<li><strong>tables</strong> &#8211; Optional list of <tt class="docutils literal"><span class="pre">Table</span></tt> objects, which is a subset of the total
tables in the <tt class="docutils literal"><span class="pre">MetaData</span></tt> (others are ignored).</li>
<li><strong>checkfirst</strong> &#8211; Defaults to True, don&#8217;t issue CREATEs for tables already present
in the target database.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.schema.MetaData.drop_all">
<tt class="descname">drop_all</tt><big>(</big><em>bind=None</em>, <em>tables=None</em>, <em>checkfirst=True</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.MetaData.drop_all" title="Permalink to this definition">¶</a></dt>
<dd><p>Drop all tables stored in this metadata.</p>
<p>Conditional by default, will not attempt to drop tables not present in
the target database.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>bind</strong> &#8211; A <a class="reference internal" href="connections.html#sqlalchemy.engine.base.Connectable" title="sqlalchemy.engine.base.Connectable"><tt class="xref py py-class docutils literal"><span class="pre">Connectable</span></tt></a> used to access the
database; if None, uses the existing bind on this <tt class="docutils literal"><span class="pre">MetaData</span></tt>, if
any.</li>
<li><strong>tables</strong> &#8211; Optional list of <tt class="docutils literal"><span class="pre">Table</span></tt> objects, which is a subset of the
total tables in the <tt class="docutils literal"><span class="pre">MetaData</span></tt> (others are ignored).</li>
<li><strong>checkfirst</strong> &#8211; Defaults to True, only issue DROPs for tables confirmed to be
present in the target database.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.schema.MetaData.is_bound">
<tt class="descname">is_bound</tt><big>(</big><big>)</big><a class="headerlink" href="#sqlalchemy.schema.MetaData.is_bound" title="Permalink to this definition">¶</a></dt>
<dd><p>True if this MetaData is bound to an Engine or Connection.</p>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.schema.MetaData.reflect">
<tt class="descname">reflect</tt><big>(</big><em>bind=None</em>, <em>schema=None</em>, <em>views=False</em>, <em>only=None</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.MetaData.reflect" title="Permalink to this definition">¶</a></dt>
<dd><p>Load all available table definitions from the database.</p>
<p>Automatically creates <tt class="docutils literal"><span class="pre">Table</span></tt> entries in this <tt class="docutils literal"><span class="pre">MetaData</span></tt> for any
table available in the database but not yet present in the
<tt class="docutils literal"><span class="pre">MetaData</span></tt>.  May be called multiple times to pick up tables recently
added to the database, however no special action is taken if a table
in this <tt class="docutils literal"><span class="pre">MetaData</span></tt> no longer exists in the database.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>bind</strong> &#8211; A <a class="reference internal" href="connections.html#sqlalchemy.engine.base.Connectable" title="sqlalchemy.engine.base.Connectable"><tt class="xref py py-class docutils literal"><span class="pre">Connectable</span></tt></a> used to access the
database; if None, uses the existing bind on this <tt class="docutils literal"><span class="pre">MetaData</span></tt>, if
any.</li>
<li><strong>schema</strong> &#8211; Optional, query and reflect tables from an alterate schema.</li>
<li><strong>views</strong> &#8211; If True, also reflect views.</li>
<li><strong>only</strong> &#8211; <p>Optional.  Load only a sub-set of available named tables.  May be
specified as a sequence of names or a callable.</p>
<p>If a sequence of names is provided, only those tables will be
reflected.  An error is raised if a table is requested but not
available.  Named tables already present in this <tt class="docutils literal"><span class="pre">MetaData</span></tt> are
ignored.</p>
<p>If a callable is provided, it will be used as a boolean predicate to
filter the list of potential table names.  The callable is called
with a table name and this <tt class="docutils literal"><span class="pre">MetaData</span></tt> instance as positional
arguments and should return a true value for any table to reflect.</p>
</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.schema.MetaData.remove">
<tt class="descname">remove</tt><big>(</big><em>table</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.MetaData.remove" title="Permalink to this definition">¶</a></dt>
<dd><p>Remove the given Table object from this MetaData.</p>
</dd></dl>

<dl class="attribute">
<dt id="sqlalchemy.schema.MetaData.sorted_tables">
<tt class="descname">sorted_tables</tt><a class="headerlink" href="#sqlalchemy.schema.MetaData.sorted_tables" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a list of <tt class="docutils literal"><span class="pre">Table</span></tt> objects sorted in order of
dependency.</p>
</dd></dl>

</dd></dl>

<dl class="class">
<dt id="sqlalchemy.schema.Table">
<em class="property">class </em><tt class="descclassname">sqlalchemy.schema.</tt><tt class="descname">Table</tt><big>(</big><em>*args</em>, <em>**kw</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Table" title="Permalink to this definition">¶</a></dt>
<dd><p>Bases: <tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.schema.SchemaItem</span></tt>, <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.TableClause" title="sqlalchemy.sql.expression.TableClause"><tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.sql.expression.TableClause</span></tt></a></p>
<p>Represent a table in a database.</p>
<p>e.g.:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">mytable</span> <span class="o">=</span> <span class="n">Table</span><span class="p">(</span><span class="s">&quot;mytable&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">&#39;mytable_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;value&#39;</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="p">)</span></pre></div>
</div>
<p>The Table object constructs a unique instance of itself based on its
name within the given MetaData object.   Constructor
arguments are as follows:</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>name</strong> &#8211; <p>The name of this table as represented in the database.</p>
<p>This property, along with the <em>schema</em>, indicates the <em>singleton
identity</em> of this table in relation to its parent <a class="reference internal" href="#sqlalchemy.schema.MetaData" title="sqlalchemy.schema.MetaData"><tt class="xref py py-class docutils literal"><span class="pre">MetaData</span></tt></a>.
Additional calls to <a class="reference internal" href="#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> with the same name, metadata,
and schema name will return the same <a class="reference internal" href="#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> object.</p>
<p>Names which contain no upper case characters
will be treated as case insensitive names, and will not be quoted
unless they are a reserved word.  Names with any number of upper
case characters will be quoted and sent exactly.  Note that this
behavior applies even for databases which standardize upper 
case names as case insensitive such as Oracle.</p>
</li>
<li><strong>metadata</strong> &#8211; a <a class="reference internal" href="#sqlalchemy.schema.MetaData" title="sqlalchemy.schema.MetaData"><tt class="xref py py-class docutils literal"><span class="pre">MetaData</span></tt></a> object which will contain this 
table.  The metadata is used as a point of association of this table
with other tables which are referenced via foreign key.  It also
may be used to associate this table with a particular 
<a class="reference internal" href="connections.html#sqlalchemy.engine.base.Connectable" title="sqlalchemy.engine.base.Connectable"><tt class="xref py py-class docutils literal"><span class="pre">Connectable</span></tt></a>.</li>
<li><strong>*args</strong> &#8211; Additional positional arguments are used primarily
to add the list of <a class="reference internal" href="#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a> objects contained within this
table. Similar to the style of a CREATE TABLE statement, other
<tt class="xref py py-class docutils literal"><span class="pre">SchemaItem</span></tt> constructs may be added here, including
<a class="reference internal" href="#sqlalchemy.schema.PrimaryKeyConstraint" title="sqlalchemy.schema.PrimaryKeyConstraint"><tt class="xref py py-class docutils literal"><span class="pre">PrimaryKeyConstraint</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>.</li>
<li><strong>autoload</strong> &#8211; Defaults to False: the Columns for this table should 
be reflected from the database. Usually there will be no Column
objects in the constructor if this property is set.</li>
<li><strong>autoload_with</strong> &#8211; If autoload==True, this is an optional Engine 
or Connection instance to be used for the table reflection. If
<tt class="xref docutils literal"><span class="pre">None</span></tt>, the underlying MetaData&#8217;s bound connectable will be used.</li>
<li><strong>implicit_returning</strong> &#8211; True by default - indicates that 
RETURNING can be used by default to fetch newly inserted primary key 
values, for backends which support this.  Note that 
create_engine() also provides an implicit_returning flag.</li>
<li><strong>include_columns</strong> &#8211; A list of strings indicating a subset of 
columns to be loaded via the <tt class="docutils literal"><span class="pre">autoload</span></tt> operation; table columns who
aren&#8217;t present in this list will not be represented on the resulting
<tt class="docutils literal"><span class="pre">Table</span></tt> object. Defaults to <tt class="xref docutils literal"><span class="pre">None</span></tt> which indicates all columns
should be reflected.</li>
<li><strong>info</strong> &#8211; A dictionary which defaults to <tt class="docutils literal"><span class="pre">{}</span></tt>.  A space to store
application specific data. This must be a dictionary.</li>
<li><strong>mustexist</strong> &#8211; When <tt class="xref docutils literal"><span class="pre">True</span></tt>, indicates that this Table must already 
be present in the given <tt class="xref py py-class docutils literal"><span class="pre">MetaData`</span></tt> collection.</li>
<li><strong>prefixes</strong> &#8211; A list of strings to insert after CREATE in the CREATE TABLE
statement.  They will be separated by spaces.</li>
<li><strong>quote</strong> &#8211; Force quoting of this table&#8217;s name on or off, corresponding
to <tt class="xref docutils literal"><span class="pre">True</span></tt> or <tt class="xref docutils literal"><span class="pre">False</span></tt>.  When left at its default of <tt class="xref docutils literal"><span class="pre">None</span></tt>,
the column identifier will be quoted according to whether the name is
case sensitive (identifiers with at least one upper case character are 
treated as case sensitive), or if it&#8217;s a reserved word.  This flag 
is only needed to force quoting of a reserved word which is not known
by the SQLAlchemy dialect.</li>
<li><strong>quote_schema</strong> &#8211; same as &#8216;quote&#8217; but applies to the schema identifier.</li>
<li><strong>schema</strong> &#8211; The <em>schema name</em> for this table, which is required if 
the table resides in a schema other than the default selected schema
for the engine&#8217;s database connection. Defaults to <tt class="xref docutils literal"><span class="pre">None</span></tt>.</li>
<li><strong>useexisting</strong> &#8211; When <tt class="xref docutils literal"><span class="pre">True</span></tt>, indicates that if this Table is already
present in the given <a class="reference internal" href="#sqlalchemy.schema.MetaData" title="sqlalchemy.schema.MetaData"><tt class="xref py py-class docutils literal"><span class="pre">MetaData</span></tt></a>, apply further arguments within
the constructor to the existing <a class="reference internal" href="#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a>. If this flag is not
set, an error is raised when the parameters of an existing
<a class="reference internal" href="#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> are overwritten.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<dl class="method">
<dt id="sqlalchemy.schema.Table.__init__">
<tt class="descname">__init__</tt><big>(</big><em>*args</em>, <em>**kw</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Table.__init__" title="Permalink to this definition">¶</a></dt>
<dd><p>Constructor for <a class="reference internal" href="#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a>.</p>
<p>This method is a no-op.   See the top-level
documentation for <a class="reference internal" href="#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a>
for constructor arguments.</p>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.schema.Table.add_is_dependent_on">
<tt class="descname">add_is_dependent_on</tt><big>(</big><em>table</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Table.add_is_dependent_on" title="Permalink to this definition">¶</a></dt>
<dd><p>Add a &#8216;dependency&#8217; for this Table.</p>
<p>This is another Table object which must be created
first before this one can, or dropped after this one.</p>
<p>Usually, dependencies between tables are determined via 
ForeignKey objects.   However, for other situations that 
create dependencies outside of foreign keys (rules, inheriting),
this method can manually establish such a link.</p>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.schema.Table.append_column">
<tt class="descname">append_column</tt><big>(</big><em>column</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Table.append_column" title="Permalink to this definition">¶</a></dt>
<dd><p>Append a <a class="reference internal" href="#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a> to this <a class="reference internal" href="#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a>.</p>
<p>The &#8220;key&#8221; of the newly added <a class="reference internal" href="#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a>, i.e. the
value of its <tt class="docutils literal"><span class="pre">.key</span></tt> attribute, will then be available
in the <tt class="docutils literal"><span class="pre">.c</span></tt> collection of this <a class="reference internal" href="#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a>, and the
column definition will be included in any CREATE TABLE, SELECT,
UPDATE, etc. statements generated from this <a class="reference internal" href="#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a>
construct.</p>
<p>Note that this does <strong>not</strong> change the definition of the table 
as it exists within any underlying database, assuming that
table has already been created in the database.   Relational 
databases support the addition of columns to existing tables 
using the SQL ALTER command, which would need to be 
emitted for an already-existing table that doesn&#8217;t contain
the newly added column.</p>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.schema.Table.append_constraint">
<tt class="descname">append_constraint</tt><big>(</big><em>constraint</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Table.append_constraint" title="Permalink to this definition">¶</a></dt>
<dd><p>Append a <a class="reference internal" href="#sqlalchemy.schema.Constraint" title="sqlalchemy.schema.Constraint"><tt class="xref py py-class docutils literal"><span class="pre">Constraint</span></tt></a> to this <a class="reference internal" href="#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a>.</p>
<p>This has the effect of the constraint being included in any
future CREATE TABLE statement, assuming specific DDL creation 
events have not been associated with the given <a class="reference internal" href="#sqlalchemy.schema.Constraint" title="sqlalchemy.schema.Constraint"><tt class="xref py py-class docutils literal"><span class="pre">Constraint</span></tt></a> 
object.</p>
<p>Note that this does <strong>not</strong> produce the constraint within the 
relational database automatically, for a table that already exists
in the database.   To add a constraint to an
existing relational database table, the SQL ALTER command must
be used.  SQLAlchemy also provides the <a class="reference internal" href="#sqlalchemy.schema.AddConstraint" title="sqlalchemy.schema.AddConstraint"><tt class="xref py py-class docutils literal"><span class="pre">AddConstraint</span></tt></a> construct
which can produce this SQL when invoked as an executable clause.</p>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.schema.Table.append_ddl_listener">
<tt class="descname">append_ddl_listener</tt><big>(</big><em>event</em>, <em>listener</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Table.append_ddl_listener" title="Permalink to this definition">¶</a></dt>
<dd><p>Append a DDL event listener to this <tt class="docutils literal"><span class="pre">Table</span></tt>.</p>
<p>The <tt class="docutils literal"><span class="pre">listener</span></tt> callable will be triggered when this <tt class="docutils literal"><span class="pre">Table</span></tt> is
created or dropped, either directly before or after the DDL is issued
to the database.  The listener may modify the Table, but may not abort
the event itself.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>event</strong> &#8211; One of <tt class="docutils literal"><span class="pre">Table.ddl_events</span></tt>; e.g. &#8216;before-create&#8217;, &#8216;after-create&#8217;,
&#8216;before-drop&#8217; or &#8216;after-drop&#8217;.</li>
<li><strong>listener</strong> &#8211; <p>A callable, invoked with three positional arguments:</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">event:</th><td class="field-body">The event currently being handled</td>
</tr>
<tr class="field"><th class="field-name">target:</th><td class="field-body">The <tt class="docutils literal"><span class="pre">Table</span></tt> object being created or dropped</td>
</tr>
<tr class="field"><th class="field-name">bind:</th><td class="field-body">The <tt class="docutils literal"><span class="pre">Connection</span></tt> bueing used for DDL execution.</td>
</tr>
</tbody>
</table>
</li>
</ul>
</td>
</tr>
</tbody>
</table>
<p>Listeners are added to the Table&#8217;s <tt class="docutils literal"><span class="pre">ddl_listeners</span></tt> attribute.</p>
</dd></dl>

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

<dl class="method">
<dt id="sqlalchemy.schema.Table.create">
<tt class="descname">create</tt><big>(</big><em>bind=None</em>, <em>checkfirst=False</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Table.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 table.</p>
<p>See also <tt class="docutils literal"><span class="pre">metadata.create_all()</span></tt>.</p>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.schema.Table.drop">
<tt class="descname">drop</tt><big>(</big><em>bind=None</em>, <em>checkfirst=False</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Table.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 table.</p>
<p>See also <tt class="docutils literal"><span class="pre">metadata.drop_all()</span></tt>.</p>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.schema.Table.exists">
<tt class="descname">exists</tt><big>(</big><em>bind=None</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Table.exists" title="Permalink to this definition">¶</a></dt>
<dd><p>Return True if this table exists.</p>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.schema.Table.get_children">
<tt class="descname">get_children</tt><big>(</big><em>column_collections=True</em>, <em>schema_visitor=False</em>, <em>**kw</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Table.get_children" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<dl class="attribute">
<dt id="sqlalchemy.schema.Table.key">
<tt class="descname">key</tt><a class="headerlink" href="#sqlalchemy.schema.Table.key" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<dl class="attribute">
<dt id="sqlalchemy.schema.Table.primary_key">
<tt class="descname">primary_key</tt><a class="headerlink" href="#sqlalchemy.schema.Table.primary_key" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<dl class="method">
<dt id="sqlalchemy.schema.Table.tometadata">
<tt class="descname">tometadata</tt><big>(</big><em>metadata</em>, <em>schema=&lt;symbol 'retain_schema&gt;</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Table.tometadata" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a copy of this <a class="reference internal" href="#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> associated with a different
<a class="reference internal" href="#sqlalchemy.schema.MetaData" title="sqlalchemy.schema.MetaData"><tt class="xref py py-class docutils literal"><span class="pre">MetaData</span></tt></a>.</p>
<p>E.g.:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># create two metadata</span>
<span class="n">meta1</span> <span class="o">=</span> <span class="n">MetaData</span><span class="p">(</span><span class="s">&#39;sqlite:///querytest.db&#39;</span><span class="p">)</span>
<span class="n">meta2</span> <span class="o">=</span> <span class="n">MetaData</span><span class="p">()</span>

<span class="c"># load &#39;users&#39; from the sqlite engine</span>
<span class="n">users_table</span> <span class="o">=</span> <span class="n">Table</span><span class="p">(</span><span class="s">&#39;users&#39;</span><span class="p">,</span> <span class="n">meta1</span><span class="p">,</span> <span class="n">autoload</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>

<span class="c"># create the same Table object for the plain metadata</span>
<span class="n">users_table_2</span> <span class="o">=</span> <span class="n">users_table</span><span class="o">.</span><span class="n">tometadata</span><span class="p">(</span><span class="n">meta2</span><span class="p">)</span></pre></div>
</div>
</dd></dl>

</dd></dl>

<dl class="class">
<dt id="sqlalchemy.schema.ThreadLocalMetaData">
<em class="property">class </em><tt class="descclassname">sqlalchemy.schema.</tt><tt class="descname">ThreadLocalMetaData</tt><a class="headerlink" href="#sqlalchemy.schema.ThreadLocalMetaData" title="Permalink to this definition">¶</a></dt>
<dd><p>Bases: <a class="reference internal" href="#sqlalchemy.schema.MetaData" title="sqlalchemy.schema.MetaData"><tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.schema.MetaData</span></tt></a></p>
<p>A MetaData variant that presents a different <tt class="docutils literal"><span class="pre">bind</span></tt> in every thread.</p>
<p>Makes the <tt class="docutils literal"><span class="pre">bind</span></tt> property of the MetaData a thread-local value, allowing
this collection of tables to be bound to different <tt class="docutils literal"><span class="pre">Engine</span></tt>
implementations or connections in each thread.</p>
<p>The ThreadLocalMetaData starts off bound to None in each thread.  Binds
must be made explicitly by assigning to the <tt class="docutils literal"><span class="pre">bind</span></tt> property or using
<tt class="docutils literal"><span class="pre">connect()</span></tt>.  You can also re-bind dynamically multiple times per
thread, just like a regular <tt class="docutils literal"><span class="pre">MetaData</span></tt>.</p>
<dl class="method">
<dt id="sqlalchemy.schema.ThreadLocalMetaData.__init__">
<tt class="descname">__init__</tt><big>(</big><big>)</big><a class="headerlink" href="#sqlalchemy.schema.ThreadLocalMetaData.__init__" title="Permalink to this definition">¶</a></dt>
<dd><p>Construct a ThreadLocalMetaData.</p>
</dd></dl>

<dl class="attribute">
<dt id="sqlalchemy.schema.ThreadLocalMetaData.bind">
<tt class="descname">bind</tt><a class="headerlink" href="#sqlalchemy.schema.ThreadLocalMetaData.bind" title="Permalink to this definition">¶</a></dt>
<dd><p>The bound Engine or Connection for this thread.</p>
<p>This property may be assigned an Engine or Connection, or assigned a
string or URL to automatically create a basic Engine for this bind
with <tt class="docutils literal"><span class="pre">create_engine()</span></tt>.</p>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.schema.ThreadLocalMetaData.dispose">
<tt class="descname">dispose</tt><big>(</big><big>)</big><a class="headerlink" href="#sqlalchemy.schema.ThreadLocalMetaData.dispose" title="Permalink to this definition">¶</a></dt>
<dd><p>Dispose all bound engines, in all thread contexts.</p>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.schema.ThreadLocalMetaData.is_bound">
<tt class="descname">is_bound</tt><big>(</big><big>)</big><a class="headerlink" href="#sqlalchemy.schema.ThreadLocalMetaData.is_bound" title="Permalink to this definition">¶</a></dt>
<dd><p>True if there is a bind for this thread.</p>
</dd></dl>

</dd></dl>

</div>
</div>
<div class="section" id="reflecting-database-objects">
<span id="metadata-reflection"></span><h2>Reflecting Database Objects<a class="headerlink" href="#reflecting-database-objects" title="Permalink to this headline">¶</a></h2>
<p>A <a class="reference internal" href="#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> object can be instructed to load
information about itself from the corresponding database schema object already
existing within the database. This process is called <em>reflection</em>. Most simply
you need only specify the table name, a <a class="reference internal" href="#sqlalchemy.schema.MetaData" title="sqlalchemy.schema.MetaData"><tt class="xref py py-class docutils literal"><span class="pre">MetaData</span></tt></a>
object, and the <tt class="docutils literal"><span class="pre">autoload=True</span></tt> flag. If the
<a class="reference internal" href="#sqlalchemy.schema.MetaData" title="sqlalchemy.schema.MetaData"><tt class="xref py py-class docutils literal"><span class="pre">MetaData</span></tt></a> is not persistently bound, also add the
<tt class="docutils literal"><span class="pre">autoload_with</span></tt> argument:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">messages</span> <span class="o">=</span> <span class="n">Table</span><span class="p">(</span><span class="s">&#39;messages&#39;</span><span class="p">,</span> <span class="n">meta</span><span class="p">,</span> <span class="n">autoload</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">autoload_with</span><span class="o">=</span><span class="n">engine</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">[</span><span class="n">c</span><span class="o">.</span><span class="n">name</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">messages</span><span class="o">.</span><span class="n">columns</span><span class="p">]</span>
<span class="go">[&#39;message_id&#39;, &#39;message_name&#39;, &#39;date&#39;]</span></pre></div>
</div>
<p>The above operation will use the given engine to query the database for
information about the <tt class="docutils literal"><span class="pre">messages</span></tt> table, and will then generate
<a class="reference internal" href="#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="#sqlalchemy.schema.ForeignKey" title="sqlalchemy.schema.ForeignKey"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></tt></a>,
and other objects corresponding to this information as though the
<a class="reference internal" href="#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> object were hand-constructed in Python.</p>
<p>When tables are reflected, if a given table references another one via foreign
key, a second <a class="reference internal" href="#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> object is created within the
<a class="reference internal" href="#sqlalchemy.schema.MetaData" title="sqlalchemy.schema.MetaData"><tt class="xref py py-class docutils literal"><span class="pre">MetaData</span></tt></a> object representing the connection.
Below, assume the table <tt class="docutils literal"><span class="pre">shopping_cart_items</span></tt> references a table named
<tt class="docutils literal"><span class="pre">shopping_carts</span></tt>. Reflecting the <tt class="docutils literal"><span class="pre">shopping_cart_items</span></tt> table has the
effect such that the <tt class="docutils literal"><span class="pre">shopping_carts</span></tt> table will also be loaded:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">shopping_cart_items</span> <span class="o">=</span> <span class="n">Table</span><span class="p">(</span><span class="s">&#39;shopping_cart_items&#39;</span><span class="p">,</span> <span class="n">meta</span><span class="p">,</span> <span class="n">autoload</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">autoload_with</span><span class="o">=</span><span class="n">engine</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="s">&#39;shopping_carts&#39;</span> <span class="ow">in</span> <span class="n">meta</span><span class="o">.</span><span class="n">tables</span><span class="p">:</span>
<span class="go">True</span></pre></div>
</div>
<p>The <a class="reference internal" href="#sqlalchemy.schema.MetaData" title="sqlalchemy.schema.MetaData"><tt class="xref py py-class docutils literal"><span class="pre">MetaData</span></tt></a> has an interesting &#8220;singleton-like&#8221;
behavior such that if you requested both tables individually,
<a class="reference internal" href="#sqlalchemy.schema.MetaData" title="sqlalchemy.schema.MetaData"><tt class="xref py py-class docutils literal"><span class="pre">MetaData</span></tt></a> will ensure that exactly one
<a class="reference internal" href="#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> object is created for each distinct table
name. The <a class="reference internal" href="#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> constructor actually returns to
you the already-existing <a class="reference internal" href="#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> object if one
already exists with the given name. Such as below, we can access the already
generated <tt class="docutils literal"><span class="pre">shopping_carts</span></tt> table just by naming it:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">shopping_carts</span> <span class="o">=</span> <span class="n">Table</span><span class="p">(</span><span class="s">&#39;shopping_carts&#39;</span><span class="p">,</span> <span class="n">meta</span><span class="p">)</span></pre></div>
</div>
<p>Of course, it&#8217;s a good idea to use <tt class="docutils literal"><span class="pre">autoload=True</span></tt> with the above table
regardless. This is so that the table&#8217;s attributes will be loaded if they have
not been already. The autoload operation only occurs for the table if it
hasn&#8217;t already been loaded; once loaded, new calls to
<a class="reference internal" href="#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> with the same name will not re-issue any
reflection queries.</p>
<div class="section" id="overriding-reflected-columns">
<h3>Overriding Reflected Columns<a class="headerlink" href="#overriding-reflected-columns" title="Permalink to this headline">¶</a></h3>
<p>Individual columns can be overridden with explicit values when reflecting
tables; this is handy for specifying custom datatypes, constraints such as
primary keys that may not be configured within the database, etc.:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </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="gp">... </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="c"># override reflected &#39;id&#39; to have primary key</span>
<span class="gp">... </span><span class="n">Column</span><span class="p">(</span><span class="s">&#39;mydata&#39;</span><span class="p">,</span> <span class="n">Unicode</span><span class="p">(</span><span class="mi">50</span><span class="p">)),</span>    <span class="c"># override reflected &#39;mydata&#39; to be Unicode</span>
<span class="gp">... </span><span class="n">autoload</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span></pre></div>
</div>
</div>
<div class="section" id="reflecting-views">
<h3>Reflecting Views<a class="headerlink" href="#reflecting-views" title="Permalink to this headline">¶</a></h3>
<p>The reflection system can also reflect views. Basic usage is the same as that
of a table:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">my_view</span> <span class="o">=</span> <span class="n">Table</span><span class="p">(</span><span class="s">&quot;some_view&quot;</span><span class="p">,</span> <span class="n">metadata</span><span class="p">,</span> <span class="n">autoload</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span></pre></div>
</div>
<p>Above, <tt class="docutils literal"><span class="pre">my_view</span></tt> is a <a class="reference internal" href="#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> object with
<a class="reference internal" href="#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a> objects representing the names and types of
each column within the view &#8220;some_view&#8221;.</p>
<p>Usually, it&#8217;s desired to have at least a primary key constraint when
reflecting a view, if not foreign keys as well. View reflection doesn&#8217;t
extrapolate these constraints.</p>
<p>Use the &#8220;override&#8221; technique for this, specifying explicitly those columns
which are part of the primary key or have foreign key constraints:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">my_view</span> <span class="o">=</span> <span class="n">Table</span><span class="p">(</span><span class="s">&quot;some_view&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;view_id&quot;</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">&quot;related_thing&quot;</span><span class="p">,</span> <span class="n">Integer</span><span class="p">,</span> <span class="n">ForeignKey</span><span class="p">(</span><span class="s">&quot;othertable.thing_id&quot;</span><span class="p">)),</span>
                <span class="n">autoload</span><span class="o">=</span><span class="bp">True</span>
<span class="p">)</span></pre></div>
</div>
</div>
<div class="section" id="reflecting-all-tables-at-once">
<h3>Reflecting All Tables at Once<a class="headerlink" href="#reflecting-all-tables-at-once" title="Permalink to this headline">¶</a></h3>
<p>The <a class="reference internal" href="#sqlalchemy.schema.MetaData" title="sqlalchemy.schema.MetaData"><tt class="xref py py-class docutils literal"><span class="pre">MetaData</span></tt></a> object can also get a listing of
tables and reflect the full set. This is achieved by using the
<a class="reference internal" href="#sqlalchemy.schema.MetaData.reflect" title="sqlalchemy.schema.MetaData.reflect"><tt class="xref py py-func docutils literal"><span class="pre">reflect()</span></tt></a> method. After calling it, all
located tables are present within the <a class="reference internal" href="#sqlalchemy.schema.MetaData" title="sqlalchemy.schema.MetaData"><tt class="xref py py-class docutils literal"><span class="pre">MetaData</span></tt></a>
object&#8217;s dictionary of tables:</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">meta</span><span class="o">.</span><span class="n">reflect</span><span class="p">(</span><span class="n">bind</span><span class="o">=</span><span class="n">someengine</span><span class="p">)</span>
<span class="n">users_table</span> <span class="o">=</span> <span class="n">meta</span><span class="o">.</span><span class="n">tables</span><span class="p">[</span><span class="s">&#39;users&#39;</span><span class="p">]</span>
<span class="n">addresses_table</span> <span class="o">=</span> <span class="n">meta</span><span class="o">.</span><span class="n">tables</span><span class="p">[</span><span class="s">&#39;addresses&#39;</span><span class="p">]</span></pre></div>
</div>
<p><tt class="docutils literal"><span class="pre">metadata.reflect()</span></tt> also provides a handy way to clear or delete all the rows in a database:</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">meta</span><span class="o">.</span><span class="n">reflect</span><span class="p">(</span><span class="n">bind</span><span class="o">=</span><span class="n">someengine</span><span class="p">)</span>
<span class="k">for</span> <span class="n">table</span> <span class="ow">in</span> <span class="nb">reversed</span><span class="p">(</span><span class="n">meta</span><span class="o">.</span><span class="n">sorted_tables</span><span class="p">):</span>
    <span class="n">someengine</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="n">table</span><span class="o">.</span><span class="n">delete</span><span class="p">())</span></pre></div>
</div>
</div>
<div class="section" id="fine-grained-reflection-with-inspector">
<h3>Fine Grained Reflection with Inspector<a class="headerlink" href="#fine-grained-reflection-with-inspector" title="Permalink to this headline">¶</a></h3>
<p>A low level interface which provides a backend-agnostic system of loading
lists of schema, table, column, and constraint descriptions from a given
database is also available. This is known as the &#8220;Inspector&#8221;:</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">create_engine</span>
<span class="kn">from</span> <span class="nn">sqlalchemy.engine</span> <span class="kn">import</span> <span class="n">reflection</span>
<span class="n">engine</span> <span class="o">=</span> <span class="n">create_engine</span><span class="p">(</span><span class="s">&#39;...&#39;</span><span class="p">)</span>
<span class="n">insp</span> <span class="o">=</span> <span class="n">reflection</span><span class="o">.</span><span class="n">Inspector</span><span class="o">.</span><span class="n">from_engine</span><span class="p">(</span><span class="n">engine</span><span class="p">)</span>
<span class="k">print</span> <span class="n">insp</span><span class="o">.</span><span class="n">get_table_names</span><span class="p">()</span></pre></div>
</div>
<dl class="class">
<dt id="sqlalchemy.engine.reflection.Inspector">
<em class="property">class </em><tt class="descclassname">sqlalchemy.engine.reflection.</tt><tt class="descname">Inspector</tt><big>(</big><em>bind</em><big>)</big><a class="headerlink" href="#sqlalchemy.engine.reflection.Inspector" title="Permalink to this definition">¶</a></dt>
<dd><p>Bases: <tt class="xref py py-class docutils literal"><span class="pre">object</span></tt></p>
<p>Performs database schema inspection.</p>
<p>The Inspector acts as a proxy to the reflection methods of the
<tt class="xref py py-class docutils literal"><span class="pre">Dialect</span></tt>, providing a
consistent interface as well as caching support for previously
fetched metadata.</p>
<p>The preferred method to construct an <a class="reference internal" href="#sqlalchemy.engine.reflection.Inspector" title="sqlalchemy.engine.reflection.Inspector"><tt class="xref py py-class docutils literal"><span class="pre">Inspector</span></tt></a> is via the
<tt class="xref py py-meth docutils literal"><span class="pre">Inspector.from_engine()</span></tt> method.   I.e.:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">engine</span> <span class="o">=</span> <span class="n">create_engine</span><span class="p">(</span><span class="s">&#39;...&#39;</span><span class="p">)</span>
<span class="n">insp</span> <span class="o">=</span> <span class="n">Inspector</span><span class="o">.</span><span class="n">from_engine</span><span class="p">(</span><span class="n">engine</span><span class="p">)</span></pre></div>
</div>
<p>Where above, the <tt class="xref py py-class docutils literal"><span class="pre">Dialect</span></tt> may opt
to return an <a class="reference internal" href="#sqlalchemy.engine.reflection.Inspector" title="sqlalchemy.engine.reflection.Inspector"><tt class="xref py py-class docutils literal"><span class="pre">Inspector</span></tt></a> subclass that provides additional
methods specific to the dialect&#8217;s target database.</p>
<dl class="method">
<dt id="sqlalchemy.engine.reflection.Inspector.__init__">
<tt class="descname">__init__</tt><big>(</big><em>bind</em><big>)</big><a class="headerlink" href="#sqlalchemy.engine.reflection.Inspector.__init__" title="Permalink to this definition">¶</a></dt>
<dd><p>Initialize a new <a class="reference internal" href="#sqlalchemy.engine.reflection.Inspector" title="sqlalchemy.engine.reflection.Inspector"><tt class="xref py py-class docutils literal"><span class="pre">Inspector</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"><th class="field-name">Parameters:</th><td class="field-body"><strong>bind</strong> &#8211; a <a class="reference internal" href="connections.html#sqlalchemy.engine.base.Connectable" title="sqlalchemy.engine.base.Connectable"><tt class="xref py py-class docutils literal"><span class="pre">Connectable</span></tt></a>, 
which is typically an instance of 
<a class="reference internal" href="connections.html#sqlalchemy.engine.base.Engine" title="sqlalchemy.engine.base.Engine"><tt class="xref py py-class docutils literal"><span class="pre">Engine</span></tt></a> or 
<a class="reference internal" href="connections.html#sqlalchemy.engine.base.Connection" title="sqlalchemy.engine.base.Connection"><tt class="xref py py-class docutils literal"><span class="pre">Connection</span></tt></a>.</td>
</tr>
</tbody>
</table>
<p>For a dialect-specific instance of <a class="reference internal" href="#sqlalchemy.engine.reflection.Inspector" title="sqlalchemy.engine.reflection.Inspector"><tt class="xref py py-class docutils literal"><span class="pre">Inspector</span></tt></a>, see
<tt class="xref py py-meth docutils literal"><span class="pre">Inspector.from_engine()</span></tt></p>
</dd></dl>

<dl class="attribute">
<dt id="sqlalchemy.engine.reflection.Inspector.default_schema_name">
<tt class="descname">default_schema_name</tt><a class="headerlink" href="#sqlalchemy.engine.reflection.Inspector.default_schema_name" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the default schema name presented by the dialect
for the current engine&#8217;s database user.</p>
<p>E.g. this is typically <tt class="docutils literal"><span class="pre">public</span></tt> for Postgresql and <tt class="docutils literal"><span class="pre">dbo</span></tt>
for SQL Server.</p>
</dd></dl>

<dl class="classmethod">
<dt id="sqlalchemy.engine.reflection.Inspector.from_engine">
<em class="property">classmethod </em><tt class="descname">from_engine</tt><big>(</big><em>bind</em><big>)</big><a class="headerlink" href="#sqlalchemy.engine.reflection.Inspector.from_engine" title="Permalink to this definition">¶</a></dt>
<dd><p>Construct a new dialect-specific Inspector object from the given engine or connection.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Parameters:</th><td class="field-body"><strong>bind</strong> &#8211; a <a class="reference internal" href="connections.html#sqlalchemy.engine.base.Connectable" title="sqlalchemy.engine.base.Connectable"><tt class="xref py py-class docutils literal"><span class="pre">Connectable</span></tt></a>, 
which is typically an instance of 
<a class="reference internal" href="connections.html#sqlalchemy.engine.base.Engine" title="sqlalchemy.engine.base.Engine"><tt class="xref py py-class docutils literal"><span class="pre">Engine</span></tt></a> or 
<a class="reference internal" href="connections.html#sqlalchemy.engine.base.Connection" title="sqlalchemy.engine.base.Connection"><tt class="xref py py-class docutils literal"><span class="pre">Connection</span></tt></a>.</td>
</tr>
</tbody>
</table>
<p>This method differs from direct a direct constructor call of <a class="reference internal" href="#sqlalchemy.engine.reflection.Inspector" title="sqlalchemy.engine.reflection.Inspector"><tt class="xref py py-class docutils literal"><span class="pre">Inspector</span></tt></a>
in that the <tt class="xref py py-class docutils literal"><span class="pre">Dialect</span></tt> is given a chance to provide
a dialect-specific <a class="reference internal" href="#sqlalchemy.engine.reflection.Inspector" title="sqlalchemy.engine.reflection.Inspector"><tt class="xref py py-class docutils literal"><span class="pre">Inspector</span></tt></a> instance, which may provide additional
methods.</p>
<p>See the example at <a class="reference internal" href="#sqlalchemy.engine.reflection.Inspector" title="sqlalchemy.engine.reflection.Inspector"><tt class="xref py py-class docutils literal"><span class="pre">Inspector</span></tt></a>.</p>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.engine.reflection.Inspector.get_columns">
<tt class="descname">get_columns</tt><big>(</big><em>table_name</em>, <em>schema=None</em>, <em>**kw</em><big>)</big><a class="headerlink" href="#sqlalchemy.engine.reflection.Inspector.get_columns" title="Permalink to this definition">¶</a></dt>
<dd><p>Return information about columns in <cite>table_name</cite>.</p>
<p>Given a string <cite>table_name</cite> and an optional string <cite>schema</cite>, return
column information as a list of dicts with these keys:</p>
<dl class="docutils">
<dt>name</dt>
<dd>the column&#8217;s name</dd>
<dt>type</dt>
<dd><a class="reference internal" href="types.html#sqlalchemy.types.TypeEngine" title="sqlalchemy.types.TypeEngine"><tt class="xref py py-class docutils literal"><span class="pre">TypeEngine</span></tt></a></dd>
<dt>nullable</dt>
<dd>boolean</dd>
<dt>default</dt>
<dd>the column&#8217;s default value</dd>
<dt>attrs</dt>
<dd>dict containing optional column attributes</dd>
</dl>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.engine.reflection.Inspector.get_foreign_keys">
<tt class="descname">get_foreign_keys</tt><big>(</big><em>table_name</em>, <em>schema=None</em>, <em>**kw</em><big>)</big><a class="headerlink" href="#sqlalchemy.engine.reflection.Inspector.get_foreign_keys" title="Permalink to this definition">¶</a></dt>
<dd><p>Return information about foreign_keys in <cite>table_name</cite>.</p>
<p>Given a string <cite>table_name</cite>, and an optional string <cite>schema</cite>, return
foreign key information as a list of dicts with these keys:</p>
<dl class="docutils">
<dt>constrained_columns</dt>
<dd>a list of column names that make up the foreign key</dd>
<dt>referred_schema</dt>
<dd>the name of the referred schema</dd>
<dt>referred_table</dt>
<dd>the name of the referred table</dd>
<dt>referred_columns</dt>
<dd>a list of column names in the referred table that correspond to
constrained_columns</dd>
<dt>name</dt>
<dd>optional name of the foreign key constraint.</dd>
<dt>**kw</dt>
<dd>other options passed to the dialect&#8217;s get_foreign_keys() method.</dd>
</dl>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.engine.reflection.Inspector.get_indexes">
<tt class="descname">get_indexes</tt><big>(</big><em>table_name</em>, <em>schema=None</em>, <em>**kw</em><big>)</big><a class="headerlink" href="#sqlalchemy.engine.reflection.Inspector.get_indexes" title="Permalink to this definition">¶</a></dt>
<dd><p>Return information about indexes in <cite>table_name</cite>.</p>
<p>Given a string <cite>table_name</cite> and an optional string <cite>schema</cite>, return
index information as a list of dicts with these keys:</p>
<dl class="docutils">
<dt>name</dt>
<dd>the index&#8217;s name</dd>
<dt>column_names</dt>
<dd>list of column names in order</dd>
<dt>unique</dt>
<dd>boolean</dd>
<dt>**kw</dt>
<dd>other options passed to the dialect&#8217;s get_indexes() method.</dd>
</dl>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.engine.reflection.Inspector.get_pk_constraint">
<tt class="descname">get_pk_constraint</tt><big>(</big><em>table_name</em>, <em>schema=None</em>, <em>**kw</em><big>)</big><a class="headerlink" href="#sqlalchemy.engine.reflection.Inspector.get_pk_constraint" title="Permalink to this definition">¶</a></dt>
<dd><p>Return information about primary key constraint on <cite>table_name</cite>.</p>
<p>Given a string <cite>table_name</cite>, and an optional string <cite>schema</cite>, return
primary key information as a dictionary with these keys:</p>
<dl class="docutils">
<dt>constrained_columns</dt>
<dd>a list of column names that make up the primary key</dd>
<dt>name</dt>
<dd>optional name of the primary key constraint.</dd>
</dl>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.engine.reflection.Inspector.get_primary_keys">
<tt class="descname">get_primary_keys</tt><big>(</big><em>table_name</em>, <em>schema=None</em>, <em>**kw</em><big>)</big><a class="headerlink" href="#sqlalchemy.engine.reflection.Inspector.get_primary_keys" title="Permalink to this definition">¶</a></dt>
<dd><p>Return information about primary keys in <cite>table_name</cite>.</p>
<p>Given a string <cite>table_name</cite>, and an optional string <cite>schema</cite>, return
primary key information as a list of column names.</p>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.engine.reflection.Inspector.get_schema_names">
<tt class="descname">get_schema_names</tt><big>(</big><big>)</big><a class="headerlink" href="#sqlalchemy.engine.reflection.Inspector.get_schema_names" title="Permalink to this definition">¶</a></dt>
<dd><p>Return all schema names.</p>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.engine.reflection.Inspector.get_table_names">
<tt class="descname">get_table_names</tt><big>(</big><em>schema=None</em>, <em>order_by=None</em><big>)</big><a class="headerlink" href="#sqlalchemy.engine.reflection.Inspector.get_table_names" title="Permalink to this definition">¶</a></dt>
<dd><p>Return all table names in <cite>schema</cite>.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>schema</strong> &#8211; Optional, retrieve names from a non-default schema.</li>
<li><strong>order_by</strong> &#8211; Optional, may be the string &#8220;foreign_key&#8221; to sort
the result on foreign key dependencies.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<p>This should probably not return view names or maybe it should return
them with an indicator t or v.</p>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.engine.reflection.Inspector.get_table_options">
<tt class="descname">get_table_options</tt><big>(</big><em>table_name</em>, <em>schema=None</em>, <em>**kw</em><big>)</big><a class="headerlink" href="#sqlalchemy.engine.reflection.Inspector.get_table_options" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a dictionary of options specified when the table of the given name was created.</p>
<p>This currently includes some options that apply to MySQL tables.</p>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.engine.reflection.Inspector.get_view_definition">
<tt class="descname">get_view_definition</tt><big>(</big><em>view_name</em>, <em>schema=None</em><big>)</big><a class="headerlink" href="#sqlalchemy.engine.reflection.Inspector.get_view_definition" title="Permalink to this definition">¶</a></dt>
<dd><p>Return definition for <cite>view_name</cite>.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Parameters:</th><td class="field-body"><strong>schema</strong> &#8211; Optional, retrieve names from a non-default schema.</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.engine.reflection.Inspector.get_view_names">
<tt class="descname">get_view_names</tt><big>(</big><em>schema=None</em><big>)</big><a class="headerlink" href="#sqlalchemy.engine.reflection.Inspector.get_view_names" title="Permalink to this definition">¶</a></dt>
<dd><p>Return all view names in <cite>schema</cite>.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Parameters:</th><td class="field-body"><strong>schema</strong> &#8211; Optional, retrieve names from a non-default schema.</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.engine.reflection.Inspector.reflecttable">
<tt class="descname">reflecttable</tt><big>(</big><em>table</em>, <em>include_columns</em><big>)</big><a class="headerlink" href="#sqlalchemy.engine.reflection.Inspector.reflecttable" title="Permalink to this definition">¶</a></dt>
<dd><p>Given a Table object, load its internal constructs based on introspection.</p>
<p>This is the underlying method used by most dialects to produce 
table reflection.  Direct usage is like:</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">create_engine</span><span class="p">,</span> <span class="n">MetaData</span><span class="p">,</span> <span class="n">Table</span>
<span class="kn">from</span> <span class="nn">sqlalchemy.engine</span> <span class="kn">import</span> <span class="n">reflection</span>

<span class="n">engine</span> <span class="o">=</span> <span class="n">create_engine</span><span class="p">(</span><span class="s">&#39;...&#39;</span><span class="p">)</span>
<span class="n">meta</span> <span class="o">=</span> <span class="n">MetaData</span><span class="p">()</span>
<span class="n">user_table</span> <span class="o">=</span> <span class="n">Table</span><span class="p">(</span><span class="s">&#39;user&#39;</span><span class="p">,</span> <span class="n">meta</span><span class="p">)</span>
<span class="n">insp</span> <span class="o">=</span> <span class="n">Inspector</span><span class="o">.</span><span class="n">from_engine</span><span class="p">(</span><span class="n">engine</span><span class="p">)</span>
<span class="n">insp</span><span class="o">.</span><span class="n">reflecttable</span><span class="p">(</span><span class="n">user_table</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span></pre></div>
</div>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>table</strong> &#8211; a <a class="reference internal" href="#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> instance.</li>
<li><strong>include_columns</strong> &#8211; a list of string column names to include
in the reflection process.  If <tt class="xref docutils literal"><span class="pre">None</span></tt>, all columns are reflected.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

</dd></dl>

</div>
</div>
<div class="section" id="column-insert-update-defaults">
<span id="metadata-defaults"></span><h2>Column Insert/Update Defaults<a class="headerlink" href="#column-insert-update-defaults" title="Permalink to this headline">¶</a></h2>
<p>SQLAlchemy provides a very rich featureset regarding column level events which
take place during INSERT and UPDATE statements. Options include:</p>
<ul class="simple">
<li>Scalar values used as defaults during INSERT and UPDATE operations</li>
<li>Python functions which execute upon INSERT and UPDATE operations</li>
<li>SQL expressions which are embedded in INSERT statements (or in some cases execute beforehand)</li>
<li>SQL expressions which are embedded in UPDATE statements</li>
<li>Server side default values used during INSERT</li>
<li>Markers for server-side triggers used during UPDATE</li>
</ul>
<p>The general rule for all insert/update defaults is that they only take effect
if no value for a particular column is passed as an <tt class="docutils literal"><span class="pre">execute()</span></tt> parameter;
otherwise, the given value is used.</p>
<div class="section" id="scalar-defaults">
<h3>Scalar Defaults<a class="headerlink" href="#scalar-defaults" title="Permalink to this headline">¶</a></h3>
<p>The simplest kind of default is a scalar value used as the default value of a column:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">Table</span><span class="p">(</span><span class="s">&quot;mytable&quot;</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">&quot;somecolumn&quot;</span><span class="p">,</span> <span class="n">Integer</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="mi">12</span><span class="p">)</span>
<span class="p">)</span></pre></div>
</div>
<p>Above, the value &#8220;12&#8221; will be bound as the column value during an INSERT if no
other value is supplied.</p>
<p>A scalar value may also be associated with an UPDATE statement, though this is
not very common (as UPDATE statements are usually looking for dynamic
defaults):</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">Table</span><span class="p">(</span><span class="s">&quot;mytable&quot;</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">&quot;somecolumn&quot;</span><span class="p">,</span> <span class="n">Integer</span><span class="p">,</span> <span class="n">onupdate</span><span class="o">=</span><span class="mi">25</span><span class="p">)</span>
<span class="p">)</span></pre></div>
</div>
</div>
<div class="section" id="python-executed-functions">
<h3>Python-Executed Functions<a class="headerlink" href="#python-executed-functions" title="Permalink to this headline">¶</a></h3>
<p>The <tt class="docutils literal"><span class="pre">default</span></tt> and <tt class="docutils literal"><span class="pre">onupdate</span></tt> keyword arguments also accept Python
functions. These functions are invoked at the time of insert or update if no
other value for that column is supplied, and the value returned is used for
the column&#8217;s value. Below illustrates a crude &#8220;sequence&#8221; that assigns an
incrementing counter to a primary key column:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># a function which counts upwards</span>
<span class="n">i</span> <span class="o">=</span> <span class="mi">0</span>
<span class="k">def</span> <span class="nf">mydefault</span><span class="p">():</span>
    <span class="k">global</span> <span class="n">i</span>
    <span class="n">i</span> <span class="o">+=</span> <span class="mi">1</span>
    <span class="k">return</span> <span class="n">i</span>

<span class="n">t</span> <span class="o">=</span> <span class="n">Table</span><span class="p">(</span><span class="s">&quot;mytable&quot;</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">default</span><span class="o">=</span><span class="n">mydefault</span><span class="p">),</span>
<span class="p">)</span></pre></div>
</div>
<p>It should be noted that for real &#8220;incrementing sequence&#8221; behavior, the
built-in capabilities of the database should normally be used, which may
include sequence objects or other autoincrementing capabilities. For primary
key columns, SQLAlchemy will in most cases use these capabilities
automatically. See the API documentation for
<a class="reference internal" href="#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a> including the <tt class="docutils literal"><span class="pre">autoincrement</span></tt> flag, as
well as the section on <a class="reference internal" href="#sqlalchemy.schema.Sequence" title="sqlalchemy.schema.Sequence"><tt class="xref py py-class docutils literal"><span class="pre">Sequence</span></tt></a> later in this
chapter for background on standard primary key generation techniques.</p>
<p>To illustrate onupdate, we assign the Python <tt class="docutils literal"><span class="pre">datetime</span></tt> function <tt class="docutils literal"><span class="pre">now</span></tt> to
the <tt class="docutils literal"><span class="pre">onupdate</span></tt> attribute:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">datetime</span>

<span class="n">t</span> <span class="o">=</span> <span class="n">Table</span><span class="p">(</span><span class="s">&quot;mytable&quot;</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="c"># define &#39;last_updated&#39; to be populated with datetime.now()</span>
    <span class="n">Column</span><span class="p">(</span><span class="s">&#39;last_updated&#39;</span><span class="p">,</span> <span class="n">DateTime</span><span class="p">,</span> <span class="n">onupdate</span><span class="o">=</span><span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">),</span>
<span class="p">)</span></pre></div>
</div>
<p>When an update statement executes and no value is passed for <tt class="docutils literal"><span class="pre">last_updated</span></tt>,
the <tt class="docutils literal"><span class="pre">datetime.datetime.now()</span></tt> Python function is executed and its return
value used as the value for <tt class="docutils literal"><span class="pre">last_updated</span></tt>. Notice that we provide <tt class="docutils literal"><span class="pre">now</span></tt>
as the function itself without calling it (i.e. there are no parenthesis
following) - SQLAlchemy will execute the function at the time the statement
executes.</p>
<div class="section" id="context-sensitive-default-functions">
<h4>Context-Sensitive Default Functions<a class="headerlink" href="#context-sensitive-default-functions" title="Permalink to this headline">¶</a></h4>
<p>The Python functions used by <tt class="docutils literal"><span class="pre">default</span></tt> and <tt class="docutils literal"><span class="pre">onupdate</span></tt> may also make use of
the current statement&#8217;s context in order to determine a value. The <cite>context</cite>
of a statement is an internal SQLAlchemy object which contains all information
about the statement being executed, including its source expression, the
parameters associated with it and the cursor. The typical use case for this
context with regards to default generation is to have access to the other
values being inserted or updated on the row. To access the context, provide a
function that accepts a single <tt class="docutils literal"><span class="pre">context</span></tt> argument:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">mydefault</span><span class="p">(</span><span class="n">context</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">context</span><span class="o">.</span><span class="n">current_parameters</span><span class="p">[</span><span class="s">&#39;counter&#39;</span><span class="p">]</span> <span class="o">+</span> <span class="mi">12</span>

<span class="n">t</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;counter&#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;counter_plus_twelve&#39;</span><span class="p">,</span> <span class="n">Integer</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="n">mydefault</span><span class="p">,</span> <span class="n">onupdate</span><span class="o">=</span><span class="n">mydefault</span><span class="p">)</span>
<span class="p">)</span></pre></div>
</div>
<p>Above we illustrate a default function which will execute for all INSERT and
UPDATE statements where a value for <tt class="docutils literal"><span class="pre">counter_plus_twelve</span></tt> was otherwise not
provided, and the value will be that of whatever value is present in the
execution for the <tt class="docutils literal"><span class="pre">counter</span></tt> column, plus the number 12.</p>
<p>While the context object passed to the default function has many attributes,
the <tt class="docutils literal"><span class="pre">current_parameters</span></tt> member is a special member provided only during the
execution of a default function for the purposes of deriving defaults from its
existing values. For a single statement that is executing many sets of bind
parameters, the user-defined function is called for each set of parameters,
and <tt class="docutils literal"><span class="pre">current_parameters</span></tt> will be provided with each individual parameter set
for each execution.</p>
</div>
</div>
<div class="section" id="sql-expressions">
<h3>SQL Expressions<a class="headerlink" href="#sql-expressions" title="Permalink to this headline">¶</a></h3>
<p>The &#8220;default&#8221; and &#8220;onupdate&#8221; keywords may also be passed SQL expressions,
including select statements or direct function calls:</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;mytable&quot;</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="c"># define &#39;create_date&#39; to default to now()</span>
    <span class="n">Column</span><span class="p">(</span><span class="s">&#39;create_date&#39;</span><span class="p">,</span> <span class="n">DateTime</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="n">func</span><span class="o">.</span><span class="n">now</span><span class="p">()),</span>

    <span class="c"># define &#39;key&#39; to pull its default from the &#39;keyvalues&#39; table</span>
    <span class="n">Column</span><span class="p">(</span><span class="s">&#39;key&#39;</span><span class="p">,</span> <span class="n">String</span><span class="p">(</span><span class="mi">20</span><span class="p">),</span> <span class="n">default</span><span class="o">=</span><span class="n">keyvalues</span><span class="o">.</span><span class="n">select</span><span class="p">(</span><span class="n">keyvalues</span><span class="o">.</span><span class="n">c</span><span class="o">.</span><span class="n">type</span><span class="o">=</span><span class="s">&#39;type1&#39;</span><span class="p">,</span> <span class="n">limit</span><span class="o">=</span><span class="mi">1</span><span class="p">)),</span>

    <span class="c"># define &#39;last_modified&#39; to use the current_timestamp SQL function on update</span>
    <span class="n">Column</span><span class="p">(</span><span class="s">&#39;last_modified&#39;</span><span class="p">,</span> <span class="n">DateTime</span><span class="p">,</span> <span class="n">onupdate</span><span class="o">=</span><span class="n">func</span><span class="o">.</span><span class="n">utc_timestamp</span><span class="p">())</span>
    <span class="p">)</span></pre></div>
</div>
<p>Above, the <tt class="docutils literal"><span class="pre">create_date</span></tt> column will be populated with the result of the
<tt class="docutils literal"><span class="pre">now()</span></tt> SQL function (which, depending on backend, compiles into <tt class="docutils literal"><span class="pre">NOW()</span></tt>
or <tt class="docutils literal"><span class="pre">CURRENT_TIMESTAMP</span></tt> in most cases) during an INSERT statement, and the
<tt class="docutils literal"><span class="pre">key</span></tt> column with the result of a SELECT subquery from another table. The
<tt class="docutils literal"><span class="pre">last_modified</span></tt> column will be populated with the value of
<tt class="docutils literal"><span class="pre">UTC_TIMESTAMP()</span></tt>, a function specific to MySQL, when an UPDATE statement is
emitted for this table.</p>
<p>Note that when using <tt class="docutils literal"><span class="pre">func</span></tt> functions, unlike when using Python <cite>datetime</cite>
functions we <em>do</em> call the function, i.e. with parenthesis &#8220;()&#8221; - this is
because what we want in this case is the return value of the function, which
is the SQL expression construct that will be rendered into the INSERT or
UPDATE statement.</p>
<p>The above SQL functions are usually executed &#8220;inline&#8221; with the INSERT or
UPDATE statement being executed, meaning, a single statement is executed which
embeds the given expressions or subqueries within the VALUES or SET clause of
the statement. Although in some cases, the function is &#8220;pre-executed&#8221; in a
SELECT statement of its own beforehand. This happens when all of the following
is true:</p>
<ul class="simple">
<li>the column is a primary key column</li>
<li>the database dialect does not support a usable <tt class="docutils literal"><span class="pre">cursor.lastrowid</span></tt> accessor
(or equivalent); this currently includes PostgreSQL, Oracle, and Firebird, as
well as some MySQL dialects.</li>
<li>the dialect does not support the &#8220;RETURNING&#8221; clause or similar, or the
<tt class="docutils literal"><span class="pre">implicit_returning</span></tt> flag is set to <tt class="xref docutils literal"><span class="pre">False</span></tt> for the dialect. Dialects
which support RETURNING currently include Postgresql, Oracle, Firebird, and
MS-SQL.</li>
<li>the statement is a single execution, i.e. only supplies one set of
parameters and doesn&#8217;t use &#8220;executemany&#8221; behavior</li>
<li>the <tt class="docutils literal"><span class="pre">inline=True</span></tt> flag is not set on the
<a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.Insert" title="sqlalchemy.sql.expression.Insert"><tt class="xref py py-class docutils literal"><span class="pre">Insert()</span></tt></a> or
<a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.Update" title="sqlalchemy.sql.expression.Update"><tt class="xref py py-class docutils literal"><span class="pre">Update()</span></tt></a> construct, and the statement has
not defined an explicit <cite>returning()</cite> clause.</li>
</ul>
<p>Whether or not the default generation clause &#8220;pre-executes&#8221; is not something
that normally needs to be considered, unless it is being addressed for
performance reasons.</p>
<p>When the statement is executed with a single set of parameters (that is, it is
not an &#8220;executemany&#8221; style execution), the returned
<a class="reference internal" href="connections.html#sqlalchemy.engine.base.ResultProxy" title="sqlalchemy.engine.base.ResultProxy"><tt class="xref py py-class docutils literal"><span class="pre">ResultProxy</span></tt></a> will contain a collection
accessible via <tt class="docutils literal"><span class="pre">result.postfetch_cols()</span></tt> which contains a list of all
<a class="reference internal" href="#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a> objects which had an inline-executed
default. Similarly, all parameters which were bound to the statement,
including all Python and SQL expressions which were pre-executed, are present
in the <tt class="docutils literal"><span class="pre">last_inserted_params()</span></tt> or <tt class="docutils literal"><span class="pre">last_updated_params()</span></tt> collections on
<a class="reference internal" href="connections.html#sqlalchemy.engine.base.ResultProxy" title="sqlalchemy.engine.base.ResultProxy"><tt class="xref py py-class docutils literal"><span class="pre">ResultProxy</span></tt></a>. The <tt class="docutils literal"><span class="pre">inserted_primary_key</span></tt>
collection contains a list of primary key values for the row inserted (a list
so that single-column and composite-column primary keys are represented in the
same format).</p>
</div>
<div class="section" id="server-side-defaults">
<h3>Server Side Defaults<a class="headerlink" href="#server-side-defaults" title="Permalink to this headline">¶</a></h3>
<p>A variant on the SQL expression default is the <tt class="docutils literal"><span class="pre">server_default</span></tt>, which gets
placed in the CREATE TABLE statement during a <tt class="docutils literal"><span class="pre">create()</span></tt> operation:</p>
<div class="highlight-python+sql"><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">&#39;test&#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;abc&#39;</span><span class="p">,</span> <span class="n">String</span><span class="p">(</span><span class="mi">20</span><span class="p">),</span> <span class="n">server_default</span><span class="o">=</span><span class="s">&#39;abc&#39;</span><span class="p">),</span>
    <span class="n">Column</span><span class="p">(</span><span class="s">&#39;created_at&#39;</span><span class="p">,</span> <span class="n">DateTime</span><span class="p">,</span> <span class="n">server_default</span><span class="o">=</span><span class="n">text</span><span class="p">(</span><span class="s">&quot;sysdate&quot;</span><span class="p">))</span>
<span class="p">)</span></pre></div>
</div>
<p>A create call for the above table will produce:</p>
<div class="highlight-python"><pre>CREATE TABLE test (
    abc varchar(20) default 'abc',
    created_at datetime default sysdate
)</pre>
</div>
<p>The behavior of <tt class="docutils literal"><span class="pre">server_default</span></tt> is similar to that of a regular SQL
default; if it&#8217;s placed on a primary key column for a database which doesn&#8217;t
have a way to &#8220;postfetch&#8221; the ID, and the statement is not &#8220;inlined&#8221;, the SQL
expression is pre-executed; otherwise, SQLAlchemy lets the default fire off on
the database side normally.</p>
</div>
<div class="section" id="triggered-columns">
<h3>Triggered Columns<a class="headerlink" href="#triggered-columns" title="Permalink to this headline">¶</a></h3>
<p>Columns with values set by a database trigger or other external process may be
called out with a marker:</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">&#39;test&#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;abc&#39;</span><span class="p">,</span> <span class="n">String</span><span class="p">(</span><span class="mi">20</span><span class="p">),</span> <span class="n">server_default</span><span class="o">=</span><span class="n">FetchedValue</span><span class="p">()),</span>
    <span class="n">Column</span><span class="p">(</span><span class="s">&#39;def&#39;</span><span class="p">,</span> <span class="n">String</span><span class="p">(</span><span class="mi">20</span><span class="p">),</span> <span class="n">server_onupdate</span><span class="o">=</span><span class="n">FetchedValue</span><span class="p">())</span>
<span class="p">)</span></pre></div>
</div>
<p>These markers do not emit a &#8220;default&#8221; clause when the table is created,
however they do set the same internal flags as a static <tt class="docutils literal"><span class="pre">server_default</span></tt>
clause, providing hints to higher-level tools that a &#8220;post-fetch&#8221; of these
rows should be performed after an insert or update.</p>
</div>
<div class="section" id="defining-sequences">
<h3>Defining Sequences<a class="headerlink" href="#defining-sequences" title="Permalink to this headline">¶</a></h3>
<p>SQLAlchemy represents database sequences using the
<a class="reference internal" href="#sqlalchemy.schema.Sequence" title="sqlalchemy.schema.Sequence"><tt class="xref py py-class docutils literal"><span class="pre">Sequence</span></tt></a> object, which is considered to be a
special case of &#8220;column default&#8221;. It only has an effect on databases which
have explicit support for sequences, which currently includes Postgresql,
Oracle, and Firebird. The <a class="reference internal" href="#sqlalchemy.schema.Sequence" title="sqlalchemy.schema.Sequence"><tt class="xref py py-class docutils literal"><span class="pre">Sequence</span></tt></a> object is
otherwise ignored.</p>
<p>The <a class="reference internal" href="#sqlalchemy.schema.Sequence" title="sqlalchemy.schema.Sequence"><tt class="xref py py-class docutils literal"><span class="pre">Sequence</span></tt></a> may be placed on any column as a
&#8220;default&#8221; generator to be used during INSERT operations, and can also be
configured to fire off during UPDATE operations if desired. It is most
commonly used in conjunction with a single integer primary key column:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">table</span> <span class="o">=</span> <span class="n">Table</span><span class="p">(</span><span class="s">&quot;cartitems&quot;</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">&quot;cart_id&quot;</span><span class="p">,</span> <span class="n">Integer</span><span class="p">,</span> <span class="n">Sequence</span><span class="p">(</span><span class="s">&#39;cart_id_seq&#39;</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">&quot;description&quot;</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">Column</span><span class="p">(</span><span class="s">&quot;createdate&quot;</span><span class="p">,</span> <span class="n">DateTime</span><span class="p">())</span>
<span class="p">)</span></pre></div>
</div>
<p>Where above, the table &#8220;cartitems&#8221; is associated with a sequence named
&#8220;cart_id_seq&#8221;. When INSERT statements take place for &#8220;cartitems&#8221;, and no value
is passed for the &#8220;cart_id&#8221; column, the &#8220;cart_id_seq&#8221; sequence will be used to
generate a value.</p>
<p>When the <a class="reference internal" href="#sqlalchemy.schema.Sequence" title="sqlalchemy.schema.Sequence"><tt class="xref py py-class docutils literal"><span class="pre">Sequence</span></tt></a> is associated with a table,
CREATE and DROP statements issued for that table will also issue CREATE/DROP
for the sequence object as well, thus &#8220;bundling&#8221; the sequence object with its
parent table.</p>
<p>The <a class="reference internal" href="#sqlalchemy.schema.Sequence" title="sqlalchemy.schema.Sequence"><tt class="xref py py-class docutils literal"><span class="pre">Sequence</span></tt></a> object also implements special
functionality to accommodate Postgresql&#8217;s SERIAL datatype. The SERIAL type in
PG automatically generates a sequence that is used implicitly during inserts.
This means that if a <a class="reference internal" href="#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> object defines a
<a class="reference internal" href="#sqlalchemy.schema.Sequence" title="sqlalchemy.schema.Sequence"><tt class="xref py py-class docutils literal"><span class="pre">Sequence</span></tt></a> on its primary key column so that it
works with Oracle and Firebird, the <a class="reference internal" href="#sqlalchemy.schema.Sequence" title="sqlalchemy.schema.Sequence"><tt class="xref py py-class docutils literal"><span class="pre">Sequence</span></tt></a> would
get in the way of the &#8220;implicit&#8221; sequence that PG would normally use. For this
use case, add the flag <tt class="docutils literal"><span class="pre">optional=True</span></tt> to the
<a class="reference internal" href="#sqlalchemy.schema.Sequence" title="sqlalchemy.schema.Sequence"><tt class="xref py py-class docutils literal"><span class="pre">Sequence</span></tt></a> object - this indicates that the
<a class="reference internal" href="#sqlalchemy.schema.Sequence" title="sqlalchemy.schema.Sequence"><tt class="xref py py-class docutils literal"><span class="pre">Sequence</span></tt></a> should only be used if the database
provides no other option for generating primary key identifiers.</p>
<p>The <a class="reference internal" href="#sqlalchemy.schema.Sequence" title="sqlalchemy.schema.Sequence"><tt class="xref py py-class docutils literal"><span class="pre">Sequence</span></tt></a> object also has the ability to be
executed standalone like a SQL expression, which has the effect of calling its
&#8220;next value&#8221; function:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">seq</span> <span class="o">=</span> <span class="n">Sequence</span><span class="p">(</span><span class="s">&#39;some_sequence&#39;</span><span class="p">)</span>
<span class="n">nextid</span> <span class="o">=</span> <span class="n">connection</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="n">seq</span><span class="p">)</span></pre></div>
</div>
</div>
<div class="section" id="default-geneation-api-constructs">
<h3>Default Geneation API Constructs<a class="headerlink" href="#default-geneation-api-constructs" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="sqlalchemy.schema.ColumnDefault">
<em class="property">class </em><tt class="descclassname">sqlalchemy.schema.</tt><tt class="descname">ColumnDefault</tt><big>(</big><em>arg</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.ColumnDefault" title="Permalink to this definition">¶</a></dt>
<dd><p>Bases: <a class="reference internal" href="#sqlalchemy.schema.DefaultGenerator" title="sqlalchemy.schema.DefaultGenerator"><tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.schema.DefaultGenerator</span></tt></a></p>
<p>A plain default value on a column.</p>
<p>This could correspond to a constant, a callable function, 
or a SQL clause.</p>
<p><a class="reference internal" href="#sqlalchemy.schema.ColumnDefault" title="sqlalchemy.schema.ColumnDefault"><tt class="xref py py-class docutils literal"><span class="pre">ColumnDefault</span></tt></a> is generated automatically
whenever the <tt class="docutils literal"><span class="pre">default</span></tt>, <tt class="docutils literal"><span class="pre">onupdate</span></tt> arguments of
<a class="reference internal" href="#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a> are used.  A <a class="reference internal" href="#sqlalchemy.schema.ColumnDefault" title="sqlalchemy.schema.ColumnDefault"><tt class="xref py py-class docutils literal"><span class="pre">ColumnDefault</span></tt></a>
can be passed positionally as well.</p>
<p>For example, the following:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">Column</span><span class="p">(</span><span class="s">&#39;foo&#39;</span><span class="p">,</span> <span class="n">Integer</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="mi">50</span><span class="p">)</span></pre></div>
</div>
<p>Is equivalent to:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">Column</span><span class="p">(</span><span class="s">&#39;foo&#39;</span><span class="p">,</span> <span class="n">Integer</span><span class="p">,</span> <span class="n">ColumnDefault</span><span class="p">(</span><span class="mi">50</span><span class="p">))</span></pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="sqlalchemy.schema.DefaultClause">
<em class="property">class </em><tt class="descclassname">sqlalchemy.schema.</tt><tt class="descname">DefaultClause</tt><big>(</big><em>arg</em>, <em>for_update=False</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.DefaultClause" title="Permalink to this definition">¶</a></dt>
<dd><p>Bases: <a class="reference internal" href="#sqlalchemy.schema.FetchedValue" title="sqlalchemy.schema.FetchedValue"><tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.schema.FetchedValue</span></tt></a></p>
<p>A DDL-specified DEFAULT column value.</p>
<p><a class="reference internal" href="#sqlalchemy.schema.DefaultClause" title="sqlalchemy.schema.DefaultClause"><tt class="xref py py-class docutils literal"><span class="pre">DefaultClause</span></tt></a> is a <a class="reference internal" href="#sqlalchemy.schema.FetchedValue" title="sqlalchemy.schema.FetchedValue"><tt class="xref py py-class docutils literal"><span class="pre">FetchedValue</span></tt></a>
that also generates a &#8220;DEFAULT&#8221; clause when
&#8220;CREATE TABLE&#8221; is emitted.</p>
<p><a class="reference internal" href="#sqlalchemy.schema.DefaultClause" title="sqlalchemy.schema.DefaultClause"><tt class="xref py py-class docutils literal"><span class="pre">DefaultClause</span></tt></a> is generated automatically
whenever the <tt class="docutils literal"><span class="pre">server_default</span></tt>, <tt class="docutils literal"><span class="pre">server_onupdate</span></tt> arguments of
<a class="reference internal" href="#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a> are used.  A <a class="reference internal" href="#sqlalchemy.schema.DefaultClause" title="sqlalchemy.schema.DefaultClause"><tt class="xref py py-class docutils literal"><span class="pre">DefaultClause</span></tt></a>
can be passed positionally as well.</p>
<p>For example, the following:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">Column</span><span class="p">(</span><span class="s">&#39;foo&#39;</span><span class="p">,</span> <span class="n">Integer</span><span class="p">,</span> <span class="n">server_default</span><span class="o">=</span><span class="s">&quot;50&quot;</span><span class="p">)</span></pre></div>
</div>
<p>Is equivalent to:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">Column</span><span class="p">(</span><span class="s">&#39;foo&#39;</span><span class="p">,</span> <span class="n">Integer</span><span class="p">,</span> <span class="n">DefaultClause</span><span class="p">(</span><span class="s">&quot;50&quot;</span><span class="p">))</span></pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="sqlalchemy.schema.DefaultGenerator">
<em class="property">class </em><tt class="descclassname">sqlalchemy.schema.</tt><tt class="descname">DefaultGenerator</tt><big>(</big><em>for_update=False</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.DefaultGenerator" title="Permalink to this definition">¶</a></dt>
<dd><p>Bases: <tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.schema.SchemaItem</span></tt></p>
<p>Base class for column <em>default</em> values.</p>
</dd></dl>

<dl class="class">
<dt id="sqlalchemy.schema.FetchedValue">
<em class="property">class </em><tt class="descclassname">sqlalchemy.schema.</tt><tt class="descname">FetchedValue</tt><big>(</big><em>for_update=False</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.FetchedValue" title="Permalink to this definition">¶</a></dt>
<dd><p>Bases: <tt class="xref py py-class docutils literal"><span class="pre">object</span></tt></p>
<p>A marker for a transparent database-side default.</p>
<p>Use <a class="reference internal" href="#sqlalchemy.schema.FetchedValue" title="sqlalchemy.schema.FetchedValue"><tt class="xref py py-class docutils literal"><span class="pre">FetchedValue</span></tt></a> when the database is configured
to provide some automatic default for a column.</p>
<p>E.g.:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">Column</span><span class="p">(</span><span class="s">&#39;foo&#39;</span><span class="p">,</span> <span class="n">Integer</span><span class="p">,</span> <span class="n">FetchedValue</span><span class="p">())</span></pre></div>
</div>
<p>Would indicate that some trigger or default generator
will create a new value for the <tt class="docutils literal"><span class="pre">foo</span></tt> column during an
INSERT.</p>
</dd></dl>

<dl class="class">
<dt id="sqlalchemy.schema.PassiveDefault">
<em class="property">class </em><tt class="descclassname">sqlalchemy.schema.</tt><tt class="descname">PassiveDefault</tt><big>(</big><em>*arg</em>, <em>**kw</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.PassiveDefault" title="Permalink to this definition">¶</a></dt>
<dd><p>Bases: <a class="reference internal" href="#sqlalchemy.schema.DefaultClause" title="sqlalchemy.schema.DefaultClause"><tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.schema.DefaultClause</span></tt></a></p>
<p>A DDL-specified DEFAULT column value.</p>
<p class="deprecated">
<span class="versionmodified">Deprecated since version 0.6: </span><a class="reference internal" href="#sqlalchemy.schema.PassiveDefault" title="sqlalchemy.schema.PassiveDefault"><tt class="xref py py-class docutils literal"><span class="pre">PassiveDefault</span></tt></a> is deprecated. 
Use <a class="reference internal" href="#sqlalchemy.schema.DefaultClause" title="sqlalchemy.schema.DefaultClause"><tt class="xref py py-class docutils literal"><span class="pre">DefaultClause</span></tt></a>.</p>
</dd></dl>

<dl class="class">
<dt id="sqlalchemy.schema.Sequence">
<em class="property">class </em><tt class="descclassname">sqlalchemy.schema.</tt><tt class="descname">Sequence</tt><big>(</big><em>name</em>, <em>start=None</em>, <em>increment=None</em>, <em>schema=None</em>, <em>optional=False</em>, <em>quote=None</em>, <em>metadata=None</em>, <em>for_update=False</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Sequence" title="Permalink to this definition">¶</a></dt>
<dd><p>Bases: <a class="reference internal" href="#sqlalchemy.schema.DefaultGenerator" title="sqlalchemy.schema.DefaultGenerator"><tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.schema.DefaultGenerator</span></tt></a></p>
<p>Represents a named database sequence.</p>
</dd></dl>

</div>
</div>
<div class="section" id="defining-constraints-and-indexes">
<h2>Defining Constraints and Indexes<a class="headerlink" href="#defining-constraints-and-indexes" title="Permalink to this headline">¶</a></h2>
<div class="section" id="defining-foreign-keys">
<span id="metadata-constraints"></span><span id="metadata-foreignkeys"></span><h3>Defining Foreign Keys<a class="headerlink" href="#defining-foreign-keys" title="Permalink to this headline">¶</a></h3>
<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="#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="#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="#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">
<h4>Creating/Dropping Foreign Key Constraints via ALTER<a class="headerlink" href="#creating-dropping-foreign-key-constraints-via-alter" title="Permalink to this headline">¶</a></h4>
<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="#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">
<h4>ON UPDATE and ON DELETE<a class="headerlink" href="#on-update-and-on-delete" title="Permalink to this headline">¶</a></h4>
<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 class="section" id="foreign-key-api-constructs">
<h4>Foreign Key API Constructs<a class="headerlink" href="#foreign-key-api-constructs" title="Permalink to this headline">¶</a></h4>
<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>initially=None</em>, <em>link_to_name=False</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.ForeignKey" title="Permalink to this definition">¶</a></dt>
<dd><p>Bases: <tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.schema.SchemaItem</span></tt></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="#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="#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="#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="#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="#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="#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="#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
<a class="reference internal" href="#metadata-foreignkeys"><em>Defining Foreign Keys</em></a>.</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>initially=None</em>, <em>link_to_name=False</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="#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"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>column</strong> &#8211; A single target column for the key relationship. A
<a class="reference internal" href="#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="xref docutils literal"><span class="pre">True</span></tt> in which case the rendered name of the column is used.</li>
<li><strong>name</strong> &#8211; Optional string. An in-database name for the key if
<cite>constraint</cite> is not provided.</li>
<li><strong>onupdate</strong> &#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><strong>ondelete</strong> &#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><strong>deferrable</strong> &#8211; Optional bool. If set, emit DEFERRABLE or NOT
DEFERRABLE when issuing DDL for this constraint.</li>
<li><strong>initially</strong> &#8211; Optional string. If set, emit INITIALLY &lt;value&gt; when
issuing DDL for this constraint.</li>
<li><strong>link_to_name</strong> &#8211; if True, the string name given in <tt class="docutils literal"><span class="pre">column</span></tt> is
the rendered name of the referenced column, not its locally
assigned <tt class="docutils literal"><span class="pre">key</span></tt>.</li>
<li><strong>use_alter</strong> &#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>
</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="#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="#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="#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="#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="#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="#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="#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="#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="#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"><th class="field-name">Parameters:</th><td class="field-body"><strong>schema</strong> &#8211; The returned <a class="reference internal" href="#sqlalchemy.schema.ForeignKey" title="sqlalchemy.schema.ForeignKey"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></tt></a> will
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="#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="#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="#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="#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>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="#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 <a class="reference internal" href="#metadata-foreignkeys"><em>Defining Foreign Keys</em></a>.</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>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"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>columns</strong> &#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><strong>refcolumns</strong> &#8211; A sequence of foreign column names or Column
objects. The columns must all be located within the same Table.</li>
<li><strong>name</strong> &#8211; Optional, the in-database name of the key.</li>
<li><strong>onupdate</strong> &#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><strong>ondelete</strong> &#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><strong>deferrable</strong> &#8211; Optional bool. If set, emit DEFERRABLE or NOT
DEFERRABLE when issuing DDL for this constraint.</li>
<li><strong>initially</strong> &#8211; Optional string. If set, emit INITIALLY &lt;value&gt; when
issuing DDL for this constraint.</li>
<li><strong>link_to_name</strong> &#8211; if True, the string name given in <tt class="docutils literal"><span class="pre">column</span></tt> is
the rendered name of the referenced column, not its locally assigned
<tt class="docutils literal"><span class="pre">key</span></tt>.</li>
<li><strong>use_alter</strong> &#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="#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="#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>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

</dd></dl>

</div>
</div>
<div class="section" id="unique-constraint">
<h3>UNIQUE Constraint<a class="headerlink" href="#unique-constraint" title="Permalink to this headline">¶</a></h3>
<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="#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>
<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="check-constraint">
<h3>CHECK Constraint<a class="headerlink" href="#check-constraint" title="Permalink to this headline">¶</a></h3>
<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>
<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><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>

</div>
<div class="section" id="other-constraint-classes">
<h3>Other Constraint Classes<a class="headerlink" href="#other-constraint-classes" title="Permalink to this headline">¶</a></h3>
<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><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Constraint" title="Permalink to this definition">¶</a></dt>
<dd><p>Bases: <tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.schema.SchemaItem</span></tt></p>
<p>A table-level SQL constraint.</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: <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.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>

</div>
<div class="section" id="indexes">
<h3>Indexes<a class="headerlink" href="#indexes" title="Permalink to this headline">¶</a></h3>
<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="#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>Note that 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
<strong>externally</strong> to the table which it corresponds, using
<a class="reference internal" href="#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a> objects and not strings.</p>
<p>Below we illustrate a <a class="reference internal" href="#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>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>
<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>*columns</em>, <em>**kwargs</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.SchemaItem</span></tt></p>
<p>A table-level INDEX.</p>
<p>Defines a composite (one or more column) INDEX. For a no-frills, single
column index, adding <tt class="docutils literal"><span class="pre">index=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 Index.</p>
</dd></dl>

</div>
</div>
<div class="section" id="customizing-ddl">
<span id="metadata-ddl"></span><h2>Customizing DDL<a class="headerlink" href="#customizing-ddl" title="Permalink to this headline">¶</a></h2>
<p>In the preceding sections we&#8217;ve discussed a variety of schema constructs
including <a class="reference internal" href="#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></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>,
<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>, and
<a class="reference internal" href="#sqlalchemy.schema.Sequence" title="sqlalchemy.schema.Sequence"><tt class="xref py py-class docutils literal"><span class="pre">Sequence</span></tt></a>. Throughout, we&#8217;ve relied upon the
<tt class="docutils literal"><span class="pre">create()</span></tt> and <a class="reference internal" href="#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> methods of
<a class="reference internal" href="#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="#sqlalchemy.schema.MetaData" title="sqlalchemy.schema.MetaData"><tt class="xref py py-class docutils literal"><span class="pre">MetaData</span></tt></a> in
order to issue data definition language (DDL) for all constructs. When issued,
a pre-determined order of operations is invoked, and DDL to create each table
is created unconditionally including all constraints and other objects
associated with it. For more complex scenarios where database-specific DDL is
required, SQLAlchemy offers two techniques which can be used to add any DDL
based on any condition, either accompanying the standard generation of tables
or by itself.</p>
<div class="section" id="controlling-ddl-sequences">
<h3>Controlling DDL Sequences<a class="headerlink" href="#controlling-ddl-sequences" title="Permalink to this headline">¶</a></h3>
<p>The <tt class="docutils literal"><span class="pre">sqlalchemy.schema</span></tt> package contains SQL expression constructs that
provide DDL expressions. For example, to produce a <tt class="docutils literal"><span class="pre">CREATE</span> <span class="pre">TABLE</span></tt> statement:</p>
<div class="highlight-python+sql"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">sqlalchemy.schema</span> <span class="kn">import</span> <span class="n">CreateTable</span>
<a href='#' class='sql_link'>sql</a><span class="n">engine</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="n">CreateTable</span><span class="p">(</span><span class="n">mytable</span><span class="p">))</span>
<div class='popup_sql'>CREATE TABLE mytable (
    col1 INTEGER,
    col2 INTEGER,
    col3 INTEGER,
    col4 INTEGER,
    col5 INTEGER,
    col6 INTEGER
)</div></pre></div>
</div>
<p>Above, the <a class="reference internal" href="#sqlalchemy.schema.CreateTable" title="sqlalchemy.schema.CreateTable"><tt class="xref py py-class docutils literal"><span class="pre">CreateTable</span></tt></a> construct works like any
other expression construct (such as <tt class="docutils literal"><span class="pre">select()</span></tt>, <tt class="docutils literal"><span class="pre">table.insert()</span></tt>, etc.). A
full reference of available constructs is in <a class="reference internal" href="#schema-api-ddl"><em>DDL API</em></a>.</p>
<p>The DDL constructs all extend a common base class which provides the
capability to be associated with an individual
<a class="reference internal" href="#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> or <a class="reference internal" href="#sqlalchemy.schema.MetaData" title="sqlalchemy.schema.MetaData"><tt class="xref py py-class docutils literal"><span class="pre">MetaData</span></tt></a>
object, to be invoked upon create/drop events. Consider the example of a table
which contains a CHECK constraint:</p>
<div class="highlight-python+sql"><div class="highlight"><pre><span class="n">users</span> <span class="o">=</span> <span class="n">Table</span><span class="p">(</span><span class="s">&#39;users&#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;user_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_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">CheckConstraint</span><span class="p">(</span><span class="s">&#39;length(user_name) &gt;= 8&#39;</span><span class="p">,</span><span class="n">name</span><span class="o">=</span><span class="s">&quot;cst_user_name_length&quot;</span><span class="p">)</span>
               <span class="p">)</span>

<a href='#' class='sql_link'>sql</a><span class="n">users</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 users (
    user_id SERIAL NOT NULL,
    user_name VARCHAR(40) NOT NULL,
    PRIMARY KEY (user_id),
    CONSTRAINT cst_user_name_length  CHECK (length(user_name) >= 8)
)</div></pre></div>
</div>
<p>The above table contains a column &#8220;user_name&#8221; which is subject to a CHECK
constraint that validates that the length of the string is at least eight
characters. When a <tt class="docutils literal"><span class="pre">create()</span></tt> is issued for this table, DDL for 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> will also be issued inline within
the table definition.</p>
<p>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 can also be
constructed externally and associated with the
<a class="reference internal" href="#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> afterwards:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">constraint</span> <span class="o">=</span> <span class="n">CheckConstraint</span><span class="p">(</span><span class="s">&#39;length(user_name) &gt;= 8&#39;</span><span class="p">,</span><span class="n">name</span><span class="o">=</span><span class="s">&quot;cst_user_name_length&quot;</span><span class="p">)</span>
<span class="n">users</span><span class="o">.</span><span class="n">append_constraint</span><span class="p">(</span><span class="n">constraint</span><span class="p">)</span></pre></div>
</div>
<p>So far, the effect is the same. However, if we create DDL elements
corresponding to the creation and removal of this constraint, and associate
them with the <a class="reference internal" href="#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> as events, these new events
will take over the job of issuing DDL for the constraint. Additionally, the
constraint will be added via ALTER:</p>
<div class="highlight-python+sql"><div class="highlight"><pre><span class="n">AddConstraint</span><span class="p">(</span><span class="n">constraint</span><span class="p">)</span><span class="o">.</span><span class="n">execute_at</span><span class="p">(</span><span class="s">&quot;after-create&quot;</span><span class="p">,</span> <span class="n">users</span><span class="p">)</span>
<span class="n">DropConstraint</span><span class="p">(</span><span class="n">constraint</span><span class="p">)</span><span class="o">.</span><span class="n">execute_at</span><span class="p">(</span><span class="s">&quot;before-drop&quot;</span><span class="p">,</span> <span class="n">users</span><span class="p">)</span>

<a href='#' class='sql_link'>sql</a><span class="n">users</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 users (
    user_id SERIAL NOT NULL,
    user_name VARCHAR(40) NOT NULL,
    PRIMARY KEY (user_id)
)

ALTER TABLE users ADD CONSTRAINT cst_user_name_length  CHECK (length(user_name) >= 8)</div>
<a href='#' class='sql_link'>sql</a><span class="n">users</span><span class="o">.</span><span class="n">drop</span><span class="p">(</span><span class="n">engine</span><span class="p">)</span>
<div class='popup_sql'>ALTER TABLE users DROP CONSTRAINT cst_user_name_length
DROP TABLE user</div></pre></div>
</div>
<p>The real usefulness of the above becomes clearer once we illustrate the <tt class="docutils literal"><span class="pre">on</span></tt>
attribute of a DDL event. The <tt class="docutils literal"><span class="pre">on</span></tt> parameter is part of the constructor, and
may be a string name of a database dialect name, a tuple containing dialect
names, or a Python callable. This will limit the execution of the item to just
those dialects, or when the return value of the callable is <tt class="xref docutils literal"><span class="pre">True</span></tt>. So if
our <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> was only supported by
Postgresql and not other databases, we could limit it to just that dialect:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">AddConstraint</span><span class="p">(</span><span class="n">constraint</span><span class="p">,</span> <span class="n">on</span><span class="o">=</span><span class="s">&#39;postgresql&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">execute_at</span><span class="p">(</span><span class="s">&quot;after-create&quot;</span><span class="p">,</span> <span class="n">users</span><span class="p">)</span>
<span class="n">DropConstraint</span><span class="p">(</span><span class="n">constraint</span><span class="p">,</span> <span class="n">on</span><span class="o">=</span><span class="s">&#39;postgresql&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">execute_at</span><span class="p">(</span><span class="s">&quot;before-drop&quot;</span><span class="p">,</span> <span class="n">users</span><span class="p">)</span></pre></div>
</div>
<p>Or to any set of dialects:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">AddConstraint</span><span class="p">(</span><span class="n">constraint</span><span class="p">,</span> <span class="n">on</span><span class="o">=</span><span class="p">(</span><span class="s">&#39;postgresql&#39;</span><span class="p">,</span> <span class="s">&#39;mysql&#39;</span><span class="p">))</span><span class="o">.</span><span class="n">execute_at</span><span class="p">(</span><span class="s">&quot;after-create&quot;</span><span class="p">,</span> <span class="n">users</span><span class="p">)</span>
<span class="n">DropConstraint</span><span class="p">(</span><span class="n">constraint</span><span class="p">,</span> <span class="n">on</span><span class="o">=</span><span class="p">(</span><span class="s">&#39;postgresql&#39;</span><span class="p">,</span> <span class="s">&#39;mysql&#39;</span><span class="p">))</span><span class="o">.</span><span class="n">execute_at</span><span class="p">(</span><span class="s">&quot;before-drop&quot;</span><span class="p">,</span> <span class="n">users</span><span class="p">)</span></pre></div>
</div>
<p>When using a callable, the callable is passed the ddl element, event name, the
<a class="reference internal" href="#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> or <a class="reference internal" href="#sqlalchemy.schema.MetaData" title="sqlalchemy.schema.MetaData"><tt class="xref py py-class docutils literal"><span class="pre">MetaData</span></tt></a>
object whose &#8220;create&#8221; or &#8220;drop&#8221; event is in progress, and the
<a class="reference internal" href="connections.html#sqlalchemy.engine.base.Connection" title="sqlalchemy.engine.base.Connection"><tt class="xref py py-class docutils literal"><span class="pre">Connection</span></tt></a> object being used for the
operation, as well as additional information as keyword arguments. The
callable can perform checks, such as whether or not a given item already
exists. Below we define <tt class="docutils literal"><span class="pre">should_create()</span></tt> and <tt class="docutils literal"><span class="pre">should_drop()</span></tt> callables
that check for the presence of our named constraint:</p>
<div class="highlight-python+sql"><div class="highlight"><pre><span class="k">def</span> <span class="nf">should_create</span><span class="p">(</span><span class="n">ddl</span><span class="p">,</span> <span class="n">event</span><span class="p">,</span> <span class="n">target</span><span class="p">,</span> <span class="n">connection</span><span class="p">,</span> <span class="o">**</span><span class="n">kw</span><span class="p">):</span>
    <span class="n">row</span> <span class="o">=</span> <span class="n">connection</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;select conname from pg_constraint where conname=&#39;</span><span class="si">%s</span><span class="s">&#39;&quot;</span> <span class="o">%</span> <span class="n">ddl</span><span class="o">.</span><span class="n">element</span><span class="o">.</span><span class="n">name</span><span class="p">)</span><span class="o">.</span><span class="n">scalar</span><span class="p">()</span>
    <span class="k">return</span> <span class="ow">not</span> <span class="nb">bool</span><span class="p">(</span><span class="n">row</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">should_drop</span><span class="p">(</span><span class="n">ddl</span><span class="p">,</span> <span class="n">event</span><span class="p">,</span> <span class="n">target</span><span class="p">,</span> <span class="n">connection</span><span class="p">,</span> <span class="o">**</span><span class="n">kw</span><span class="p">):</span>
    <span class="k">return</span> <span class="ow">not</span> <span class="n">should_create</span><span class="p">(</span><span class="n">ddl</span><span class="p">,</span> <span class="n">event</span><span class="p">,</span> <span class="n">target</span><span class="p">,</span> <span class="n">connection</span><span class="p">,</span> <span class="o">**</span><span class="n">kw</span><span class="p">)</span>

<span class="n">AddConstraint</span><span class="p">(</span><span class="n">constraint</span><span class="p">,</span> <span class="n">on</span><span class="o">=</span><span class="n">should_create</span><span class="p">)</span><span class="o">.</span><span class="n">execute_at</span><span class="p">(</span><span class="s">&quot;after-create&quot;</span><span class="p">,</span> <span class="n">users</span><span class="p">)</span>
<span class="n">DropConstraint</span><span class="p">(</span><span class="n">constraint</span><span class="p">,</span> <span class="n">on</span><span class="o">=</span><span class="n">should_drop</span><span class="p">)</span><span class="o">.</span><span class="n">execute_at</span><span class="p">(</span><span class="s">&quot;before-drop&quot;</span><span class="p">,</span> <span class="n">users</span><span class="p">)</span>

<a href='#' class='sql_link'>sql</a><span class="n">users</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 users (
    user_id SERIAL NOT NULL,
    user_name VARCHAR(40) NOT NULL,
    PRIMARY KEY (user_id)
)

select conname from pg_constraint where conname='cst_user_name_length'
ALTER TABLE users ADD CONSTRAINT cst_user_name_length  CHECK (length(user_name) >= 8)</div>
<a href='#' class='sql_link'>sql</a><span class="n">users</span><span class="o">.</span><span class="n">drop</span><span class="p">(</span><span class="n">engine</span><span class="p">)</span>
<div class='popup_sql'>select conname from pg_constraint where conname='cst_user_name_length'
ALTER TABLE users DROP CONSTRAINT cst_user_name_length
DROP TABLE user</div></pre></div>
</div>
</div>
<div class="section" id="custom-ddl">
<h3>Custom DDL<a class="headerlink" href="#custom-ddl" title="Permalink to this headline">¶</a></h3>
<p>Custom DDL phrases are most easily achieved using the
<a class="reference internal" href="#sqlalchemy.schema.DDL" title="sqlalchemy.schema.DDL"><tt class="xref py py-class docutils literal"><span class="pre">DDL</span></tt></a> construct. This construct works like all the
other DDL elements except it accepts a string which is the text to be emitted:</p>
<div class="highlight-python+sql"><div class="highlight"><pre><span class="n">DDL</span><span class="p">(</span><span class="s">&quot;ALTER TABLE users ADD CONSTRAINT &quot;</span>
    <span class="s">&quot;cst_user_name_length &quot;</span>
    <span class="s">&quot; CHECK (length(user_name) &gt;= 8)&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">execute_at</span><span class="p">(</span><span class="s">&quot;after-create&quot;</span><span class="p">,</span> <span class="n">metadata</span><span class="p">)</span></pre></div>
</div>
<p>A more comprehensive method of creating libraries of DDL constructs is to use
custom compilation - see <a class="reference internal" href="compiler.html"><em>Custom SQL Constructs and Compilation Extension</em></a> for
details.</p>
</div>
<div class="section" id="ddl-api">
<span id="schema-api-ddl"></span><h3>DDL API<a class="headerlink" href="#ddl-api" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="sqlalchemy.schema.DDLElement">
<em class="property">class </em><tt class="descclassname">sqlalchemy.schema.</tt><tt class="descname">DDLElement</tt><a class="headerlink" href="#sqlalchemy.schema.DDLElement" title="Permalink to this definition">¶</a></dt>
<dd><p>Bases: <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.Executable" title="sqlalchemy.sql.expression.Executable"><tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.sql.expression.Executable</span></tt></a>, <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.ClauseElement" title="sqlalchemy.sql.expression.ClauseElement"><tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.sql.expression.ClauseElement</span></tt></a></p>
<p>Base class for DDL expression constructs.</p>
<dl class="method">
<dt id="sqlalchemy.schema.DDLElement.against">
<tt class="descname">against</tt><big>(</big><em>target</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.DDLElement.against" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a copy of this DDL against a specific schema item.</p>
</dd></dl>

<dl class="attribute">
<dt id="sqlalchemy.schema.DDLElement.bind">
<tt class="descname">bind</tt><a class="headerlink" href="#sqlalchemy.schema.DDLElement.bind" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<dl class="method">
<dt id="sqlalchemy.schema.DDLElement.execute">
<tt class="descname">execute</tt><big>(</big><em>bind=None</em>, <em>target=None</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.DDLElement.execute" title="Permalink to this definition">¶</a></dt>
<dd><p>Execute this DDL immediately.</p>
<p>Executes the DDL statement in isolation using the supplied
<a class="reference internal" href="connections.html#sqlalchemy.engine.base.Connectable" title="sqlalchemy.engine.base.Connectable"><tt class="xref py py-class docutils literal"><span class="pre">Connectable</span></tt></a> or
<a class="reference internal" href="connections.html#sqlalchemy.engine.base.Connectable" title="sqlalchemy.engine.base.Connectable"><tt class="xref py py-class docutils literal"><span class="pre">Connectable</span></tt></a> assigned to the <tt class="docutils literal"><span class="pre">.bind</span></tt>
property, if not supplied. If the DDL has a conditional <tt class="docutils literal"><span class="pre">on</span></tt>
criteria, it will be invoked with None as the event.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>bind</strong> &#8211; Optional, an <tt class="docutils literal"><span class="pre">Engine</span></tt> or <tt class="docutils literal"><span class="pre">Connection</span></tt>. If not supplied, a valid
<a class="reference internal" href="connections.html#sqlalchemy.engine.base.Connectable" title="sqlalchemy.engine.base.Connectable"><tt class="xref py py-class docutils literal"><span class="pre">Connectable</span></tt></a> must be present in the
<tt class="docutils literal"><span class="pre">.bind</span></tt> property.</li>
<li><strong>target</strong> &#8211; Optional, defaults to None.  The target SchemaItem for the 
execute call.  Will be passed to the <tt class="docutils literal"><span class="pre">on</span></tt> callable if any, 
and may also provide string expansion data for the
statement. See <tt class="docutils literal"><span class="pre">execute_at</span></tt> for more information.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="method">
<dt id="sqlalchemy.schema.DDLElement.execute_at">
<tt class="descname">execute_at</tt><big>(</big><em>event</em>, <em>target</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.DDLElement.execute_at" title="Permalink to this definition">¶</a></dt>
<dd><p>Link execution of this DDL to the DDL lifecycle of a SchemaItem.</p>
<p>Links this <tt class="docutils literal"><span class="pre">DDLElement</span></tt> to a <tt class="docutils literal"><span class="pre">Table</span></tt> or <tt class="docutils literal"><span class="pre">MetaData</span></tt> instance,
executing it when that schema item is created or dropped. The DDL
statement will be executed using the same Connection and transactional
context as the Table create/drop itself. The <tt class="docutils literal"><span class="pre">.bind</span></tt> property of
this statement is ignored.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>event</strong> &#8211; One of the events defined in the schema item&#8217;s <tt class="docutils literal"><span class="pre">.ddl_events</span></tt>;
e.g. &#8216;before-create&#8217;, &#8216;after-create&#8217;, &#8216;before-drop&#8217; or &#8216;after-drop&#8217;</li>
<li><strong>target</strong> &#8211; The Table or MetaData instance for which this DDLElement will
be associated with.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<p>A DDLElement instance can be linked to any number of schema items.</p>
<p><tt class="docutils literal"><span class="pre">execute_at</span></tt> builds on the <tt class="docutils literal"><span class="pre">append_ddl_listener</span></tt> interface of
<a class="reference internal" href="#sqlalchemy.schema.MetaData" title="sqlalchemy.schema.MetaData"><tt class="xref py py-class docutils literal"><span class="pre">MetaData</span></tt></a> and <a class="reference internal" href="#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> objects.</p>
<p>Caveat: Creating or dropping a Table in isolation will also trigger
any DDL set to <tt class="docutils literal"><span class="pre">execute_at</span></tt> that Table&#8217;s MetaData.  This may change
in a future release.</p>
</dd></dl>

</dd></dl>

<dl class="class">
<dt id="sqlalchemy.schema.DDL">
<em class="property">class </em><tt class="descclassname">sqlalchemy.schema.</tt><tt class="descname">DDL</tt><big>(</big><em>statement</em>, <em>on=None</em>, <em>context=None</em>, <em>bind=None</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.DDL" title="Permalink to this definition">¶</a></dt>
<dd><p>Bases: <a class="reference internal" href="#sqlalchemy.schema.DDLElement" title="sqlalchemy.schema.DDLElement"><tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.schema.DDLElement</span></tt></a></p>
<p>A literal DDL statement.</p>
<p>Specifies literal SQL DDL to be executed by the database.  DDL objects can
be attached to <tt class="docutils literal"><span class="pre">Tables</span></tt> or <tt class="docutils literal"><span class="pre">MetaData</span></tt> instances, conditionally
executing SQL as part of the DDL lifecycle of those schema items.  Basic
templating support allows a single DDL instance to handle repetitive tasks
for multiple tables.</p>
<p>Examples:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">tbl</span> <span class="o">=</span> <span class="n">Table</span><span class="p">(</span><span class="s">&#39;users&#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;uid&#39;</span><span class="p">,</span> <span class="n">Integer</span><span class="p">))</span> <span class="c"># ...</span>
<span class="n">DDL</span><span class="p">(</span><span class="s">&#39;DROP TRIGGER users_trigger&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">execute_at</span><span class="p">(</span><span class="s">&#39;before-create&#39;</span><span class="p">,</span> <span class="n">tbl</span><span class="p">)</span>

<span class="n">spow</span> <span class="o">=</span> <span class="n">DDL</span><span class="p">(</span><span class="s">&#39;ALTER TABLE </span><span class="si">%(table)s</span><span class="s"> SET secretpowers TRUE&#39;</span><span class="p">,</span> <span class="n">on</span><span class="o">=</span><span class="s">&#39;somedb&#39;</span><span class="p">)</span>
<span class="n">spow</span><span class="o">.</span><span class="n">execute_at</span><span class="p">(</span><span class="s">&#39;after-create&#39;</span><span class="p">,</span> <span class="n">tbl</span><span class="p">)</span>

<span class="n">drop_spow</span> <span class="o">=</span> <span class="n">DDL</span><span class="p">(</span><span class="s">&#39;ALTER TABLE users SET secretpowers FALSE&#39;</span><span class="p">)</span>
<span class="n">connection</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="n">drop_spow</span><span class="p">)</span></pre></div>
</div>
<p>When operating on Table events, the following <tt class="docutils literal"><span class="pre">statement</span></tt>
string substitions are available:</p>
<div class="highlight-python"><pre>%(table)s  - the Table name, with any required quoting applied
%(schema)s - the schema name, with any required quoting applied
%(fullname)s - the Table name including schema, quoted if needed</pre>
</div>
<p>The DDL&#8217;s <tt class="docutils literal"><span class="pre">context</span></tt>, if any, will be combined with the standard
substutions noted above.  Keys present in the context will override
the standard substitutions.</p>
<dl class="method">
<dt id="sqlalchemy.schema.DDL.__init__">
<tt class="descname">__init__</tt><big>(</big><em>statement</em>, <em>on=None</em>, <em>context=None</em>, <em>bind=None</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.DDL.__init__" title="Permalink to this definition">¶</a></dt>
<dd><p>Create a DDL statement.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>statement</strong> &#8211; <p>A string or unicode string to be executed.  Statements will be
processed with Python&#8217;s string formatting operator.  See the
<tt class="docutils literal"><span class="pre">context</span></tt> argument and the <tt class="docutils literal"><span class="pre">execute_at</span></tt> method.</p>
<p>A literal &#8216;%&#8217; in a statement must be escaped as &#8216;%%&#8217;.</p>
<p>SQL bind parameters are not available in DDL statements.</p>
</li>
<li><strong>on</strong> &#8211; <p>Optional filtering criteria.  May be a string, tuple or a callable
predicate.  If a string, it will be compared to the name of the
executing database dialect:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">DDL</span><span class="p">(</span><span class="s">&#39;something&#39;</span><span class="p">,</span> <span class="n">on</span><span class="o">=</span><span class="s">&#39;postgresql&#39;</span><span class="p">)</span></pre></div>
</div>
<p>If a tuple, specifies multiple dialect names:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">DDL</span><span class="p">(</span><span class="s">&#39;something&#39;</span><span class="p">,</span> <span class="n">on</span><span class="o">=</span><span class="p">(</span><span class="s">&#39;postgresql&#39;</span><span class="p">,</span> <span class="s">&#39;mysql&#39;</span><span class="p">))</span></pre></div>
</div>
<p>If a callable, it will be invoked with four positional arguments
as well as optional keyword arguments:</p>
<blockquote>
<div><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">ddl:</th><td class="field-body">This DDL element.</td>
</tr>
<tr class="field"><th class="field-name">event:</th><td class="field-body">The name of the event that has triggered this DDL, such as
&#8216;after-create&#8217; Will be None if the DDL is executed explicitly.</td>
</tr>
<tr class="field"><th class="field-name">target:</th><td class="field-body">The <tt class="docutils literal"><span class="pre">Table</span></tt> or <tt class="docutils literal"><span class="pre">MetaData</span></tt> object which is the target of 
this event. May be None if the DDL is executed explicitly.</td>
</tr>
<tr class="field"><th class="field-name">connection:</th><td class="field-body">The <tt class="docutils literal"><span class="pre">Connection</span></tt> being used for DDL execution</td>
</tr>
<tr class="field"><th class="field-name">tables:</th><td class="field-body">Optional keyword argument - a list of Table objects which are to
be created/ dropped within a MetaData.create_all() or drop_all()
method call.</td>
</tr>
</tbody>
</table>
</div></blockquote>
<p>If the callable returns a true value, the DDL statement will be
executed.</p>
</li>
<li><strong>context</strong> &#8211; Optional dictionary, defaults to None.  These values will be
available for use in string substitutions on the DDL statement.</li>
<li><strong>bind</strong> &#8211; Optional. A <a class="reference internal" href="connections.html#sqlalchemy.engine.base.Connectable" title="sqlalchemy.engine.base.Connectable"><tt class="xref py py-class docutils literal"><span class="pre">Connectable</span></tt></a>, used by
default when <tt class="docutils literal"><span class="pre">execute()</span></tt> is invoked without a bind argument.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>

</dd></dl>

<dl class="class">
<dt id="sqlalchemy.schema.CreateTable">
<em class="property">class </em><tt class="descclassname">sqlalchemy.schema.</tt><tt class="descname">CreateTable</tt><big>(</big><em>element</em>, <em>on=None</em>, <em>bind=None</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.CreateTable" title="Permalink to this definition">¶</a></dt>
<dd><p>Bases: <tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.schema._CreateDropBase</span></tt></p>
<p>Represent a CREATE TABLE statement.</p>
</dd></dl>

<dl class="class">
<dt id="sqlalchemy.schema.DropTable">
<em class="property">class </em><tt class="descclassname">sqlalchemy.schema.</tt><tt class="descname">DropTable</tt><big>(</big><em>element</em>, <em>on=None</em>, <em>bind=None</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.DropTable" title="Permalink to this definition">¶</a></dt>
<dd><p>Bases: <tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.schema._CreateDropBase</span></tt></p>
<p>Represent a DROP TABLE statement.</p>
</dd></dl>

<dl class="class">
<dt id="sqlalchemy.schema.CreateSequence">
<em class="property">class </em><tt class="descclassname">sqlalchemy.schema.</tt><tt class="descname">CreateSequence</tt><big>(</big><em>element</em>, <em>on=None</em>, <em>bind=None</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.CreateSequence" title="Permalink to this definition">¶</a></dt>
<dd><p>Bases: <tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.schema._CreateDropBase</span></tt></p>
<p>Represent a CREATE SEQUENCE statement.</p>
</dd></dl>

<dl class="class">
<dt id="sqlalchemy.schema.DropSequence">
<em class="property">class </em><tt class="descclassname">sqlalchemy.schema.</tt><tt class="descname">DropSequence</tt><big>(</big><em>element</em>, <em>on=None</em>, <em>bind=None</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.DropSequence" title="Permalink to this definition">¶</a></dt>
<dd><p>Bases: <tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.schema._CreateDropBase</span></tt></p>
<p>Represent a DROP SEQUENCE statement.</p>
</dd></dl>

<dl class="class">
<dt id="sqlalchemy.schema.CreateIndex">
<em class="property">class </em><tt class="descclassname">sqlalchemy.schema.</tt><tt class="descname">CreateIndex</tt><big>(</big><em>element</em>, <em>on=None</em>, <em>bind=None</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.CreateIndex" title="Permalink to this definition">¶</a></dt>
<dd><p>Bases: <tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.schema._CreateDropBase</span></tt></p>
<p>Represent a CREATE INDEX statement.</p>
</dd></dl>

<dl class="class">
<dt id="sqlalchemy.schema.DropIndex">
<em class="property">class </em><tt class="descclassname">sqlalchemy.schema.</tt><tt class="descname">DropIndex</tt><big>(</big><em>element</em>, <em>on=None</em>, <em>bind=None</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.DropIndex" title="Permalink to this definition">¶</a></dt>
<dd><p>Bases: <tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.schema._CreateDropBase</span></tt></p>
<p>Represent a DROP INDEX statement.</p>
</dd></dl>

<dl class="class">
<dt id="sqlalchemy.schema.AddConstraint">
<em class="property">class </em><tt class="descclassname">sqlalchemy.schema.</tt><tt class="descname">AddConstraint</tt><big>(</big><em>element</em>, <em>*args</em>, <em>**kw</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.AddConstraint" title="Permalink to this definition">¶</a></dt>
<dd><p>Bases: <tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.schema._CreateDropBase</span></tt></p>
<p>Represent an ALTER TABLE ADD CONSTRAINT statement.</p>
<dl class="method">
<dt id="sqlalchemy.schema.AddConstraint.__init__">
<tt class="descname">__init__</tt><big>(</big><em>element</em>, <em>*args</em>, <em>**kw</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.AddConstraint.__init__" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

</dd></dl>

<dl class="class">
<dt id="sqlalchemy.schema.DropConstraint">
<em class="property">class </em><tt class="descclassname">sqlalchemy.schema.</tt><tt class="descname">DropConstraint</tt><big>(</big><em>element</em>, <em>cascade=False</em>, <em>**kw</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.DropConstraint" title="Permalink to this definition">¶</a></dt>
<dd><p>Bases: <tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.schema._CreateDropBase</span></tt></p>
<p>Represent an ALTER TABLE DROP CONSTRAINT statement.</p>
<dl class="method">
<dt id="sqlalchemy.schema.DropConstraint.__init__">
<tt class="descname">__init__</tt><big>(</big><em>element</em>, <em>cascade=False</em>, <em>**kw</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.DropConstraint.__init__" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

</dd></dl>

</div>
</div>
</div>

    </div>
</div>


    <div class="bottomnav">
            Previous:
            <a href="pooling.html" title="previous chapter">Connection Pooling</a>
            Next:
            <a href="types.html" title="next chapter">Column and Data Types</a>
        <div class="doc_copyright">
            &copy; <a href="../copyright.html">Copyright</a> 2007-2011, the SQLAlchemy authors and contributors.
            Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.
        </div>
    </div>




    </body>
</html>