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

« back to all changes in this revision

Viewing changes to doc/core/schema.html

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

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
    <script type="text/javascript">
21
21
      var DOCUMENTATION_OPTIONS = {
22
22
          URL_ROOT:    '../',
23
 
          VERSION:     '0.8.2',
 
23
          VERSION:     '0.8.3',
24
24
          COLLAPSE_MODINDEX: false,
25
25
          FILE_SUFFIX: '.html'
26
26
      };
34
34
        <link rel="copyright" title="Copyright" href="../copyright.html" />
35
35
    <link rel="top" title="SQLAlchemy 0.8 Documentation" href="../index.html" />
36
36
        <link rel="up" title="SQLAlchemy Core" href="index.html" />
37
 
        <link rel="next" title="Column and Data Types" href="types.html" />
38
 
        <link rel="prev" title="Connection Pooling" href="pooling.html" />
 
37
        <link rel="next" title="Describing Databases with MetaData" href="metadata.html" />
 
38
        <link rel="prev" title="Column and Data Types" href="types.html" />
39
39
 
40
40
    </head>
41
41
    <body>
67
67
    </div>
68
68
 
69
69
    <div id="docs-version-header">
70
 
        Release: <span class="version-num">0.8.2</span> | Release Date: July 3, 2013
 
70
        Release: <span class="version-num">0.8.3</span> | Release Date: October 26, 2013
71
71
 
72
72
 
73
73
    </div>
78
78
    <div id="docs-top-page-control" class="docs-navigation-links">
79
79
        <ul>
80
80
            <li>Prev:
81
 
            <a href="pooling.html" title="previous chapter">Connection Pooling</a>
 
81
            <a href="types.html" title="previous chapter">Column and Data Types</a>
82
82
            </li>
83
83
            <li>Next:
84
 
            <a href="types.html" title="next chapter">Column and Data Types</a>
 
84
            <a href="metadata.html" title="next chapter">Describing Databases with MetaData</a>
85
85
            </li>
86
86
 
87
87
        <li>
114
114
    <h3><a href="../index.html">Table of Contents</a></h3>
115
115
    <ul>
116
116
<li><a class="reference internal" href="#">Schema Definition Language</a><ul>
117
 
<li><a class="reference internal" href="#describing-databases-with-metadata">Describing Databases with MetaData</a><ul>
118
 
<li><a class="reference internal" href="#accessing-tables-and-columns">Accessing Tables and Columns</a></li>
119
 
<li><a class="reference internal" href="#creating-and-dropping-database-tables">Creating and Dropping Database Tables</a></li>
120
 
<li><a class="reference internal" href="#altering-schemas-through-migrations">Altering Schemas through Migrations</a></li>
121
 
<li><a class="reference internal" href="#specifying-the-schema-name">Specifying the Schema Name</a></li>
122
 
<li><a class="reference internal" href="#backend-specific-options">Backend-Specific Options</a></li>
123
 
<li><a class="reference internal" href="#column-table-metadata-api">Column, Table, MetaData API</a></li>
124
 
</ul>
125
 
</li>
126
 
<li><a class="reference internal" href="#reflecting-database-objects">Reflecting Database Objects</a><ul>
127
 
<li><a class="reference internal" href="#overriding-reflected-columns">Overriding Reflected Columns</a></li>
128
 
<li><a class="reference internal" href="#reflecting-views">Reflecting Views</a></li>
129
 
<li><a class="reference internal" href="#reflecting-all-tables-at-once">Reflecting All Tables at Once</a></li>
130
 
<li><a class="reference internal" href="#fine-grained-reflection-with-inspector">Fine Grained Reflection with Inspector</a></li>
131
 
</ul>
132
 
</li>
133
 
<li><a class="reference internal" href="#column-insert-update-defaults">Column Insert/Update Defaults</a><ul>
134
 
<li><a class="reference internal" href="#scalar-defaults">Scalar Defaults</a></li>
135
 
<li><a class="reference internal" href="#python-executed-functions">Python-Executed Functions</a><ul>
136
 
<li><a class="reference internal" href="#context-sensitive-default-functions">Context-Sensitive Default Functions</a></li>
137
 
</ul>
138
 
</li>
139
 
<li><a class="reference internal" href="#sql-expressions">SQL Expressions</a></li>
140
 
<li><a class="reference internal" href="#server-side-defaults">Server Side Defaults</a></li>
141
 
<li><a class="reference internal" href="#triggered-columns">Triggered Columns</a></li>
142
 
<li><a class="reference internal" href="#defining-sequences">Defining Sequences</a></li>
143
 
<li><a class="reference internal" href="#default-objects-api">Default Objects API</a></li>
144
 
</ul>
145
 
</li>
146
 
<li><a class="reference internal" href="#defining-constraints-and-indexes">Defining Constraints and Indexes</a><ul>
147
 
<li><a class="reference internal" href="#defining-foreign-keys">Defining Foreign Keys</a><ul>
148
 
<li><a class="reference internal" href="#creating-dropping-foreign-key-constraints-via-alter">Creating/Dropping Foreign Key Constraints via ALTER</a></li>
149
 
<li><a class="reference internal" href="#on-update-and-on-delete">ON UPDATE and ON DELETE</a></li>
150
 
</ul>
151
 
</li>
152
 
<li><a class="reference internal" href="#unique-constraint">UNIQUE Constraint</a></li>
153
 
<li><a class="reference internal" href="#check-constraint">CHECK Constraint</a></li>
154
 
<li><a class="reference internal" href="#setting-up-constraints-when-using-the-declarative-orm-extension">Setting up Constraints when using the Declarative ORM Extension</a></li>
155
 
<li><a class="reference internal" href="#constraints-api">Constraints API</a></li>
156
 
<li><a class="reference internal" href="#indexes">Indexes</a><ul>
157
 
<li><a class="reference internal" href="#functional-indexes">Functional Indexes</a></li>
158
 
</ul>
159
 
</li>
160
 
<li><a class="reference internal" href="#index-api">Index API</a></li>
161
 
</ul>
162
 
</li>
163
 
<li><a class="reference internal" href="#customizing-ddl">Customizing DDL</a><ul>
164
 
<li><a class="reference internal" href="#controlling-ddl-sequences">Controlling DDL Sequences</a></li>
165
 
<li><a class="reference internal" href="#custom-ddl">Custom DDL</a></li>
166
 
<li><a class="reference internal" href="#ddl-expression-constructs-api">DDL Expression Constructs API</a></li>
167
 
</ul>
168
 
</li>
169
117
</ul>
170
118
</li>
171
119
</ul>
173
121
 
174
122
    <h4>Previous Topic</h4>
175
123
    <p>
176
 
    <a href="pooling.html" title="previous chapter">Connection Pooling</a>
 
124
    <a href="types.html" title="previous chapter">Column and Data Types</a>
177
125
    </p>
178
126
    <h4>Next Topic</h4>
179
127
    <p>
180
 
    <a href="types.html" title="next chapter">Column and Data Types</a>
 
128
    <a href="metadata.html" title="next chapter">Describing Databases with MetaData</a>
181
129
    </p>
182
130
 
183
131
 
195
143
    <div id="docs-body" class="withsidebar" >
196
144
        
197
145
<div class="section" id="module-sqlalchemy.schema">
198
 
<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>
199
 
<div class="section" id="describing-databases-with-metadata">
200
 
<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>
 
146
<span id="schema-definition-language"></span><span id="schema-toplevel"></span><h1>Schema Definition Language<a class="headerlink" href="#module-sqlalchemy.schema" title="Permalink to this headline">¶</a></h1>
 
147
<p>This section references SQLAlchemy <strong>schema metadata</strong>, a comprehensive system of describing and inspecting
 
148
database schemas.</p>
201
149
<p>The core of SQLAlchemy&#8217;s query and object mapping operations are supported by
202
150
<em>database metadata</em>, which is comprised of Python objects that describe tables
203
151
and other schema-level objects. These objects are at the core of three major
206
154
already exist within the database.</p>
207
155
<p>Database metadata can be expressed by explicitly naming the various components
208
156
and their properties, using constructs such as
209
 
<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>,
210
 
<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
211
 
<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
 
157
<a class="reference internal" href="metadata.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a>, <a class="reference internal" href="metadata.html#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a>,
 
158
<a class="reference internal" href="constraints.html#sqlalchemy.schema.ForeignKey" title="sqlalchemy.schema.ForeignKey"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></tt></a> and
 
159
<a class="reference internal" href="defaults.html#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
212
160
<tt class="docutils literal"><span class="pre">sqlalchemy.schema</span></tt> package. It can also be generated by SQLAlchemy using a
213
161
process called <em>reflection</em>, which means you start with a single object such
214
 
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
 
162
as <a class="reference internal" href="metadata.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a>, assign it a name, and then instruct
215
163
SQLAlchemy to load all the additional information related to that name from a
216
164
particular engine source.</p>
217
165
<p>A key feature of SQLAlchemy&#8217;s database metadata constructs is that they are
218
166
designed to be used in a <em>declarative</em> style which closely resembles that of
219
167
real DDL. They are therefore most intuitive to those who have some background
220
168
in creating real schema generation scripts.</p>
221
 
<p>A collection of metadata entities is stored in an object aptly named
222
 
<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>
223
 
<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>
224
 
 
225
 
<span class="n">metadata</span> <span class="o">=</span> <span class="n">MetaData</span><span class="p">()</span></pre></div>
226
 
</div>
227
 
<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
228
 
many different features of a database (or multiple databases) being described.</p>
229
 
<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
230
 
primary arguments are the table name, then the
231
 
<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.
232
 
The remaining positional arguments are mostly
233
 
<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>
234
 
<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>
235
 
    <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>
236
 
    <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>
237
 
    <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>
238
 
    <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>
239
 
<span class="p">)</span></pre></div>
240
 
</div>
241
 
<p>Above, a table called <tt class="docutils literal"><span class="pre">user</span></tt> is described, which contains four columns. The
242
 
primary key of the table consists of the <tt class="docutils literal"><span class="pre">user_id</span></tt> column. Multiple columns
243
 
may be assigned the <tt class="docutils literal"><span class="pre">primary_key=True</span></tt> flag which denotes a multi-column
244
 
primary key, known as a <em>composite</em> primary key.</p>
245
 
<p>Note also that each column describes its datatype using objects corresponding
246
 
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
247
 
<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
248
 
varying levels of specificity as well as the ability to create custom types.
249
 
Documentation on the type system can be found at <em class="xref std std-ref">types</em>.</p>
250
 
<div class="section" id="accessing-tables-and-columns">
251
 
<h3>Accessing Tables and Columns<a class="headerlink" href="#accessing-tables-and-columns" title="Permalink to this headline">¶</a></h3>
252
 
<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
253
 
constructs we&#8217;ve associated with it. It supports a few methods of accessing
254
 
these table objects, such as the <tt class="docutils literal"><span class="pre">sorted_tables</span></tt> accessor which returns a
255
 
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
256
 
dependency (that is, each table is preceded by all tables which it
257
 
references):</p>
258
 
<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>
259
 
<span class="gp">... </span>   <span class="k">print</span> <span class="n">t</span><span class="o">.</span><span class="n">name</span>
260
 
<span class="go">user</span>
261
 
<span class="go">user_preference</span>
262
 
<span class="go">invoice</span>
263
 
<span class="go">invoice_item</span></pre></div>
264
 
</div>
265
 
<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
266
 
explicitly declared, and these objects are typically accessed directly as
267
 
module-level variables in an application. Once a
268
 
<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
269
 
accessors which allow inspection of its properties. Given the following
270
 
<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>
271
 
<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>
272
 
    <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>
273
 
    <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>
274
 
    <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>
275
 
<span class="p">)</span></pre></div>
276
 
</div>
277
 
<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 -
278
 
this construct defines a reference to a remote table, and is fully described
279
 
in <a class="reference internal" href="#metadata-foreignkeys"><em>Defining Foreign Keys</em></a>. Methods of accessing information about this
280
 
table include:</p>
281
 
<div class="highlight-python"><div class="highlight"><pre><span class="c"># access the column &quot;EMPLOYEE_ID&quot;:</span>
282
 
<span class="n">employees</span><span class="o">.</span><span class="n">columns</span><span class="o">.</span><span class="n">employee_id</span>
283
 
 
284
 
<span class="c"># or just</span>
285
 
<span class="n">employees</span><span class="o">.</span><span class="n">c</span><span class="o">.</span><span class="n">employee_id</span>
286
 
 
287
 
<span class="c"># via string</span>
288
 
<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>
289
 
 
290
 
<span class="c"># iterate through all columns</span>
291
 
<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>
292
 
    <span class="k">print</span> <span class="n">c</span>
293
 
 
294
 
<span class="c"># get the table&#39;s primary key columns</span>
295
 
<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>
296
 
    <span class="k">print</span> <span class="n">primary_key</span>
297
 
 
298
 
<span class="c"># get the table&#39;s foreign key objects:</span>
299
 
<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>
300
 
    <span class="k">print</span> <span class="n">fkey</span>
301
 
 
302
 
<span class="c"># access the table&#39;s MetaData:</span>
303
 
<span class="n">employees</span><span class="o">.</span><span class="n">metadata</span>
304
 
 
305
 
<span class="c"># access the table&#39;s bound Engine or Connection, if its MetaData is bound:</span>
306
 
<span class="n">employees</span><span class="o">.</span><span class="n">bind</span>
307
 
 
308
 
<span class="c"># access a column&#39;s name, type, nullable, primary key, foreign key</span>
309
 
<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>
310
 
<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>
311
 
<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>
312
 
<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>
313
 
<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>
314
 
 
315
 
<span class="c"># get the &quot;key&quot; of a column, which defaults to its name, but can</span>
316
 
<span class="c"># be any user-defined string:</span>
317
 
<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>
318
 
 
319
 
<span class="c"># access a column&#39;s table:</span>
320
 
<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>
321
 
 
322
 
<span class="c"># get the table related by a foreign key</span>
323
 
<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>
324
 
</div>
325
 
</div>
326
 
<div class="section" id="creating-and-dropping-database-tables">
327
 
<h3>Creating and Dropping Database Tables<a class="headerlink" href="#creating-and-dropping-database-tables" title="Permalink to this headline">¶</a></h3>
328
 
<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
329
 
you&#8217;re working with a brand new database one thing you might want to do is
330
 
issue CREATE statements for those tables and their related constructs (as an
331
 
aside, it&#8217;s also quite possible that you <em>don&#8217;t</em> want to do this, if you
332
 
already have some preferred methodology such as tools included with your
333
 
database or an existing scripting system - if that&#8217;s the case, feel free to
334
 
skip this section - SQLAlchemy has no requirement that it be used to create
335
 
your tables).</p>
336
 
<p>The usual way to issue CREATE is to use
337
 
<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
338
 
<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
339
 
that first check for the existence of each individual table, and if not found
340
 
will issue the CREATE statements:</p>
341
 
<blockquote>
342
 
<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>
343
 
 
344
 
<span class="n">metadata</span> <span class="o">=</span> <span class="n">MetaData</span><span class="p">()</span>
345
 
 
346
 
<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>
347
 
    <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>
348
 
    <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>
349
 
    <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>
350
 
    <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>
351
 
<span class="p">)</span>
352
 
 
353
 
<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>
354
 
    <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>
355
 
    <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>
356
 
    <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>
357
 
    <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>
358
 
<span class="p">)</span>
359
 
 
360
 
<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>
361
 
<div class='popup_sql'>PRAGMA table_info(user){}
362
 
CREATE TABLE user(
363
 
        user_id INTEGER NOT NULL PRIMARY KEY,
364
 
        user_name VARCHAR(16) NOT NULL,
365
 
        email_address VARCHAR(60),
366
 
        password VARCHAR(20) NOT NULL
367
 
)
368
 
PRAGMA table_info(user_prefs){}
369
 
CREATE TABLE user_prefs(
370
 
        pref_id INTEGER NOT NULL PRIMARY KEY,
371
 
        user_id INTEGER NOT NULL REFERENCES user(user_id),
372
 
        pref_name VARCHAR(40) NOT NULL,
373
 
        pref_value VARCHAR(100)
374
 
)</div></pre></div>
375
 
</div>
376
 
</div></blockquote>
377
 
<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
378
 
between tables usually inline with the table definition itself, and for this
379
 
reason it also generates the tables in order of their dependency. There are
380
 
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>
381
 
<p>Dropping all tables is similarly achieved using the
382
 
<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
383
 
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
384
 
presence of each table is checked first, and tables are dropped in reverse
385
 
order of dependency.</p>
386
 
<p>Creating and dropping individual tables can be done via the <tt class="docutils literal"><span class="pre">create()</span></tt> and
387
 
<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
388
 
default issue the CREATE or DROP regardless of the table being present:</p>
389
 
<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>
390
 
 
391
 
<span class="n">meta</span> <span class="o">=</span> <span class="n">MetaData</span><span class="p">()</span>
392
 
 
393
 
<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>
394
 
    <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>
395
 
    <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>
396
 
    <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>
397
 
<span class="p">)</span>
398
 
<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>
399
 
<div class='popup_sql'>CREATE TABLE employees(
400
 
employee_id SERIAL NOT NULL PRIMARY KEY,
401
 
employee_name VARCHAR(60) NOT NULL,
402
 
employee_dept INTEGER REFERENCES departments(department_id)
403
 
)</div></pre></div>
404
 
</div>
405
 
<p><tt class="docutils literal"><span class="pre">drop()</span></tt> method:</p>
406
 
<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>
407
 
<div class='popup_sql'>DROP TABLE employee</div></pre></div>
408
 
</div>
409
 
<p>To enable the &#8220;check first for the table existing&#8221; logic, add the
410
 
<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>
411
 
<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>
412
 
<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>
413
 
</div>
414
 
</div>
415
 
<div class="section" id="altering-schemas-through-migrations">
416
 
<h3>Altering Schemas through Migrations<a class="headerlink" href="#altering-schemas-through-migrations" title="Permalink to this headline">¶</a></h3>
417
 
<p>While SQLAlchemy directly supports emitting CREATE and DROP statements for schema
418
 
constructs, the ability to alter those constructs, usually via the ALTER statement
419
 
as well as other database-specific constructs, is outside of the scope of SQLAlchemy
420
 
itself.  While it&#8217;s easy enough to emit ALTER statements and similar by hand,
421
 
such as by passing a string to <a class="reference internal" href="connections.html#sqlalchemy.engine.Connection.execute" title="sqlalchemy.engine.Connection.execute"><tt class="xref py py-meth docutils literal"><span class="pre">Connection.execute()</span></tt></a> or by using the
422
 
<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, it&#8217;s a common practice to automate the maintenance of
423
 
database schemas in relation to application code using schema migration tools.</p>
424
 
<p>There are two major migration tools available for SQLAlchemy:</p>
425
 
<ul class="simple">
426
 
<li><a class="reference external" href="http://alembic.readthedocs.org">Alembic</a> - Written by the author of SQLAlchemy,
427
 
Alembic features a highly customizable environment and a minimalistic usage pattern,
428
 
supporting such features as transactional DDL, automatic generation of &#8220;candidate&#8221;
429
 
migrations, an &#8220;offline&#8221; mode which generates SQL scripts, and support for branch
430
 
resolution.</li>
431
 
<li><a class="reference external" href="http://code.google.com/p/sqlalchemy-migrate/">SQLAlchemy-Migrate</a> - The original
432
 
migration tool for SQLAlchemy, SQLAlchemy-Migrate is widely used and continues
433
 
under active development.   SQLAlchemy-Migrate includes features such as
434
 
SQL script generation, ORM class generation, ORM model comparison, and extensive
435
 
support for SQLite migrations.</li>
436
 
</ul>
437
 
</div>
438
 
<div class="section" id="specifying-the-schema-name">
439
 
<h3>Specifying the Schema Name<a class="headerlink" href="#specifying-the-schema-name" title="Permalink to this headline">¶</a></h3>
440
 
<p>Some databases support the concept of multiple schemas. A
441
 
<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
442
 
<tt class="docutils literal"><span class="pre">schema</span></tt> keyword argument:</p>
443
 
<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>
444
 
    <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>
445
 
    <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>
446
 
    <span class="n">schema</span><span class="o">=</span><span class="s">&#39;remote_banks&#39;</span>
447
 
<span class="p">)</span></pre></div>
448
 
</div>
449
 
<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
450
 
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
451
 
another table called <tt class="docutils literal"><span class="pre">financial_info</span></tt> is referenced without the
452
 
<tt class="docutils literal"><span class="pre">remote_banks</span></tt> schema, it will refer to a different
453
 
<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>
454
 
objects can specify references to columns in this table using the form
455
 
<tt class="docutils literal"><span class="pre">remote_banks.financial_info.id</span></tt>.</p>
456
 
<p>The <tt class="docutils literal"><span class="pre">schema</span></tt> argument should be used for any name qualifiers required,
457
 
including Oracle&#8217;s &#8220;owner&#8221; attribute and similar. It also can accommodate a
458
 
dotted name for longer schemes:</p>
459
 
<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>
460
 
</div>
461
 
</div>
462
 
<div class="section" id="backend-specific-options">
463
 
<h3>Backend-Specific Options<a class="headerlink" href="#backend-specific-options" title="Permalink to this headline">¶</a></h3>
464
 
<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
465
 
example, MySQL has different table backend types, including &#8220;MyISAM&#8221; and
466
 
&#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
467
 
<tt class="docutils literal"><span class="pre">mysql_engine</span></tt>:</p>
468
 
<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>
469
 
    <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>
470
 
    <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>
471
 
    <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>
472
 
    <span class="n">mysql_engine</span><span class="o">=</span><span class="s">&#39;InnoDB&#39;</span>
473
 
<span class="p">)</span></pre></div>
474
 
</div>
475
 
<p>Other backends may support table-level options as well - these would be
476
 
described in the individual documentation sections for each dialect.</p>
477
 
</div>
478
 
<div class="section" id="column-table-metadata-api">
479
 
<h3>Column, Table, MetaData API<a class="headerlink" href="#column-table-metadata-api" title="Permalink to this headline">¶</a></h3>
480
 
<dl class="class">
481
 
<dt id="sqlalchemy.schema.Column">
482
 
<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>
483
 
<dd><p>Bases: <a class="reference internal" href="#sqlalchemy.schema.SchemaItem" title="sqlalchemy.schema.SchemaItem"><tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.schema.SchemaItem</span></tt></a>, <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>
484
 
<p>Represents a column in a database table.</p>
485
 
<dl class="method">
486
 
<dt id="sqlalchemy.schema.Column.__eq__">
487
 
<tt class="descname">__eq__</tt><big>(</big><em>other</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Column.__eq__" title="Permalink to this definition">¶</a></dt>
488
 
<dd><div class="inherited-member container">
489
 
<em>inherited from the</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.ColumnOperators.__eq__" title="sqlalchemy.sql.operators.ColumnOperators.__eq__"><tt class="xref py py-meth docutils literal"><span class="pre">__eq__()</span></tt></a> <em>method of</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.ColumnOperators" title="sqlalchemy.sql.operators.ColumnOperators"><tt class="xref py py-class docutils literal"><span class="pre">ColumnOperators</span></tt></a></div>
490
 
<p>Implement the <tt class="docutils literal"><span class="pre">==</span></tt> operator.</p>
491
 
<p>In a column context, produces the clause <tt class="docutils literal"><span class="pre">a</span> <span class="pre">=</span> <span class="pre">b</span></tt>.
492
 
If the target is <tt class="docutils literal"><span class="pre">None</span></tt>, produces <tt class="docutils literal"><span class="pre">a</span> <span class="pre">IS</span> <span class="pre">NULL</span></tt>.</p>
493
 
</dd></dl>
494
 
 
495
 
<dl class="method">
496
 
<dt id="sqlalchemy.schema.Column.__init__">
497
 
<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>
498
 
<dd><p>Construct a new <tt class="docutils literal"><span class="pre">Column</span></tt> object.</p>
499
 
<table class="docutils field-list" frame="void" rules="none">
500
 
<col class="field-name" />
501
 
<col class="field-body" />
502
 
<tbody valign="top">
503
 
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
504
 
<li><strong>name</strong> &#8211; <p>The name of this column as represented in the database.
505
 
This argument may be the first positional argument, or specified
506
 
via keyword.</p>
507
 
<p>Names which contain no upper case characters
508
 
will be treated as case insensitive names, and will not be quoted
509
 
unless they are a reserved word.  Names with any number of upper
510
 
case characters will be quoted and sent exactly.  Note that this
511
 
behavior applies even for databases which standardize upper
512
 
case names as case insensitive such as Oracle.</p>
513
 
<p>The name field may be omitted at construction time and applied
514
 
later, at any time before the Column is associated with a
515
 
<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
516
 
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>
517
 
</li>
518
 
<li><strong>type_</strong> &#8211; <p>The column&#8217;s type, indicated using an instance which
519
 
subclasses <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>.  If no arguments
520
 
are required for the type, the class of the type can be sent
521
 
as well, e.g.:</p>
522
 
<div class="highlight-python"><div class="highlight"><pre><span class="c"># use a type with arguments</span>
523
 
<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>
524
 
 
525
 
<span class="c"># use no arguments</span>
526
 
<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>
527
 
</div>
528
 
<p>The <tt class="docutils literal"><span class="pre">type</span></tt> argument may be the second positional argument
529
 
or specified by keyword.</p>
530
 
<p>There is partial support for automatic detection of the
531
 
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
532
 
with this column, if the type is specified as <tt class="docutils literal"><span class="pre">None</span></tt>.
533
 
However, this feature is not fully implemented and
534
 
may not function in all cases.</p>
535
 
</li>
536
 
<li><strong>*args</strong> &#8211; Additional positional arguments include various
537
 
<a class="reference internal" href="#sqlalchemy.schema.SchemaItem" title="sqlalchemy.schema.SchemaItem"><tt class="xref py py-class docutils literal"><span class="pre">SchemaItem</span></tt></a> derived constructs which will be applied
538
 
as options to the column.  These include instances of
539
 
<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>,
540
 
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
541
 
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>
542
 
and <tt class="docutils literal"><span class="pre">unique</span></tt>.</li>
543
 
<li><strong>autoincrement</strong> &#8211; <p>This flag may be set to <tt class="docutils literal"><span class="pre">False</span></tt> to
544
 
indicate an integer primary key column that should not be
545
 
considered to be the &#8220;autoincrement&#8221; column, that is
546
 
the integer primary key column which generates values
547
 
implicitly upon INSERT and whose value is usually returned
548
 
via the DBAPI cursor.lastrowid attribute.   It defaults
549
 
to <tt class="docutils literal"><span class="pre">True</span></tt> to satisfy the common use case of a table
550
 
with a single integer primary key column.  If the table
551
 
has a composite primary key consisting of more than one
552
 
integer column, set this flag to True only on the
553
 
column that should be considered &#8220;autoincrement&#8221;.</p>
554
 
<p>The setting <em>only</em> has an effect for columns which are:</p>
555
 
<ul>
556
 
<li>Integer derived (i.e. INT, SMALLINT, BIGINT).</li>
557
 
<li>Part of the primary key</li>
558
 
<li>Are not referenced by any foreign keys, unless
559
 
the value is specified as <tt class="docutils literal"><span class="pre">'ignore_fk'</span></tt><p class="versionadded">
560
 
<span class="versionmodified">New in version 0.7.4.</span></p>
561
 
</li>
562
 
<li>have no server side or client side defaults (with the exception
563
 
of Postgresql SERIAL).</li>
564
 
</ul>
565
 
<p>The setting has these two effects on columns that meet the
566
 
above criteria:</p>
567
 
<ul>
568
 
<li>DDL issued for the column will include database-specific
569
 
keywords intended to signify this column as an
570
 
&#8220;autoincrement&#8221; column, such as AUTO INCREMENT on MySQL,
571
 
SERIAL on Postgresql, and IDENTITY on MS-SQL.  It does
572
 
<em>not</em> issue AUTOINCREMENT for SQLite since this is a
573
 
special SQLite flag that is not required for autoincrementing
574
 
behavior.  See the SQLite dialect documentation for
575
 
information on SQLite&#8217;s AUTOINCREMENT.</li>
576
 
<li>The column will be considered to be available as
577
 
cursor.lastrowid or equivalent, for those dialects which
578
 
&#8220;post fetch&#8221; newly inserted identifiers after a row has
579
 
been inserted (SQLite, MySQL, MS-SQL).  It does not have
580
 
any effect in this regard for databases that use sequences
581
 
to generate primary key identifiers (i.e. Firebird, Postgresql,
582
 
Oracle).</li>
583
 
</ul>
584
 
<p class="versionchanged">
585
 
<span class="versionmodified">Changed in version 0.7.4: </span><tt class="docutils literal"><span class="pre">autoincrement</span></tt> accepts a special value <tt class="docutils literal"><span class="pre">'ignore_fk'</span></tt>
586
 
to indicate that autoincrementing status regardless of foreign
587
 
key references.  This applies to certain composite foreign key
588
 
setups, such as the one demonstrated in the ORM documentation
589
 
at <a class="reference internal" href="../orm/relationships.html#post-update"><em>Rows that point to themselves / Mutually Dependent Rows</em></a>.</p>
590
 
</li>
591
 
<li><strong>default</strong> &#8211; <p>A scalar, Python callable, or
592
 
<a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.ColumnElement" title="sqlalchemy.sql.expression.ColumnElement"><tt class="xref py py-class docutils literal"><span class="pre">ColumnElement</span></tt></a> expression representing the
593
 
<em>default value</em> for this column, which will be invoked upon insert
594
 
if this column is otherwise not specified in the VALUES clause of
595
 
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
596
 
a positional argument; see that class for full detail on the
597
 
structure of the argument.</p>
598
 
<p>Contrast this argument to <tt class="docutils literal"><span class="pre">server_default</span></tt> which creates a
599
 
default generator on the database side.</p>
600
 
</li>
601
 
<li><strong>doc</strong> &#8211; optional String that can be used by the ORM or similar
602
 
to document attributes.   This attribute does not render SQL
603
 
comments (a future attribute &#8216;comment&#8217; will achieve that).</li>
604
 
<li><strong>key</strong> &#8211; An optional string identifier which will identify this
605
 
<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,
606
 
this is the only identifier referencing the <tt class="docutils literal"><span class="pre">Column</span></tt> within the
607
 
application, including ORM attribute mapping; the <tt class="docutils literal"><span class="pre">name</span></tt> field
608
 
is used only when rendering SQL.</li>
609
 
<li><strong>index</strong> &#8211; When <tt class="docutils literal"><span class="pre">True</span></tt>, indicates that the column is indexed.
610
 
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
611
 
table. To specify indexes with explicit names or indexes that
612
 
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
613
 
instead.</li>
614
 
<li><strong>info</strong> &#8211; Optional data dictionary which will be populated into the
615
 
<a class="reference internal" href="#sqlalchemy.schema.SchemaItem.info" title="sqlalchemy.schema.SchemaItem.info"><tt class="xref py py-attr docutils literal"><span class="pre">SchemaItem.info</span></tt></a> attribute of this object.</li>
616
 
<li><strong>nullable</strong> &#8211; If set to the default of <tt class="docutils literal"><span class="pre">True</span></tt>, indicates the
617
 
column will be rendered as allowing NULL, else it&#8217;s rendered as
618
 
NOT NULL. This parameter is only used when issuing CREATE TABLE
619
 
statements.</li>
620
 
<li><strong>onupdate</strong> &#8211; A scalar, Python callable, or
621
 
<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
622
 
default value to be applied to the column within UPDATE
623
 
statements, which wil be invoked upon update if this column is not
624
 
present in the SET clause of the update. This is a shortcut to
625
 
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
626
 
<tt class="docutils literal"><span class="pre">for_update=True</span></tt>.</li>
627
 
<li><strong>primary_key</strong> &#8211; If <tt class="docutils literal"><span class="pre">True</span></tt>, marks this column as a primary key
628
 
column. Multiple columns can have this flag set to specify
629
 
composite primary keys. As an alternative, the primary key of a
630
 
<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
631
 
<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>
632
 
<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
633
 
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
634
 
the DDL DEFAULT value for the column.</p>
635
 
<p>String types will be emitted as-is, surrounded by single quotes:</p>
636
 
<div class="highlight-python"><pre>Column('x', Text, server_default="val")
637
 
 
638
 
x TEXT DEFAULT 'val'</pre>
639
 
</div>
640
 
<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
641
 
rendered as-is, without quotes:</p>
642
 
<div class="highlight-python"><pre>Column('y', DateTime, server_default=text('NOW()'))0
643
 
 
644
 
y DATETIME DEFAULT NOW()</pre>
645
 
</div>
646
 
<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>
647
 
object upon initialization.</p>
648
 
<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
649
 
column will generate a default value on the database side which
650
 
will be available to SQLAlchemy for post-fetch after inserts. This
651
 
construct does not specify any DDL and the implementation is left
652
 
to the database, such as via a trigger.</p>
653
 
</li>
654
 
<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
655
 
representing a database-side default generation function. This
656
 
indicates to SQLAlchemy that a newly generated value will be
657
 
available after updates. This construct does not specify any DDL
658
 
and the implementation is left to the database, such as via a
659
 
trigger.</li>
660
 
<li><strong>quote</strong> &#8211; Force quoting of this column&#8217;s name on or off,
661
 
corresponding to <tt class="docutils literal"><span class="pre">True</span></tt> or <tt class="docutils literal"><span class="pre">False</span></tt>. When left at its default
662
 
of <tt class="docutils literal"><span class="pre">None</span></tt>, the column identifier will be quoted according to
663
 
whether the name is case sensitive (identifiers with at least one
664
 
upper case character are treated as case sensitive), or if it&#8217;s a
665
 
reserved word. This flag is only needed to force quoting of a
666
 
reserved word which is not known by the SQLAlchemy dialect.</li>
667
 
<li><strong>unique</strong> &#8211; When <tt class="docutils literal"><span class="pre">True</span></tt>, indicates that this column contains a
668
 
unique constraint, or if <tt class="docutils literal"><span class="pre">index</span></tt> is <tt class="docutils literal"><span class="pre">True</span></tt> as well, indicates
669
 
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.
670
 
To specify multiple columns in the constraint/index or to specify
671
 
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
672
 
<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>
673
 
</ul>
674
 
</td>
675
 
</tr>
676
 
</tbody>
677
 
</table>
678
 
</dd></dl>
679
 
 
680
 
<dl class="method">
681
 
<dt id="sqlalchemy.schema.Column.__le__">
682
 
<tt class="descname">__le__</tt><big>(</big><em>other</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Column.__le__" title="Permalink to this definition">¶</a></dt>
683
 
<dd><div class="inherited-member container">
684
 
<em>inherited from the</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.ColumnOperators.__le__" title="sqlalchemy.sql.operators.ColumnOperators.__le__"><tt class="xref py py-meth docutils literal"><span class="pre">__le__()</span></tt></a> <em>method of</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.ColumnOperators" title="sqlalchemy.sql.operators.ColumnOperators"><tt class="xref py py-class docutils literal"><span class="pre">ColumnOperators</span></tt></a></div>
685
 
<p>Implement the <tt class="docutils literal"><span class="pre">&lt;=</span></tt> operator.</p>
686
 
<p>In a column context, produces the clause <tt class="docutils literal"><span class="pre">a</span> <span class="pre">&lt;=</span> <span class="pre">b</span></tt>.</p>
687
 
</dd></dl>
688
 
 
689
 
<dl class="method">
690
 
<dt id="sqlalchemy.schema.Column.__lt__">
691
 
<tt class="descname">__lt__</tt><big>(</big><em>other</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Column.__lt__" title="Permalink to this definition">¶</a></dt>
692
 
<dd><div class="inherited-member container">
693
 
<em>inherited from the</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.ColumnOperators.__lt__" title="sqlalchemy.sql.operators.ColumnOperators.__lt__"><tt class="xref py py-meth docutils literal"><span class="pre">__lt__()</span></tt></a> <em>method of</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.ColumnOperators" title="sqlalchemy.sql.operators.ColumnOperators"><tt class="xref py py-class docutils literal"><span class="pre">ColumnOperators</span></tt></a></div>
694
 
<p>Implement the <tt class="docutils literal"><span class="pre">&lt;</span></tt> operator.</p>
695
 
<p>In a column context, produces the clause <tt class="docutils literal"><span class="pre">a</span> <span class="pre">&lt;</span> <span class="pre">b</span></tt>.</p>
696
 
</dd></dl>
697
 
 
698
 
<dl class="method">
699
 
<dt id="sqlalchemy.schema.Column.__ne__">
700
 
<tt class="descname">__ne__</tt><big>(</big><em>other</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Column.__ne__" title="Permalink to this definition">¶</a></dt>
701
 
<dd><div class="inherited-member container">
702
 
<em>inherited from the</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.ColumnOperators.__ne__" title="sqlalchemy.sql.operators.ColumnOperators.__ne__"><tt class="xref py py-meth docutils literal"><span class="pre">__ne__()</span></tt></a> <em>method of</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.ColumnOperators" title="sqlalchemy.sql.operators.ColumnOperators"><tt class="xref py py-class docutils literal"><span class="pre">ColumnOperators</span></tt></a></div>
703
 
<p>Implement the <tt class="docutils literal"><span class="pre">!=</span></tt> operator.</p>
704
 
<p>In a column context, produces the clause <tt class="docutils literal"><span class="pre">a</span> <span class="pre">!=</span> <span class="pre">b</span></tt>.
705
 
If the target is <tt class="docutils literal"><span class="pre">None</span></tt>, produces <tt class="docutils literal"><span class="pre">a</span> <span class="pre">IS</span> <span class="pre">NOT</span> <span class="pre">NULL</span></tt>.</p>
706
 
</dd></dl>
707
 
 
708
 
<dl class="attribute">
709
 
<dt id="sqlalchemy.schema.Column.anon_label">
710
 
<tt class="descname">anon_label</tt><a class="headerlink" href="#sqlalchemy.schema.Column.anon_label" title="Permalink to this definition">¶</a></dt>
711
 
<dd><div class="inherited-member container">
712
 
<em>inherited from the</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.ColumnElement.anon_label" title="sqlalchemy.sql.expression.ColumnElement.anon_label"><tt class="xref py py-attr docutils literal"><span class="pre">anon_label</span></tt></a> <em>attribute of</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.ColumnElement" title="sqlalchemy.sql.expression.ColumnElement"><tt class="xref py py-class docutils literal"><span class="pre">ColumnElement</span></tt></a></div>
713
 
<p>provides a constant &#8216;anonymous label&#8217; for this ColumnElement.</p>
714
 
<p>This is a label() expression which will be named at compile time.
715
 
The same label() is returned each time anon_label is called so
716
 
that expressions can reference anon_label multiple times, producing
717
 
the same label name at compile time.</p>
718
 
<p>the compiler uses this function automatically at compile time
719
 
for expressions that are known to be &#8216;unnamed&#8217; like binary
720
 
expressions and function calls.</p>
721
 
</dd></dl>
722
 
 
723
 
<dl class="method">
724
 
<dt id="sqlalchemy.schema.Column.append_foreign_key">
725
 
<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>
726
 
<dd></dd></dl>
727
 
 
728
 
<dl class="method">
729
 
<dt id="sqlalchemy.schema.Column.asc">
730
 
<tt class="descname">asc</tt><big>(</big><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Column.asc" title="Permalink to this definition">¶</a></dt>
731
 
<dd><div class="inherited-member container">
732
 
<em>inherited from the</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.ColumnOperators.asc" title="sqlalchemy.sql.operators.ColumnOperators.asc"><tt class="xref py py-meth docutils literal"><span class="pre">asc()</span></tt></a> <em>method of</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.ColumnOperators" title="sqlalchemy.sql.operators.ColumnOperators"><tt class="xref py py-class docutils literal"><span class="pre">ColumnOperators</span></tt></a></div>
733
 
<p>Produce a <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.asc" title="sqlalchemy.sql.expression.asc"><tt class="xref py py-func docutils literal"><span class="pre">asc()</span></tt></a> clause against the
734
 
parent object.</p>
735
 
</dd></dl>
736
 
 
737
 
<dl class="attribute">
738
 
<dt id="sqlalchemy.schema.Column.base_columns">
739
 
<tt class="descname">base_columns</tt><a class="headerlink" href="#sqlalchemy.schema.Column.base_columns" title="Permalink to this definition">¶</a></dt>
740
 
<dd><div class="inherited-member container">
741
 
<em>inherited from the</em> <tt class="xref py py-attr docutils literal"><span class="pre">base_columns</span></tt> <em>attribute of</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.ColumnElement" title="sqlalchemy.sql.expression.ColumnElement"><tt class="xref py py-class docutils literal"><span class="pre">ColumnElement</span></tt></a></div>
742
 
</dd></dl>
743
 
 
744
 
<dl class="method">
745
 
<dt id="sqlalchemy.schema.Column.between">
746
 
<tt class="descname">between</tt><big>(</big><em>cleft</em>, <em>cright</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Column.between" title="Permalink to this definition">¶</a></dt>
747
 
<dd><div class="inherited-member container">
748
 
<em>inherited from the</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.ColumnOperators.between" title="sqlalchemy.sql.operators.ColumnOperators.between"><tt class="xref py py-meth docutils literal"><span class="pre">between()</span></tt></a> <em>method of</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.ColumnOperators" title="sqlalchemy.sql.operators.ColumnOperators"><tt class="xref py py-class docutils literal"><span class="pre">ColumnOperators</span></tt></a></div>
749
 
<p>Produce a <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.between" title="sqlalchemy.sql.expression.between"><tt class="xref py py-func docutils literal"><span class="pre">between()</span></tt></a> clause against
750
 
the parent object, given the lower and upper range.</p>
751
 
</dd></dl>
752
 
 
753
 
<dl class="attribute">
754
 
<dt id="sqlalchemy.schema.Column.bind">
755
 
<tt class="descname">bind</tt><em class="property"> = None</em><a class="headerlink" href="#sqlalchemy.schema.Column.bind" title="Permalink to this definition">¶</a></dt>
756
 
<dd></dd></dl>
757
 
 
758
 
<dl class="method">
759
 
<dt id="sqlalchemy.schema.Column.collate">
760
 
<tt class="descname">collate</tt><big>(</big><em>collation</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Column.collate" title="Permalink to this definition">¶</a></dt>
761
 
<dd><div class="inherited-member container">
762
 
<em>inherited from the</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.ColumnOperators.collate" title="sqlalchemy.sql.operators.ColumnOperators.collate"><tt class="xref py py-meth docutils literal"><span class="pre">collate()</span></tt></a> <em>method of</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.ColumnOperators" title="sqlalchemy.sql.operators.ColumnOperators"><tt class="xref py py-class docutils literal"><span class="pre">ColumnOperators</span></tt></a></div>
763
 
<p>Produce a <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.collate" title="sqlalchemy.sql.expression.collate"><tt class="xref py py-func docutils literal"><span class="pre">collate()</span></tt></a> clause against
764
 
the parent object, given the collation string.</p>
765
 
</dd></dl>
766
 
 
767
 
<dl class="attribute">
768
 
<dt id="sqlalchemy.schema.Column.comparator">
769
 
<tt class="descname">comparator</tt><a class="headerlink" href="#sqlalchemy.schema.Column.comparator" title="Permalink to this definition">¶</a></dt>
770
 
<dd><div class="inherited-member container">
771
 
<em>inherited from the</em> <tt class="xref py py-attr docutils literal"><span class="pre">comparator</span></tt> <em>attribute of</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.ColumnElement" title="sqlalchemy.sql.expression.ColumnElement"><tt class="xref py py-class docutils literal"><span class="pre">ColumnElement</span></tt></a></div>
772
 
</dd></dl>
773
 
 
774
 
<dl class="method">
775
 
<dt id="sqlalchemy.schema.Column.compare">
776
 
<tt class="descname">compare</tt><big>(</big><em>other</em>, <em>use_proxies=False</em>, <em>equivalents=None</em>, <em>**kw</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Column.compare" title="Permalink to this definition">¶</a></dt>
777
 
<dd><div class="inherited-member container">
778
 
<em>inherited from the</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.ColumnElement.compare" title="sqlalchemy.sql.expression.ColumnElement.compare"><tt class="xref py py-meth docutils literal"><span class="pre">compare()</span></tt></a> <em>method of</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.ColumnElement" title="sqlalchemy.sql.expression.ColumnElement"><tt class="xref py py-class docutils literal"><span class="pre">ColumnElement</span></tt></a></div>
779
 
<p>Compare this ColumnElement to another.</p>
780
 
<p>Special arguments understood:</p>
781
 
<table class="docutils field-list" frame="void" rules="none">
782
 
<col class="field-name" />
783
 
<col class="field-body" />
784
 
<tbody valign="top">
785
 
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
786
 
<li><strong>use_proxies</strong> &#8211; when True, consider two columns that
787
 
share a common base column as equivalent (i.e. shares_lineage())</li>
788
 
<li><strong>equivalents</strong> &#8211; a dictionary of columns as keys mapped to sets
789
 
of columns. If the given &#8220;other&#8221; column is present in this
790
 
dictionary, if any of the columns in the corresponding set() pass the
791
 
comparison test, the result is True. This is used to expand the
792
 
comparison to other columns that may be known to be equivalent to
793
 
this one via foreign key or other criterion.</li>
794
 
</ul>
795
 
</td>
796
 
</tr>
797
 
</tbody>
798
 
</table>
799
 
</dd></dl>
800
 
 
801
 
<dl class="method">
802
 
<dt id="sqlalchemy.schema.Column.compile">
803
 
<tt class="descname">compile</tt><big>(</big><em>bind=None</em>, <em>dialect=None</em>, <em>**kw</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Column.compile" title="Permalink to this definition">¶</a></dt>
804
 
<dd><div class="inherited-member container">
805
 
<em>inherited from the</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.ClauseElement.compile" title="sqlalchemy.sql.expression.ClauseElement.compile"><tt class="xref py py-meth docutils literal"><span class="pre">compile()</span></tt></a> <em>method of</em> <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></div>
806
 
<p>Compile this SQL expression.</p>
807
 
<p>The return value is a <a class="reference internal" href="internals.html#sqlalchemy.engine.interfaces.Compiled" title="sqlalchemy.engine.interfaces.Compiled"><tt class="xref py py-class docutils literal"><span class="pre">Compiled</span></tt></a> object.
808
 
Calling <tt class="docutils literal"><span class="pre">str()</span></tt> or <tt class="docutils literal"><span class="pre">unicode()</span></tt> on the returned value will yield a
809
 
string representation of the result. The
810
 
<a class="reference internal" href="internals.html#sqlalchemy.engine.interfaces.Compiled" title="sqlalchemy.engine.interfaces.Compiled"><tt class="xref py py-class docutils literal"><span class="pre">Compiled</span></tt></a> object also can return a
811
 
dictionary of bind parameter names and values
812
 
using the <tt class="docutils literal"><span class="pre">params</span></tt> accessor.</p>
813
 
<table class="docutils field-list" frame="void" rules="none">
814
 
<col class="field-name" />
815
 
<col class="field-body" />
816
 
<tbody valign="top">
817
 
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
818
 
<li><strong>bind</strong> &#8211; An <tt class="docutils literal"><span class="pre">Engine</span></tt> or <tt class="docutils literal"><span class="pre">Connection</span></tt> from which a
819
 
<tt class="docutils literal"><span class="pre">Compiled</span></tt> will be acquired. This argument takes precedence over
820
 
this <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>&#8216;s bound engine, if any.</li>
821
 
<li><strong>column_keys</strong> &#8211; Used for INSERT and UPDATE statements, a list of
822
 
column names which should be present in the VALUES clause of the
823
 
compiled statement. If <tt class="docutils literal"><span class="pre">None</span></tt>, all columns from the target table
824
 
object are rendered.</li>
825
 
<li><strong>dialect</strong> &#8211; A <tt class="docutils literal"><span class="pre">Dialect</span></tt> instance from which a <tt class="docutils literal"><span class="pre">Compiled</span></tt>
826
 
will be acquired. This argument takes precedence over the <cite>bind</cite>
827
 
argument as well as this <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>&#8216;s bound engine, if
828
 
any.</li>
829
 
<li><strong>inline</strong> &#8211; Used for INSERT statements, for a dialect which does
830
 
not support inline retrieval of newly generated primary key
831
 
columns, will force the expression used to create the new primary
832
 
key value to be rendered inline within the INSERT statement&#8217;s
833
 
VALUES clause. This typically refers to Sequence execution but may
834
 
also refer to any server-side default generation function
835
 
associated with a primary key <cite>Column</cite>.</li>
836
 
</ul>
837
 
</td>
838
 
</tr>
839
 
</tbody>
840
 
</table>
841
 
</dd></dl>
842
 
 
843
 
<dl class="method">
844
 
<dt id="sqlalchemy.schema.Column.concat">
845
 
<tt class="descname">concat</tt><big>(</big><em>other</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Column.concat" title="Permalink to this definition">¶</a></dt>
846
 
<dd><div class="inherited-member container">
847
 
<em>inherited from the</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.ColumnOperators.concat" title="sqlalchemy.sql.operators.ColumnOperators.concat"><tt class="xref py py-meth docutils literal"><span class="pre">concat()</span></tt></a> <em>method of</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.ColumnOperators" title="sqlalchemy.sql.operators.ColumnOperators"><tt class="xref py py-class docutils literal"><span class="pre">ColumnOperators</span></tt></a></div>
848
 
<p>Implement the &#8216;concat&#8217; operator.</p>
849
 
<p>In a column context, produces the clause <tt class="docutils literal"><span class="pre">a</span> <span class="pre">||</span> <span class="pre">b</span></tt>,
850
 
or uses the <tt class="docutils literal"><span class="pre">concat()</span></tt> operator on MySQL.</p>
851
 
</dd></dl>
852
 
 
853
 
<dl class="method">
854
 
<dt id="sqlalchemy.schema.Column.contains">
855
 
<tt class="descname">contains</tt><big>(</big><em>other</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Column.contains" title="Permalink to this definition">¶</a></dt>
856
 
<dd><div class="inherited-member container">
857
 
<em>inherited from the</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.ColumnOperators.contains" title="sqlalchemy.sql.operators.ColumnOperators.contains"><tt class="xref py py-meth docutils literal"><span class="pre">contains()</span></tt></a> <em>method of</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.ColumnOperators" title="sqlalchemy.sql.operators.ColumnOperators"><tt class="xref py py-class docutils literal"><span class="pre">ColumnOperators</span></tt></a></div>
858
 
<p>Implement the &#8216;contains&#8217; operator.</p>
859
 
<p>In a column context, produces the clause <tt class="docutils literal"><span class="pre">LIKE</span> <span class="pre">'%&lt;other&gt;%'</span></tt></p>
860
 
</dd></dl>
861
 
 
862
 
<dl class="method">
863
 
<dt id="sqlalchemy.schema.Column.copy">
864
 
<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>
865
 
<dd><p>Create a copy of this <tt class="docutils literal"><span class="pre">Column</span></tt>, unitialized.</p>
866
 
<p>This is used in <tt class="docutils literal"><span class="pre">Table.tometadata</span></tt>.</p>
867
 
</dd></dl>
868
 
 
869
 
<dl class="attribute">
870
 
<dt id="sqlalchemy.schema.Column.default">
871
 
<tt class="descname">default</tt><em class="property"> = None</em><a class="headerlink" href="#sqlalchemy.schema.Column.default" title="Permalink to this definition">¶</a></dt>
872
 
<dd></dd></dl>
873
 
 
874
 
<dl class="method">
875
 
<dt id="sqlalchemy.schema.Column.desc">
876
 
<tt class="descname">desc</tt><big>(</big><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Column.desc" title="Permalink to this definition">¶</a></dt>
877
 
<dd><div class="inherited-member container">
878
 
<em>inherited from the</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.ColumnOperators.desc" title="sqlalchemy.sql.operators.ColumnOperators.desc"><tt class="xref py py-meth docutils literal"><span class="pre">desc()</span></tt></a> <em>method of</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.ColumnOperators" title="sqlalchemy.sql.operators.ColumnOperators"><tt class="xref py py-class docutils literal"><span class="pre">ColumnOperators</span></tt></a></div>
879
 
<p>Produce a <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.desc" title="sqlalchemy.sql.expression.desc"><tt class="xref py py-func docutils literal"><span class="pre">desc()</span></tt></a> clause against the
880
 
parent object.</p>
881
 
</dd></dl>
882
 
 
883
 
<dl class="attribute">
884
 
<dt id="sqlalchemy.schema.Column.description">
885
 
<tt class="descname">description</tt><a class="headerlink" href="#sqlalchemy.schema.Column.description" title="Permalink to this definition">¶</a></dt>
886
 
<dd><div class="inherited-member container">
887
 
<em>inherited from the</em> <tt class="xref py py-attr docutils literal"><span class="pre">description</span></tt> <em>attribute of</em> <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">ColumnClause</span></tt></a></div>
888
 
</dd></dl>
889
 
 
890
 
<dl class="attribute">
891
 
<dt id="sqlalchemy.schema.Column.dispatch">
892
 
<tt class="descname">dispatch</tt><a class="headerlink" href="#sqlalchemy.schema.Column.dispatch" title="Permalink to this definition">¶</a></dt>
893
 
<dd><p>alias of <tt class="xref py py-class docutils literal"><span class="pre">DDLEventsDispatch</span></tt></p>
894
 
</dd></dl>
895
 
 
896
 
<dl class="method">
897
 
<dt id="sqlalchemy.schema.Column.distinct">
898
 
<tt class="descname">distinct</tt><big>(</big><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Column.distinct" title="Permalink to this definition">¶</a></dt>
899
 
<dd><div class="inherited-member container">
900
 
<em>inherited from the</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.ColumnOperators.distinct" title="sqlalchemy.sql.operators.ColumnOperators.distinct"><tt class="xref py py-meth docutils literal"><span class="pre">distinct()</span></tt></a> <em>method of</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.ColumnOperators" title="sqlalchemy.sql.operators.ColumnOperators"><tt class="xref py py-class docutils literal"><span class="pre">ColumnOperators</span></tt></a></div>
901
 
<p>Produce a <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.distinct" title="sqlalchemy.sql.expression.distinct"><tt class="xref py py-func docutils literal"><span class="pre">distinct()</span></tt></a> clause against the
902
 
parent object.</p>
903
 
</dd></dl>
904
 
 
905
 
<dl class="method">
906
 
<dt id="sqlalchemy.schema.Column.endswith">
907
 
<tt class="descname">endswith</tt><big>(</big><em>other</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Column.endswith" title="Permalink to this definition">¶</a></dt>
908
 
<dd><div class="inherited-member container">
909
 
<em>inherited from the</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.ColumnOperators.endswith" title="sqlalchemy.sql.operators.ColumnOperators.endswith"><tt class="xref py py-meth docutils literal"><span class="pre">endswith()</span></tt></a> <em>method of</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.ColumnOperators" title="sqlalchemy.sql.operators.ColumnOperators"><tt class="xref py py-class docutils literal"><span class="pre">ColumnOperators</span></tt></a></div>
910
 
<p>Implement the &#8216;endswith&#8217; operator.</p>
911
 
<p>In a column context, produces the clause <tt class="docutils literal"><span class="pre">LIKE</span> <span class="pre">'%&lt;other&gt;'</span></tt></p>
912
 
</dd></dl>
913
 
 
914
 
<dl class="attribute">
915
 
<dt id="sqlalchemy.schema.Column.expression">
916
 
<tt class="descname">expression</tt><a class="headerlink" href="#sqlalchemy.schema.Column.expression" title="Permalink to this definition">¶</a></dt>
917
 
<dd><div class="inherited-member container">
918
 
<em>inherited from the</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.ColumnElement.expression" title="sqlalchemy.sql.expression.ColumnElement.expression"><tt class="xref py py-attr docutils literal"><span class="pre">expression</span></tt></a> <em>attribute of</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.ColumnElement" title="sqlalchemy.sql.expression.ColumnElement"><tt class="xref py py-class docutils literal"><span class="pre">ColumnElement</span></tt></a></div>
919
 
<p>Return a column expression.</p>
920
 
<p>Part of the inspection interface; returns self.</p>
921
 
</dd></dl>
922
 
 
923
 
<dl class="attribute">
924
 
<dt id="sqlalchemy.schema.Column.foreign_keys">
925
 
<tt class="descname">foreign_keys</tt><em class="property"> = []</em><a class="headerlink" href="#sqlalchemy.schema.Column.foreign_keys" title="Permalink to this definition">¶</a></dt>
926
 
<dd></dd></dl>
927
 
 
928
 
<dl class="method">
929
 
<dt id="sqlalchemy.schema.Column.get_children">
930
 
<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>
931
 
<dd></dd></dl>
932
 
 
933
 
<dl class="method">
934
 
<dt id="sqlalchemy.schema.Column.ilike">
935
 
<tt class="descname">ilike</tt><big>(</big><em>other</em>, <em>escape=None</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Column.ilike" title="Permalink to this definition">¶</a></dt>
936
 
<dd><div class="inherited-member container">
937
 
<em>inherited from the</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.ColumnOperators.ilike" title="sqlalchemy.sql.operators.ColumnOperators.ilike"><tt class="xref py py-meth docutils literal"><span class="pre">ilike()</span></tt></a> <em>method of</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.ColumnOperators" title="sqlalchemy.sql.operators.ColumnOperators"><tt class="xref py py-class docutils literal"><span class="pre">ColumnOperators</span></tt></a></div>
938
 
<p>Implement the <tt class="docutils literal"><span class="pre">ilike</span></tt> operator.</p>
939
 
<p>In a column context, produces the clause <tt class="docutils literal"><span class="pre">a</span> <span class="pre">ILIKE</span> <span class="pre">other</span></tt>.</p>
940
 
<p>E.g.:</p>
941
 
<div class="highlight-python"><div class="highlight"><pre><span class="n">select</span><span class="p">([</span><span class="n">sometable</span><span class="p">])</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">sometable</span><span class="o">.</span><span class="n">c</span><span class="o">.</span><span class="n">column</span><span class="o">.</span><span class="n">ilike</span><span class="p">(</span><span class="s">&quot;</span><span class="si">%f</span><span class="s">oobar%&quot;</span><span class="p">))</span></pre></div>
942
 
</div>
943
 
<table class="docutils field-list" frame="void" rules="none">
944
 
<col class="field-name" />
945
 
<col class="field-body" />
946
 
<tbody valign="top">
947
 
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
948
 
<li><strong>other</strong> &#8211; expression to be compared</li>
949
 
<li><strong>escape</strong> &#8211; <p>optional escape character, renders the <tt class="docutils literal"><span class="pre">ESCAPE</span></tt>
950
 
keyword, e.g.:</p>
951
 
<div class="highlight-python"><div class="highlight"><pre><span class="n">somecolumn</span><span class="o">.</span><span class="n">ilike</span><span class="p">(</span><span class="s">&quot;foo/%bar&quot;</span><span class="p">,</span> <span class="n">escape</span><span class="o">=</span><span class="s">&quot;/&quot;</span><span class="p">)</span></pre></div>
952
 
</div>
953
 
</li>
954
 
</ul>
955
 
</td>
956
 
</tr>
957
 
</tbody>
958
 
</table>
959
 
<div class="admonition-see-also admonition seealso">
960
 
<p class="first admonition-title">See also</p>
961
 
<p class="last"><a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.ColumnOperators.like" title="sqlalchemy.sql.operators.ColumnOperators.like"><tt class="xref py py-meth docutils literal"><span class="pre">ColumnOperators.like()</span></tt></a></p>
962
 
</div>
963
 
</dd></dl>
964
 
 
965
 
<dl class="method">
966
 
<dt id="sqlalchemy.schema.Column.in_">
967
 
<tt class="descname">in_</tt><big>(</big><em>other</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Column.in_" title="Permalink to this definition">¶</a></dt>
968
 
<dd><div class="inherited-member container">
969
 
<em>inherited from the</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.ColumnOperators.in_" title="sqlalchemy.sql.operators.ColumnOperators.in_"><tt class="xref py py-meth docutils literal"><span class="pre">in_()</span></tt></a> <em>method of</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.ColumnOperators" title="sqlalchemy.sql.operators.ColumnOperators"><tt class="xref py py-class docutils literal"><span class="pre">ColumnOperators</span></tt></a></div>
970
 
<p>Implement the <tt class="docutils literal"><span class="pre">in</span></tt> operator.</p>
971
 
<p>In a column context, produces the clause <tt class="docutils literal"><span class="pre">a</span> <span class="pre">IN</span> <span class="pre">other</span></tt>.
972
 
&#8220;other&#8221; may be a tuple/list of column expressions,
973
 
or a <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.select" title="sqlalchemy.sql.expression.select"><tt class="xref py py-func docutils literal"><span class="pre">select()</span></tt></a> construct.</p>
974
 
</dd></dl>
975
 
 
976
 
<dl class="attribute">
977
 
<dt id="sqlalchemy.schema.Column.info">
978
 
<tt class="descname">info</tt><a class="headerlink" href="#sqlalchemy.schema.Column.info" title="Permalink to this definition">¶</a></dt>
979
 
<dd><div class="inherited-member container">
980
 
<em>inherited from the</em> <a class="reference internal" href="#sqlalchemy.schema.SchemaItem.info" title="sqlalchemy.schema.SchemaItem.info"><tt class="xref py py-attr docutils literal"><span class="pre">info</span></tt></a> <em>attribute of</em> <a class="reference internal" href="#sqlalchemy.schema.SchemaItem" title="sqlalchemy.schema.SchemaItem"><tt class="xref py py-class docutils literal"><span class="pre">SchemaItem</span></tt></a></div>
981
 
<p>Info dictionary associated with the object, allowing user-defined
982
 
data to be associated with this <a class="reference internal" href="#sqlalchemy.schema.SchemaItem" title="sqlalchemy.schema.SchemaItem"><tt class="xref py py-class docutils literal"><span class="pre">SchemaItem</span></tt></a>.</p>
983
 
<p>The dictionary is automatically generated when first accessed.
984
 
It can also be specified in the constructor of some objects,
985
 
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> and <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>
986
 
</dd></dl>
987
 
 
988
 
<dl class="method">
989
 
<dt id="sqlalchemy.schema.Column.is_">
990
 
<tt class="descname">is_</tt><big>(</big><em>other</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Column.is_" title="Permalink to this definition">¶</a></dt>
991
 
<dd><div class="inherited-member container">
992
 
<em>inherited from the</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.ColumnOperators.is_" title="sqlalchemy.sql.operators.ColumnOperators.is_"><tt class="xref py py-meth docutils literal"><span class="pre">is_()</span></tt></a> <em>method of</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.ColumnOperators" title="sqlalchemy.sql.operators.ColumnOperators"><tt class="xref py py-class docutils literal"><span class="pre">ColumnOperators</span></tt></a></div>
993
 
<p>Implement the <tt class="docutils literal"><span class="pre">IS</span></tt> operator.</p>
994
 
<p>Normally, <tt class="docutils literal"><span class="pre">IS</span></tt> is generated automatically when comparing to a
995
 
value of <tt class="docutils literal"><span class="pre">None</span></tt>, which resolves to <tt class="docutils literal"><span class="pre">NULL</span></tt>.  However, explicit
996
 
usage of <tt class="docutils literal"><span class="pre">IS</span></tt> may be desirable if comparing to boolean values
997
 
on certain platforms.</p>
998
 
<p class="versionadded">
999
 
<span class="versionmodified">New in version 0.7.9.</span></p>
1000
 
<div class="admonition-see-also admonition seealso">
1001
 
<p class="first admonition-title">See also</p>
1002
 
<p class="last"><a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.ColumnOperators.isnot" title="sqlalchemy.sql.operators.ColumnOperators.isnot"><tt class="xref py py-meth docutils literal"><span class="pre">ColumnOperators.isnot()</span></tt></a></p>
1003
 
</div>
1004
 
</dd></dl>
1005
 
 
1006
 
<dl class="attribute">
1007
 
<dt id="sqlalchemy.schema.Column.is_clause_element">
1008
 
<tt class="descname">is_clause_element</tt><em class="property"> = True</em><a class="headerlink" href="#sqlalchemy.schema.Column.is_clause_element" title="Permalink to this definition">¶</a></dt>
1009
 
<dd></dd></dl>
1010
 
 
1011
 
<dl class="attribute">
1012
 
<dt id="sqlalchemy.schema.Column.is_selectable">
1013
 
<tt class="descname">is_selectable</tt><em class="property"> = False</em><a class="headerlink" href="#sqlalchemy.schema.Column.is_selectable" title="Permalink to this definition">¶</a></dt>
1014
 
<dd></dd></dl>
1015
 
 
1016
 
<dl class="method">
1017
 
<dt id="sqlalchemy.schema.Column.isnot">
1018
 
<tt class="descname">isnot</tt><big>(</big><em>other</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Column.isnot" title="Permalink to this definition">¶</a></dt>
1019
 
<dd><div class="inherited-member container">
1020
 
<em>inherited from the</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.ColumnOperators.isnot" title="sqlalchemy.sql.operators.ColumnOperators.isnot"><tt class="xref py py-meth docutils literal"><span class="pre">isnot()</span></tt></a> <em>method of</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.ColumnOperators" title="sqlalchemy.sql.operators.ColumnOperators"><tt class="xref py py-class docutils literal"><span class="pre">ColumnOperators</span></tt></a></div>
1021
 
<p>Implement the <tt class="docutils literal"><span class="pre">IS</span> <span class="pre">NOT</span></tt> operator.</p>
1022
 
<p>Normally, <tt class="docutils literal"><span class="pre">IS</span> <span class="pre">NOT</span></tt> is generated automatically when comparing to a
1023
 
value of <tt class="docutils literal"><span class="pre">None</span></tt>, which resolves to <tt class="docutils literal"><span class="pre">NULL</span></tt>.  However, explicit
1024
 
usage of <tt class="docutils literal"><span class="pre">IS</span> <span class="pre">NOT</span></tt> may be desirable if comparing to boolean values
1025
 
on certain platforms.</p>
1026
 
<p class="versionadded">
1027
 
<span class="versionmodified">New in version 0.7.9.</span></p>
1028
 
<div class="admonition-see-also admonition seealso">
1029
 
<p class="first admonition-title">See also</p>
1030
 
<p class="last"><a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.ColumnOperators.is_" title="sqlalchemy.sql.operators.ColumnOperators.is_"><tt class="xref py py-meth docutils literal"><span class="pre">ColumnOperators.is_()</span></tt></a></p>
1031
 
</div>
1032
 
</dd></dl>
1033
 
 
1034
 
<dl class="method">
1035
 
<dt id="sqlalchemy.schema.Column.label">
1036
 
<tt class="descname">label</tt><big>(</big><em>name</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Column.label" title="Permalink to this definition">¶</a></dt>
1037
 
<dd><div class="inherited-member container">
1038
 
<em>inherited from the</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.ColumnElement.label" title="sqlalchemy.sql.expression.ColumnElement.label"><tt class="xref py py-meth docutils literal"><span class="pre">label()</span></tt></a> <em>method of</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.ColumnElement" title="sqlalchemy.sql.expression.ColumnElement"><tt class="xref py py-class docutils literal"><span class="pre">ColumnElement</span></tt></a></div>
1039
 
<p>Produce a column label, i.e. <tt class="docutils literal"><span class="pre">&lt;columnname&gt;</span> <span class="pre">AS</span> <span class="pre">&lt;name&gt;</span></tt>.</p>
1040
 
<p>This is a shortcut to the <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.label" title="sqlalchemy.sql.expression.label"><tt class="xref py py-func docutils literal"><span class="pre">label()</span></tt></a> function.</p>
1041
 
<p>if &#8216;name&#8217; is None, an anonymous label name will be generated.</p>
1042
 
</dd></dl>
1043
 
 
1044
 
<dl class="method">
1045
 
<dt id="sqlalchemy.schema.Column.like">
1046
 
<tt class="descname">like</tt><big>(</big><em>other</em>, <em>escape=None</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Column.like" title="Permalink to this definition">¶</a></dt>
1047
 
<dd><div class="inherited-member container">
1048
 
<em>inherited from the</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.ColumnOperators.like" title="sqlalchemy.sql.operators.ColumnOperators.like"><tt class="xref py py-meth docutils literal"><span class="pre">like()</span></tt></a> <em>method of</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.ColumnOperators" title="sqlalchemy.sql.operators.ColumnOperators"><tt class="xref py py-class docutils literal"><span class="pre">ColumnOperators</span></tt></a></div>
1049
 
<p>Implement the <tt class="docutils literal"><span class="pre">like</span></tt> operator.</p>
1050
 
<p>In a column context, produces the clause <tt class="docutils literal"><span class="pre">a</span> <span class="pre">LIKE</span> <span class="pre">other</span></tt>.</p>
1051
 
<p>E.g.:</p>
1052
 
<div class="highlight-python"><div class="highlight"><pre><span class="n">select</span><span class="p">([</span><span class="n">sometable</span><span class="p">])</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">sometable</span><span class="o">.</span><span class="n">c</span><span class="o">.</span><span class="n">column</span><span class="o">.</span><span class="n">like</span><span class="p">(</span><span class="s">&quot;</span><span class="si">%f</span><span class="s">oobar%&quot;</span><span class="p">))</span></pre></div>
1053
 
</div>
1054
 
<table class="docutils field-list" frame="void" rules="none">
1055
 
<col class="field-name" />
1056
 
<col class="field-body" />
1057
 
<tbody valign="top">
1058
 
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
1059
 
<li><strong>other</strong> &#8211; expression to be compared</li>
1060
 
<li><strong>escape</strong> &#8211; <p>optional escape character, renders the <tt class="docutils literal"><span class="pre">ESCAPE</span></tt>
1061
 
keyword, e.g.:</p>
1062
 
<div class="highlight-python"><div class="highlight"><pre><span class="n">somecolumn</span><span class="o">.</span><span class="n">like</span><span class="p">(</span><span class="s">&quot;foo/%bar&quot;</span><span class="p">,</span> <span class="n">escape</span><span class="o">=</span><span class="s">&quot;/&quot;</span><span class="p">)</span></pre></div>
1063
 
</div>
1064
 
</li>
1065
 
</ul>
1066
 
</td>
1067
 
</tr>
1068
 
</tbody>
1069
 
</table>
1070
 
<div class="admonition-see-also admonition seealso">
1071
 
<p class="first admonition-title">See also</p>
1072
 
<p class="last"><a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.ColumnOperators.ilike" title="sqlalchemy.sql.operators.ColumnOperators.ilike"><tt class="xref py py-meth docutils literal"><span class="pre">ColumnOperators.ilike()</span></tt></a></p>
1073
 
</div>
1074
 
</dd></dl>
1075
 
 
1076
 
<dl class="method">
1077
 
<dt id="sqlalchemy.schema.Column.match">
1078
 
<tt class="descname">match</tt><big>(</big><em>other</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Column.match" title="Permalink to this definition">¶</a></dt>
1079
 
<dd><div class="inherited-member container">
1080
 
<em>inherited from the</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.ColumnOperators.match" title="sqlalchemy.sql.operators.ColumnOperators.match"><tt class="xref py py-meth docutils literal"><span class="pre">match()</span></tt></a> <em>method of</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.ColumnOperators" title="sqlalchemy.sql.operators.ColumnOperators"><tt class="xref py py-class docutils literal"><span class="pre">ColumnOperators</span></tt></a></div>
1081
 
<p>Implements the &#8216;match&#8217; operator.</p>
1082
 
<p>In a column context, this produces a MATCH clause, i.e.
1083
 
<tt class="docutils literal"><span class="pre">MATCH</span> <span class="pre">'&lt;other&gt;'</span></tt>.  The allowed contents of <tt class="docutils literal"><span class="pre">other</span></tt>
1084
 
are database backend specific.</p>
1085
 
</dd></dl>
1086
 
 
1087
 
<dl class="method">
1088
 
<dt id="sqlalchemy.schema.Column.notilike">
1089
 
<tt class="descname">notilike</tt><big>(</big><em>other</em>, <em>escape=None</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Column.notilike" title="Permalink to this definition">¶</a></dt>
1090
 
<dd><div class="inherited-member container">
1091
 
<em>inherited from the</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.ColumnOperators.notilike" title="sqlalchemy.sql.operators.ColumnOperators.notilike"><tt class="xref py py-meth docutils literal"><span class="pre">notilike()</span></tt></a> <em>method of</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.ColumnOperators" title="sqlalchemy.sql.operators.ColumnOperators"><tt class="xref py py-class docutils literal"><span class="pre">ColumnOperators</span></tt></a></div>
1092
 
<p>implement the <tt class="docutils literal"><span class="pre">NOT</span> <span class="pre">ILIKE</span></tt> operator.</p>
1093
 
<p>This is equivalent to using negation with
1094
 
<a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.ColumnOperators.ilike" title="sqlalchemy.sql.operators.ColumnOperators.ilike"><tt class="xref py py-meth docutils literal"><span class="pre">ColumnOperators.ilike()</span></tt></a>, i.e. <tt class="docutils literal"><span class="pre">~x.ilike(y)</span></tt>.</p>
1095
 
<p class="versionadded">
1096
 
<span class="versionmodified">New in version 0.8.</span></p>
1097
 
<div class="admonition-see-also admonition seealso">
1098
 
<p class="first admonition-title">See also</p>
1099
 
<p class="last"><a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.ColumnOperators.ilike" title="sqlalchemy.sql.operators.ColumnOperators.ilike"><tt class="xref py py-meth docutils literal"><span class="pre">ColumnOperators.ilike()</span></tt></a></p>
1100
 
</div>
1101
 
</dd></dl>
1102
 
 
1103
 
<dl class="method">
1104
 
<dt id="sqlalchemy.schema.Column.notin_">
1105
 
<tt class="descname">notin_</tt><big>(</big><em>other</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Column.notin_" title="Permalink to this definition">¶</a></dt>
1106
 
<dd><div class="inherited-member container">
1107
 
<em>inherited from the</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.ColumnOperators.notin_" title="sqlalchemy.sql.operators.ColumnOperators.notin_"><tt class="xref py py-meth docutils literal"><span class="pre">notin_()</span></tt></a> <em>method of</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.ColumnOperators" title="sqlalchemy.sql.operators.ColumnOperators"><tt class="xref py py-class docutils literal"><span class="pre">ColumnOperators</span></tt></a></div>
1108
 
<p>implement the <tt class="docutils literal"><span class="pre">NOT</span> <span class="pre">IN</span></tt> operator.</p>
1109
 
<p>This is equivalent to using negation with <a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.ColumnOperators.in_" title="sqlalchemy.sql.operators.ColumnOperators.in_"><tt class="xref py py-meth docutils literal"><span class="pre">ColumnOperators.in_()</span></tt></a>,
1110
 
i.e. <tt class="docutils literal"><span class="pre">~x.in_(y)</span></tt>.</p>
1111
 
<p class="versionadded">
1112
 
<span class="versionmodified">New in version 0.8.</span></p>
1113
 
<div class="admonition-see-also admonition seealso">
1114
 
<p class="first admonition-title">See also</p>
1115
 
<p class="last"><a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.ColumnOperators.in_" title="sqlalchemy.sql.operators.ColumnOperators.in_"><tt class="xref py py-meth docutils literal"><span class="pre">ColumnOperators.in_()</span></tt></a></p>
1116
 
</div>
1117
 
</dd></dl>
1118
 
 
1119
 
<dl class="method">
1120
 
<dt id="sqlalchemy.schema.Column.notlike">
1121
 
<tt class="descname">notlike</tt><big>(</big><em>other</em>, <em>escape=None</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Column.notlike" title="Permalink to this definition">¶</a></dt>
1122
 
<dd><div class="inherited-member container">
1123
 
<em>inherited from the</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.ColumnOperators.notlike" title="sqlalchemy.sql.operators.ColumnOperators.notlike"><tt class="xref py py-meth docutils literal"><span class="pre">notlike()</span></tt></a> <em>method of</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.ColumnOperators" title="sqlalchemy.sql.operators.ColumnOperators"><tt class="xref py py-class docutils literal"><span class="pre">ColumnOperators</span></tt></a></div>
1124
 
<p>implement the <tt class="docutils literal"><span class="pre">NOT</span> <span class="pre">LIKE</span></tt> operator.</p>
1125
 
<p>This is equivalent to using negation with
1126
 
<a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.ColumnOperators.like" title="sqlalchemy.sql.operators.ColumnOperators.like"><tt class="xref py py-meth docutils literal"><span class="pre">ColumnOperators.like()</span></tt></a>, i.e. <tt class="docutils literal"><span class="pre">~x.like(y)</span></tt>.</p>
1127
 
<p class="versionadded">
1128
 
<span class="versionmodified">New in version 0.8.</span></p>
1129
 
<div class="admonition-see-also admonition seealso">
1130
 
<p class="first admonition-title">See also</p>
1131
 
<p class="last"><a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.ColumnOperators.like" title="sqlalchemy.sql.operators.ColumnOperators.like"><tt class="xref py py-meth docutils literal"><span class="pre">ColumnOperators.like()</span></tt></a></p>
1132
 
</div>
1133
 
</dd></dl>
1134
 
 
1135
 
<dl class="method">
1136
 
<dt id="sqlalchemy.schema.Column.nullsfirst">
1137
 
<tt class="descname">nullsfirst</tt><big>(</big><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Column.nullsfirst" title="Permalink to this definition">¶</a></dt>
1138
 
<dd><div class="inherited-member container">
1139
 
<em>inherited from the</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.ColumnOperators.nullsfirst" title="sqlalchemy.sql.operators.ColumnOperators.nullsfirst"><tt class="xref py py-meth docutils literal"><span class="pre">nullsfirst()</span></tt></a> <em>method of</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.ColumnOperators" title="sqlalchemy.sql.operators.ColumnOperators"><tt class="xref py py-class docutils literal"><span class="pre">ColumnOperators</span></tt></a></div>
1140
 
<p>Produce a <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.nullsfirst" title="sqlalchemy.sql.expression.nullsfirst"><tt class="xref py py-func docutils literal"><span class="pre">nullsfirst()</span></tt></a> clause against the
1141
 
parent object.</p>
1142
 
</dd></dl>
1143
 
 
1144
 
<dl class="method">
1145
 
<dt id="sqlalchemy.schema.Column.nullslast">
1146
 
<tt class="descname">nullslast</tt><big>(</big><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Column.nullslast" title="Permalink to this definition">¶</a></dt>
1147
 
<dd><div class="inherited-member container">
1148
 
<em>inherited from the</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.ColumnOperators.nullslast" title="sqlalchemy.sql.operators.ColumnOperators.nullslast"><tt class="xref py py-meth docutils literal"><span class="pre">nullslast()</span></tt></a> <em>method of</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.ColumnOperators" title="sqlalchemy.sql.operators.ColumnOperators"><tt class="xref py py-class docutils literal"><span class="pre">ColumnOperators</span></tt></a></div>
1149
 
<p>Produce a <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.nullslast" title="sqlalchemy.sql.expression.nullslast"><tt class="xref py py-func docutils literal"><span class="pre">nullslast()</span></tt></a> clause against the
1150
 
parent object.</p>
1151
 
</dd></dl>
1152
 
 
1153
 
<dl class="attribute">
1154
 
<dt id="sqlalchemy.schema.Column.onupdate">
1155
 
<tt class="descname">onupdate</tt><em class="property"> = None</em><a class="headerlink" href="#sqlalchemy.schema.Column.onupdate" title="Permalink to this definition">¶</a></dt>
1156
 
<dd></dd></dl>
1157
 
 
1158
 
<dl class="method">
1159
 
<dt id="sqlalchemy.schema.Column.op">
1160
 
<tt class="descname">op</tt><big>(</big><em>opstring</em>, <em>precedence=0</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Column.op" title="Permalink to this definition">¶</a></dt>
1161
 
<dd><div class="inherited-member container">
1162
 
<em>inherited from the</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.Operators.op" title="sqlalchemy.sql.operators.Operators.op"><tt class="xref py py-meth docutils literal"><span class="pre">op()</span></tt></a> <em>method of</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.Operators" title="sqlalchemy.sql.operators.Operators"><tt class="xref py py-class docutils literal"><span class="pre">Operators</span></tt></a></div>
1163
 
<p>produce a generic operator function.</p>
1164
 
<p>e.g.:</p>
1165
 
<div class="highlight-python"><div class="highlight"><pre><span class="n">somecolumn</span><span class="o">.</span><span class="n">op</span><span class="p">(</span><span class="s">&quot;*&quot;</span><span class="p">)(</span><span class="mi">5</span><span class="p">)</span></pre></div>
1166
 
</div>
1167
 
<p>produces:</p>
1168
 
<div class="highlight-python"><div class="highlight"><pre><span class="n">somecolumn</span> <span class="o">*</span> <span class="mi">5</span></pre></div>
1169
 
</div>
1170
 
<p>This function can also be used to make bitwise operators explicit. For
1171
 
example:</p>
1172
 
<div class="highlight-python"><div class="highlight"><pre><span class="n">somecolumn</span><span class="o">.</span><span class="n">op</span><span class="p">(</span><span class="s">&#39;&amp;&#39;</span><span class="p">)(</span><span class="mh">0xff</span><span class="p">)</span></pre></div>
1173
 
</div>
1174
 
<p>is a bitwise AND of the value in <tt class="docutils literal"><span class="pre">somecolumn</span></tt>.</p>
1175
 
<table class="docutils field-list" frame="void" rules="none">
1176
 
<col class="field-name" />
1177
 
<col class="field-body" />
1178
 
<tbody valign="top">
1179
 
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
1180
 
<li><strong>operator</strong> &#8211; a string which will be output as the infix operator
1181
 
between this element and the expression passed to the
1182
 
generated function.</li>
1183
 
<li><strong>precedence</strong> &#8211; <p>precedence to apply to the operator, when
1184
 
parenthesizing expressions.  A lower number will cause the expression
1185
 
to be parenthesized when applied against another operator with
1186
 
higher precedence.  The default value of <tt class="docutils literal"><span class="pre">0</span></tt> is lower than all
1187
 
operators except for the comma (<tt class="docutils literal"><span class="pre">,</span></tt>) and <tt class="docutils literal"><span class="pre">AS</span></tt> operators.
1188
 
A value of 100 will be higher or equal to all operators, and -100
1189
 
will be lower than or equal to all operators.</p>
1190
 
<p class="versionadded">
1191
 
<span class="versionmodified">New in version 0.8: </span>- added the &#8216;precedence&#8217; argument.</p>
1192
 
</li>
1193
 
</ul>
1194
 
</td>
1195
 
</tr>
1196
 
</tbody>
1197
 
</table>
1198
 
<div class="admonition-see-also admonition seealso">
1199
 
<p class="first admonition-title">See also</p>
1200
 
<p class="last"><a class="reference internal" href="types.html#types-operators"><em>Redefining and Creating New Operators</em></a></p>
1201
 
</div>
1202
 
</dd></dl>
1203
 
 
1204
 
<dl class="method">
1205
 
<dt id="sqlalchemy.schema.Column.operate">
1206
 
<tt class="descname">operate</tt><big>(</big><em>op</em>, <em>*other</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Column.operate" title="Permalink to this definition">¶</a></dt>
1207
 
<dd><div class="inherited-member container">
1208
 
<em>inherited from the</em> <tt class="xref py py-meth docutils literal"><span class="pre">operate()</span></tt> <em>method of</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.ColumnElement" title="sqlalchemy.sql.expression.ColumnElement"><tt class="xref py py-class docutils literal"><span class="pre">ColumnElement</span></tt></a></div>
1209
 
</dd></dl>
1210
 
 
1211
 
<dl class="method">
1212
 
<dt id="sqlalchemy.schema.Column.params">
1213
 
<tt class="descname">params</tt><big>(</big><em>*optionaldict</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Column.params" title="Permalink to this definition">¶</a></dt>
1214
 
<dd><div class="inherited-member container">
1215
 
<em>inherited from the</em> <tt class="xref py py-meth docutils literal"><span class="pre">params()</span></tt> <em>method of</em> <tt class="xref py py-class docutils literal"><span class="pre">Immutable</span></tt></div>
1216
 
</dd></dl>
1217
 
 
1218
 
<dl class="attribute">
1219
 
<dt id="sqlalchemy.schema.Column.primary_key">
1220
 
<tt class="descname">primary_key</tt><em class="property"> = False</em><a class="headerlink" href="#sqlalchemy.schema.Column.primary_key" title="Permalink to this definition">¶</a></dt>
1221
 
<dd></dd></dl>
1222
 
 
1223
 
<dl class="attribute">
1224
 
<dt id="sqlalchemy.schema.Column.proxy_set">
1225
 
<tt class="descname">proxy_set</tt><a class="headerlink" href="#sqlalchemy.schema.Column.proxy_set" title="Permalink to this definition">¶</a></dt>
1226
 
<dd><div class="inherited-member container">
1227
 
<em>inherited from the</em> <tt class="xref py py-attr docutils literal"><span class="pre">proxy_set</span></tt> <em>attribute of</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.ColumnElement" title="sqlalchemy.sql.expression.ColumnElement"><tt class="xref py py-class docutils literal"><span class="pre">ColumnElement</span></tt></a></div>
1228
 
</dd></dl>
1229
 
 
1230
 
<dl class="attribute">
1231
 
<dt id="sqlalchemy.schema.Column.quote">
1232
 
<tt class="descname">quote</tt><em class="property"> = None</em><a class="headerlink" href="#sqlalchemy.schema.Column.quote" title="Permalink to this definition">¶</a></dt>
1233
 
<dd></dd></dl>
1234
 
 
1235
 
<dl class="method">
1236
 
<dt id="sqlalchemy.schema.Column.references">
1237
 
<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>
1238
 
<dd><p>Return True if this Column references the given column via foreign
1239
 
key.</p>
1240
 
</dd></dl>
1241
 
 
1242
 
<dl class="method">
1243
 
<dt id="sqlalchemy.schema.Column.reverse_operate">
1244
 
<tt class="descname">reverse_operate</tt><big>(</big><em>op</em>, <em>other</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Column.reverse_operate" title="Permalink to this definition">¶</a></dt>
1245
 
<dd><div class="inherited-member container">
1246
 
<em>inherited from the</em> <tt class="xref py py-meth docutils literal"><span class="pre">reverse_operate()</span></tt> <em>method of</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.ColumnElement" title="sqlalchemy.sql.expression.ColumnElement"><tt class="xref py py-class docutils literal"><span class="pre">ColumnElement</span></tt></a></div>
1247
 
</dd></dl>
1248
 
 
1249
 
<dl class="method">
1250
 
<dt id="sqlalchemy.schema.Column.self_group">
1251
 
<tt class="descname">self_group</tt><big>(</big><em>against=None</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Column.self_group" title="Permalink to this definition">¶</a></dt>
1252
 
<dd><div class="inherited-member container">
1253
 
<em>inherited from the</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.ClauseElement.self_group" title="sqlalchemy.sql.expression.ClauseElement.self_group"><tt class="xref py py-meth docutils literal"><span class="pre">self_group()</span></tt></a> <em>method of</em> <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></div>
1254
 
<p>Apply a &#8216;grouping&#8217; to this <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>.</p>
1255
 
<p>This method is overridden by subclasses to return a
1256
 
&#8220;grouping&#8221; construct, i.e. parenthesis.   In particular
1257
 
it&#8217;s used by &#8220;binary&#8221; expressions to provide a grouping
1258
 
around themselves when placed into a larger expression,
1259
 
as well as by <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.select" title="sqlalchemy.sql.expression.select"><tt class="xref py py-func docutils literal"><span class="pre">select()</span></tt></a> constructs when placed into
1260
 
the FROM clause of another <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.select" title="sqlalchemy.sql.expression.select"><tt class="xref py py-func docutils literal"><span class="pre">select()</span></tt></a>.  (Note that
1261
 
subqueries should be normally created using the
1262
 
<tt class="xref py py-func docutils literal"><span class="pre">Select.alias()</span></tt> method, as many platforms require
1263
 
nested SELECT statements to be named).</p>
1264
 
<p>As expressions are composed together, the application of
1265
 
<a class="reference internal" href="#sqlalchemy.schema.Column.self_group" title="sqlalchemy.schema.Column.self_group"><tt class="xref py py-meth docutils literal"><span class="pre">self_group()</span></tt></a> is automatic - end-user code should never
1266
 
need to use this method directly.  Note that SQLAlchemy&#8217;s
1267
 
clause constructs take operator precedence into account -
1268
 
so parenthesis might not be needed, for example, in
1269
 
an expression like <tt class="docutils literal"><span class="pre">x</span> <span class="pre">OR</span> <span class="pre">(y</span> <span class="pre">AND</span> <span class="pre">z)</span></tt> - AND takes precedence
1270
 
over OR.</p>
1271
 
<p>The base <a class="reference internal" href="#sqlalchemy.schema.Column.self_group" title="sqlalchemy.schema.Column.self_group"><tt class="xref py py-meth docutils literal"><span class="pre">self_group()</span></tt></a> method of <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>
1272
 
just returns self.</p>
1273
 
</dd></dl>
1274
 
 
1275
 
<dl class="attribute">
1276
 
<dt id="sqlalchemy.schema.Column.server_default">
1277
 
<tt class="descname">server_default</tt><em class="property"> = None</em><a class="headerlink" href="#sqlalchemy.schema.Column.server_default" title="Permalink to this definition">¶</a></dt>
1278
 
<dd></dd></dl>
1279
 
 
1280
 
<dl class="attribute">
1281
 
<dt id="sqlalchemy.schema.Column.server_onupdate">
1282
 
<tt class="descname">server_onupdate</tt><em class="property"> = None</em><a class="headerlink" href="#sqlalchemy.schema.Column.server_onupdate" title="Permalink to this definition">¶</a></dt>
1283
 
<dd></dd></dl>
1284
 
 
1285
 
<dl class="method">
1286
 
<dt id="sqlalchemy.schema.Column.shares_lineage">
1287
 
<tt class="descname">shares_lineage</tt><big>(</big><em>othercolumn</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Column.shares_lineage" title="Permalink to this definition">¶</a></dt>
1288
 
<dd><div class="inherited-member container">
1289
 
<em>inherited from the</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.ColumnElement.shares_lineage" title="sqlalchemy.sql.expression.ColumnElement.shares_lineage"><tt class="xref py py-meth docutils literal"><span class="pre">shares_lineage()</span></tt></a> <em>method of</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.ColumnElement" title="sqlalchemy.sql.expression.ColumnElement"><tt class="xref py py-class docutils literal"><span class="pre">ColumnElement</span></tt></a></div>
1290
 
<p>Return True if the given <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.ColumnElement" title="sqlalchemy.sql.expression.ColumnElement"><tt class="xref py py-class docutils literal"><span class="pre">ColumnElement</span></tt></a>
1291
 
has a common ancestor to this <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.ColumnElement" title="sqlalchemy.sql.expression.ColumnElement"><tt class="xref py py-class docutils literal"><span class="pre">ColumnElement</span></tt></a>.</p>
1292
 
</dd></dl>
1293
 
 
1294
 
<dl class="method">
1295
 
<dt id="sqlalchemy.schema.Column.startswith">
1296
 
<tt class="descname">startswith</tt><big>(</big><em>other</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Column.startswith" title="Permalink to this definition">¶</a></dt>
1297
 
<dd><div class="inherited-member container">
1298
 
<em>inherited from the</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.ColumnOperators.startswith" title="sqlalchemy.sql.operators.ColumnOperators.startswith"><tt class="xref py py-meth docutils literal"><span class="pre">startswith()</span></tt></a> <em>method of</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.operators.ColumnOperators" title="sqlalchemy.sql.operators.ColumnOperators"><tt class="xref py py-class docutils literal"><span class="pre">ColumnOperators</span></tt></a></div>
1299
 
<p>Implement the <tt class="docutils literal"><span class="pre">startwith</span></tt> operator.</p>
1300
 
<p>In a column context, produces the clause <tt class="docutils literal"><span class="pre">LIKE</span> <span class="pre">'&lt;other&gt;%'</span></tt></p>
1301
 
</dd></dl>
1302
 
 
1303
 
<dl class="attribute">
1304
 
<dt id="sqlalchemy.schema.Column.supports_execution">
1305
 
<tt class="descname">supports_execution</tt><em class="property"> = False</em><a class="headerlink" href="#sqlalchemy.schema.Column.supports_execution" title="Permalink to this definition">¶</a></dt>
1306
 
<dd></dd></dl>
1307
 
 
1308
 
<dl class="attribute">
1309
 
<dt id="sqlalchemy.schema.Column.table">
1310
 
<tt class="descname">table</tt><a class="headerlink" href="#sqlalchemy.schema.Column.table" title="Permalink to this definition">¶</a></dt>
1311
 
<dd><div class="inherited-member container">
1312
 
<em>inherited from the</em> <tt class="xref py py-attr docutils literal"><span class="pre">table</span></tt> <em>attribute of</em> <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">ColumnClause</span></tt></a></div>
1313
 
</dd></dl>
1314
 
 
1315
 
<dl class="attribute">
1316
 
<dt id="sqlalchemy.schema.Column.timetuple">
1317
 
<tt class="descname">timetuple</tt><em class="property"> = None</em><a class="headerlink" href="#sqlalchemy.schema.Column.timetuple" title="Permalink to this definition">¶</a></dt>
1318
 
<dd></dd></dl>
1319
 
 
1320
 
<dl class="attribute">
1321
 
<dt id="sqlalchemy.schema.Column.type">
1322
 
<tt class="descname">type</tt><a class="headerlink" href="#sqlalchemy.schema.Column.type" title="Permalink to this definition">¶</a></dt>
1323
 
<dd><div class="inherited-member container">
1324
 
<em>inherited from the</em> <tt class="xref py py-attr docutils literal"><span class="pre">type</span></tt> <em>attribute of</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.ColumnElement" title="sqlalchemy.sql.expression.ColumnElement"><tt class="xref py py-class docutils literal"><span class="pre">ColumnElement</span></tt></a></div>
1325
 
</dd></dl>
1326
 
 
1327
 
<dl class="method">
1328
 
<dt id="sqlalchemy.schema.Column.unique_params">
1329
 
<tt class="descname">unique_params</tt><big>(</big><em>*optionaldict</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Column.unique_params" title="Permalink to this definition">¶</a></dt>
1330
 
<dd><div class="inherited-member container">
1331
 
<em>inherited from the</em> <tt class="xref py py-meth docutils literal"><span class="pre">unique_params()</span></tt> <em>method of</em> <tt class="xref py py-class docutils literal"><span class="pre">Immutable</span></tt></div>
1332
 
</dd></dl>
1333
 
 
1334
 
</dd></dl>
1335
 
 
1336
 
<dl class="class">
1337
 
<dt id="sqlalchemy.schema.MetaData">
1338
 
<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>, <em>schema=None</em>, <em>quote_schema=None</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.MetaData" title="Permalink to this definition">¶</a></dt>
1339
 
<dd><p>Bases: <a class="reference internal" href="#sqlalchemy.schema.SchemaItem" title="sqlalchemy.schema.SchemaItem"><tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.schema.SchemaItem</span></tt></a></p>
1340
 
<p>A collection 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 and their associated schema
1341
 
constructs.</p>
1342
 
<p>Holds a collection 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 as well as
1343
 
an optional binding to an <a class="reference internal" href="connections.html#sqlalchemy.engine.Engine" title="sqlalchemy.engine.Engine"><tt class="xref py py-class docutils literal"><span class="pre">Engine</span></tt></a> or
1344
 
<a class="reference internal" href="connections.html#sqlalchemy.engine.Connection" title="sqlalchemy.engine.Connection"><tt class="xref py py-class docutils literal"><span class="pre">Connection</span></tt></a>.  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
1345
 
in the collection and their columns may participate in implicit SQL
1346
 
execution.</p>
1347
 
<p>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 themselves are stored in the
1348
 
<tt class="docutils literal"><span class="pre">metadata.tables</span></tt> dictionary.</p>
1349
 
<p>The <tt class="docutils literal"><span class="pre">bind</span></tt> property may be assigned to dynamically.  A common pattern is
1350
 
to start unbound and then bind later when an engine is available:</p>
1351
 
<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>
1352
 
<span class="c"># define tables</span>
1353
 
<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>
1354
 
<span class="c"># connect to an engine later, perhaps after loading a URL from a</span>
1355
 
<span class="c"># configuration file</span>
1356
 
<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>
1357
 
</div>
1358
 
<p>MetaData is a thread-safe object after tables have been explicitly defined
1359
 
or loaded via reflection.</p>
1360
 
<p>See also:</p>
1361
 
<p><a class="reference internal" href="#metadata-describing"><em>Describing Databases with MetaData</em></a> - Introduction to database metadata</p>
1362
 
<span class="target" id="index-0"></span><dl class="method">
1363
 
<dt id="sqlalchemy.schema.MetaData.__init__">
1364
 
<tt class="descname">__init__</tt><big>(</big><em>bind=None</em>, <em>reflect=False</em>, <em>schema=None</em>, <em>quote_schema=None</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.MetaData.__init__" title="Permalink to this definition">¶</a></dt>
1365
 
<dd><p>Create a new MetaData object.</p>
1366
 
<table class="docutils field-list" frame="void" rules="none">
1367
 
<col class="field-name" />
1368
 
<col class="field-body" />
1369
 
<tbody valign="top">
1370
 
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
1371
 
<li><strong>bind</strong> &#8211; An Engine or Connection to bind to.  May also be a string or URL
1372
 
instance, these are passed to create_engine() and this MetaData will
1373
 
be bound to the resulting engine.</li>
1374
 
<li><strong>reflect</strong> &#8211; <p>Optional, automatically load all tables from the bound database.
1375
 
Defaults to False. <tt class="docutils literal"><span class="pre">bind</span></tt> is required when this option is set.</p>
1376
 
<p class="deprecated">
1377
 
<span class="versionmodified">Deprecated since version 0.8: </span>Please use the <a class="reference internal" href="#sqlalchemy.schema.MetaData.reflect" title="sqlalchemy.schema.MetaData.reflect"><tt class="xref py py-meth docutils literal"><span class="pre">MetaData.reflect()</span></tt></a> method.</p>
1378
 
</li>
1379
 
<li><strong>schema</strong> &#8211; The default schema to use for 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>,
1380
 
<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>, and other objects associated with this
1381
 
<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>. Defaults to <tt class="docutils literal"><span class="pre">None</span></tt>.</li>
1382
 
<li><strong>quote_schema</strong> &#8211; Sets the <tt class="docutils literal"><span class="pre">quote_schema</span></tt> flag for those <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>,
1383
 
<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>, and other objects which make usage of the
1384
 
local <tt class="docutils literal"><span class="pre">schema</span></tt> name.</li>
1385
 
</ul>
1386
 
</td>
1387
 
</tr>
1388
 
</tbody>
1389
 
</table>
1390
 
<p class="versionadded">
1391
 
<span class="versionmodified">New in version 0.7.4: </span><tt class="docutils literal"><span class="pre">schema</span></tt> and <tt class="docutils literal"><span class="pre">quote_schema</span></tt> parameters.</p>
1392
 
</dd></dl>
1393
 
 
1394
 
<dl class="method">
1395
 
<dt id="sqlalchemy.schema.MetaData.append_ddl_listener">
1396
 
<tt class="descname">append_ddl_listener</tt><big>(</big><em>event_name</em>, <em>listener</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.MetaData.append_ddl_listener" title="Permalink to this definition">¶</a></dt>
1397
 
<dd><p>Append a DDL event listener to this <tt class="docutils literal"><span class="pre">MetaData</span></tt>.</p>
1398
 
<p>Deprecated.  See <a class="reference internal" href="events.html#sqlalchemy.events.DDLEvents" title="sqlalchemy.events.DDLEvents"><tt class="xref py py-class docutils literal"><span class="pre">DDLEvents</span></tt></a>.</p>
1399
 
</dd></dl>
1400
 
 
1401
 
<dl class="attribute">
1402
 
<dt id="sqlalchemy.schema.MetaData.bind">
1403
 
<tt class="descname">bind</tt><a class="headerlink" href="#sqlalchemy.schema.MetaData.bind" title="Permalink to this definition">¶</a></dt>
1404
 
<dd><p>An <a class="reference internal" href="connections.html#sqlalchemy.engine.Engine" title="sqlalchemy.engine.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.Connection" title="sqlalchemy.engine.Connection"><tt class="xref py py-class docutils literal"><span class="pre">Connection</span></tt></a> to which this
1405
 
<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 bound.</p>
1406
 
<p>Typically, a <a class="reference internal" href="connections.html#sqlalchemy.engine.Engine" title="sqlalchemy.engine.Engine"><tt class="xref py py-class docutils literal"><span class="pre">Engine</span></tt></a> is assigned to this attribute
1407
 
so that &#8220;implicit execution&#8221; may be used, or alternatively
1408
 
as a means of providing engine binding information to an
1409
 
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> object:</p>
1410
 
<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">&quot;someurl://&quot;</span><span class="p">)</span>
1411
 
<span class="n">metadata</span><span class="o">.</span><span class="n">bind</span> <span class="o">=</span> <span class="n">engine</span></pre></div>
1412
 
</div>
1413
 
<div class="admonition-see-also admonition seealso">
1414
 
<p class="first admonition-title">See also</p>
1415
 
<p class="last"><a class="reference internal" href="connections.html#dbengine-implicit"><em>Connectionless Execution, Implicit Execution</em></a> - background on &#8220;bound metadata&#8221;</p>
1416
 
</div>
1417
 
</dd></dl>
1418
 
 
1419
 
<dl class="method">
1420
 
<dt id="sqlalchemy.schema.MetaData.clear">
1421
 
<tt class="descname">clear</tt><big>(</big><big>)</big><a class="headerlink" href="#sqlalchemy.schema.MetaData.clear" title="Permalink to this definition">¶</a></dt>
1422
 
<dd><p>Clear all Table objects from this MetaData.</p>
1423
 
</dd></dl>
1424
 
 
1425
 
<dl class="method">
1426
 
<dt id="sqlalchemy.schema.MetaData.create_all">
1427
 
<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>
1428
 
<dd><p>Create all tables stored in this metadata.</p>
1429
 
<p>Conditional by default, will not attempt to recreate tables already
1430
 
present in the target database.</p>
1431
 
<table class="docutils field-list" frame="void" rules="none">
1432
 
<col class="field-name" />
1433
 
<col class="field-body" />
1434
 
<tbody valign="top">
1435
 
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
1436
 
<li><strong>bind</strong> &#8211; A <a class="reference internal" href="connections.html#sqlalchemy.engine.Connectable" title="sqlalchemy.engine.Connectable"><tt class="xref py py-class docutils literal"><span class="pre">Connectable</span></tt></a> used to access the
1437
 
database; if None, uses the existing bind on this <tt class="docutils literal"><span class="pre">MetaData</span></tt>, if
1438
 
any.</li>
1439
 
<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
1440
 
tables in the <tt class="docutils literal"><span class="pre">MetaData</span></tt> (others are ignored).</li>
1441
 
<li><strong>checkfirst</strong> &#8211; Defaults to True, don&#8217;t issue CREATEs for tables already present
1442
 
in the target database.</li>
1443
 
</ul>
1444
 
</td>
1445
 
</tr>
1446
 
</tbody>
1447
 
</table>
1448
 
</dd></dl>
1449
 
 
1450
 
<dl class="method">
1451
 
<dt id="sqlalchemy.schema.MetaData.drop_all">
1452
 
<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>
1453
 
<dd><p>Drop all tables stored in this metadata.</p>
1454
 
<p>Conditional by default, will not attempt to drop tables not present in
1455
 
the target database.</p>
1456
 
<table class="docutils field-list" frame="void" rules="none">
1457
 
<col class="field-name" />
1458
 
<col class="field-body" />
1459
 
<tbody valign="top">
1460
 
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
1461
 
<li><strong>bind</strong> &#8211; A <a class="reference internal" href="connections.html#sqlalchemy.engine.Connectable" title="sqlalchemy.engine.Connectable"><tt class="xref py py-class docutils literal"><span class="pre">Connectable</span></tt></a> used to access the
1462
 
database; if None, uses the existing bind on this <tt class="docutils literal"><span class="pre">MetaData</span></tt>, if
1463
 
any.</li>
1464
 
<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
1465
 
total tables in the <tt class="docutils literal"><span class="pre">MetaData</span></tt> (others are ignored).</li>
1466
 
<li><strong>checkfirst</strong> &#8211; Defaults to True, only issue DROPs for tables confirmed to be
1467
 
present in the target database.</li>
1468
 
</ul>
1469
 
</td>
1470
 
</tr>
1471
 
</tbody>
1472
 
</table>
1473
 
</dd></dl>
1474
 
 
1475
 
<dl class="method">
1476
 
<dt id="sqlalchemy.schema.MetaData.is_bound">
1477
 
<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>
1478
 
<dd><p>True if this MetaData is bound to an Engine or Connection.</p>
1479
 
</dd></dl>
1480
 
 
1481
 
<dl class="method">
1482
 
<dt id="sqlalchemy.schema.MetaData.reflect">
1483
 
<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>
1484
 
<dd><p>Load all available table definitions from the database.</p>
1485
 
<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
1486
 
table available in the database but not yet present in the
1487
 
<tt class="docutils literal"><span class="pre">MetaData</span></tt>.  May be called multiple times to pick up tables recently
1488
 
added to the database, however no special action is taken if a table
1489
 
in this <tt class="docutils literal"><span class="pre">MetaData</span></tt> no longer exists in the database.</p>
1490
 
<table class="docutils field-list" frame="void" rules="none">
1491
 
<col class="field-name" />
1492
 
<col class="field-body" />
1493
 
<tbody valign="top">
1494
 
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
1495
 
<li><strong>bind</strong> &#8211; A <a class="reference internal" href="connections.html#sqlalchemy.engine.Connectable" title="sqlalchemy.engine.Connectable"><tt class="xref py py-class docutils literal"><span class="pre">Connectable</span></tt></a> used to access the database; if None, uses
1496
 
the existing bind on this <tt class="docutils literal"><span class="pre">MetaData</span></tt>, if any.</li>
1497
 
<li><strong>schema</strong> &#8211; Optional, query and reflect tables from an alterate schema.
1498
 
If None, the schema associated with this <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>
1499
 
is used, if any.</li>
1500
 
<li><strong>views</strong> &#8211; If True, also reflect views.</li>
1501
 
<li><strong>only</strong> &#8211; <p>Optional.  Load only a sub-set of available named tables.  May be
1502
 
specified as a sequence of names or a callable.</p>
1503
 
<p>If a sequence of names is provided, only those tables will be
1504
 
reflected.  An error is raised if a table is requested but not
1505
 
available.  Named tables already present in this <tt class="docutils literal"><span class="pre">MetaData</span></tt> are
1506
 
ignored.</p>
1507
 
<p>If a callable is provided, it will be used as a boolean predicate to
1508
 
filter the list of potential table names.  The callable is called
1509
 
with a table name and this <tt class="docutils literal"><span class="pre">MetaData</span></tt> instance as positional
1510
 
arguments and should return a true value for any table to reflect.</p>
1511
 
</li>
1512
 
</ul>
1513
 
</td>
1514
 
</tr>
1515
 
</tbody>
1516
 
</table>
1517
 
</dd></dl>
1518
 
 
1519
 
<dl class="method">
1520
 
<dt id="sqlalchemy.schema.MetaData.remove">
1521
 
<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>
1522
 
<dd><p>Remove the given Table object from this MetaData.</p>
1523
 
</dd></dl>
1524
 
 
1525
 
<dl class="attribute">
1526
 
<dt id="sqlalchemy.schema.MetaData.sorted_tables">
1527
 
<tt class="descname">sorted_tables</tt><a class="headerlink" href="#sqlalchemy.schema.MetaData.sorted_tables" title="Permalink to this definition">¶</a></dt>
1528
 
<dd><p>Returns a list 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 sorted in order of
1529
 
foreign key dependency.</p>
1530
 
<p>The sorting will place <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 that have dependencies
1531
 
first, before the dependencies themselves, representing the
1532
 
order in which they can be created.   To get the order in which
1533
 
the tables would be dropped, use the <tt class="docutils literal"><span class="pre">reversed()</span></tt> Python built-in.</p>
1534
 
<div class="admonition-see-also admonition seealso">
1535
 
<p class="first admonition-title">See also</p>
1536
 
<p class="last"><tt class="xref py py-meth docutils literal"><span class="pre">Inspector.sorted_tables()</span></tt></p>
1537
 
</div>
1538
 
</dd></dl>
1539
 
 
1540
 
</dd></dl>
1541
 
 
1542
 
<dl class="class">
1543
 
<dt id="sqlalchemy.schema.SchemaItem">
1544
 
<em class="property">class </em><tt class="descclassname">sqlalchemy.schema.</tt><tt class="descname">SchemaItem</tt><a class="headerlink" href="#sqlalchemy.schema.SchemaItem" title="Permalink to this definition">¶</a></dt>
1545
 
<dd><p>Bases: <a class="reference internal" href="events.html#sqlalchemy.events.SchemaEventTarget" title="sqlalchemy.events.SchemaEventTarget"><tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.events.SchemaEventTarget</span></tt></a>, <tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.sql.visitors.Visitable</span></tt></p>
1546
 
<p>Base class for items that define a database schema.</p>
1547
 
<dl class="method">
1548
 
<dt id="sqlalchemy.schema.SchemaItem.get_children">
1549
 
<tt class="descname">get_children</tt><big>(</big><em>**kwargs</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.SchemaItem.get_children" title="Permalink to this definition">¶</a></dt>
1550
 
<dd><p>used to allow SchemaVisitor access</p>
1551
 
</dd></dl>
1552
 
 
1553
 
<dl class="attribute">
1554
 
<dt id="sqlalchemy.schema.SchemaItem.info">
1555
 
<tt class="descname">info</tt><a class="headerlink" href="#sqlalchemy.schema.SchemaItem.info" title="Permalink to this definition">¶</a></dt>
1556
 
<dd><p>Info dictionary associated with the object, allowing user-defined
1557
 
data to be associated with this <a class="reference internal" href="#sqlalchemy.schema.SchemaItem" title="sqlalchemy.schema.SchemaItem"><tt class="xref py py-class docutils literal"><span class="pre">SchemaItem</span></tt></a>.</p>
1558
 
<p>The dictionary is automatically generated when first accessed.
1559
 
It can also be specified in the constructor of some objects,
1560
 
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> and <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>
1561
 
</dd></dl>
1562
 
 
1563
 
</dd></dl>
1564
 
 
1565
 
<dl class="class">
1566
 
<dt id="sqlalchemy.schema.Table">
1567
 
<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>
1568
 
<dd><p>Bases: <a class="reference internal" href="#sqlalchemy.schema.SchemaItem" title="sqlalchemy.schema.SchemaItem"><tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.schema.SchemaItem</span></tt></a>, <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>
1569
 
<p>Represent a table in a database.</p>
1570
 
<p>e.g.:</p>
1571
 
<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>
1572
 
                <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>
1573
 
                <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>
1574
 
           <span class="p">)</span></pre></div>
1575
 
</div>
1576
 
<p>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 constructs a unique instance of itself based
1577
 
on its name and optional schema name within the given
1578
 
<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. Calling 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>
1579
 
constructor with the same name and same <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> argument
1580
 
a second time will return the <em>same</em> <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 this way
1581
 
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 acts as a registry function.</p>
1582
 
<p>See also:</p>
1583
 
<p><a class="reference internal" href="#metadata-describing"><em>Describing Databases with MetaData</em></a> - Introduction to database metadata</p>
1584
 
<p>Constructor arguments are as follows:</p>
1585
 
<table class="docutils field-list" frame="void" rules="none">
1586
 
<col class="field-name" />
1587
 
<col class="field-body" />
1588
 
<tbody valign="top">
1589
 
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
1590
 
<li><strong>name</strong> &#8211; <p>The name of this table as represented in the database.</p>
1591
 
<p>This property, along with the <em>schema</em>, indicates the <em>singleton
1592
 
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>.
1593
 
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,
1594
 
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>
1595
 
<p>Names which contain no upper case characters
1596
 
will be treated as case insensitive names, and will not be quoted
1597
 
unless they are a reserved word.  Names with any number of upper
1598
 
case characters will be quoted and sent exactly.  Note that this
1599
 
behavior applies even for databases which standardize upper
1600
 
case names as case insensitive such as Oracle.</p>
1601
 
</li>
1602
 
<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
1603
 
table.  The metadata is used as a point of association of this table
1604
 
with other tables which are referenced via foreign key.  It also
1605
 
may be used to associate this table with a particular
1606
 
<a class="reference internal" href="connections.html#sqlalchemy.engine.Connectable" title="sqlalchemy.engine.Connectable"><tt class="xref py py-class docutils literal"><span class="pre">Connectable</span></tt></a>.</li>
1607
 
<li><strong>*args</strong> &#8211; Additional positional arguments are used primarily
1608
 
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
1609
 
table. Similar to the style of a CREATE TABLE statement, other
1610
 
<a class="reference internal" href="#sqlalchemy.schema.SchemaItem" title="sqlalchemy.schema.SchemaItem"><tt class="xref py py-class docutils literal"><span class="pre">SchemaItem</span></tt></a> constructs may be added here, including
1611
 
<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>
1612
 
<li><strong>autoload</strong> &#8211; Defaults to False: the Columns for this table should
1613
 
be reflected from the database. Usually there will be no Column
1614
 
objects in the constructor if this property is set.</li>
1615
 
<li><strong>autoload_replace</strong> &#8211; <p>If <tt class="docutils literal"><span class="pre">True</span></tt>, when using <tt class="docutils literal"><span class="pre">autoload=True</span></tt>
1616
 
and <tt class="docutils literal"><span class="pre">extend_existing=True</span></tt>,
1617
 
replace <tt class="docutils literal"><span class="pre">Column</span></tt> objects already present in the <tt class="docutils literal"><span class="pre">Table</span></tt> that&#8217;s
1618
 
in the <tt class="docutils literal"><span class="pre">MetaData</span></tt> registry with
1619
 
what&#8217;s reflected.  Otherwise, all existing columns will be
1620
 
excluded from the reflection process.    Note that this does
1621
 
not impact <tt class="docutils literal"><span class="pre">Column</span></tt> objects specified in the same call to <tt class="docutils literal"><span class="pre">Table</span></tt>
1622
 
which includes <tt class="docutils literal"><span class="pre">autoload</span></tt>, those always take precedence.
1623
 
Defaults to <tt class="docutils literal"><span class="pre">True</span></tt>.</p>
1624
 
<p class="versionadded">
1625
 
<span class="versionmodified">New in version 0.7.5.</span></p>
1626
 
</li>
1627
 
<li><strong>autoload_with</strong> &#8211; If autoload==True, this is an optional Engine
1628
 
or Connection instance to be used for the table reflection. If
1629
 
<tt class="docutils literal"><span class="pre">None</span></tt>, the underlying MetaData&#8217;s bound connectable will be used.</li>
1630
 
<li><strong>extend_existing</strong> &#8211; <p>When <tt class="docutils literal"><span class="pre">True</span></tt>, indicates that if this
1631
 
<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 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>,
1632
 
apply further arguments within the constructor to the existing
1633
 
<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>
1634
 
<p>If <tt class="docutils literal"><span class="pre">extend_existing</span></tt> or <tt class="docutils literal"><span class="pre">keep_existing</span></tt> are not set, an error is
1635
 
raised if additional table modifiers are specified when
1636
 
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 already present in 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>.</p>
1637
 
<p class="versionchanged">
1638
 
<span class="versionmodified">Changed in version 0.7.4: </span><tt class="docutils literal"><span class="pre">extend_existing</span></tt> will work in conjunction
1639
 
with <tt class="docutils literal"><span class="pre">autoload=True</span></tt> to run a new reflection operation against
1640
 
the database; new <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 will be produced
1641
 
from database metadata to replace those existing with the same
1642
 
name, and additional <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 not present
1643
 
in 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> will be added.</p>
1644
 
<p>As is always the case with <tt class="docutils literal"><span class="pre">autoload=True</span></tt>, <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>
1645
 
objects can be specified in 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> constructor,
1646
 
which will take precedence.  I.e.:</p>
1647
 
<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">metadata</span><span class="p">,</span>
1648
 
            <span class="n">Column</span><span class="p">(</span><span class="s">&#39;y&#39;</span><span class="p">,</span> <span class="n">Integer</span><span class="p">),</span>
1649
 
            <span class="n">extend_existing</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span>
1650
 
            <span class="n">autoload</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span>
1651
 
            <span class="n">autoload_with</span><span class="o">=</span><span class="n">engine</span>
1652
 
        <span class="p">)</span></pre></div>
1653
 
</div>
1654
 
<p>The above will overwrite all columns within <tt class="docutils literal"><span class="pre">mytable</span></tt> which
1655
 
are present in the database, except for <tt class="docutils literal"><span class="pre">y</span></tt> which will be used as is
1656
 
from the above definition.   If the <tt class="docutils literal"><span class="pre">autoload_replace</span></tt> flag
1657
 
is set to False, no existing columns will be replaced.</p>
1658
 
</li>
1659
 
<li><strong>implicit_returning</strong> &#8211; True by default - indicates that
1660
 
RETURNING can be used by default to fetch newly inserted primary key
1661
 
values, for backends which support this.  Note that
1662
 
create_engine() also provides an implicit_returning flag.</li>
1663
 
<li><strong>include_columns</strong> &#8211; A list of strings indicating a subset of
1664
 
columns to be loaded via the <tt class="docutils literal"><span class="pre">autoload</span></tt> operation; table columns who
1665
 
aren&#8217;t present in this list will not be represented on the resulting
1666
 
<tt class="docutils literal"><span class="pre">Table</span></tt> object. Defaults to <tt class="docutils literal"><span class="pre">None</span></tt> which indicates all columns
1667
 
should be reflected.</li>
1668
 
<li><strong>info</strong> &#8211; Optional data dictionary which will be populated into the
1669
 
<a class="reference internal" href="#sqlalchemy.schema.SchemaItem.info" title="sqlalchemy.schema.SchemaItem.info"><tt class="xref py py-attr docutils literal"><span class="pre">SchemaItem.info</span></tt></a> attribute of this object.</li>
1670
 
<li><strong>keep_existing</strong> &#8211; <p>When <tt class="docutils literal"><span class="pre">True</span></tt>, indicates that if this Table
1671
 
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>, ignore
1672
 
further arguments within the constructor to the existing
1673
 
<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 return 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 as
1674
 
originally created. This is to allow a function that wishes
1675
 
to define a new <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> on first call, but on
1676
 
subsequent calls 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>,
1677
 
without any of the declarations (particularly constraints)
1678
 
being applied a second time. Also see extend_existing.</p>
1679
 
<p>If extend_existing or keep_existing are not set, an error is
1680
 
raised if additional table modifiers are specified when
1681
 
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 already present in 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>.</p>
1682
 
</li>
1683
 
<li><strong>listeners</strong> &#8211; <p>A list of tuples of the form <tt class="docutils literal"><span class="pre">(&lt;eventname&gt;,</span> <span class="pre">&lt;fn&gt;)</span></tt>
1684
 
which will be passed to <a class="reference internal" href="event.html#sqlalchemy.event.listen" title="sqlalchemy.event.listen"><tt class="xref py py-func docutils literal"><span class="pre">event.listen()</span></tt></a> upon construction.
1685
 
This alternate hook to <a class="reference internal" href="event.html#sqlalchemy.event.listen" title="sqlalchemy.event.listen"><tt class="xref py py-func docutils literal"><span class="pre">event.listen()</span></tt></a> allows the establishment
1686
 
of a listener function specific 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> before
1687
 
the &#8220;autoload&#8221; process begins.  Particularly useful for
1688
 
the <a class="reference internal" href="events.html#sqlalchemy.events.DDLEvents.column_reflect" title="sqlalchemy.events.DDLEvents.column_reflect"><tt class="xref py py-meth docutils literal"><span class="pre">DDLEvents.column_reflect()</span></tt></a> event:</p>
1689
 
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">listen_for_reflect</span><span class="p">(</span><span class="n">table</span><span class="p">,</span> <span class="n">column_info</span><span class="p">):</span>
1690
 
    <span class="s">&quot;handle the column reflection event&quot;</span>
1691
 
    <span class="c"># ...</span>
1692
 
 
1693
 
<span class="n">t</span> <span class="o">=</span> <span class="n">Table</span><span class="p">(</span>
1694
 
    <span class="s">&#39;sometable&#39;</span><span class="p">,</span>
1695
 
    <span class="n">autoload</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span>
1696
 
    <span class="n">listeners</span><span class="o">=</span><span class="p">[</span>
1697
 
        <span class="p">(</span><span class="s">&#39;column_reflect&#39;</span><span class="p">,</span> <span class="n">listen_for_reflect</span><span class="p">)</span>
1698
 
    <span class="p">])</span></pre></div>
1699
 
</div>
1700
 
</li>
1701
 
<li><strong>mustexist</strong> &#8211; When <tt class="docutils literal"><span class="pre">True</span></tt>, indicates that this Table must already
1702
 
be 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> collection, else
1703
 
an exception is raised.</li>
1704
 
<li><strong>prefixes</strong> &#8211; A list of strings to insert after CREATE in the CREATE TABLE
1705
 
statement.  They will be separated by spaces.</li>
1706
 
<li><strong>quote</strong> &#8211; Force quoting of this table&#8217;s name on or off, corresponding
1707
 
to <tt class="docutils literal"><span class="pre">True</span></tt> or <tt class="docutils literal"><span class="pre">False</span></tt>.  When left at its default of <tt class="docutils literal"><span class="pre">None</span></tt>,
1708
 
the column identifier will be quoted according to whether the name is
1709
 
case sensitive (identifiers with at least one upper case character are
1710
 
treated as case sensitive), or if it&#8217;s a reserved word.  This flag
1711
 
is only needed to force quoting of a reserved word which is not known
1712
 
by the SQLAlchemy dialect.</li>
1713
 
<li><strong>quote_schema</strong> &#8211; same as &#8216;quote&#8217; but applies to the schema identifier.</li>
1714
 
<li><strong>schema</strong> &#8211; The <em>schema name</em> for this table, which is required if
1715
 
the table resides in a schema other than the default selected schema
1716
 
for the engine&#8217;s database connection. Defaults to <tt class="docutils literal"><span class="pre">None</span></tt>.</li>
1717
 
<li><strong>useexisting</strong> &#8211; Deprecated.  Use extend_existing.</li>
1718
 
</ul>
1719
 
</td>
1720
 
</tr>
1721
 
</tbody>
1722
 
</table>
1723
 
<dl class="method">
1724
 
<dt id="sqlalchemy.schema.Table.__init__">
1725
 
<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>
1726
 
<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>
1727
 
<p>This method is a no-op.   See the top-level
1728
 
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>
1729
 
for constructor arguments.</p>
1730
 
</dd></dl>
1731
 
 
1732
 
<dl class="method">
1733
 
<dt id="sqlalchemy.schema.Table.add_is_dependent_on">
1734
 
<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>
1735
 
<dd><p>Add a &#8216;dependency&#8217; for this Table.</p>
1736
 
<p>This is another Table object which must be created
1737
 
first before this one can, or dropped after this one.</p>
1738
 
<p>Usually, dependencies between tables are determined via
1739
 
ForeignKey objects.   However, for other situations that
1740
 
create dependencies outside of foreign keys (rules, inheriting),
1741
 
this method can manually establish such a link.</p>
1742
 
</dd></dl>
1743
 
 
1744
 
<dl class="method">
1745
 
<dt id="sqlalchemy.schema.Table.alias">
1746
 
<tt class="descname">alias</tt><big>(</big><em>name=None</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Table.alias" title="Permalink to this definition">¶</a></dt>
1747
 
<dd><div class="inherited-member container">
1748
 
<em>inherited from the</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.FromClause.alias" title="sqlalchemy.sql.expression.FromClause.alias"><tt class="xref py py-meth docutils literal"><span class="pre">alias()</span></tt></a> <em>method of</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.FromClause" title="sqlalchemy.sql.expression.FromClause"><tt class="xref py py-class docutils literal"><span class="pre">FromClause</span></tt></a></div>
1749
 
<p>return an alias of this <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.FromClause" title="sqlalchemy.sql.expression.FromClause"><tt class="xref py py-class docutils literal"><span class="pre">FromClause</span></tt></a>.</p>
1750
 
<p>This is shorthand for calling:</p>
1751
 
<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">alias</span>
1752
 
<span class="n">a</span> <span class="o">=</span> <span class="n">alias</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="n">name</span><span class="p">)</span></pre></div>
1753
 
</div>
1754
 
<p>See <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.alias" title="sqlalchemy.sql.expression.alias"><tt class="xref py py-func docutils literal"><span class="pre">alias()</span></tt></a> for details.</p>
1755
 
</dd></dl>
1756
 
 
1757
 
<dl class="method">
1758
 
<dt id="sqlalchemy.schema.Table.append_column">
1759
 
<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>
1760
 
<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>
1761
 
<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
1762
 
value of its <tt class="docutils literal"><span class="pre">.key</span></tt> attribute, will then be available
1763
 
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
1764
 
column definition will be included in any CREATE TABLE, SELECT,
1765
 
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>
1766
 
construct.</p>
1767
 
<p>Note that this does <strong>not</strong> change the definition of the table
1768
 
as it exists within any underlying database, assuming that
1769
 
table has already been created in the database.   Relational
1770
 
databases support the addition of columns to existing tables
1771
 
using the SQL ALTER command, which would need to be
1772
 
emitted for an already-existing table that doesn&#8217;t contain
1773
 
the newly added column.</p>
1774
 
</dd></dl>
1775
 
 
1776
 
<dl class="method">
1777
 
<dt id="sqlalchemy.schema.Table.append_constraint">
1778
 
<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>
1779
 
<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
1780
 
<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>
1781
 
<p>This has the effect of the constraint being included in any
1782
 
future CREATE TABLE statement, assuming specific DDL creation
1783
 
events have not been associated with the given
1784
 
<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>
1785
 
<p>Note that this does <strong>not</strong> produce the constraint within the
1786
 
relational database automatically, for a table that already exists
1787
 
in the database.   To add a constraint to an
1788
 
existing relational database table, the SQL ALTER command must
1789
 
be used.  SQLAlchemy also provides the
1790
 
<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
1791
 
invoked as an executable clause.</p>
1792
 
</dd></dl>
1793
 
 
1794
 
<dl class="method">
1795
 
<dt id="sqlalchemy.schema.Table.append_ddl_listener">
1796
 
<tt class="descname">append_ddl_listener</tt><big>(</big><em>event_name</em>, <em>listener</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Table.append_ddl_listener" title="Permalink to this definition">¶</a></dt>
1797
 
<dd><p>Append a DDL event listener to this <tt class="docutils literal"><span class="pre">Table</span></tt>.</p>
1798
 
<p>Deprecated.  See <a class="reference internal" href="events.html#sqlalchemy.events.DDLEvents" title="sqlalchemy.events.DDLEvents"><tt class="xref py py-class docutils literal"><span class="pre">DDLEvents</span></tt></a>.</p>
1799
 
</dd></dl>
1800
 
 
1801
 
<dl class="attribute">
1802
 
<dt id="sqlalchemy.schema.Table.bind">
1803
 
<tt class="descname">bind</tt><a class="headerlink" href="#sqlalchemy.schema.Table.bind" title="Permalink to this definition">¶</a></dt>
1804
 
<dd><p>Return the connectable associated with this Table.</p>
1805
 
</dd></dl>
1806
 
 
1807
 
<dl class="attribute">
1808
 
<dt id="sqlalchemy.schema.Table.c">
1809
 
<tt class="descname">c</tt><a class="headerlink" href="#sqlalchemy.schema.Table.c" title="Permalink to this definition">¶</a></dt>
1810
 
<dd><div class="inherited-member container">
1811
 
<em>inherited from the</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.FromClause.c" title="sqlalchemy.sql.expression.FromClause.c"><tt class="xref py py-attr docutils literal"><span class="pre">c</span></tt></a> <em>attribute of</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.FromClause" title="sqlalchemy.sql.expression.FromClause"><tt class="xref py py-class docutils literal"><span class="pre">FromClause</span></tt></a></div>
1812
 
<p>An alias for the <a class="reference internal" href="#sqlalchemy.schema.Table.columns" title="sqlalchemy.schema.Table.columns"><tt class="xref py py-attr docutils literal"><span class="pre">columns</span></tt></a> attribute.</p>
1813
 
</dd></dl>
1814
 
 
1815
 
<dl class="attribute">
1816
 
<dt id="sqlalchemy.schema.Table.columns">
1817
 
<tt class="descname">columns</tt><a class="headerlink" href="#sqlalchemy.schema.Table.columns" title="Permalink to this definition">¶</a></dt>
1818
 
<dd><div class="inherited-member container">
1819
 
<em>inherited from the</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.FromClause.columns" title="sqlalchemy.sql.expression.FromClause.columns"><tt class="xref py py-attr docutils literal"><span class="pre">columns</span></tt></a> <em>attribute of</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.FromClause" title="sqlalchemy.sql.expression.FromClause"><tt class="xref py py-class docutils literal"><span class="pre">FromClause</span></tt></a></div>
1820
 
<p>A named-based collection of <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.ColumnElement" title="sqlalchemy.sql.expression.ColumnElement"><tt class="xref py py-class docutils literal"><span class="pre">ColumnElement</span></tt></a> objects
1821
 
maintained by this <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.FromClause" title="sqlalchemy.sql.expression.FromClause"><tt class="xref py py-class docutils literal"><span class="pre">FromClause</span></tt></a>.</p>
1822
 
<p>The <a class="reference internal" href="#sqlalchemy.schema.Table.columns" title="sqlalchemy.schema.Table.columns"><tt class="xref py py-attr docutils literal"><span class="pre">columns</span></tt></a>, or <a class="reference internal" href="#sqlalchemy.schema.Table.c" title="sqlalchemy.schema.Table.c"><tt class="xref py py-attr docutils literal"><span class="pre">c</span></tt></a> collection, is the gateway
1823
 
to the construction of SQL expressions using table-bound or
1824
 
other selectable-bound columns:</p>
1825
 
<div class="highlight-python"><div class="highlight"><pre><span class="n">select</span><span class="p">([</span><span class="n">mytable</span><span class="p">])</span><span class="o">.</span><span class="n">where</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">somecolumn</span> <span class="o">==</span> <span class="mi">5</span><span class="p">)</span></pre></div>
1826
 
</div>
1827
 
</dd></dl>
1828
 
 
1829
 
<dl class="method">
1830
 
<dt id="sqlalchemy.schema.Table.compare">
1831
 
<tt class="descname">compare</tt><big>(</big><em>other</em>, <em>**kw</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Table.compare" title="Permalink to this definition">¶</a></dt>
1832
 
<dd><div class="inherited-member container">
1833
 
<em>inherited from the</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.ClauseElement.compare" title="sqlalchemy.sql.expression.ClauseElement.compare"><tt class="xref py py-meth docutils literal"><span class="pre">compare()</span></tt></a> <em>method of</em> <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></div>
1834
 
<p>Compare this ClauseElement to the given ClauseElement.</p>
1835
 
<p>Subclasses should override the default behavior, which is a
1836
 
straight identity comparison.</p>
1837
 
<p>**kw are arguments consumed by subclass compare() methods and
1838
 
may be used to modify the criteria for comparison.
1839
 
(see <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.ColumnElement" title="sqlalchemy.sql.expression.ColumnElement"><tt class="xref py py-class docutils literal"><span class="pre">ColumnElement</span></tt></a>)</p>
1840
 
</dd></dl>
1841
 
 
1842
 
<dl class="method">
1843
 
<dt id="sqlalchemy.schema.Table.compile">
1844
 
<tt class="descname">compile</tt><big>(</big><em>bind=None</em>, <em>dialect=None</em>, <em>**kw</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Table.compile" title="Permalink to this definition">¶</a></dt>
1845
 
<dd><div class="inherited-member container">
1846
 
<em>inherited from the</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.ClauseElement.compile" title="sqlalchemy.sql.expression.ClauseElement.compile"><tt class="xref py py-meth docutils literal"><span class="pre">compile()</span></tt></a> <em>method of</em> <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></div>
1847
 
<p>Compile this SQL expression.</p>
1848
 
<p>The return value is a <a class="reference internal" href="internals.html#sqlalchemy.engine.interfaces.Compiled" title="sqlalchemy.engine.interfaces.Compiled"><tt class="xref py py-class docutils literal"><span class="pre">Compiled</span></tt></a> object.
1849
 
Calling <tt class="docutils literal"><span class="pre">str()</span></tt> or <tt class="docutils literal"><span class="pre">unicode()</span></tt> on the returned value will yield a
1850
 
string representation of the result. The
1851
 
<a class="reference internal" href="internals.html#sqlalchemy.engine.interfaces.Compiled" title="sqlalchemy.engine.interfaces.Compiled"><tt class="xref py py-class docutils literal"><span class="pre">Compiled</span></tt></a> object also can return a
1852
 
dictionary of bind parameter names and values
1853
 
using the <tt class="docutils literal"><span class="pre">params</span></tt> accessor.</p>
1854
 
<table class="docutils field-list" frame="void" rules="none">
1855
 
<col class="field-name" />
1856
 
<col class="field-body" />
1857
 
<tbody valign="top">
1858
 
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
1859
 
<li><strong>bind</strong> &#8211; An <tt class="docutils literal"><span class="pre">Engine</span></tt> or <tt class="docutils literal"><span class="pre">Connection</span></tt> from which a
1860
 
<tt class="docutils literal"><span class="pre">Compiled</span></tt> will be acquired. This argument takes precedence over
1861
 
this <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>&#8216;s bound engine, if any.</li>
1862
 
<li><strong>column_keys</strong> &#8211; Used for INSERT and UPDATE statements, a list of
1863
 
column names which should be present in the VALUES clause of the
1864
 
compiled statement. If <tt class="docutils literal"><span class="pre">None</span></tt>, all columns from the target table
1865
 
object are rendered.</li>
1866
 
<li><strong>dialect</strong> &#8211; A <tt class="docutils literal"><span class="pre">Dialect</span></tt> instance from which a <tt class="docutils literal"><span class="pre">Compiled</span></tt>
1867
 
will be acquired. This argument takes precedence over the <cite>bind</cite>
1868
 
argument as well as this <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>&#8216;s bound engine, if
1869
 
any.</li>
1870
 
<li><strong>inline</strong> &#8211; Used for INSERT statements, for a dialect which does
1871
 
not support inline retrieval of newly generated primary key
1872
 
columns, will force the expression used to create the new primary
1873
 
key value to be rendered inline within the INSERT statement&#8217;s
1874
 
VALUES clause. This typically refers to Sequence execution but may
1875
 
also refer to any server-side default generation function
1876
 
associated with a primary key <cite>Column</cite>.</li>
1877
 
</ul>
1878
 
</td>
1879
 
</tr>
1880
 
</tbody>
1881
 
</table>
1882
 
</dd></dl>
1883
 
 
1884
 
<dl class="method">
1885
 
<dt id="sqlalchemy.schema.Table.correspond_on_equivalents">
1886
 
<tt class="descname">correspond_on_equivalents</tt><big>(</big><em>column</em>, <em>equivalents</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Table.correspond_on_equivalents" title="Permalink to this definition">¶</a></dt>
1887
 
<dd><div class="inherited-member container">
1888
 
<em>inherited from the</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.FromClause.correspond_on_equivalents" title="sqlalchemy.sql.expression.FromClause.correspond_on_equivalents"><tt class="xref py py-meth docutils literal"><span class="pre">correspond_on_equivalents()</span></tt></a> <em>method of</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.FromClause" title="sqlalchemy.sql.expression.FromClause"><tt class="xref py py-class docutils literal"><span class="pre">FromClause</span></tt></a></div>
1889
 
<p>Return corresponding_column for the given column, or if None
1890
 
search for a match in the given dictionary.</p>
1891
 
</dd></dl>
1892
 
 
1893
 
<dl class="method">
1894
 
<dt id="sqlalchemy.schema.Table.corresponding_column">
1895
 
<tt class="descname">corresponding_column</tt><big>(</big><em>column</em>, <em>require_embedded=False</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Table.corresponding_column" title="Permalink to this definition">¶</a></dt>
1896
 
<dd><div class="inherited-member container">
1897
 
<em>inherited from the</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.FromClause.corresponding_column" title="sqlalchemy.sql.expression.FromClause.corresponding_column"><tt class="xref py py-meth docutils literal"><span class="pre">corresponding_column()</span></tt></a> <em>method of</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.FromClause" title="sqlalchemy.sql.expression.FromClause"><tt class="xref py py-class docutils literal"><span class="pre">FromClause</span></tt></a></div>
1898
 
<p>Given a <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.ColumnElement" title="sqlalchemy.sql.expression.ColumnElement"><tt class="xref py py-class docutils literal"><span class="pre">ColumnElement</span></tt></a>, return the exported
1899
 
<a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.ColumnElement" title="sqlalchemy.sql.expression.ColumnElement"><tt class="xref py py-class docutils literal"><span class="pre">ColumnElement</span></tt></a> object from this <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.Selectable" title="sqlalchemy.sql.expression.Selectable"><tt class="xref py py-class docutils literal"><span class="pre">Selectable</span></tt></a>
1900
 
which corresponds to that original
1901
 
<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> via a common ancestor
1902
 
column.</p>
1903
 
<table class="docutils field-list" frame="void" rules="none">
1904
 
<col class="field-name" />
1905
 
<col class="field-body" />
1906
 
<tbody valign="top">
1907
 
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
1908
 
<li><strong>column</strong> &#8211; the target <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.ColumnElement" title="sqlalchemy.sql.expression.ColumnElement"><tt class="xref py py-class docutils literal"><span class="pre">ColumnElement</span></tt></a> to be matched</li>
1909
 
<li><strong>require_embedded</strong> &#8211; only return corresponding columns for</li>
1910
 
</ul>
1911
 
</td>
1912
 
</tr>
1913
 
</tbody>
1914
 
</table>
1915
 
<p>the given <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.ColumnElement" title="sqlalchemy.sql.expression.ColumnElement"><tt class="xref py py-class docutils literal"><span class="pre">ColumnElement</span></tt></a>, if the given
1916
 
<a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.ColumnElement" title="sqlalchemy.sql.expression.ColumnElement"><tt class="xref py py-class docutils literal"><span class="pre">ColumnElement</span></tt></a> is actually present within a sub-element
1917
 
of this <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.FromClause" title="sqlalchemy.sql.expression.FromClause"><tt class="xref py py-class docutils literal"><span class="pre">FromClause</span></tt></a>.  Normally the column will match if
1918
 
it merely shares a common ancestor with one of the exported
1919
 
columns of this <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.FromClause" title="sqlalchemy.sql.expression.FromClause"><tt class="xref py py-class docutils literal"><span class="pre">FromClause</span></tt></a>.</p>
1920
 
</dd></dl>
1921
 
 
1922
 
<dl class="method">
1923
 
<dt id="sqlalchemy.schema.Table.count">
1924
 
<tt class="descname">count</tt><big>(</big><em>whereclause=None</em>, <em>**params</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Table.count" title="Permalink to this definition">¶</a></dt>
1925
 
<dd><div class="inherited-member container">
1926
 
<em>inherited from the</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.TableClause.count" title="sqlalchemy.sql.expression.TableClause.count"><tt class="xref py py-meth docutils literal"><span class="pre">count()</span></tt></a> <em>method of</em> <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">TableClause</span></tt></a></div>
1927
 
<p>return a SELECT COUNT generated against this
1928
 
<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">TableClause</span></tt></a>.</p>
1929
 
</dd></dl>
1930
 
 
1931
 
<dl class="method">
1932
 
<dt id="sqlalchemy.schema.Table.create">
1933
 
<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>
1934
 
<dd><p>Issue a <tt class="docutils literal"><span class="pre">CREATE</span></tt> statement for this
1935
 
<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 the given <a class="reference internal" href="connections.html#sqlalchemy.engine.Connectable" title="sqlalchemy.engine.Connectable"><tt class="xref py py-class docutils literal"><span class="pre">Connectable</span></tt></a>
1936
 
for connectivity.</p>
1937
 
<p>See also <a class="reference internal" href="#sqlalchemy.schema.MetaData.create_all" title="sqlalchemy.schema.MetaData.create_all"><tt class="xref py py-meth docutils literal"><span class="pre">MetaData.create_all()</span></tt></a>.</p>
1938
 
</dd></dl>
1939
 
 
1940
 
<dl class="method">
1941
 
<dt id="sqlalchemy.schema.Table.delete">
1942
 
<tt class="descname">delete</tt><big>(</big><em>whereclause=None</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Table.delete" title="Permalink to this definition">¶</a></dt>
1943
 
<dd><div class="inherited-member container">
1944
 
<em>inherited from the</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.TableClause.delete" title="sqlalchemy.sql.expression.TableClause.delete"><tt class="xref py py-meth docutils literal"><span class="pre">delete()</span></tt></a> <em>method of</em> <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">TableClause</span></tt></a></div>
1945
 
<p>Generate a <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.delete" title="sqlalchemy.sql.expression.delete"><tt class="xref py py-func docutils literal"><span class="pre">delete()</span></tt></a> construct against this
1946
 
<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">TableClause</span></tt></a>.</p>
1947
 
<p>E.g.:</p>
1948
 
<div class="highlight-python"><div class="highlight"><pre><span class="n">table</span><span class="o">.</span><span class="n">delete</span><span class="p">()</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">table</span><span class="o">.</span><span class="n">c</span><span class="o">.</span><span class="n">id</span><span class="o">==</span><span class="mi">7</span><span class="p">)</span></pre></div>
1949
 
</div>
1950
 
<p>See <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.delete" title="sqlalchemy.sql.expression.delete"><tt class="xref py py-func docutils literal"><span class="pre">delete()</span></tt></a> for argument and usage information.</p>
1951
 
</dd></dl>
1952
 
 
1953
 
<dl class="attribute">
1954
 
<dt id="sqlalchemy.schema.Table.description">
1955
 
<tt class="descname">description</tt><a class="headerlink" href="#sqlalchemy.schema.Table.description" title="Permalink to this definition">¶</a></dt>
1956
 
<dd><div class="inherited-member container">
1957
 
<em>inherited from the</em> <tt class="xref py py-attr docutils literal"><span class="pre">description</span></tt> <em>attribute of</em> <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">TableClause</span></tt></a></div>
1958
 
</dd></dl>
1959
 
 
1960
 
<dl class="attribute">
1961
 
<dt id="sqlalchemy.schema.Table.dispatch">
1962
 
<tt class="descname">dispatch</tt><a class="headerlink" href="#sqlalchemy.schema.Table.dispatch" title="Permalink to this definition">¶</a></dt>
1963
 
<dd><p>alias of <tt class="xref py py-class docutils literal"><span class="pre">DDLEventsDispatch</span></tt></p>
1964
 
</dd></dl>
1965
 
 
1966
 
<dl class="method">
1967
 
<dt id="sqlalchemy.schema.Table.drop">
1968
 
<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>
1969
 
<dd><p>Issue a <tt class="docutils literal"><span class="pre">DROP</span></tt> statement for this
1970
 
<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 the given <a class="reference internal" href="connections.html#sqlalchemy.engine.Connectable" title="sqlalchemy.engine.Connectable"><tt class="xref py py-class docutils literal"><span class="pre">Connectable</span></tt></a>
1971
 
for connectivity.</p>
1972
 
<p>See also <a class="reference internal" href="#sqlalchemy.schema.MetaData.drop_all" title="sqlalchemy.schema.MetaData.drop_all"><tt class="xref py py-meth docutils literal"><span class="pre">MetaData.drop_all()</span></tt></a>.</p>
1973
 
</dd></dl>
1974
 
 
1975
 
<dl class="method">
1976
 
<dt id="sqlalchemy.schema.Table.exists">
1977
 
<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>
1978
 
<dd><p>Return True if this table exists.</p>
1979
 
</dd></dl>
1980
 
 
1981
 
<dl class="attribute">
1982
 
<dt id="sqlalchemy.schema.Table.foreign_keys">
1983
 
<tt class="descname">foreign_keys</tt><a class="headerlink" href="#sqlalchemy.schema.Table.foreign_keys" title="Permalink to this definition">¶</a></dt>
1984
 
<dd><div class="inherited-member container">
1985
 
<em>inherited from the</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.FromClause.foreign_keys" title="sqlalchemy.sql.expression.FromClause.foreign_keys"><tt class="xref py py-attr docutils literal"><span class="pre">foreign_keys</span></tt></a> <em>attribute of</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.FromClause" title="sqlalchemy.sql.expression.FromClause"><tt class="xref py py-class docutils literal"><span class="pre">FromClause</span></tt></a></div>
1986
 
<p>Return the collection of ForeignKey objects which this
1987
 
FromClause references.</p>
1988
 
</dd></dl>
1989
 
 
1990
 
<dl class="method">
1991
 
<dt id="sqlalchemy.schema.Table.get_children">
1992
 
<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>
1993
 
<dd></dd></dl>
1994
 
 
1995
 
<dl class="attribute">
1996
 
<dt id="sqlalchemy.schema.Table.implicit_returning">
1997
 
<tt class="descname">implicit_returning</tt><em class="property"> = False</em><a class="headerlink" href="#sqlalchemy.schema.Table.implicit_returning" title="Permalink to this definition">¶</a></dt>
1998
 
<dd></dd></dl>
1999
 
 
2000
 
<dl class="attribute">
2001
 
<dt id="sqlalchemy.schema.Table.info">
2002
 
<tt class="descname">info</tt><a class="headerlink" href="#sqlalchemy.schema.Table.info" title="Permalink to this definition">¶</a></dt>
2003
 
<dd><div class="inherited-member container">
2004
 
<em>inherited from the</em> <a class="reference internal" href="#sqlalchemy.schema.SchemaItem.info" title="sqlalchemy.schema.SchemaItem.info"><tt class="xref py py-attr docutils literal"><span class="pre">info</span></tt></a> <em>attribute of</em> <a class="reference internal" href="#sqlalchemy.schema.SchemaItem" title="sqlalchemy.schema.SchemaItem"><tt class="xref py py-class docutils literal"><span class="pre">SchemaItem</span></tt></a></div>
2005
 
<p>Info dictionary associated with the object, allowing user-defined
2006
 
data to be associated with this <a class="reference internal" href="#sqlalchemy.schema.SchemaItem" title="sqlalchemy.schema.SchemaItem"><tt class="xref py py-class docutils literal"><span class="pre">SchemaItem</span></tt></a>.</p>
2007
 
<p>The dictionary is automatically generated when first accessed.
2008
 
It can also be specified in the constructor of some objects,
2009
 
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> and <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>
2010
 
</dd></dl>
2011
 
 
2012
 
<dl class="method">
2013
 
<dt id="sqlalchemy.schema.Table.insert">
2014
 
<tt class="descname">insert</tt><big>(</big><em>values=None</em>, <em>inline=False</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Table.insert" title="Permalink to this definition">¶</a></dt>
2015
 
<dd><div class="inherited-member container">
2016
 
<em>inherited from the</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.TableClause.insert" title="sqlalchemy.sql.expression.TableClause.insert"><tt class="xref py py-meth docutils literal"><span class="pre">insert()</span></tt></a> <em>method of</em> <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">TableClause</span></tt></a></div>
2017
 
<p>Generate an <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.insert" title="sqlalchemy.sql.expression.insert"><tt class="xref py py-func docutils literal"><span class="pre">insert()</span></tt></a> construct against this
2018
 
<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">TableClause</span></tt></a>.</p>
2019
 
<p>E.g.:</p>
2020
 
<div class="highlight-python"><div class="highlight"><pre><span class="n">table</span><span class="o">.</span><span class="n">insert</span><span class="p">()</span><span class="o">.</span><span class="n">values</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">&#39;foo&#39;</span><span class="p">)</span></pre></div>
2021
 
</div>
2022
 
<p>See <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.insert" title="sqlalchemy.sql.expression.insert"><tt class="xref py py-func docutils literal"><span class="pre">insert()</span></tt></a> for argument and usage information.</p>
2023
 
</dd></dl>
2024
 
 
2025
 
<dl class="attribute">
2026
 
<dt id="sqlalchemy.schema.Table.is_clause_element">
2027
 
<tt class="descname">is_clause_element</tt><em class="property"> = True</em><a class="headerlink" href="#sqlalchemy.schema.Table.is_clause_element" title="Permalink to this definition">¶</a></dt>
2028
 
<dd></dd></dl>
2029
 
 
2030
 
<dl class="method">
2031
 
<dt id="sqlalchemy.schema.Table.is_derived_from">
2032
 
<tt class="descname">is_derived_from</tt><big>(</big><em>fromclause</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Table.is_derived_from" title="Permalink to this definition">¶</a></dt>
2033
 
<dd><div class="inherited-member container">
2034
 
<em>inherited from the</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.FromClause.is_derived_from" title="sqlalchemy.sql.expression.FromClause.is_derived_from"><tt class="xref py py-meth docutils literal"><span class="pre">is_derived_from()</span></tt></a> <em>method of</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.FromClause" title="sqlalchemy.sql.expression.FromClause"><tt class="xref py py-class docutils literal"><span class="pre">FromClause</span></tt></a></div>
2035
 
<p>Return True if this FromClause is &#8216;derived&#8217; from the given
2036
 
FromClause.</p>
2037
 
<p>An example would be an Alias of a Table is derived from that Table.</p>
2038
 
</dd></dl>
2039
 
 
2040
 
<dl class="attribute">
2041
 
<dt id="sqlalchemy.schema.Table.is_selectable">
2042
 
<tt class="descname">is_selectable</tt><em class="property"> = True</em><a class="headerlink" href="#sqlalchemy.schema.Table.is_selectable" title="Permalink to this definition">¶</a></dt>
2043
 
<dd></dd></dl>
2044
 
 
2045
 
<dl class="method">
2046
 
<dt id="sqlalchemy.schema.Table.join">
2047
 
<tt class="descname">join</tt><big>(</big><em>right</em>, <em>onclause=None</em>, <em>isouter=False</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Table.join" title="Permalink to this definition">¶</a></dt>
2048
 
<dd><div class="inherited-member container">
2049
 
<em>inherited from the</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.FromClause.join" title="sqlalchemy.sql.expression.FromClause.join"><tt class="xref py py-meth docutils literal"><span class="pre">join()</span></tt></a> <em>method of</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.FromClause" title="sqlalchemy.sql.expression.FromClause"><tt class="xref py py-class docutils literal"><span class="pre">FromClause</span></tt></a></div>
2050
 
<p>return a join of this <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.FromClause" title="sqlalchemy.sql.expression.FromClause"><tt class="xref py py-class docutils literal"><span class="pre">FromClause</span></tt></a> against another
2051
 
<a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.FromClause" title="sqlalchemy.sql.expression.FromClause"><tt class="xref py py-class docutils literal"><span class="pre">FromClause</span></tt></a>.</p>
2052
 
</dd></dl>
2053
 
 
2054
 
<dl class="attribute">
2055
 
<dt id="sqlalchemy.schema.Table.key">
2056
 
<tt class="descname">key</tt><a class="headerlink" href="#sqlalchemy.schema.Table.key" title="Permalink to this definition">¶</a></dt>
2057
 
<dd></dd></dl>
2058
 
 
2059
 
<dl class="attribute">
2060
 
<dt id="sqlalchemy.schema.Table.named_with_column">
2061
 
<tt class="descname">named_with_column</tt><em class="property"> = True</em><a class="headerlink" href="#sqlalchemy.schema.Table.named_with_column" title="Permalink to this definition">¶</a></dt>
2062
 
<dd></dd></dl>
2063
 
 
2064
 
<dl class="method">
2065
 
<dt id="sqlalchemy.schema.Table.outerjoin">
2066
 
<tt class="descname">outerjoin</tt><big>(</big><em>right</em>, <em>onclause=None</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Table.outerjoin" title="Permalink to this definition">¶</a></dt>
2067
 
<dd><div class="inherited-member container">
2068
 
<em>inherited from the</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.FromClause.outerjoin" title="sqlalchemy.sql.expression.FromClause.outerjoin"><tt class="xref py py-meth docutils literal"><span class="pre">outerjoin()</span></tt></a> <em>method of</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.FromClause" title="sqlalchemy.sql.expression.FromClause"><tt class="xref py py-class docutils literal"><span class="pre">FromClause</span></tt></a></div>
2069
 
<p>return an outer join of this <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.FromClause" title="sqlalchemy.sql.expression.FromClause"><tt class="xref py py-class docutils literal"><span class="pre">FromClause</span></tt></a> against another
2070
 
<a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.FromClause" title="sqlalchemy.sql.expression.FromClause"><tt class="xref py py-class docutils literal"><span class="pre">FromClause</span></tt></a>.</p>
2071
 
</dd></dl>
2072
 
 
2073
 
<dl class="method">
2074
 
<dt id="sqlalchemy.schema.Table.params">
2075
 
<tt class="descname">params</tt><big>(</big><em>*optionaldict</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Table.params" title="Permalink to this definition">¶</a></dt>
2076
 
<dd><div class="inherited-member container">
2077
 
<em>inherited from the</em> <tt class="xref py py-meth docutils literal"><span class="pre">params()</span></tt> <em>method of</em> <tt class="xref py py-class docutils literal"><span class="pre">Immutable</span></tt></div>
2078
 
</dd></dl>
2079
 
 
2080
 
<dl class="attribute">
2081
 
<dt id="sqlalchemy.schema.Table.primary_key">
2082
 
<tt class="descname">primary_key</tt><a class="headerlink" href="#sqlalchemy.schema.Table.primary_key" title="Permalink to this definition">¶</a></dt>
2083
 
<dd><div class="inherited-member container">
2084
 
<em>inherited from the</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.FromClause.primary_key" title="sqlalchemy.sql.expression.FromClause.primary_key"><tt class="xref py py-attr docutils literal"><span class="pre">primary_key</span></tt></a> <em>attribute of</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.FromClause" title="sqlalchemy.sql.expression.FromClause"><tt class="xref py py-class docutils literal"><span class="pre">FromClause</span></tt></a></div>
2085
 
<p>Return the collection of Column objects which comprise the
2086
 
primary key of this FromClause.</p>
2087
 
</dd></dl>
2088
 
 
2089
 
<dl class="attribute">
2090
 
<dt id="sqlalchemy.schema.Table.quote">
2091
 
<tt class="descname">quote</tt><em class="property"> = None</em><a class="headerlink" href="#sqlalchemy.schema.Table.quote" title="Permalink to this definition">¶</a></dt>
2092
 
<dd></dd></dl>
2093
 
 
2094
 
<dl class="method">
2095
 
<dt id="sqlalchemy.schema.Table.replace_selectable">
2096
 
<tt class="descname">replace_selectable</tt><big>(</big><em>old</em>, <em>alias</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Table.replace_selectable" title="Permalink to this definition">¶</a></dt>
2097
 
<dd><div class="inherited-member container">
2098
 
<em>inherited from the</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.FromClause.replace_selectable" title="sqlalchemy.sql.expression.FromClause.replace_selectable"><tt class="xref py py-meth docutils literal"><span class="pre">replace_selectable()</span></tt></a> <em>method of</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.FromClause" title="sqlalchemy.sql.expression.FromClause"><tt class="xref py py-class docutils literal"><span class="pre">FromClause</span></tt></a></div>
2099
 
<p>replace all occurrences of FromClause &#8216;old&#8217; with the given Alias
2100
 
object, returning a copy of this <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.FromClause" title="sqlalchemy.sql.expression.FromClause"><tt class="xref py py-class docutils literal"><span class="pre">FromClause</span></tt></a>.</p>
2101
 
</dd></dl>
2102
 
 
2103
 
<dl class="attribute">
2104
 
<dt id="sqlalchemy.schema.Table.schema">
2105
 
<tt class="descname">schema</tt><em class="property"> = None</em><a class="headerlink" href="#sqlalchemy.schema.Table.schema" title="Permalink to this definition">¶</a></dt>
2106
 
<dd></dd></dl>
2107
 
 
2108
 
<dl class="method">
2109
 
<dt id="sqlalchemy.schema.Table.select">
2110
 
<tt class="descname">select</tt><big>(</big><em>whereclause=None</em>, <em>**params</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Table.select" title="Permalink to this definition">¶</a></dt>
2111
 
<dd><div class="inherited-member container">
2112
 
<em>inherited from the</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.FromClause.select" title="sqlalchemy.sql.expression.FromClause.select"><tt class="xref py py-meth docutils literal"><span class="pre">select()</span></tt></a> <em>method of</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.FromClause" title="sqlalchemy.sql.expression.FromClause"><tt class="xref py py-class docutils literal"><span class="pre">FromClause</span></tt></a></div>
2113
 
<p>return a SELECT of this <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.FromClause" title="sqlalchemy.sql.expression.FromClause"><tt class="xref py py-class docutils literal"><span class="pre">FromClause</span></tt></a>.</p>
2114
 
<div class="admonition-see-also admonition seealso">
2115
 
<p class="first admonition-title">See also</p>
2116
 
<p class="last"><a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.select" title="sqlalchemy.sql.expression.select"><tt class="xref py py-func docutils literal"><span class="pre">select()</span></tt></a> - general purpose
2117
 
method which allows for arbitrary column lists.</p>
2118
 
</div>
2119
 
</dd></dl>
2120
 
 
2121
 
<dl class="attribute">
2122
 
<dt id="sqlalchemy.schema.Table.selectable">
2123
 
<tt class="descname">selectable</tt><a class="headerlink" href="#sqlalchemy.schema.Table.selectable" title="Permalink to this definition">¶</a></dt>
2124
 
<dd><div class="inherited-member container">
2125
 
<em>inherited from the</em> <tt class="xref py py-attr docutils literal"><span class="pre">selectable</span></tt> <em>attribute of</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.Selectable" title="sqlalchemy.sql.expression.Selectable"><tt class="xref py py-class docutils literal"><span class="pre">Selectable</span></tt></a></div>
2126
 
</dd></dl>
2127
 
 
2128
 
<dl class="method">
2129
 
<dt id="sqlalchemy.schema.Table.self_group">
2130
 
<tt class="descname">self_group</tt><big>(</big><em>against=None</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Table.self_group" title="Permalink to this definition">¶</a></dt>
2131
 
<dd><div class="inherited-member container">
2132
 
<em>inherited from the</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.ClauseElement.self_group" title="sqlalchemy.sql.expression.ClauseElement.self_group"><tt class="xref py py-meth docutils literal"><span class="pre">self_group()</span></tt></a> <em>method of</em> <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></div>
2133
 
<p>Apply a &#8216;grouping&#8217; to this <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>.</p>
2134
 
<p>This method is overridden by subclasses to return a
2135
 
&#8220;grouping&#8221; construct, i.e. parenthesis.   In particular
2136
 
it&#8217;s used by &#8220;binary&#8221; expressions to provide a grouping
2137
 
around themselves when placed into a larger expression,
2138
 
as well as by <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.select" title="sqlalchemy.sql.expression.select"><tt class="xref py py-func docutils literal"><span class="pre">select()</span></tt></a> constructs when placed into
2139
 
the FROM clause of another <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.select" title="sqlalchemy.sql.expression.select"><tt class="xref py py-func docutils literal"><span class="pre">select()</span></tt></a>.  (Note that
2140
 
subqueries should be normally created using the
2141
 
<tt class="xref py py-func docutils literal"><span class="pre">Select.alias()</span></tt> method, as many platforms require
2142
 
nested SELECT statements to be named).</p>
2143
 
<p>As expressions are composed together, the application of
2144
 
<a class="reference internal" href="#sqlalchemy.schema.Table.self_group" title="sqlalchemy.schema.Table.self_group"><tt class="xref py py-meth docutils literal"><span class="pre">self_group()</span></tt></a> is automatic - end-user code should never
2145
 
need to use this method directly.  Note that SQLAlchemy&#8217;s
2146
 
clause constructs take operator precedence into account -
2147
 
so parenthesis might not be needed, for example, in
2148
 
an expression like <tt class="docutils literal"><span class="pre">x</span> <span class="pre">OR</span> <span class="pre">(y</span> <span class="pre">AND</span> <span class="pre">z)</span></tt> - AND takes precedence
2149
 
over OR.</p>
2150
 
<p>The base <a class="reference internal" href="#sqlalchemy.schema.Table.self_group" title="sqlalchemy.schema.Table.self_group"><tt class="xref py py-meth docutils literal"><span class="pre">self_group()</span></tt></a> method of <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>
2151
 
just returns self.</p>
2152
 
</dd></dl>
2153
 
 
2154
 
<dl class="attribute">
2155
 
<dt id="sqlalchemy.schema.Table.supports_execution">
2156
 
<tt class="descname">supports_execution</tt><em class="property"> = False</em><a class="headerlink" href="#sqlalchemy.schema.Table.supports_execution" title="Permalink to this definition">¶</a></dt>
2157
 
<dd></dd></dl>
2158
 
 
2159
 
<dl class="method">
2160
 
<dt id="sqlalchemy.schema.Table.tometadata">
2161
 
<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>
2162
 
<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
2163
 
<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>
2164
 
<p>E.g.:</p>
2165
 
<div class="highlight-python"><div class="highlight"><pre><span class="n">some_engine</span> <span class="o">=</span> <span class="n">create_engine</span><span class="p">(</span><span class="s">&quot;sqlite:///some.db&quot;</span><span class="p">)</span>
2166
 
 
2167
 
<span class="c"># create two metadata</span>
2168
 
<span class="n">meta1</span> <span class="o">=</span> <span class="n">MetaData</span><span class="p">()</span>
2169
 
<span class="n">meta2</span> <span class="o">=</span> <span class="n">MetaData</span><span class="p">()</span>
2170
 
 
2171
 
<span class="c"># load &#39;users&#39; from the sqlite engine</span>
2172
 
<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>
2173
 
                        <span class="n">autoload_with</span><span class="o">=</span><span class="n">some_engine</span><span class="p">)</span>
2174
 
 
2175
 
<span class="c"># create the same Table object for the plain metadata</span>
2176
 
<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>
2177
 
</div>
2178
 
<table class="docutils field-list" frame="void" rules="none">
2179
 
<col class="field-name" />
2180
 
<col class="field-body" />
2181
 
<tbody valign="top">
2182
 
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
2183
 
<li><strong>metadata</strong> &#8211; Target <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.</li>
2184
 
<li><strong>schema</strong> &#8211; Optional string name of a target schema, or
2185
 
<tt class="docutils literal"><span class="pre">None</span></tt> for no schema.  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 will be
2186
 
given this schema name upon copy.   Defaults to the special
2187
 
symbol <tt class="xref py py-attr docutils literal"><span class="pre">RETAIN_SCHEMA</span></tt> which indicates no change should be
2188
 
made to the schema name of the resulting <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>.</li>
2189
 
</ul>
2190
 
</td>
2191
 
</tr>
2192
 
</tbody>
2193
 
</table>
2194
 
</dd></dl>
2195
 
 
2196
 
<dl class="method">
2197
 
<dt id="sqlalchemy.schema.Table.unique_params">
2198
 
<tt class="descname">unique_params</tt><big>(</big><em>*optionaldict</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Table.unique_params" title="Permalink to this definition">¶</a></dt>
2199
 
<dd><div class="inherited-member container">
2200
 
<em>inherited from the</em> <tt class="xref py py-meth docutils literal"><span class="pre">unique_params()</span></tt> <em>method of</em> <tt class="xref py py-class docutils literal"><span class="pre">Immutable</span></tt></div>
2201
 
</dd></dl>
2202
 
 
2203
 
<dl class="method">
2204
 
<dt id="sqlalchemy.schema.Table.update">
2205
 
<tt class="descname">update</tt><big>(</big><em>whereclause=None</em>, <em>values=None</em>, <em>inline=False</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Table.update" title="Permalink to this definition">¶</a></dt>
2206
 
<dd><div class="inherited-member container">
2207
 
<em>inherited from the</em> <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.TableClause.update" title="sqlalchemy.sql.expression.TableClause.update"><tt class="xref py py-meth docutils literal"><span class="pre">update()</span></tt></a> <em>method of</em> <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">TableClause</span></tt></a></div>
2208
 
<p>Generate an <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.update" title="sqlalchemy.sql.expression.update"><tt class="xref py py-func docutils literal"><span class="pre">update()</span></tt></a> construct against this
2209
 
<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">TableClause</span></tt></a>.</p>
2210
 
<p>E.g.:</p>
2211
 
<div class="highlight-python"><div class="highlight"><pre><span class="n">table</span><span class="o">.</span><span class="n">update</span><span class="p">()</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">table</span><span class="o">.</span><span class="n">c</span><span class="o">.</span><span class="n">id</span><span class="o">==</span><span class="mi">7</span><span class="p">)</span><span class="o">.</span><span class="n">values</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">&#39;foo&#39;</span><span class="p">)</span></pre></div>
2212
 
</div>
2213
 
<p>See <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.update" title="sqlalchemy.sql.expression.update"><tt class="xref py py-func docutils literal"><span class="pre">update()</span></tt></a> for argument and usage information.</p>
2214
 
</dd></dl>
2215
 
 
2216
 
</dd></dl>
2217
 
 
2218
 
<dl class="class">
2219
 
<dt id="sqlalchemy.schema.ThreadLocalMetaData">
2220
 
<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>
2221
 
<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>
2222
 
<p>A MetaData variant that presents a different <tt class="docutils literal"><span class="pre">bind</span></tt> in every thread.</p>
2223
 
<p>Makes the <tt class="docutils literal"><span class="pre">bind</span></tt> property of the MetaData a thread-local value, allowing
2224
 
this collection of tables to be bound to different <tt class="docutils literal"><span class="pre">Engine</span></tt>
2225
 
implementations or connections in each thread.</p>
2226
 
<p>The ThreadLocalMetaData starts off bound to None in each thread.  Binds
2227
 
must be made explicitly by assigning to the <tt class="docutils literal"><span class="pre">bind</span></tt> property or using
2228
 
<tt class="docutils literal"><span class="pre">connect()</span></tt>.  You can also re-bind dynamically multiple times per
2229
 
thread, just like a regular <tt class="docutils literal"><span class="pre">MetaData</span></tt>.</p>
2230
 
<dl class="method">
2231
 
<dt id="sqlalchemy.schema.ThreadLocalMetaData.__init__">
2232
 
<tt class="descname">__init__</tt><big>(</big><big>)</big><a class="headerlink" href="#sqlalchemy.schema.ThreadLocalMetaData.__init__" title="Permalink to this definition">¶</a></dt>
2233
 
<dd><p>Construct a ThreadLocalMetaData.</p>
2234
 
</dd></dl>
2235
 
 
2236
 
<dl class="attribute">
2237
 
<dt id="sqlalchemy.schema.ThreadLocalMetaData.bind">
2238
 
<tt class="descname">bind</tt><a class="headerlink" href="#sqlalchemy.schema.ThreadLocalMetaData.bind" title="Permalink to this definition">¶</a></dt>
2239
 
<dd><p>The bound Engine or Connection for this thread.</p>
2240
 
<p>This property may be assigned an Engine or Connection, or assigned a
2241
 
string or URL to automatically create a basic Engine for this bind
2242
 
with <tt class="docutils literal"><span class="pre">create_engine()</span></tt>.</p>
2243
 
</dd></dl>
2244
 
 
2245
 
<dl class="method">
2246
 
<dt id="sqlalchemy.schema.ThreadLocalMetaData.dispose">
2247
 
<tt class="descname">dispose</tt><big>(</big><big>)</big><a class="headerlink" href="#sqlalchemy.schema.ThreadLocalMetaData.dispose" title="Permalink to this definition">¶</a></dt>
2248
 
<dd><p>Dispose all bound engines, in all thread contexts.</p>
2249
 
</dd></dl>
2250
 
 
2251
 
<dl class="method">
2252
 
<dt id="sqlalchemy.schema.ThreadLocalMetaData.is_bound">
2253
 
<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>
2254
 
<dd><p>True if there is a bind for this thread.</p>
2255
 
</dd></dl>
2256
 
 
2257
 
</dd></dl>
2258
 
 
2259
 
</div>
2260
 
</div>
2261
 
<div class="section" id="reflecting-database-objects">
2262
 
<span id="metadata-reflection"></span><h2>Reflecting Database Objects<a class="headerlink" href="#reflecting-database-objects" title="Permalink to this headline">¶</a></h2>
2263
 
<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
2264
 
information about itself from the corresponding database schema object already
2265
 
existing within the database. This process is called <em>reflection</em>. In the
2266
 
most simple case 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>
2267
 
object, and the <tt class="docutils literal"><span class="pre">autoload=True</span></tt> flag. If the
2268
 
<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
2269
 
<tt class="docutils literal"><span class="pre">autoload_with</span></tt> argument:</p>
2270
 
<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>
2271
 
<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>
2272
 
<span class="go">[&#39;message_id&#39;, &#39;message_name&#39;, &#39;date&#39;]</span></pre></div>
2273
 
</div>
2274
 
<p>The above operation will use the given engine to query the database for
2275
 
information about the <tt class="docutils literal"><span class="pre">messages</span></tt> table, and will then generate
2276
 
<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>,
2277
 
and other objects corresponding to this information as though the
2278
 
<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>
2279
 
<p>When tables are reflected, if a given table references another one via foreign
2280
 
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
2281
 
<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.
2282
 
Below, assume the table <tt class="docutils literal"><span class="pre">shopping_cart_items</span></tt> references a table named
2283
 
<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
2284
 
effect such that the <tt class="docutils literal"><span class="pre">shopping_carts</span></tt> table will also be loaded:</p>
2285
 
<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>
2286
 
<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>
2287
 
<span class="go">True</span></pre></div>
2288
 
</div>
2289
 
<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;
2290
 
behavior such that if you requested both tables individually,
2291
 
<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
2292
 
<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
2293
 
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
2294
 
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
2295
 
already exists with the given name. Such as below, we can access the already
2296
 
generated <tt class="docutils literal"><span class="pre">shopping_carts</span></tt> table just by naming it:</p>
2297
 
<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>
2298
 
</div>
2299
 
<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
2300
 
regardless. This is so that the table&#8217;s attributes will be loaded if they have
2301
 
not been already. The autoload operation only occurs for the table if it
2302
 
hasn&#8217;t already been loaded; once loaded, new calls to
2303
 
<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
2304
 
reflection queries.</p>
2305
 
<div class="section" id="overriding-reflected-columns">
2306
 
<h3>Overriding Reflected Columns<a class="headerlink" href="#overriding-reflected-columns" title="Permalink to this headline">¶</a></h3>
2307
 
<p>Individual columns can be overridden with explicit values when reflecting
2308
 
tables; this is handy for specifying custom datatypes, constraints such as
2309
 
primary keys that may not be configured within the database, etc.:</p>
2310
 
<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>
2311
 
<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>
2312
 
<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>
2313
 
<span class="gp">... </span><span class="n">autoload</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span></pre></div>
2314
 
</div>
2315
 
</div>
2316
 
<div class="section" id="reflecting-views">
2317
 
<h3>Reflecting Views<a class="headerlink" href="#reflecting-views" title="Permalink to this headline">¶</a></h3>
2318
 
<p>The reflection system can also reflect views. Basic usage is the same as that
2319
 
of a table:</p>
2320
 
<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>
2321
 
</div>
2322
 
<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
2323
 
<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
2324
 
each column within the view &#8220;some_view&#8221;.</p>
2325
 
<p>Usually, it&#8217;s desired to have at least a primary key constraint when
2326
 
reflecting a view, if not foreign keys as well. View reflection doesn&#8217;t
2327
 
extrapolate these constraints.</p>
2328
 
<p>Use the &#8220;override&#8221; technique for this, specifying explicitly those columns
2329
 
which are part of the primary key or have foreign key constraints:</p>
2330
 
<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>
2331
 
                <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>
2332
 
                <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>
2333
 
                <span class="n">autoload</span><span class="o">=</span><span class="bp">True</span>
2334
 
<span class="p">)</span></pre></div>
2335
 
</div>
2336
 
</div>
2337
 
<div class="section" id="reflecting-all-tables-at-once">
2338
 
<h3>Reflecting All Tables at Once<a class="headerlink" href="#reflecting-all-tables-at-once" title="Permalink to this headline">¶</a></h3>
2339
 
<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
2340
 
tables and reflect the full set. This is achieved by using the
2341
 
<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
2342
 
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>
2343
 
object&#8217;s dictionary of tables:</p>
2344
 
<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>
2345
 
<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>
2346
 
<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>
2347
 
<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>
2348
 
</div>
2349
 
<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>
2350
 
<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>
2351
 
<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>
2352
 
<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>
2353
 
    <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>
2354
 
</div>
2355
 
</div>
2356
 
<div class="section" id="fine-grained-reflection-with-inspector">
2357
 
<h3>Fine Grained Reflection with Inspector<a class="headerlink" href="#fine-grained-reflection-with-inspector" title="Permalink to this headline">¶</a></h3>
2358
 
<p>A low level interface which provides a backend-agnostic system of loading
2359
 
lists of schema, table, column, and constraint descriptions from a given
2360
 
database is also available. This is known as the &#8220;Inspector&#8221;:</p>
2361
 
<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>
2362
 
<span class="kn">from</span> <span class="nn">sqlalchemy.engine</span> <span class="kn">import</span> <span class="n">reflection</span>
2363
 
<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>
2364
 
<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>
2365
 
<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>
2366
 
</div>
2367
 
<dl class="class">
2368
 
<dt id="sqlalchemy.engine.reflection.Inspector">
2369
 
<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>
2370
 
<dd><p>Bases: <tt class="xref py py-class docutils literal"><span class="pre">object</span></tt></p>
2371
 
<p>Performs database schema inspection.</p>
2372
 
<p>The Inspector acts as a proxy to the reflection methods of the
2373
 
<a class="reference internal" href="internals.html#sqlalchemy.engine.interfaces.Dialect" title="sqlalchemy.engine.interfaces.Dialect"><tt class="xref py py-class docutils literal"><span class="pre">Dialect</span></tt></a>, providing a
2374
 
consistent interface as well as caching support for previously
2375
 
fetched metadata.</p>
2376
 
<p>A <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> object is usually created via the
2377
 
<a class="reference internal" href="inspection.html#sqlalchemy.inspection.inspect" title="sqlalchemy.inspection.inspect"><tt class="xref py py-func docutils literal"><span class="pre">inspect()</span></tt></a> function:</p>
2378
 
<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">inspect</span><span class="p">,</span> <span class="n">create_engine</span>
2379
 
<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>
2380
 
<span class="n">insp</span> <span class="o">=</span> <span class="n">inspect</span><span class="p">(</span><span class="n">engine</span><span class="p">)</span></pre></div>
2381
 
</div>
2382
 
<p>The inspection method above is equivalent to using the
2383
 
<a class="reference internal" href="#sqlalchemy.engine.reflection.Inspector.from_engine" title="sqlalchemy.engine.reflection.Inspector.from_engine"><tt class="xref py py-meth docutils literal"><span class="pre">Inspector.from_engine()</span></tt></a> method, i.e.:</p>
2384
 
<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>
2385
 
<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>
2386
 
</div>
2387
 
<p>Where above, the <a class="reference internal" href="internals.html#sqlalchemy.engine.interfaces.Dialect" title="sqlalchemy.engine.interfaces.Dialect"><tt class="xref py py-class docutils literal"><span class="pre">Dialect</span></tt></a> may opt
2388
 
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
2389
 
methods specific to the dialect&#8217;s target database.</p>
2390
 
<dl class="method">
2391
 
<dt id="sqlalchemy.engine.reflection.Inspector.__init__">
2392
 
<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>
2393
 
<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>
2394
 
<table class="docutils field-list" frame="void" rules="none">
2395
 
<col class="field-name" />
2396
 
<col class="field-body" />
2397
 
<tbody valign="top">
2398
 
<tr class="field-odd 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.Connectable" title="sqlalchemy.engine.Connectable"><tt class="xref py py-class docutils literal"><span class="pre">Connectable</span></tt></a>,
2399
 
which is typically an instance of
2400
 
<a class="reference internal" href="connections.html#sqlalchemy.engine.Engine" title="sqlalchemy.engine.Engine"><tt class="xref py py-class docutils literal"><span class="pre">Engine</span></tt></a> or
2401
 
<a class="reference internal" href="connections.html#sqlalchemy.engine.Connection" title="sqlalchemy.engine.Connection"><tt class="xref py py-class docutils literal"><span class="pre">Connection</span></tt></a>.</td>
2402
 
</tr>
2403
 
</tbody>
2404
 
</table>
2405
 
<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
2406
 
<a class="reference internal" href="#sqlalchemy.engine.reflection.Inspector.from_engine" title="sqlalchemy.engine.reflection.Inspector.from_engine"><tt class="xref py py-meth docutils literal"><span class="pre">Inspector.from_engine()</span></tt></a></p>
2407
 
</dd></dl>
2408
 
 
2409
 
<dl class="attribute">
2410
 
<dt id="sqlalchemy.engine.reflection.Inspector.default_schema_name">
2411
 
<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>
2412
 
<dd><p>Return the default schema name presented by the dialect
2413
 
for the current engine&#8217;s database user.</p>
2414
 
<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>
2415
 
for SQL Server.</p>
2416
 
</dd></dl>
2417
 
 
2418
 
<dl class="classmethod">
2419
 
<dt id="sqlalchemy.engine.reflection.Inspector.from_engine">
2420
 
<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>
2421
 
<dd><p>Construct a new dialect-specific Inspector object from the given
2422
 
engine or connection.</p>
2423
 
<table class="docutils field-list" frame="void" rules="none">
2424
 
<col class="field-name" />
2425
 
<col class="field-body" />
2426
 
<tbody valign="top">
2427
 
<tr class="field-odd 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.Connectable" title="sqlalchemy.engine.Connectable"><tt class="xref py py-class docutils literal"><span class="pre">Connectable</span></tt></a>,
2428
 
which is typically an instance of
2429
 
<a class="reference internal" href="connections.html#sqlalchemy.engine.Engine" title="sqlalchemy.engine.Engine"><tt class="xref py py-class docutils literal"><span class="pre">Engine</span></tt></a> or
2430
 
<a class="reference internal" href="connections.html#sqlalchemy.engine.Connection" title="sqlalchemy.engine.Connection"><tt class="xref py py-class docutils literal"><span class="pre">Connection</span></tt></a>.</td>
2431
 
</tr>
2432
 
</tbody>
2433
 
</table>
2434
 
<p>This method differs from direct a direct constructor call of
2435
 
<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
2436
 
<a class="reference internal" href="internals.html#sqlalchemy.engine.interfaces.Dialect" title="sqlalchemy.engine.interfaces.Dialect"><tt class="xref py py-class docutils literal"><span class="pre">Dialect</span></tt></a> is given a chance to
2437
 
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
2438
 
provide additional methods.</p>
2439
 
<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>
2440
 
</dd></dl>
2441
 
 
2442
 
<dl class="method">
2443
 
<dt id="sqlalchemy.engine.reflection.Inspector.get_columns">
2444
 
<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>
2445
 
<dd><p>Return information about columns in <cite>table_name</cite>.</p>
2446
 
<p>Given a string <cite>table_name</cite> and an optional string <cite>schema</cite>, return
2447
 
column information as a list of dicts with these keys:</p>
2448
 
<dl class="docutils">
2449
 
<dt>name</dt>
2450
 
<dd>the column&#8217;s name</dd>
2451
 
<dt>type</dt>
2452
 
<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>
2453
 
<dt>nullable</dt>
2454
 
<dd>boolean</dd>
2455
 
<dt>default</dt>
2456
 
<dd>the column&#8217;s default value</dd>
2457
 
<dt>attrs</dt>
2458
 
<dd>dict containing optional column attributes</dd>
2459
 
</dl>
2460
 
</dd></dl>
2461
 
 
2462
 
<dl class="method">
2463
 
<dt id="sqlalchemy.engine.reflection.Inspector.get_foreign_keys">
2464
 
<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>
2465
 
<dd><p>Return information about foreign_keys in <cite>table_name</cite>.</p>
2466
 
<p>Given a string <cite>table_name</cite>, and an optional string <cite>schema</cite>, return
2467
 
foreign key information as a list of dicts with these keys:</p>
2468
 
<dl class="docutils">
2469
 
<dt>constrained_columns</dt>
2470
 
<dd>a list of column names that make up the foreign key</dd>
2471
 
<dt>referred_schema</dt>
2472
 
<dd>the name of the referred schema</dd>
2473
 
<dt>referred_table</dt>
2474
 
<dd>the name of the referred table</dd>
2475
 
<dt>referred_columns</dt>
2476
 
<dd>a list of column names in the referred table that correspond to
2477
 
constrained_columns</dd>
2478
 
<dt>name</dt>
2479
 
<dd>optional name of the foreign key constraint.</dd>
2480
 
<dt>**kw</dt>
2481
 
<dd>other options passed to the dialect&#8217;s get_foreign_keys() method.</dd>
2482
 
</dl>
2483
 
</dd></dl>
2484
 
 
2485
 
<dl class="method">
2486
 
<dt id="sqlalchemy.engine.reflection.Inspector.get_indexes">
2487
 
<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>
2488
 
<dd><p>Return information about indexes in <cite>table_name</cite>.</p>
2489
 
<p>Given a string <cite>table_name</cite> and an optional string <cite>schema</cite>, return
2490
 
index information as a list of dicts with these keys:</p>
2491
 
<dl class="docutils">
2492
 
<dt>name</dt>
2493
 
<dd>the index&#8217;s name</dd>
2494
 
<dt>column_names</dt>
2495
 
<dd>list of column names in order</dd>
2496
 
<dt>unique</dt>
2497
 
<dd>boolean</dd>
2498
 
<dt>**kw</dt>
2499
 
<dd>other options passed to the dialect&#8217;s get_indexes() method.</dd>
2500
 
</dl>
2501
 
</dd></dl>
2502
 
 
2503
 
<dl class="method">
2504
 
<dt id="sqlalchemy.engine.reflection.Inspector.get_pk_constraint">
2505
 
<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>
2506
 
<dd><p>Return information about primary key constraint on <cite>table_name</cite>.</p>
2507
 
<p>Given a string <cite>table_name</cite>, and an optional string <cite>schema</cite>, return
2508
 
primary key information as a dictionary with these keys:</p>
2509
 
<dl class="docutils">
2510
 
<dt>constrained_columns</dt>
2511
 
<dd>a list of column names that make up the primary key</dd>
2512
 
<dt>name</dt>
2513
 
<dd>optional name of the primary key constraint.</dd>
2514
 
</dl>
2515
 
</dd></dl>
2516
 
 
2517
 
<dl class="method">
2518
 
<dt id="sqlalchemy.engine.reflection.Inspector.get_primary_keys">
2519
 
<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>
2520
 
<dd><p>Return information about primary keys in <cite>table_name</cite>.</p>
2521
 
<p class="deprecated">
2522
 
<span class="versionmodified">Deprecated since version 0.7: </span>Call to deprecated method get_primary_keys.  Use get_pk_constraint instead.</p>
2523
 
<p>Given a string <cite>table_name</cite>, and an optional string <cite>schema</cite>, return
2524
 
primary key information as a list of column names.</p>
2525
 
</dd></dl>
2526
 
 
2527
 
<dl class="method">
2528
 
<dt id="sqlalchemy.engine.reflection.Inspector.get_schema_names">
2529
 
<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>
2530
 
<dd><p>Return all schema names.</p>
2531
 
</dd></dl>
2532
 
 
2533
 
<dl class="method">
2534
 
<dt id="sqlalchemy.engine.reflection.Inspector.get_table_names">
2535
 
<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>
2536
 
<dd><p>Return all table names in referred to within a particular schema.</p>
2537
 
<p>The names are expected to be real tables only, not views.
2538
 
Views are instead returned using the <a class="reference internal" href="internals.html#sqlalchemy.engine.interfaces.Dialect.get_view_names" title="sqlalchemy.engine.interfaces.Dialect.get_view_names"><tt class="xref py py-meth docutils literal"><span class="pre">get_view_names()</span></tt></a>
2539
 
method.</p>
2540
 
<table class="docutils field-list" frame="void" rules="none">
2541
 
<col class="field-name" />
2542
 
<col class="field-body" />
2543
 
<tbody valign="top">
2544
 
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
2545
 
<li><strong>schema</strong> &#8211; Schema name. If <tt class="docutils literal"><span class="pre">schema</span></tt> is left at <tt class="docutils literal"><span class="pre">None</span></tt>, the
2546
 
database&#8217;s default schema is
2547
 
used, else the named schema is searched.  If the database does not
2548
 
support named schemas, behavior is undefined if <tt class="docutils literal"><span class="pre">schema</span></tt> is not
2549
 
passed as <tt class="docutils literal"><span class="pre">None</span></tt>.</li>
2550
 
<li><strong>order_by</strong> &#8211; <p>Optional, may be the string &#8220;foreign_key&#8221; to sort
2551
 
the result on foreign key dependencies.</p>
2552
 
<p class="versionchanged">
2553
 
<span class="versionmodified">Changed in version 0.8: </span>the &#8220;foreign_key&#8221; sorting sorts tables
2554
 
in order of dependee to dependent; that is, in creation
2555
 
order, rather than in drop order.  This is to maintain
2556
 
consistency with similar features such as
2557
 
<a class="reference internal" href="#sqlalchemy.schema.MetaData.sorted_tables" title="sqlalchemy.schema.MetaData.sorted_tables"><tt class="xref py py-attr docutils literal"><span class="pre">MetaData.sorted_tables</span></tt></a> and <tt class="xref py py-func docutils literal"><span class="pre">util.sort_tables()</span></tt>.</p>
2558
 
</li>
2559
 
</ul>
2560
 
</td>
2561
 
</tr>
2562
 
</tbody>
2563
 
</table>
2564
 
<div class="admonition-see-also admonition seealso">
2565
 
<p class="first admonition-title">See also</p>
2566
 
<p class="last"><a class="reference internal" href="#sqlalchemy.schema.MetaData.sorted_tables" title="sqlalchemy.schema.MetaData.sorted_tables"><tt class="xref py py-attr docutils literal"><span class="pre">MetaData.sorted_tables</span></tt></a></p>
2567
 
</div>
2568
 
</dd></dl>
2569
 
 
2570
 
<dl class="method">
2571
 
<dt id="sqlalchemy.engine.reflection.Inspector.get_table_options">
2572
 
<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>
2573
 
<dd><p>Return a dictionary of options specified when the table of the
2574
 
given name was created.</p>
2575
 
<p>This currently includes some options that apply to MySQL tables.</p>
2576
 
</dd></dl>
2577
 
 
2578
 
<dl class="method">
2579
 
<dt id="sqlalchemy.engine.reflection.Inspector.get_view_definition">
2580
 
<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>
2581
 
<dd><p>Return definition for <cite>view_name</cite>.</p>
2582
 
<table class="docutils field-list" frame="void" rules="none">
2583
 
<col class="field-name" />
2584
 
<col class="field-body" />
2585
 
<tbody valign="top">
2586
 
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>schema</strong> &#8211; Optional, retrieve names from a non-default schema.</td>
2587
 
</tr>
2588
 
</tbody>
2589
 
</table>
2590
 
</dd></dl>
2591
 
 
2592
 
<dl class="method">
2593
 
<dt id="sqlalchemy.engine.reflection.Inspector.get_view_names">
2594
 
<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>
2595
 
<dd><p>Return all view names in <cite>schema</cite>.</p>
2596
 
<table class="docutils field-list" frame="void" rules="none">
2597
 
<col class="field-name" />
2598
 
<col class="field-body" />
2599
 
<tbody valign="top">
2600
 
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>schema</strong> &#8211; Optional, retrieve names from a non-default schema.</td>
2601
 
</tr>
2602
 
</tbody>
2603
 
</table>
2604
 
</dd></dl>
2605
 
 
2606
 
<dl class="method">
2607
 
<dt id="sqlalchemy.engine.reflection.Inspector.reflecttable">
2608
 
<tt class="descname">reflecttable</tt><big>(</big><em>table</em>, <em>include_columns</em>, <em>exclude_columns=()</em><big>)</big><a class="headerlink" href="#sqlalchemy.engine.reflection.Inspector.reflecttable" title="Permalink to this definition">¶</a></dt>
2609
 
<dd><p>Given a Table object, load its internal constructs based on
2610
 
introspection.</p>
2611
 
<p>This is the underlying method used by most dialects to produce
2612
 
table reflection.  Direct usage is like:</p>
2613
 
<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>
2614
 
<span class="kn">from</span> <span class="nn">sqlalchemy.engine</span> <span class="kn">import</span> <span class="n">reflection</span>
2615
 
 
2616
 
<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>
2617
 
<span class="n">meta</span> <span class="o">=</span> <span class="n">MetaData</span><span class="p">()</span>
2618
 
<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>
2619
 
<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>
2620
 
<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>
2621
 
</div>
2622
 
<table class="docutils field-list" frame="void" rules="none">
2623
 
<col class="field-name" />
2624
 
<col class="field-body" />
2625
 
<tbody valign="top">
2626
 
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
2627
 
<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>
2628
 
<li><strong>include_columns</strong> &#8211; a list of string column names to include
2629
 
in the reflection process.  If <tt class="docutils literal"><span class="pre">None</span></tt>, all columns are reflected.</li>
2630
 
</ul>
2631
 
</td>
2632
 
</tr>
2633
 
</tbody>
2634
 
</table>
2635
 
</dd></dl>
2636
 
 
2637
 
</dd></dl>
2638
 
 
2639
 
</div>
2640
 
</div>
2641
 
<div class="section" id="column-insert-update-defaults">
2642
 
<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>
2643
 
<p>SQLAlchemy provides a very rich featureset regarding column level events which
2644
 
take place during INSERT and UPDATE statements. Options include:</p>
2645
 
<ul class="simple">
2646
 
<li>Scalar values used as defaults during INSERT and UPDATE operations</li>
2647
 
<li>Python functions which execute upon INSERT and UPDATE operations</li>
2648
 
<li>SQL expressions which are embedded in INSERT statements (or in some cases execute beforehand)</li>
2649
 
<li>SQL expressions which are embedded in UPDATE statements</li>
2650
 
<li>Server side default values used during INSERT</li>
2651
 
<li>Markers for server-side triggers used during UPDATE</li>
2652
 
</ul>
2653
 
<p>The general rule for all insert/update defaults is that they only take effect
2654
 
if no value for a particular column is passed as an <tt class="docutils literal"><span class="pre">execute()</span></tt> parameter;
2655
 
otherwise, the given value is used.</p>
2656
 
<div class="section" id="scalar-defaults">
2657
 
<h3>Scalar Defaults<a class="headerlink" href="#scalar-defaults" title="Permalink to this headline">¶</a></h3>
2658
 
<p>The simplest kind of default is a scalar value used as the default value of a column:</p>
2659
 
<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>
2660
 
    <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>
2661
 
<span class="p">)</span></pre></div>
2662
 
</div>
2663
 
<p>Above, the value &#8220;12&#8221; will be bound as the column value during an INSERT if no
2664
 
other value is supplied.</p>
2665
 
<p>A scalar value may also be associated with an UPDATE statement, though this is
2666
 
not very common (as UPDATE statements are usually looking for dynamic
2667
 
defaults):</p>
2668
 
<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>
2669
 
    <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>
2670
 
<span class="p">)</span></pre></div>
2671
 
</div>
2672
 
</div>
2673
 
<div class="section" id="python-executed-functions">
2674
 
<h3>Python-Executed Functions<a class="headerlink" href="#python-executed-functions" title="Permalink to this headline">¶</a></h3>
2675
 
<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
2676
 
functions. These functions are invoked at the time of insert or update if no
2677
 
other value for that column is supplied, and the value returned is used for
2678
 
the column&#8217;s value. Below illustrates a crude &#8220;sequence&#8221; that assigns an
2679
 
incrementing counter to a primary key column:</p>
2680
 
<div class="highlight-python"><div class="highlight"><pre><span class="c"># a function which counts upwards</span>
2681
 
<span class="n">i</span> <span class="o">=</span> <span class="mi">0</span>
2682
 
<span class="k">def</span> <span class="nf">mydefault</span><span class="p">():</span>
2683
 
    <span class="k">global</span> <span class="n">i</span>
2684
 
    <span class="n">i</span> <span class="o">+=</span> <span class="mi">1</span>
2685
 
    <span class="k">return</span> <span class="n">i</span>
2686
 
 
2687
 
<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>
2688
 
    <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>
2689
 
<span class="p">)</span></pre></div>
2690
 
</div>
2691
 
<p>It should be noted that for real &#8220;incrementing sequence&#8221; behavior, the
2692
 
built-in capabilities of the database should normally be used, which may
2693
 
include sequence objects or other autoincrementing capabilities. For primary
2694
 
key columns, SQLAlchemy will in most cases use these capabilities
2695
 
automatically. See the API documentation for
2696
 
<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
2697
 
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
2698
 
chapter for background on standard primary key generation techniques.</p>
2699
 
<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
2700
 
the <tt class="docutils literal"><span class="pre">onupdate</span></tt> attribute:</p>
2701
 
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">datetime</span>
2702
 
 
2703
 
<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>
2704
 
    <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>
2705
 
 
2706
 
    <span class="c"># define &#39;last_updated&#39; to be populated with datetime.now()</span>
2707
 
    <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>
2708
 
<span class="p">)</span></pre></div>
2709
 
</div>
2710
 
<p>When an update statement executes and no value is passed for <tt class="docutils literal"><span class="pre">last_updated</span></tt>,
2711
 
the <tt class="docutils literal"><span class="pre">datetime.datetime.now()</span></tt> Python function is executed and its return
2712
 
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>
2713
 
as the function itself without calling it (i.e. there are no parenthesis
2714
 
following) - SQLAlchemy will execute the function at the time the statement
2715
 
executes.</p>
2716
 
<div class="section" id="context-sensitive-default-functions">
2717
 
<h4>Context-Sensitive Default Functions<a class="headerlink" href="#context-sensitive-default-functions" title="Permalink to this headline">¶</a></h4>
2718
 
<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
2719
 
the current statement&#8217;s context in order to determine a value. The <cite>context</cite>
2720
 
of a statement is an internal SQLAlchemy object which contains all information
2721
 
about the statement being executed, including its source expression, the
2722
 
parameters associated with it and the cursor. The typical use case for this
2723
 
context with regards to default generation is to have access to the other
2724
 
values being inserted or updated on the row. To access the context, provide a
2725
 
function that accepts a single <tt class="docutils literal"><span class="pre">context</span></tt> argument:</p>
2726
 
<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>
2727
 
    <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>
2728
 
 
2729
 
<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>
2730
 
    <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>
2731
 
    <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>
2732
 
<span class="p">)</span></pre></div>
2733
 
</div>
2734
 
<p>Above we illustrate a default function which will execute for all INSERT and
2735
 
UPDATE statements where a value for <tt class="docutils literal"><span class="pre">counter_plus_twelve</span></tt> was otherwise not
2736
 
provided, and the value will be that of whatever value is present in the
2737
 
execution for the <tt class="docutils literal"><span class="pre">counter</span></tt> column, plus the number 12.</p>
2738
 
<p>While the context object passed to the default function has many attributes,
2739
 
the <tt class="docutils literal"><span class="pre">current_parameters</span></tt> member is a special member provided only during the
2740
 
execution of a default function for the purposes of deriving defaults from its
2741
 
existing values. For a single statement that is executing many sets of bind
2742
 
parameters, the user-defined function is called for each set of parameters,
2743
 
and <tt class="docutils literal"><span class="pre">current_parameters</span></tt> will be provided with each individual parameter set
2744
 
for each execution.</p>
2745
 
</div>
2746
 
</div>
2747
 
<div class="section" id="sql-expressions">
2748
 
<h3>SQL Expressions<a class="headerlink" href="#sql-expressions" title="Permalink to this headline">¶</a></h3>
2749
 
<p>The &#8220;default&#8221; and &#8220;onupdate&#8221; keywords may also be passed SQL expressions,
2750
 
including select statements or direct function calls:</p>
2751
 
<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>
2752
 
    <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>
2753
 
 
2754
 
    <span class="c"># define &#39;create_date&#39; to default to now()</span>
2755
 
    <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>
2756
 
 
2757
 
    <span class="c"># define &#39;key&#39; to pull its default from the &#39;keyvalues&#39; table</span>
2758
 
    <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>
2759
 
 
2760
 
    <span class="c"># define &#39;last_modified&#39; to use the current_timestamp SQL function on update</span>
2761
 
    <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>
2762
 
    <span class="p">)</span></pre></div>
2763
 
</div>
2764
 
<p>Above, the <tt class="docutils literal"><span class="pre">create_date</span></tt> column will be populated with the result of the
2765
 
<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>
2766
 
or <tt class="docutils literal"><span class="pre">CURRENT_TIMESTAMP</span></tt> in most cases) during an INSERT statement, and the
2767
 
<tt class="docutils literal"><span class="pre">key</span></tt> column with the result of a SELECT subquery from another table. The
2768
 
<tt class="docutils literal"><span class="pre">last_modified</span></tt> column will be populated with the value of
2769
 
<tt class="docutils literal"><span class="pre">UTC_TIMESTAMP()</span></tt>, a function specific to MySQL, when an UPDATE statement is
2770
 
emitted for this table.</p>
2771
 
<p>Note that when using <tt class="docutils literal"><span class="pre">func</span></tt> functions, unlike when using Python <cite>datetime</cite>
2772
 
functions we <em>do</em> call the function, i.e. with parenthesis &#8220;()&#8221; - this is
2773
 
because what we want in this case is the return value of the function, which
2774
 
is the SQL expression construct that will be rendered into the INSERT or
2775
 
UPDATE statement.</p>
2776
 
<p>The above SQL functions are usually executed &#8220;inline&#8221; with the INSERT or
2777
 
UPDATE statement being executed, meaning, a single statement is executed which
2778
 
embeds the given expressions or subqueries within the VALUES or SET clause of
2779
 
the statement. Although in some cases, the function is &#8220;pre-executed&#8221; in a
2780
 
SELECT statement of its own beforehand. This happens when all of the following
2781
 
is true:</p>
2782
 
<ul class="simple">
2783
 
<li>the column is a primary key column</li>
2784
 
<li>the database dialect does not support a usable <tt class="docutils literal"><span class="pre">cursor.lastrowid</span></tt> accessor
2785
 
(or equivalent); this currently includes PostgreSQL, Oracle, and Firebird, as
2786
 
well as some MySQL dialects.</li>
2787
 
<li>the dialect does not support the &#8220;RETURNING&#8221; clause or similar, or the
2788
 
<tt class="docutils literal"><span class="pre">implicit_returning</span></tt> flag is set to <tt class="docutils literal"><span class="pre">False</span></tt> for the dialect. Dialects
2789
 
which support RETURNING currently include Postgresql, Oracle, Firebird, and
2790
 
MS-SQL.</li>
2791
 
<li>the statement is a single execution, i.e. only supplies one set of
2792
 
parameters and doesn&#8217;t use &#8220;executemany&#8221; behavior</li>
2793
 
<li>the <tt class="docutils literal"><span class="pre">inline=True</span></tt> flag is not set on the
2794
 
<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
2795
 
<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
2796
 
not defined an explicit <cite>returning()</cite> clause.</li>
2797
 
</ul>
2798
 
<p>Whether or not the default generation clause &#8220;pre-executes&#8221; is not something
2799
 
that normally needs to be considered, unless it is being addressed for
2800
 
performance reasons.</p>
2801
 
<p>When the statement is executed with a single set of parameters (that is, it is
2802
 
not an &#8220;executemany&#8221; style execution), the returned
2803
 
<a class="reference internal" href="connections.html#sqlalchemy.engine.ResultProxy" title="sqlalchemy.engine.ResultProxy"><tt class="xref py py-class docutils literal"><span class="pre">ResultProxy</span></tt></a> will contain a collection
2804
 
accessible via <tt class="docutils literal"><span class="pre">result.postfetch_cols()</span></tt> which contains a list of all
2805
 
<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
2806
 
default. Similarly, all parameters which were bound to the statement,
2807
 
including all Python and SQL expressions which were pre-executed, are present
2808
 
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
2809
 
<a class="reference internal" href="connections.html#sqlalchemy.engine.ResultProxy" title="sqlalchemy.engine.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>
2810
 
collection contains a list of primary key values for the row inserted (a list
2811
 
so that single-column and composite-column primary keys are represented in the
2812
 
same format).</p>
2813
 
</div>
2814
 
<div class="section" id="server-side-defaults">
2815
 
<h3>Server Side Defaults<a class="headerlink" href="#server-side-defaults" title="Permalink to this headline">¶</a></h3>
2816
 
<p>A variant on the SQL expression default is the <tt class="docutils literal"><span class="pre">server_default</span></tt>, which gets
2817
 
placed in the CREATE TABLE statement during a <tt class="docutils literal"><span class="pre">create()</span></tt> operation:</p>
2818
 
<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>
2819
 
    <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>
2820
 
    <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>
2821
 
<span class="p">)</span></pre></div>
2822
 
</div>
2823
 
<p>A create call for the above table will produce:</p>
2824
 
<div class="highlight-python"><pre>CREATE TABLE test (
2825
 
    abc varchar(20) default 'abc',
2826
 
    created_at datetime default sysdate
2827
 
)</pre>
2828
 
</div>
2829
 
<p>The behavior of <tt class="docutils literal"><span class="pre">server_default</span></tt> is similar to that of a regular SQL
2830
 
default; if it&#8217;s placed on a primary key column for a database which doesn&#8217;t
2831
 
have a way to &#8220;postfetch&#8221; the ID, and the statement is not &#8220;inlined&#8221;, the SQL
2832
 
expression is pre-executed; otherwise, SQLAlchemy lets the default fire off on
2833
 
the database side normally.</p>
2834
 
</div>
2835
 
<div class="section" id="triggered-columns">
2836
 
<span id="id1"></span><h3>Triggered Columns<a class="headerlink" href="#triggered-columns" title="Permalink to this headline">¶</a></h3>
2837
 
<p>Columns with values set by a database trigger or other external process may be
2838
 
called out using <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> as a marker:</p>
2839
 
<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>
2840
 
    <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>
2841
 
    <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>
2842
 
<span class="p">)</span></pre></div>
2843
 
</div>
2844
 
<p class="versionchanged">
2845
 
<span class="versionmodified">Changed in version 0.8.0b2,0.7.10: </span>The <tt class="docutils literal"><span class="pre">for_update</span></tt> argument on <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> is set automatically
2846
 
when specified as the <tt class="docutils literal"><span class="pre">server_onupdate</span></tt> argument.  If using an older version,
2847
 
specify the onupdate above as <tt class="docutils literal"><span class="pre">server_onupdate=FetchedValue(for_update=True)</span></tt>.</p>
2848
 
<p>These markers do not emit a &#8220;default&#8221; clause when the table is created,
2849
 
however they do set the same internal flags as a static <tt class="docutils literal"><span class="pre">server_default</span></tt>
2850
 
clause, providing hints to higher-level tools that a &#8220;post-fetch&#8221; of these
2851
 
rows should be performed after an insert or update.</p>
2852
 
<div class="admonition note">
2853
 
<p class="first admonition-title">Note</p>
2854
 
<p>It&#8217;s generally not appropriate to 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> in
2855
 
conjunction with a primary key column, particularly when using the
2856
 
ORM or any other scenario where the <a class="reference internal" href="connections.html#sqlalchemy.engine.ResultProxy.inserted_primary_key" title="sqlalchemy.engine.ResultProxy.inserted_primary_key"><tt class="xref py py-attr docutils literal"><span class="pre">ResultProxy.inserted_primary_key</span></tt></a>
2857
 
attribute is required.  This is becaue the &#8220;post-fetch&#8221; operation requires
2858
 
that the primary key value already be available, so that the
2859
 
row can be selected on its primary key.</p>
2860
 
<p>For a server-generated primary key value, all databases provide special
2861
 
accessors or other techniques in order to acquire the &#8220;last inserted
2862
 
primary key&#8221; column of a table.  These mechanisms aren&#8217;t affected by the presence
2863
 
of <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>.  For special situations where triggers are
2864
 
used to generate primary key values, and the database in use does not
2865
 
support the <tt class="docutils literal"><span class="pre">RETURNING</span></tt> clause, it may be necessary to forego the usage
2866
 
of the trigger and instead apply the SQL expression or function as a
2867
 
&#8220;pre execute&#8221; expression:</p>
2868
 
<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>
2869
 
        <span class="n">Column</span><span class="p">(</span><span class="s">&#39;abc&#39;</span><span class="p">,</span> <span class="n">MyType</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">generate_new_value</span><span class="p">(),</span> <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
2870
 
<span class="p">)</span></pre></div>
2871
 
</div>
2872
 
<p class="last">Where above, when <a class="reference internal" href="#sqlalchemy.schema.Table.insert" title="sqlalchemy.schema.Table.insert"><tt class="xref py py-meth docutils literal"><span class="pre">Table.insert()</span></tt></a> is used,
2873
 
the <tt class="docutils literal"><span class="pre">func.generate_new_value()</span></tt> expression will be pre-executed
2874
 
in the context of a scalar <tt class="docutils literal"><span class="pre">SELECT</span></tt> statement, and the new value will
2875
 
be applied to the subsequent <tt class="docutils literal"><span class="pre">INSERT</span></tt>, while at the same time being
2876
 
made available to the <a class="reference internal" href="connections.html#sqlalchemy.engine.ResultProxy.inserted_primary_key" title="sqlalchemy.engine.ResultProxy.inserted_primary_key"><tt class="xref py py-attr docutils literal"><span class="pre">ResultProxy.inserted_primary_key</span></tt></a>
2877
 
attribute.</p>
2878
 
</div>
2879
 
</div>
2880
 
<div class="section" id="defining-sequences">
2881
 
<h3>Defining Sequences<a class="headerlink" href="#defining-sequences" title="Permalink to this headline">¶</a></h3>
2882
 
<p>SQLAlchemy represents database sequences using the
2883
 
<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
2884
 
special case of &#8220;column default&#8221;. It only has an effect on databases which
2885
 
have explicit support for sequences, which currently includes Postgresql,
2886
 
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
2887
 
otherwise ignored.</p>
2888
 
<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
2889
 
&#8220;default&#8221; generator to be used during INSERT operations, and can also be
2890
 
configured to fire off during UPDATE operations if desired. It is most
2891
 
commonly used in conjunction with a single integer primary key column:</p>
2892
 
<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>
2893
 
    <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>
2894
 
    <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>
2895
 
    <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>
2896
 
<span class="p">)</span></pre></div>
2897
 
</div>
2898
 
<p>Where above, the table &#8220;cartitems&#8221; is associated with a sequence named
2899
 
&#8220;cart_id_seq&#8221;. When INSERT statements take place for &#8220;cartitems&#8221;, and no value
2900
 
is passed for the &#8220;cart_id&#8221; column, the &#8220;cart_id_seq&#8221; sequence will be used to
2901
 
generate a value.</p>
2902
 
<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,
2903
 
CREATE and DROP statements issued for that table will also issue CREATE/DROP
2904
 
for the sequence object as well, thus &#8220;bundling&#8221; the sequence object with its
2905
 
parent table.</p>
2906
 
<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
2907
 
functionality to accommodate Postgresql&#8217;s SERIAL datatype. The SERIAL type in
2908
 
PG automatically generates a sequence that is used implicitly during inserts.
2909
 
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
2910
 
<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
2911
 
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
2912
 
get in the way of the &#8220;implicit&#8221; sequence that PG would normally use. For this
2913
 
use case, add the flag <tt class="docutils literal"><span class="pre">optional=True</span></tt> to the
2914
 
<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
2915
 
<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
2916
 
provides no other option for generating primary key identifiers.</p>
2917
 
<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
2918
 
executed standalone like a SQL expression, which has the effect of calling its
2919
 
&#8220;next value&#8221; function:</p>
2920
 
<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>
2921
 
<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>
2922
 
</div>
2923
 
</div>
2924
 
<div class="section" id="default-objects-api">
2925
 
<h3>Default Objects API<a class="headerlink" href="#default-objects-api" title="Permalink to this headline">¶</a></h3>
2926
 
<dl class="class">
2927
 
<dt id="sqlalchemy.schema.ColumnDefault">
2928
 
<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>
2929
 
<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>
2930
 
<p>A plain default value on a column.</p>
2931
 
<p>This could correspond to a constant, a callable function,
2932
 
or a SQL clause.</p>
2933
 
<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
2934
 
whenever the <tt class="docutils literal"><span class="pre">default</span></tt>, <tt class="docutils literal"><span class="pre">onupdate</span></tt> arguments of
2935
 
<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>
2936
 
can be passed positionally as well.</p>
2937
 
<p>For example, the following:</p>
2938
 
<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>
2939
 
</div>
2940
 
<p>Is equivalent to:</p>
2941
 
<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>
2942
 
</div>
2943
 
</dd></dl>
2944
 
 
2945
 
<dl class="class">
2946
 
<dt id="sqlalchemy.schema.DefaultClause">
2947
 
<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>, <em>_reflected=False</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.DefaultClause" title="Permalink to this definition">¶</a></dt>
2948
 
<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>
2949
 
<p>A DDL-specified DEFAULT column value.</p>
2950
 
<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>
2951
 
that also generates a &#8220;DEFAULT&#8221; clause when
2952
 
&#8220;CREATE TABLE&#8221; is emitted.</p>
2953
 
<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
2954
 
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
2955
 
<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>
2956
 
can be passed positionally as well.</p>
2957
 
<p>For example, the following:</p>
2958
 
<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>
2959
 
</div>
2960
 
<p>Is equivalent to:</p>
2961
 
<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>
2962
 
</div>
2963
 
</dd></dl>
2964
 
 
2965
 
<dl class="class">
2966
 
<dt id="sqlalchemy.schema.DefaultGenerator">
2967
 
<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>
2968
 
<dd><p>Bases: <tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.schema._NotAColumnExpr</span></tt>, <a class="reference internal" href="#sqlalchemy.schema.SchemaItem" title="sqlalchemy.schema.SchemaItem"><tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.schema.SchemaItem</span></tt></a></p>
2969
 
<p>Base class for column <em>default</em> values.</p>
2970
 
</dd></dl>
2971
 
 
2972
 
<dl class="class">
2973
 
<dt id="sqlalchemy.schema.FetchedValue">
2974
 
<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>
2975
 
<dd><p>Bases: <tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.schema._NotAColumnExpr</span></tt>, <a class="reference internal" href="events.html#sqlalchemy.events.SchemaEventTarget" title="sqlalchemy.events.SchemaEventTarget"><tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.events.SchemaEventTarget</span></tt></a></p>
2976
 
<p>A marker for a transparent database-side default.</p>
2977
 
<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
2978
 
to provide some automatic default for a column.</p>
2979
 
<p>E.g.:</p>
2980
 
<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>
2981
 
</div>
2982
 
<p>Would indicate that some trigger or default generator
2983
 
will create a new value for the <tt class="docutils literal"><span class="pre">foo</span></tt> column during an
2984
 
INSERT.</p>
2985
 
<div class="admonition-see-also admonition seealso">
2986
 
<p class="first admonition-title">See also</p>
2987
 
<p class="last"><a class="reference internal" href="#triggered-columns"><em>Triggered Columns</em></a></p>
2988
 
</div>
2989
 
</dd></dl>
2990
 
 
2991
 
<dl class="class">
2992
 
<dt id="sqlalchemy.schema.PassiveDefault">
2993
 
<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>
2994
 
<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>
2995
 
<p>A DDL-specified DEFAULT column value.</p>
2996
 
<p class="deprecated">
2997
 
<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.
2998
 
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>
2999
 
</dd></dl>
3000
 
 
3001
 
<dl class="class">
3002
 
<dt id="sqlalchemy.schema.Sequence">
3003
 
<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>quote_schema=None</em>, <em>for_update=False</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Sequence" title="Permalink to this definition">¶</a></dt>
3004
 
<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>
3005
 
<p>Represents a named database sequence.</p>
3006
 
<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 represents the name and configurational
3007
 
parameters of a database sequence.   It also represents
3008
 
a construct that can be &#8220;executed&#8221; by a SQLAlchemy <a class="reference internal" href="connections.html#sqlalchemy.engine.Engine" title="sqlalchemy.engine.Engine"><tt class="xref py py-class docutils literal"><span class="pre">Engine</span></tt></a>
3009
 
or <a class="reference internal" href="connections.html#sqlalchemy.engine.Connection" title="sqlalchemy.engine.Connection"><tt class="xref py py-class docutils literal"><span class="pre">Connection</span></tt></a>, rendering the appropriate &#8220;next value&#8221; function
3010
 
for the target database and returning a result.</p>
3011
 
<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> is typically associated with a primary key column:</p>
3012
 
<div class="highlight-python"><div class="highlight"><pre><span class="n">some_table</span> <span class="o">=</span> <span class="n">Table</span><span class="p">(</span><span class="s">&#39;some_table&#39;</span><span class="p">,</span> <span class="n">metadata</span><span class="p">,</span>
3013
 
    <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">Sequence</span><span class="p">(</span><span class="s">&#39;some_table_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>
3014
 
<span class="p">)</span></pre></div>
3015
 
</div>
3016
 
<p>When CREATE TABLE is emitted for the above <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 the
3017
 
target platform supports sequences, a CREATE SEQUENCE statement will
3018
 
be emitted as well.   For platforms that don&#8217;t support sequences,
3019
 
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> construct is ignored.</p>
3020
 
<p>See also: <a class="reference internal" href="#sqlalchemy.schema.CreateSequence" title="sqlalchemy.schema.CreateSequence"><tt class="xref py py-class docutils literal"><span class="pre">CreateSequence</span></tt></a> <a class="reference internal" href="#sqlalchemy.schema.DropSequence" title="sqlalchemy.schema.DropSequence"><tt class="xref py py-class docutils literal"><span class="pre">DropSequence</span></tt></a></p>
3021
 
<dl class="method">
3022
 
<dt id="sqlalchemy.schema.Sequence.__init__">
3023
 
<tt class="descname">__init__</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>quote_schema=None</em>, <em>for_update=False</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Sequence.__init__" title="Permalink to this definition">¶</a></dt>
3024
 
<dd><p>Construct 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> object.</p>
3025
 
<table class="docutils field-list" frame="void" rules="none">
3026
 
<col class="field-name" />
3027
 
<col class="field-body" />
3028
 
<tbody valign="top">
3029
 
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
3030
 
<li><strong>name</strong> &#8211; The name of the sequence.</li>
3031
 
<li><strong>start</strong> &#8211; the starting index of the sequence.  This value is
3032
 
used when the CREATE SEQUENCE command is emitted to the database
3033
 
as the value of the &#8220;START WITH&#8221; clause.   If <tt class="docutils literal"><span class="pre">None</span></tt>, the
3034
 
clause is omitted, which on most platforms indicates a starting
3035
 
value of 1.</li>
3036
 
<li><strong>increment</strong> &#8211; the increment value of the sequence.  This
3037
 
value is used when the CREATE SEQUENCE command is emitted to
3038
 
the database as the value of the &#8220;INCREMENT BY&#8221; clause.  If <tt class="docutils literal"><span class="pre">None</span></tt>,
3039
 
the clause is omitted, which on most platforms indicates an
3040
 
increment of 1.</li>
3041
 
<li><strong>schema</strong> &#8211; Optional schema name for the sequence, if located
3042
 
in a schema other than the default.</li>
3043
 
<li><strong>optional</strong> &#8211; boolean value, when <tt class="docutils literal"><span class="pre">True</span></tt>, indicates that this
3044
 
<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 only needs to be explicitly generated
3045
 
on backends that don&#8217;t provide another way to generate primary
3046
 
key identifiers.  Currently, it essentially means, &#8220;don&#8217;t create
3047
 
this sequence on the Postgresql backend, where the SERIAL keyword
3048
 
creates a sequence for us automatically&#8221;.</li>
3049
 
<li><strong>quote</strong> &#8211; boolean value, when <tt class="docutils literal"><span class="pre">True</span></tt> or <tt class="docutils literal"><span class="pre">False</span></tt>, explicitly
3050
 
forces quoting of the schema name on or off.  When left at its
3051
 
default of <tt class="docutils literal"><span class="pre">None</span></tt>, normal quoting rules based on casing and reserved
3052
 
words take place.</li>
3053
 
<li><strong>metadata</strong> &#8211; <p>optional <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 be
3054
 
associated with this <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>.  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>
3055
 
that is associated with 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> gains access to the
3056
 
<tt class="docutils literal"><span class="pre">bind</span></tt> of that <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>, meaning the
3057
 
<a class="reference internal" href="#sqlalchemy.schema.Sequence.create" title="sqlalchemy.schema.Sequence.create"><tt class="xref py py-meth docutils literal"><span class="pre">Sequence.create()</span></tt></a> and <a class="reference internal" href="#sqlalchemy.schema.Sequence.drop" title="sqlalchemy.schema.Sequence.drop"><tt class="xref py py-meth docutils literal"><span class="pre">Sequence.drop()</span></tt></a> methods will
3058
 
make usage of that engine automatically.</p>
3059
 
<p class="versionchanged">
3060
 
<span class="versionmodified">Changed in version 0.7: </span>Additionally, the appropriate CREATE SEQUENCE/
3061
 
DROP SEQUENCE DDL commands will be emitted corresponding to this
3062
 
<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> when <a class="reference internal" href="#sqlalchemy.schema.MetaData.create_all" title="sqlalchemy.schema.MetaData.create_all"><tt class="xref py py-meth docutils literal"><span class="pre">MetaData.create_all()</span></tt></a> and
3063
 
<a class="reference internal" href="#sqlalchemy.schema.MetaData.drop_all" title="sqlalchemy.schema.MetaData.drop_all"><tt class="xref py py-meth docutils literal"><span class="pre">MetaData.drop_all()</span></tt></a> are invoked.</p>
3064
 
<p>Note that when 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> is applied 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>,
3065
 
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 automatically associated with the
3066
 
<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 of that column&#8217;s 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>,
3067
 
when that association is made.   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> will then
3068
 
be subject to automatic CREATE SEQUENCE/DROP SEQUENCE corresponding
3069
 
to when 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 itself is created or dropped,
3070
 
rather than that of 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 overall.</p>
3071
 
</li>
3072
 
<li><strong>for_update</strong> &#8211; Indicates this <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>, when associated
3073
 
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>, should be invoked for UPDATE statements
3074
 
on that column&#8217;s table, rather than for INSERT statements, when
3075
 
no value is otherwise present for that column in the statement.</li>
3076
 
</ul>
3077
 
</td>
3078
 
</tr>
3079
 
</tbody>
3080
 
</table>
3081
 
</dd></dl>
3082
 
 
3083
 
<dl class="method">
3084
 
<dt id="sqlalchemy.schema.Sequence.create">
3085
 
<tt class="descname">create</tt><big>(</big><em>bind=None</em>, <em>checkfirst=True</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Sequence.create" title="Permalink to this definition">¶</a></dt>
3086
 
<dd><p>Creates this sequence in the database.</p>
3087
 
</dd></dl>
3088
 
 
3089
 
<dl class="method">
3090
 
<dt id="sqlalchemy.schema.Sequence.drop">
3091
 
<tt class="descname">drop</tt><big>(</big><em>bind=None</em>, <em>checkfirst=True</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Sequence.drop" title="Permalink to this definition">¶</a></dt>
3092
 
<dd><p>Drops this sequence from the database.</p>
3093
 
</dd></dl>
3094
 
 
3095
 
<dl class="method">
3096
 
<dt id="sqlalchemy.schema.Sequence.next_value">
3097
 
<tt class="descname">next_value</tt><big>(</big><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Sequence.next_value" title="Permalink to this definition">¶</a></dt>
3098
 
<dd><p>Return a <a class="reference internal" href="expression_api.html#sqlalchemy.sql.functions.next_value" title="sqlalchemy.sql.functions.next_value"><tt class="xref py py-class docutils literal"><span class="pre">next_value</span></tt></a> function element
3099
 
which will render the appropriate increment function
3100
 
for this <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> within any SQL expression.</p>
3101
 
</dd></dl>
3102
 
 
3103
 
</dd></dl>
3104
 
 
3105
 
</div>
3106
 
</div>
3107
 
<div class="section" id="defining-constraints-and-indexes">
3108
 
<h2>Defining Constraints and Indexes<a class="headerlink" href="#defining-constraints-and-indexes" title="Permalink to this headline">¶</a></h2>
3109
 
<div class="section" id="defining-foreign-keys">
3110
 
<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>
3111
 
<p>A <em>foreign key</em> in SQL is a table-level construct that constrains one or more
3112
 
columns in that table to only allow values that are present in a different set
3113
 
of columns, typically but not always located on a different table. We call the
3114
 
columns which are constrained the <em>foreign key</em> columns and the columns which
3115
 
they are constrained towards the <em>referenced</em> columns. The referenced columns
3116
 
almost always define the primary key for their owning table, though there are
3117
 
exceptions to this. The foreign key is the &#8220;joint&#8221; that connects together
3118
 
pairs of rows which have a relationship with each other, and SQLAlchemy
3119
 
assigns very deep importance to this concept in virtually every area of its
3120
 
operation.</p>
3121
 
<p>In SQLAlchemy as well as in DDL, foreign key constraints can be defined as
3122
 
additional attributes within the table clause, or for single-column foreign
3123
 
keys they may optionally be specified within the definition of a single
3124
 
column. The single column foreign key is more common, and at the column level
3125
 
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
3126
 
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>
3127
 
<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>
3128
 
    <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>
3129
 
    <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>
3130
 
    <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>
3131
 
    <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>
3132
 
<span class="p">)</span></pre></div>
3133
 
</div>
3134
 
<p>Above, we define a new table <tt class="docutils literal"><span class="pre">user_preference</span></tt> for which each row must
3135
 
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>
3136
 
table&#8217;s <tt class="docutils literal"><span class="pre">user_id</span></tt> column.</p>
3137
 
<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
3138
 
string of the form <em>&lt;tablename&gt;.&lt;columnname&gt;</em>, or for a table in a remote
3139
 
schema or &#8220;owner&#8221; of the form <em>&lt;schemaname&gt;.&lt;tablename&gt;.&lt;columnname&gt;</em>. It may
3140
 
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
3141
 
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>
3142
 
object via its <tt class="docutils literal"><span class="pre">c</span></tt> collection:</p>
3143
 
<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>
3144
 
</div>
3145
 
<p>The advantage to using a string is that the in-python linkage between <tt class="docutils literal"><span class="pre">user</span></tt>
3146
 
and <tt class="docutils literal"><span class="pre">user_preference</span></tt> is resolved only when first needed, so that table
3147
 
objects can be easily spread across multiple modules and defined in any order.</p>
3148
 
<p>Foreign keys may also be defined at the table level, using the
3149
 
<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
3150
 
describe a single- or multi-column foreign key. A multi-column foreign key is
3151
 
known as a <em>composite</em> foreign key, and almost always references a table that
3152
 
has a composite primary key. Below we define a table <tt class="docutils literal"><span class="pre">invoice</span></tt> which has a
3153
 
composite primary key:</p>
3154
 
<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>
3155
 
    <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>
3156
 
    <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>
3157
 
    <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>
3158
 
<span class="p">)</span></pre></div>
3159
 
</div>
3160
 
<p>And then a table <tt class="docutils literal"><span class="pre">invoice_item</span></tt> with a composite foreign key referencing
3161
 
<tt class="docutils literal"><span class="pre">invoice</span></tt>:</p>
3162
 
<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>
3163
 
    <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>
3164
 
    <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>
3165
 
    <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>
3166
 
    <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>
3167
 
    <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>
3168
 
<span class="p">)</span></pre></div>
3169
 
</div>
3170
 
<p>It&#8217;s important to note that the
3171
 
<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
3172
 
composite foreign key. While we could also have placed individual
3173
 
<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
3174
 
<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
3175
 
would not be aware that these two values should be paired together - it would
3176
 
be two individual foreign key constraints instead of a single composite
3177
 
foreign key referencing two columns.</p>
3178
 
<div class="section" id="creating-dropping-foreign-key-constraints-via-alter">
3179
 
<span id="use-alter"></span><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>
3180
 
<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
3181
 
causes the &#8220;REFERENCES&#8221; keyword to be added inline to a column definition
3182
 
within a &#8220;CREATE TABLE&#8221; statement when
3183
 
<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
3184
 
<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;
3185
 
keyword inline with &#8220;CREATE TABLE&#8221;. There are some cases where this is
3186
 
undesireable, particularly when two tables reference each other mutually, each
3187
 
with a foreign key referencing the other. In such a situation at least one of
3188
 
the foreign key constraints must be generated after both tables have been
3189
 
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
3190
 
<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
3191
 
<tt class="docutils literal"><span class="pre">use_alter=True</span></tt>. When using this flag, the constraint will be generated
3192
 
using a definition similar to &#8220;ALTER TABLE &lt;tablename&gt; ADD CONSTRAINT &lt;name&gt;
3193
 
...&#8221;. Since a name is required, the <tt class="docutils literal"><span class="pre">name</span></tt> attribute must also be specified.
3194
 
For example:</p>
3195
 
<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>
3196
 
    <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>
3197
 
    <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>
3198
 
        <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>
3199
 
    <span class="p">)</span>
3200
 
<span class="p">)</span>
3201
 
 
3202
 
<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>
3203
 
    <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>
3204
 
    <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>
3205
 
    <span class="n">ForeignKeyConstraint</span><span class="p">(</span>
3206
 
        <span class="p">[</span><span class="s">&#39;parent_node_id&#39;</span><span class="p">],</span>
3207
 
        <span class="p">[</span><span class="s">&#39;node.node_id&#39;</span><span class="p">],</span>
3208
 
        <span class="n">use_alter</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span>
3209
 
        <span class="n">name</span><span class="o">=</span><span class="s">&#39;fk_element_parent_node_id&#39;</span>
3210
 
    <span class="p">)</span>
3211
 
<span class="p">)</span></pre></div>
3212
 
</div>
3213
 
</div>
3214
 
<div class="section" id="on-update-and-on-delete">
3215
 
<h4>ON UPDATE and ON DELETE<a class="headerlink" href="#on-update-and-on-delete" title="Permalink to this headline">¶</a></h4>
3216
 
<p>Most databases support <em>cascading</em> of foreign key values, that is the when a
3217
 
parent row is updated the new value is placed in child rows, or when the
3218
 
parent row is deleted all corresponding child rows are set to null or deleted.
3219
 
In data definition language these are specified using phrases like &#8220;ON UPDATE
3220
 
CASCADE&#8221;, &#8220;ON DELETE CASCADE&#8221;, and &#8220;ON DELETE SET NULL&#8221;, corresponding to
3221
 
foreign key constraints. The phrase after &#8220;ON UPDATE&#8221; or &#8220;ON DELETE&#8221; may also
3222
 
other allow other phrases that are specific to the database in use. The
3223
 
<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
3224
 
<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
3225
 
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
3226
 
arguments. The value is any string which will be output after the appropriate
3227
 
&#8220;ON UPDATE&#8221; or &#8220;ON DELETE&#8221; phrase:</p>
3228
 
<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>
3229
 
    <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>
3230
 
            <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>
3231
 
            <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span>
3232
 
    <span class="p">)</span>
3233
 
<span class="p">)</span>
3234
 
 
3235
 
<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>
3236
 
    <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>
3237
 
    <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>
3238
 
    <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>
3239
 
    <span class="n">ForeignKeyConstraint</span><span class="p">(</span>
3240
 
                <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>
3241
 
                <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>
3242
 
                <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>
3243
 
    <span class="p">)</span>
3244
 
<span class="p">)</span></pre></div>
3245
 
</div>
3246
 
<p>Note that these clauses are not supported on SQLite, and require <tt class="docutils literal"><span class="pre">InnoDB</span></tt>
3247
 
tables when used with MySQL. They may also not be supported on other
3248
 
databases.</p>
3249
 
</div>
3250
 
</div>
3251
 
<div class="section" id="unique-constraint">
3252
 
<h3>UNIQUE Constraint<a class="headerlink" href="#unique-constraint" title="Permalink to this headline">¶</a></h3>
3253
 
<p>Unique constraints can be created anonymously on a single column using the
3254
 
<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
3255
 
unique constraints and/or those with multiple columns are created via the
3256
 
<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>
3257
 
<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>
3258
 
<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>
3259
 
 
3260
 
    <span class="c"># per-column anonymous unique constraint</span>
3261
 
    <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>
3262
 
 
3263
 
    <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>
3264
 
    <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>
3265
 
 
3266
 
    <span class="c"># explicit/composite unique constraint.  &#39;name&#39; is optional.</span>
3267
 
    <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>
3268
 
    <span class="p">)</span></pre></div>
3269
 
</div>
3270
 
</div>
3271
 
<div class="section" id="check-constraint">
3272
 
<h3>CHECK Constraint<a class="headerlink" href="#check-constraint" title="Permalink to this headline">¶</a></h3>
3273
 
<p>Check constraints can be named or unnamed and can be created at the Column or
3274
 
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.
3275
 
The text of the check constraint is passed directly through to the database,
3276
 
so there is limited &#8220;database independent&#8221; behavior. Column level check
3277
 
constraints generally should only refer to the column to which they are
3278
 
placed, while table level constraints can refer to any columns in the table.</p>
3279
 
<p>Note that some databases do not actively support check constraints such as
3280
 
MySQL.</p>
3281
 
<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>
3282
 
<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>
3283
 
 
3284
 
    <span class="c"># per-column CHECK constraint</span>
3285
 
    <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>
3286
 
 
3287
 
    <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>
3288
 
    <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>
3289
 
 
3290
 
    <span class="c"># table level CHECK constraint.  &#39;name&#39; is optional.</span>
3291
 
    <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>
3292
 
    <span class="p">)</span>
3293
 
 
3294
 
<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>
3295
 
<div class='popup_sql'>CREATE TABLE mytable (
3296
 
    col1 INTEGER  CHECK (col1>5),
3297
 
    col2 INTEGER,
3298
 
    col3 INTEGER,
3299
 
    CONSTRAINT check1  CHECK (col2 > col3 + 5)
3300
 
)</div></pre></div>
3301
 
</div>
3302
 
</div>
3303
 
<div class="section" id="setting-up-constraints-when-using-the-declarative-orm-extension">
3304
 
<h3>Setting up Constraints when using the Declarative ORM Extension<a class="headerlink" href="#setting-up-constraints-when-using-the-declarative-orm-extension" title="Permalink to this headline">¶</a></h3>
3305
 
<p>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> is the SQLAlchemy Core construct that allows one to define
3306
 
table metadata, which among other things can be used by the SQLAlchemy ORM
3307
 
as a target to map a class.  The <a class="reference internal" href="../orm/extensions/declarative.html"><em>Declarative</em></a>
3308
 
extension allows 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 to be created automatically, given
3309
 
the contents of the table primarily as a mapping 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.</p>
3310
 
<p>To apply table-level constraint objects such as <a class="reference internal" href="#sqlalchemy.schema.ForeignKeyConstraint" title="sqlalchemy.schema.ForeignKeyConstraint"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKeyConstraint</span></tt></a>
3311
 
to a table defined using Declarative, use the <tt class="docutils literal"><span class="pre">__table_args__</span></tt> attribute,
3312
 
described at <a class="reference internal" href="../orm/extensions/declarative.html#declarative-table-args"><em>Table Configuration</em></a>.</p>
3313
 
</div>
3314
 
<div class="section" id="constraints-api">
3315
 
<h3>Constraints API<a class="headerlink" href="#constraints-api" title="Permalink to this headline">¶</a></h3>
3316
 
<dl class="class">
3317
 
<dt id="sqlalchemy.schema.Constraint">
3318
 
<em class="property">class </em><tt class="descclassname">sqlalchemy.schema.</tt><tt class="descname">Constraint</tt><big>(</big><em>name=None</em>, <em>deferrable=None</em>, <em>initially=None</em>, <em>_create_rule=None</em>, <em>**kw</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Constraint" title="Permalink to this definition">¶</a></dt>
3319
 
<dd><p>Bases: <a class="reference internal" href="#sqlalchemy.schema.SchemaItem" title="sqlalchemy.schema.SchemaItem"><tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.schema.SchemaItem</span></tt></a></p>
3320
 
<p>A table-level SQL constraint.</p>
3321
 
</dd></dl>
3322
 
 
3323
 
<dl class="class">
3324
 
<dt id="sqlalchemy.schema.CheckConstraint">
3325
 
<em class="property">class </em><tt class="descclassname">sqlalchemy.schema.</tt><tt class="descname">CheckConstraint</tt><big>(</big><em>sqltext</em>, <em>name=None</em>, <em>deferrable=None</em>, <em>initially=None</em>, <em>table=None</em>, <em>_create_rule=None</em>, <em>_autoattach=True</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.CheckConstraint" title="Permalink to this definition">¶</a></dt>
3326
 
<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>
3327
 
<p>A table- or column-level CHECK constraint.</p>
3328
 
<p>Can be included in the definition of a Table or Column.</p>
3329
 
</dd></dl>
3330
 
 
3331
 
<dl class="class">
3332
 
<dt id="sqlalchemy.schema.ColumnCollectionConstraint">
3333
 
<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>
3334
 
<dd><p>Bases: <tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.schema.ColumnCollectionMixin</span></tt>, <a class="reference internal" href="#sqlalchemy.schema.Constraint" title="sqlalchemy.schema.Constraint"><tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.schema.Constraint</span></tt></a></p>
3335
 
<p>A constraint that proxies a ColumnCollection.</p>
3336
 
</dd></dl>
3337
 
 
3338
 
<dl class="class">
3339
 
<dt id="sqlalchemy.schema.ForeignKey">
3340
 
<em class="property">class </em><tt class="descclassname">sqlalchemy.schema.</tt><tt class="descname">ForeignKey</tt><big>(</big><em>column</em>, <em>_constraint=None</em>, <em>use_alter=False</em>, <em>name=None</em>, <em>onupdate=None</em>, <em>ondelete=None</em>, <em>deferrable=None</em>, <em>schema=None</em>, <em>initially=None</em>, <em>link_to_name=False</em>, <em>match=None</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.ForeignKey" title="Permalink to this definition">¶</a></dt>
3341
 
<dd><p>Bases: <a class="reference internal" href="#sqlalchemy.schema.SchemaItem" title="sqlalchemy.schema.SchemaItem"><tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.schema.SchemaItem</span></tt></a></p>
3342
 
<p>Defines a dependency between two columns.</p>
3343
 
<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,
3344
 
e.g.:</p>
3345
 
<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>
3346
 
    <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>
3347
 
<span class="p">)</span></pre></div>
3348
 
</div>
3349
 
<p>Note that <tt class="docutils literal"><span class="pre">ForeignKey</span></tt> is only a marker object that defines
3350
 
a dependency between two columns.   The actual constraint
3351
 
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>
3352
 
object.   This object will be generated automatically when
3353
 
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
3354
 
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,
3355
 
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>,
3356
 
<tt class="docutils literal"><span class="pre">ForeignKey</span></tt> markers are automatically generated to be
3357
 
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
3358
 
associated with the constraint object.</p>
3359
 
<p>Note that you cannot define a &#8220;composite&#8221; foreign key constraint,
3360
 
that is a constraint between a grouping of multiple parent/child
3361
 
columns, using <tt class="docutils literal"><span class="pre">ForeignKey</span></tt> objects.   To define this grouping,
3362
 
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
3363
 
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
3364
 
are created automatically.</p>
3365
 
<p>The <tt class="docutils literal"><span class="pre">ForeignKey</span></tt> objects associated with an individual
3366
 
<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
3367
 
of that column.</p>
3368
 
<p>Further examples of foreign key configuration are in
3369
 
<a class="reference internal" href="#metadata-foreignkeys"><em>Defining Foreign Keys</em></a>.</p>
3370
 
<dl class="method">
3371
 
<dt id="sqlalchemy.schema.ForeignKey.__init__">
3372
 
<tt class="descname">__init__</tt><big>(</big><em>column</em>, <em>_constraint=None</em>, <em>use_alter=False</em>, <em>name=None</em>, <em>onupdate=None</em>, <em>ondelete=None</em>, <em>deferrable=None</em>, <em>schema=None</em>, <em>initially=None</em>, <em>link_to_name=False</em>, <em>match=None</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.ForeignKey.__init__" title="Permalink to this definition">¶</a></dt>
3373
 
<dd><p>Construct a column-level FOREIGN KEY.</p>
3374
 
<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
3375
 
<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
3376
 
<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>
3377
 
<table class="docutils field-list" frame="void" rules="none">
3378
 
<col class="field-name" />
3379
 
<col class="field-body" />
3380
 
<tbody valign="top">
3381
 
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
3382
 
<li><strong>column</strong> &#8211; <p>A single target column for the key relationship. A
3383
 
<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:
3384
 
<tt class="docutils literal"><span class="pre">tablename.columnkey</span></tt> or <tt class="docutils literal"><span class="pre">schema.tablename.columnkey</span></tt>.
3385
 
<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
3386
 
(defaults to the column name itself), unless <tt class="docutils literal"><span class="pre">link_to_name</span></tt> is
3387
 
<tt class="docutils literal"><span class="pre">True</span></tt> in which case the rendered name of the column is used.</p>
3388
 
<p class="versionadded">
3389
 
<span class="versionmodified">New in version 0.7.4: </span>Note that if the schema name is not included, and the
3390
 
underlying <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 a &#8220;schema&#8221;, that value will
3391
 
be used.</p>
3392
 
</li>
3393
 
<li><strong>name</strong> &#8211; Optional string. An in-database name for the key if
3394
 
<cite>constraint</cite> is not provided.</li>
3395
 
<li><strong>onupdate</strong> &#8211; Optional string. If set, emit ON UPDATE &lt;value&gt; when
3396
 
issuing DDL for this constraint. Typical values include CASCADE,
3397
 
DELETE and RESTRICT.</li>
3398
 
<li><strong>ondelete</strong> &#8211; Optional string. If set, emit ON DELETE &lt;value&gt; when
3399
 
issuing DDL for this constraint. Typical values include CASCADE,
3400
 
DELETE and RESTRICT.</li>
3401
 
<li><strong>deferrable</strong> &#8211; Optional bool. If set, emit DEFERRABLE or NOT
3402
 
DEFERRABLE when issuing DDL for this constraint.</li>
3403
 
<li><strong>initially</strong> &#8211; Optional string. If set, emit INITIALLY &lt;value&gt; when
3404
 
issuing DDL for this constraint.</li>
3405
 
<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
3406
 
the rendered name of the referenced column, not its locally
3407
 
assigned <tt class="docutils literal"><span class="pre">key</span></tt>.</li>
3408
 
<li><strong>use_alter</strong> &#8211; passed to the underlying
3409
 
<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
3410
 
generated/dropped externally from the CREATE TABLE/ DROP TABLE
3411
 
statement. See that classes&#8217; constructor for details.</li>
3412
 
<li><strong>match</strong> &#8211; Optional string. If set, emit MATCH &lt;value&gt; when issuing
3413
 
DDL for this constraint. Typical values include SIMPLE, PARTIAL
3414
 
and FULL.</li>
3415
 
</ul>
3416
 
</td>
3417
 
</tr>
3418
 
</tbody>
3419
 
</table>
3420
 
</dd></dl>
3421
 
 
3422
 
<dl class="attribute">
3423
 
<dt id="sqlalchemy.schema.ForeignKey.column">
3424
 
<tt class="descname">column</tt><a class="headerlink" href="#sqlalchemy.schema.ForeignKey.column" title="Permalink to this definition">¶</a></dt>
3425
 
<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
3426
 
<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>
3427
 
<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
3428
 
string-based target column specification, this
3429
 
attribute will on first access initiate a resolution
3430
 
process to locate the referenced remote
3431
 
<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
3432
 
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
3433
 
<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
3434
 
yet present, an error is raised.</p>
3435
 
</dd></dl>
3436
 
 
3437
 
<dl class="method">
3438
 
<dt id="sqlalchemy.schema.ForeignKey.copy">
3439
 
<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>
3440
 
<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>
3441
 
<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
3442
 
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>
3443
 
<p>This method is usually used by the internal
3444
 
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>,
3445
 
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>
3446
 
<table class="docutils field-list" frame="void" rules="none">
3447
 
<col class="field-name" />
3448
 
<col class="field-body" />
3449
 
<tbody valign="top">
3450
 
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>schema</strong> &#8211; The returned <a class="reference internal" href="#sqlalchemy.schema.ForeignKey" title="sqlalchemy.schema.ForeignKey"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></tt></a> will
3451
 
reference the original table and column name, qualified
3452
 
by the given string schema name.</td>
3453
 
</tr>
3454
 
</tbody>
3455
 
</table>
3456
 
</dd></dl>
3457
 
 
3458
 
<dl class="method">
3459
 
<dt id="sqlalchemy.schema.ForeignKey.get_referent">
3460
 
<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>
3461
 
<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>
3462
 
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>
3463
 
<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
3464
 
<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>
3465
 
</dd></dl>
3466
 
 
3467
 
<dl class="method">
3468
 
<dt id="sqlalchemy.schema.ForeignKey.references">
3469
 
<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>
3470
 
<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
3471
 
<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>
3472
 
</dd></dl>
3473
 
 
3474
 
<dl class="attribute">
3475
 
<dt id="sqlalchemy.schema.ForeignKey.target_fullname">
3476
 
<tt class="descname">target_fullname</tt><a class="headerlink" href="#sqlalchemy.schema.ForeignKey.target_fullname" title="Permalink to this definition">¶</a></dt>
3477
 
<dd><p>Return a string based &#8216;column specification&#8217; for this
3478
 
<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>
3479
 
<p>This is usually the equivalent of the string-based &#8220;tablename.colname&#8221;
3480
 
argument first passed to the object&#8217;s constructor.</p>
3481
 
</dd></dl>
3482
 
 
3483
 
</dd></dl>
3484
 
 
3485
 
<dl class="class">
3486
 
<dt id="sqlalchemy.schema.ForeignKeyConstraint">
3487
 
<em class="property">class </em><tt class="descclassname">sqlalchemy.schema.</tt><tt class="descname">ForeignKeyConstraint</tt><big>(</big><em>columns</em>, <em>refcolumns</em>, <em>name=None</em>, <em>onupdate=None</em>, <em>ondelete=None</em>, <em>deferrable=None</em>, <em>initially=None</em>, <em>use_alter=False</em>, <em>link_to_name=False</em>, <em>match=None</em>, <em>table=None</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.ForeignKeyConstraint" title="Permalink to this definition">¶</a></dt>
3488
 
<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>
3489
 
<p>A table-level FOREIGN KEY constraint.</p>
3490
 
<p>Defines a single column or composite FOREIGN KEY ... REFERENCES
3491
 
constraint. For a no-frills, single column foreign key, adding a
3492
 
<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
3493
 
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>
3494
 
<p>Examples of foreign key configuration are in <a class="reference internal" href="#metadata-foreignkeys"><em>Defining Foreign Keys</em></a>.</p>
3495
 
<dl class="method">
3496
 
<dt id="sqlalchemy.schema.ForeignKeyConstraint.__init__">
3497
 
<tt class="descname">__init__</tt><big>(</big><em>columns</em>, <em>refcolumns</em>, <em>name=None</em>, <em>onupdate=None</em>, <em>ondelete=None</em>, <em>deferrable=None</em>, <em>initially=None</em>, <em>use_alter=False</em>, <em>link_to_name=False</em>, <em>match=None</em>, <em>table=None</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.ForeignKeyConstraint.__init__" title="Permalink to this definition">¶</a></dt>
3498
 
<dd><p>Construct a composite-capable FOREIGN KEY.</p>
3499
 
<table class="docutils field-list" frame="void" rules="none">
3500
 
<col class="field-name" />
3501
 
<col class="field-body" />
3502
 
<tbody valign="top">
3503
 
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
3504
 
<li><strong>columns</strong> &#8211; A sequence of local column names. The named columns
3505
 
must be defined and present in the parent Table. The names should
3506
 
match the <tt class="docutils literal"><span class="pre">key</span></tt> given to each column (defaults to the name) unless
3507
 
<tt class="docutils literal"><span class="pre">link_to_name</span></tt> is True.</li>
3508
 
<li><strong>refcolumns</strong> &#8211; A sequence of foreign column names or Column
3509
 
objects. The columns must all be located within the same Table.</li>
3510
 
<li><strong>name</strong> &#8211; Optional, the in-database name of the key.</li>
3511
 
<li><strong>onupdate</strong> &#8211; Optional string. If set, emit ON UPDATE &lt;value&gt; when
3512
 
issuing DDL for this constraint. Typical values include CASCADE,
3513
 
DELETE and RESTRICT.</li>
3514
 
<li><strong>ondelete</strong> &#8211; Optional string. If set, emit ON DELETE &lt;value&gt; when
3515
 
issuing DDL for this constraint. Typical values include CASCADE,
3516
 
DELETE and RESTRICT.</li>
3517
 
<li><strong>deferrable</strong> &#8211; Optional bool. If set, emit DEFERRABLE or NOT
3518
 
DEFERRABLE when issuing DDL for this constraint.</li>
3519
 
<li><strong>initially</strong> &#8211; Optional string. If set, emit INITIALLY &lt;value&gt; when
3520
 
issuing DDL for this constraint.</li>
3521
 
<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
3522
 
the rendered name of the referenced column, not its locally assigned
3523
 
<tt class="docutils literal"><span class="pre">key</span></tt>.</li>
3524
 
<li><strong>use_alter</strong> &#8211; If True, do not emit the DDL for this constraint as
3525
 
part of the CREATE TABLE definition. Instead, generate it via an
3526
 
ALTER TABLE statement issued after the full collection of tables
3527
 
have been created, and drop it via an ALTER TABLE statement before
3528
 
the full collection of tables are dropped. This is shorthand for the
3529
 
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
3530
 
as &#8220;after-create&#8221; and &#8220;before-drop&#8221; events on the MetaData object.
3531
 
This is normally used to generate/drop constraints on objects that
3532
 
are mutually dependent on each other.</li>
3533
 
<li><strong>match</strong> &#8211; Optional string. If set, emit MATCH &lt;value&gt; when issuing
3534
 
DDL for this constraint. Typical values include SIMPLE, PARTIAL
3535
 
and FULL.</li>
3536
 
</ul>
3537
 
</td>
3538
 
</tr>
3539
 
</tbody>
3540
 
</table>
3541
 
</dd></dl>
3542
 
 
3543
 
</dd></dl>
3544
 
 
3545
 
<dl class="class">
3546
 
<dt id="sqlalchemy.schema.PrimaryKeyConstraint">
3547
 
<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>
3548
 
<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>
3549
 
<p>A table-level PRIMARY KEY constraint.</p>
3550
 
<p>Defines a single column or composite PRIMARY KEY constraint. For a
3551
 
no-frills primary key, adding <tt class="docutils literal"><span class="pre">primary_key=True</span></tt> to one or more
3552
 
<tt class="docutils literal"><span class="pre">Column</span></tt> definitions is a shorthand equivalent for an unnamed single- or
3553
 
multiple-column PrimaryKeyConstraint.</p>
3554
 
</dd></dl>
3555
 
 
3556
 
<dl class="class">
3557
 
<dt id="sqlalchemy.schema.UniqueConstraint">
3558
 
<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>
3559
 
<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>
3560
 
<p>A table-level UNIQUE constraint.</p>
3561
 
<p>Defines a single column or composite UNIQUE constraint. For a no-frills,
3562
 
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>
3563
 
definition is a shorthand equivalent for an unnamed, single column
3564
 
UniqueConstraint.</p>
3565
 
</dd></dl>
3566
 
 
3567
 
</div>
3568
 
<div class="section" id="indexes">
3569
 
<span id="schema-indexes"></span><h3>Indexes<a class="headerlink" href="#indexes" title="Permalink to this headline">¶</a></h3>
3570
 
<p>Indexes can be created anonymously (using an auto-generated name <tt class="docutils literal"><span class="pre">ix_&lt;column</span>
3571
 
<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
3572
 
<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
3573
 
<tt class="docutils literal"><span class="pre">unique</span></tt> to apply the uniqueness to the index itself, instead of adding a
3574
 
separate UNIQUE constraint. For indexes with specific names or which encompass
3575
 
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,
3576
 
which requires a name.</p>
3577
 
<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
3578
 
<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
3579
 
INDEX&#8221; is issued right after the create statements for the table:</p>
3580
 
<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>
3581
 
<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>
3582
 
    <span class="c"># an indexed column, with index &quot;ix_mytable_col1&quot;</span>
3583
 
    <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>
3584
 
 
3585
 
    <span class="c"># a uniquely indexed column with index &quot;ix_mytable_col2&quot;</span>
3586
 
    <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>
3587
 
 
3588
 
    <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>
3589
 
    <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>
3590
 
 
3591
 
    <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>
3592
 
    <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>
3593
 
    <span class="p">)</span>
3594
 
 
3595
 
<span class="c"># place an index on col3, col4</span>
3596
 
<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>
3597
 
 
3598
 
<span class="c"># place a unique index on col5, col6</span>
3599
 
<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>
3600
 
 
3601
 
<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>
3602
 
<div class='popup_sql'>CREATE TABLE mytable (
3603
 
    col1 INTEGER,
3604
 
    col2 INTEGER,
3605
 
    col3 INTEGER,
3606
 
    col4 INTEGER,
3607
 
    col5 INTEGER,
3608
 
    col6 INTEGER
3609
 
)
3610
 
CREATE INDEX ix_mytable_col1 ON mytable (col1)
3611
 
CREATE UNIQUE INDEX ix_mytable_col2 ON mytable (col2)
3612
 
CREATE UNIQUE INDEX myindex ON mytable (col5, col6)
3613
 
CREATE INDEX idx_col34 ON mytable (col3, col4)</div></pre></div>
3614
 
</div>
3615
 
<p>Note in the example above, the <a class="reference internal" href="#sqlalchemy.schema.Index" title="sqlalchemy.schema.Index"><tt class="xref py py-class docutils literal"><span class="pre">Index</span></tt></a> construct is created
3616
 
externally 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>
3617
 
objects directly.  <a class="reference internal" href="#sqlalchemy.schema.Index" title="sqlalchemy.schema.Index"><tt class="xref py py-class docutils literal"><span class="pre">Index</span></tt></a> also supports
3618
 
&#8220;inline&#8221; definition inside 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>, using string names to
3619
 
identify columns:</p>
3620
 
<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>
3621
 
<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>
3622
 
    <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>
3623
 
 
3624
 
    <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>
3625
 
 
3626
 
    <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>
3627
 
    <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>
3628
 
 
3629
 
    <span class="c"># place an index on col1, col2</span>
3630
 
    <span class="n">Index</span><span class="p">(</span><span class="s">&#39;idx_col12&#39;</span><span class="p">,</span> <span class="s">&#39;col1&#39;</span><span class="p">,</span> <span class="s">&#39;col2&#39;</span><span class="p">),</span>
3631
 
 
3632
 
    <span class="c"># place a unique index on col3, col4</span>
3633
 
    <span class="n">Index</span><span class="p">(</span><span class="s">&#39;idx_col34&#39;</span><span class="p">,</span> <span class="s">&#39;col3&#39;</span><span class="p">,</span> <span class="s">&#39;col4&#39;</span><span class="p">,</span> <span class="n">unique</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
3634
 
<span class="p">)</span></pre></div>
3635
 
</div>
3636
 
<p class="versionadded">
3637
 
<span class="versionmodified">New in version 0.7: </span>Support of &#8220;inline&#8221; definition inside 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>
3638
 
for <a class="reference internal" href="#sqlalchemy.schema.Index" title="sqlalchemy.schema.Index"><tt class="xref py py-class docutils literal"><span class="pre">Index</span></tt></a>.</p>
3639
 
<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>
3640
 
<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>
3641
 
<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>
3642
 
<div class='popup_sql'>CREATE INDEX someindex ON mytable (col5)</div></pre></div>
3643
 
</div>
3644
 
<div class="section" id="functional-indexes">
3645
 
<span id="schema-indexes-functional"></span><h4>Functional Indexes<a class="headerlink" href="#functional-indexes" title="Permalink to this headline">¶</a></h4>
3646
 
<p><a class="reference internal" href="#sqlalchemy.schema.Index" title="sqlalchemy.schema.Index"><tt class="xref py py-class docutils literal"><span class="pre">Index</span></tt></a> supports SQL and function expressions, as supported by the
3647
 
target backend.  To create an index against a column using a descending
3648
 
value, the <a class="reference internal" href="expression_api.html#sqlalchemy.sql.expression.ColumnElement.desc" title="sqlalchemy.sql.expression.ColumnElement.desc"><tt class="xref py py-meth docutils literal"><span class="pre">ColumnElement.desc()</span></tt></a> modifier may be used:</p>
3649
 
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">sqlalchemy</span> <span class="kn">import</span> <span class="n">Index</span>
3650
 
 
3651
 
<span class="n">Index</span><span class="p">(</span><span class="s">&#39;someindex&#39;</span><span class="p">,</span> <span class="n">mytable</span><span class="o">.</span><span class="n">c</span><span class="o">.</span><span class="n">somecol</span><span class="o">.</span><span class="n">desc</span><span class="p">())</span></pre></div>
3652
 
</div>
3653
 
<p>Or with a backend that supports functional indexes such as Postgresql,
3654
 
a &#8220;case insensitive&#8221; index can be created using the <tt class="docutils literal"><span class="pre">lower()</span></tt> function:</p>
3655
 
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">sqlalchemy</span> <span class="kn">import</span> <span class="n">func</span><span class="p">,</span> <span class="n">Index</span>
3656
 
 
3657
 
<span class="n">Index</span><span class="p">(</span><span class="s">&#39;someindex&#39;</span><span class="p">,</span> <span class="n">func</span><span class="o">.</span><span class="n">lower</span><span class="p">(</span><span class="n">mytable</span><span class="o">.</span><span class="n">c</span><span class="o">.</span><span class="n">somecol</span><span class="p">))</span></pre></div>
3658
 
</div>
3659
 
<p class="versionadded">
3660
 
<span class="versionmodified">New in version 0.8: </span><a class="reference internal" href="#sqlalchemy.schema.Index" title="sqlalchemy.schema.Index"><tt class="xref py py-class docutils literal"><span class="pre">Index</span></tt></a> supports SQL expressions and functions
3661
 
as well as plain columns.</p>
3662
 
</div>
3663
 
</div>
3664
 
<div class="section" id="index-api">
3665
 
<h3>Index API<a class="headerlink" href="#index-api" title="Permalink to this headline">¶</a></h3>
3666
 
<dl class="class">
3667
 
<dt id="sqlalchemy.schema.Index">
3668
 
<em class="property">class </em><tt class="descclassname">sqlalchemy.schema.</tt><tt class="descname">Index</tt><big>(</big><em>name</em>, <em>*expressions</em>, <em>**kw</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Index" title="Permalink to this definition">¶</a></dt>
3669
 
<dd><p>Bases: <tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.schema.ColumnCollectionMixin</span></tt>, <a class="reference internal" href="#sqlalchemy.schema.SchemaItem" title="sqlalchemy.schema.SchemaItem"><tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.schema.SchemaItem</span></tt></a></p>
3670
 
<p>A table-level INDEX.</p>
3671
 
<p>Defines a composite (one or more column) INDEX. For a no-frills, single
3672
 
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
3673
 
a shorthand equivalent for an unnamed, single column <a class="reference internal" href="#sqlalchemy.schema.Index" title="sqlalchemy.schema.Index"><tt class="xref py py-class docutils literal"><span class="pre">Index</span></tt></a>.</p>
3674
 
<div class="admonition-see-also admonition seealso">
3675
 
<p class="first admonition-title">See also</p>
3676
 
<p><a class="reference internal" href="#schema-indexes"><em>Indexes</em></a> - General information on <a class="reference internal" href="#sqlalchemy.schema.Index" title="sqlalchemy.schema.Index"><tt class="xref py py-class docutils literal"><span class="pre">Index</span></tt></a>.</p>
3677
 
<p><a class="reference internal" href="../dialects/postgresql.html#postgresql-indexes"><em>Postgresql-Specific Index Options</em></a> - PostgreSQL-specific options available for the
3678
 
<a class="reference internal" href="#sqlalchemy.schema.Index" title="sqlalchemy.schema.Index"><tt class="xref py py-class docutils literal"><span class="pre">Index</span></tt></a> construct.</p>
3679
 
<p><a class="reference internal" href="../dialects/mysql.html#mysql-indexes"><em>MySQL Specific Index Options</em></a> - MySQL-specific options available for the
3680
 
<a class="reference internal" href="#sqlalchemy.schema.Index" title="sqlalchemy.schema.Index"><tt class="xref py py-class docutils literal"><span class="pre">Index</span></tt></a> construct.</p>
3681
 
<p class="last"><a class="reference internal" href="../dialects/mssql.html#mssql-indexes"><em>MSSQL-Specific Index Options</em></a> - MSSQL-specific options available for the
3682
 
<a class="reference internal" href="#sqlalchemy.schema.Index" title="sqlalchemy.schema.Index"><tt class="xref py py-class docutils literal"><span class="pre">Index</span></tt></a> construct.</p>
3683
 
</div>
3684
 
<dl class="method">
3685
 
<dt id="sqlalchemy.schema.Index.__init__">
3686
 
<tt class="descname">__init__</tt><big>(</big><em>name</em>, <em>*expressions</em>, <em>**kw</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Index.__init__" title="Permalink to this definition">¶</a></dt>
3687
 
<dd><p>Construct an index object.</p>
3688
 
<table class="docutils field-list" frame="void" rules="none">
3689
 
<col class="field-name" />
3690
 
<col class="field-body" />
3691
 
<tbody valign="top">
3692
 
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
3693
 
<li><strong>name</strong> &#8211; The name of the index</li>
3694
 
<li><strong>*expressions</strong> &#8211; <p>Column expressions to include in the index.   The expressions
3695
 
are normally instances 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>, but may also
3696
 
be arbitrary SQL expressions which ultmately refer to a
3697
 
<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>
3698
 
<p class="versionadded">
3699
 
<span class="versionmodified">New in version 0.8: </span><a class="reference internal" href="#sqlalchemy.schema.Index" title="sqlalchemy.schema.Index"><tt class="xref py py-class docutils literal"><span class="pre">Index</span></tt></a> supports SQL expressions as
3700
 
well as plain columns.</p>
3701
 
</li>
3702
 
<li><strong>unique</strong> &#8211; Defaults to False: create a unique index.</li>
3703
 
<li><strong>**kw</strong> &#8211; Other keyword arguments may be interpreted by specific dialects.</li>
3704
 
</ul>
3705
 
</td>
3706
 
</tr>
3707
 
</tbody>
3708
 
</table>
3709
 
</dd></dl>
3710
 
 
3711
 
<dl class="attribute">
3712
 
<dt id="sqlalchemy.schema.Index.bind">
3713
 
<tt class="descname">bind</tt><a class="headerlink" href="#sqlalchemy.schema.Index.bind" title="Permalink to this definition">¶</a></dt>
3714
 
<dd><p>Return the connectable associated with this Index.</p>
3715
 
</dd></dl>
3716
 
 
3717
 
<dl class="method">
3718
 
<dt id="sqlalchemy.schema.Index.create">
3719
 
<tt class="descname">create</tt><big>(</big><em>bind=None</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Index.create" title="Permalink to this definition">¶</a></dt>
3720
 
<dd><p>Issue a <tt class="docutils literal"><span class="pre">CREATE</span></tt> statement for this
3721
 
<a class="reference internal" href="#sqlalchemy.schema.Index" title="sqlalchemy.schema.Index"><tt class="xref py py-class docutils literal"><span class="pre">Index</span></tt></a>, using the given <a class="reference internal" href="connections.html#sqlalchemy.engine.Connectable" title="sqlalchemy.engine.Connectable"><tt class="xref py py-class docutils literal"><span class="pre">Connectable</span></tt></a>
3722
 
for connectivity.</p>
3723
 
<p>See also <a class="reference internal" href="#sqlalchemy.schema.MetaData.create_all" title="sqlalchemy.schema.MetaData.create_all"><tt class="xref py py-meth docutils literal"><span class="pre">MetaData.create_all()</span></tt></a>.</p>
3724
 
</dd></dl>
3725
 
 
3726
 
<dl class="method">
3727
 
<dt id="sqlalchemy.schema.Index.drop">
3728
 
<tt class="descname">drop</tt><big>(</big><em>bind=None</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.Index.drop" title="Permalink to this definition">¶</a></dt>
3729
 
<dd><p>Issue a <tt class="docutils literal"><span class="pre">DROP</span></tt> statement for this
3730
 
<a class="reference internal" href="#sqlalchemy.schema.Index" title="sqlalchemy.schema.Index"><tt class="xref py py-class docutils literal"><span class="pre">Index</span></tt></a>, using the given <a class="reference internal" href="connections.html#sqlalchemy.engine.Connectable" title="sqlalchemy.engine.Connectable"><tt class="xref py py-class docutils literal"><span class="pre">Connectable</span></tt></a>
3731
 
for connectivity.</p>
3732
 
<p>See also <a class="reference internal" href="#sqlalchemy.schema.MetaData.drop_all" title="sqlalchemy.schema.MetaData.drop_all"><tt class="xref py py-meth docutils literal"><span class="pre">MetaData.drop_all()</span></tt></a>.</p>
3733
 
</dd></dl>
3734
 
 
3735
 
</dd></dl>
3736
 
 
3737
 
</div>
3738
 
</div>
3739
 
<div class="section" id="customizing-ddl">
3740
 
<span id="metadata-ddl"></span><h2>Customizing DDL<a class="headerlink" href="#customizing-ddl" title="Permalink to this headline">¶</a></h2>
3741
 
<p>In the preceding sections we&#8217;ve discussed a variety of schema constructs
3742
 
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>,
3743
 
<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>,
3744
 
<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
3745
 
<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
3746
 
<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
3747
 
<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
3748
 
order to issue data definition language (DDL) for all constructs. When issued,
3749
 
a pre-determined order of operations is invoked, and DDL to create each table
3750
 
is created unconditionally including all constraints and other objects
3751
 
associated with it. For more complex scenarios where database-specific DDL is
3752
 
required, SQLAlchemy offers two techniques which can be used to add any DDL
3753
 
based on any condition, either accompanying the standard generation of tables
3754
 
or by itself.</p>
3755
 
<div class="section" id="controlling-ddl-sequences">
3756
 
<span id="schema-ddl-sequences"></span><h3>Controlling DDL Sequences<a class="headerlink" href="#controlling-ddl-sequences" title="Permalink to this headline">¶</a></h3>
3757
 
<p>The <tt class="docutils literal"><span class="pre">sqlalchemy.schema</span></tt> package contains SQL expression constructs that
3758
 
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>
3759
 
<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>
3760
 
<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>
3761
 
<div class='popup_sql'>CREATE TABLE mytable (
3762
 
    col1 INTEGER,
3763
 
    col2 INTEGER,
3764
 
    col3 INTEGER,
3765
 
    col4 INTEGER,
3766
 
    col5 INTEGER,
3767
 
    col6 INTEGER
3768
 
)</div></pre></div>
3769
 
</div>
3770
 
<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
3771
 
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
3772
 
full reference of available constructs is in <a class="reference internal" href="#schema-api-ddl"><em>DDL Expression Constructs API</em></a>.</p>
3773
 
<p>The DDL constructs all extend a common base class which provides the
3774
 
capability to be associated with an individual
3775
 
<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>
3776
 
object, to be invoked upon create/drop events. Consider the example of a table
3777
 
which contains a CHECK constraint:</p>
3778
 
<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>
3779
 
               <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>
3780
 
               <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>
3781
 
               <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>
3782
 
               <span class="p">)</span>
3783
 
 
3784
 
<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>
3785
 
<div class='popup_sql'>CREATE TABLE users (
3786
 
    user_id SERIAL NOT NULL,
3787
 
    user_name VARCHAR(40) NOT NULL,
3788
 
    PRIMARY KEY (user_id),
3789
 
    CONSTRAINT cst_user_name_length  CHECK (length(user_name) >= 8)
3790
 
)</div></pre></div>
3791
 
</div>
3792
 
<p>The above table contains a column &#8220;user_name&#8221; which is subject to a CHECK
3793
 
constraint that validates that the length of the string is at least eight
3794
 
characters. When a <tt class="docutils literal"><span class="pre">create()</span></tt> is issued for this table, DDL for the
3795
 
<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
3796
 
the table definition.</p>
3797
 
<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
3798
 
constructed externally and associated with the
3799
 
<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>
3800
 
<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>
3801
 
<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>
3802
 
</div>
3803
 
<p>So far, the effect is the same. However, if we create DDL elements
3804
 
corresponding to the creation and removal of this constraint, and associate
3805
 
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
3806
 
will take over the job of issuing DDL for the constraint. Additionally, the
3807
 
constraint will be added via ALTER:</p>
3808
 
<div class="highlight-python+sql"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">sqlalchemy</span> <span class="kn">import</span> <span class="n">event</span>
3809
 
 
3810
 
<span class="n">event</span><span class="o">.</span><span class="n">listen</span><span class="p">(</span>
3811
 
    <span class="n">users</span><span class="p">,</span>
3812
 
    <span class="s">&quot;after_create&quot;</span><span class="p">,</span>
3813
 
    <span class="n">AddConstraint</span><span class="p">(</span><span class="n">constraint</span><span class="p">)</span>
3814
 
<span class="p">)</span>
3815
 
<span class="n">event</span><span class="o">.</span><span class="n">listen</span><span class="p">(</span>
3816
 
    <span class="n">users</span><span class="p">,</span>
3817
 
    <span class="s">&quot;before_drop&quot;</span><span class="p">,</span>
3818
 
    <span class="n">DropConstraint</span><span class="p">(</span><span class="n">constraint</span><span class="p">)</span>
3819
 
<span class="p">)</span>
3820
 
 
3821
 
<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>
3822
 
<div class='popup_sql'>CREATE TABLE users (
3823
 
    user_id SERIAL NOT NULL,
3824
 
    user_name VARCHAR(40) NOT NULL,
3825
 
    PRIMARY KEY (user_id)
3826
 
)
3827
 
 
3828
 
ALTER TABLE users ADD CONSTRAINT cst_user_name_length  CHECK (length(user_name) >= 8)</div>
3829
 
<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>
3830
 
<div class='popup_sql'>ALTER TABLE users DROP CONSTRAINT cst_user_name_length
3831
 
DROP TABLE user</div></pre></div>
3832
 
</div>
3833
 
<p>The real usefulness of the above becomes clearer once we illustrate the
3834
 
<a class="reference internal" href="#sqlalchemy.schema.DDLElement.execute_if" title="sqlalchemy.schema.DDLElement.execute_if"><tt class="xref py py-meth docutils literal"><span class="pre">DDLElement.execute_if()</span></tt></a> method.  This method returns a modified form of
3835
 
the DDL callable which will filter on criteria before responding to a
3836
 
received event.   It accepts a parameter <tt class="docutils literal"><span class="pre">dialect</span></tt>, which is the string
3837
 
name of a dialect or a tuple of such, which will limit the execution of the
3838
 
item to just those dialects.  It also accepts a <tt class="docutils literal"><span class="pre">callable_</span></tt> parameter which
3839
 
may reference a Python callable which will be invoked upon event reception,
3840
 
returning <tt class="docutils literal"><span class="pre">True</span></tt> or <tt class="docutils literal"><span class="pre">False</span></tt> indicating if the event should proceed.</p>
3841
 
<p>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
3842
 
Postgresql and not other databases, we could limit its usage to just that dialect:</p>
3843
 
<div class="highlight-python"><div class="highlight"><pre><span class="n">event</span><span class="o">.</span><span class="n">listen</span><span class="p">(</span>
3844
 
    <span class="n">users</span><span class="p">,</span>
3845
 
    <span class="s">&#39;after_create&#39;</span><span class="p">,</span>
3846
 
    <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_if</span><span class="p">(</span><span class="n">dialect</span><span class="o">=</span><span class="s">&#39;postgresql&#39;</span><span class="p">)</span>
3847
 
<span class="p">)</span>
3848
 
<span class="n">event</span><span class="o">.</span><span class="n">listen</span><span class="p">(</span>
3849
 
    <span class="n">users</span><span class="p">,</span>
3850
 
    <span class="s">&#39;before_drop&#39;</span><span class="p">,</span>
3851
 
    <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_if</span><span class="p">(</span><span class="n">dialect</span><span class="o">=</span><span class="s">&#39;postgresql&#39;</span><span class="p">)</span>
3852
 
<span class="p">)</span></pre></div>
3853
 
</div>
3854
 
<p>Or to any set of dialects:</p>
3855
 
<div class="highlight-python"><div class="highlight"><pre><span class="n">event</span><span class="o">.</span><span class="n">listen</span><span class="p">(</span>
3856
 
    <span class="n">users</span><span class="p">,</span>
3857
 
    <span class="s">&quot;after_create&quot;</span><span class="p">,</span>
3858
 
    <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_if</span><span class="p">(</span><span class="n">dialect</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>
3859
 
<span class="p">)</span>
3860
 
<span class="n">event</span><span class="o">.</span><span class="n">listen</span><span class="p">(</span>
3861
 
    <span class="n">users</span><span class="p">,</span>
3862
 
    <span class="s">&quot;before_drop&quot;</span><span class="p">,</span>
3863
 
    <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_if</span><span class="p">(</span><span class="n">dialect</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>
3864
 
<span class="p">)</span></pre></div>
3865
 
</div>
3866
 
<p>When using a callable, the callable is passed the ddl element, the
3867
 
<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>
3868
 
object whose &#8220;create&#8221; or &#8220;drop&#8221; event is in progress, and the
3869
 
<a class="reference internal" href="connections.html#sqlalchemy.engine.Connection" title="sqlalchemy.engine.Connection"><tt class="xref py py-class docutils literal"><span class="pre">Connection</span></tt></a> object being used for the
3870
 
operation, as well as additional information as keyword arguments. The
3871
 
callable can perform checks, such as whether or not a given item already
3872
 
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
3873
 
that check for the presence of our named constraint:</p>
3874
 
<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">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>
3875
 
    <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>
3876
 
    <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>
3877
 
 
3878
 
<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">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>
3879
 
    <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">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>
3880
 
 
3881
 
<span class="n">event</span><span class="o">.</span><span class="n">listen</span><span class="p">(</span>
3882
 
    <span class="n">users</span><span class="p">,</span>
3883
 
    <span class="s">&quot;after_create&quot;</span><span class="p">,</span>
3884
 
    <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_if</span><span class="p">(</span><span class="n">callable_</span><span class="o">=</span><span class="n">should_create</span><span class="p">)</span>
3885
 
<span class="p">)</span>
3886
 
<span class="n">event</span><span class="o">.</span><span class="n">listen</span><span class="p">(</span>
3887
 
    <span class="n">users</span><span class="p">,</span>
3888
 
    <span class="s">&quot;before_drop&quot;</span><span class="p">,</span>
3889
 
    <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_if</span><span class="p">(</span><span class="n">callable_</span><span class="o">=</span><span class="n">should_drop</span><span class="p">)</span>
3890
 
<span class="p">)</span>
3891
 
 
3892
 
<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>
3893
 
<div class='popup_sql'>CREATE TABLE users (
3894
 
    user_id SERIAL NOT NULL,
3895
 
    user_name VARCHAR(40) NOT NULL,
3896
 
    PRIMARY KEY (user_id)
3897
 
)
3898
 
 
3899
 
select conname from pg_constraint where conname='cst_user_name_length'
3900
 
ALTER TABLE users ADD CONSTRAINT cst_user_name_length  CHECK (length(user_name) >= 8)</div>
3901
 
<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>
3902
 
<div class='popup_sql'>select conname from pg_constraint where conname='cst_user_name_length'
3903
 
ALTER TABLE users DROP CONSTRAINT cst_user_name_length
3904
 
DROP TABLE user</div></pre></div>
3905
 
</div>
3906
 
</div>
3907
 
<div class="section" id="custom-ddl">
3908
 
<h3>Custom DDL<a class="headerlink" href="#custom-ddl" title="Permalink to this headline">¶</a></h3>
3909
 
<p>Custom DDL phrases are most easily achieved using the
3910
 
<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
3911
 
other DDL elements except it accepts a string which is the text to be emitted:</p>
3912
 
<div class="highlight-python+sql"><div class="highlight"><pre><span class="n">event</span><span class="o">.</span><span class="n">listen</span><span class="p">(</span>
3913
 
    <span class="n">metadata</span><span class="p">,</span>
3914
 
    <span class="s">&quot;after_create&quot;</span><span class="p">,</span>
3915
 
    <span class="n">DDL</span><span class="p">(</span><span class="s">&quot;ALTER TABLE users ADD CONSTRAINT &quot;</span>
3916
 
        <span class="s">&quot;cst_user_name_length &quot;</span>
3917
 
        <span class="s">&quot; CHECK (length(user_name) &gt;= 8)&quot;</span><span class="p">)</span>
3918
 
<span class="p">)</span></pre></div>
3919
 
</div>
3920
 
<p>A more comprehensive method of creating libraries of DDL constructs is to use
3921
 
custom compilation - see <a class="reference internal" href="compiler.html"><em>Custom SQL Constructs and Compilation Extension</em></a> for
3922
 
details.</p>
3923
 
</div>
3924
 
<div class="section" id="ddl-expression-constructs-api">
3925
 
<span id="schema-api-ddl"></span><h3>DDL Expression Constructs API<a class="headerlink" href="#ddl-expression-constructs-api" title="Permalink to this headline">¶</a></h3>
3926
 
<dl class="class">
3927
 
<dt id="sqlalchemy.schema.DDLElement">
3928
 
<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>
3929
 
<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>, <tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.schema._DDLCompiles</span></tt></p>
3930
 
<p>Base class for DDL expression constructs.</p>
3931
 
<p>This class is the base for the general purpose <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> class,
3932
 
as well as the various create/drop clause constructs such as
3933
 
<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>, <a class="reference internal" href="#sqlalchemy.schema.DropTable" title="sqlalchemy.schema.DropTable"><tt class="xref py py-class docutils literal"><span class="pre">DropTable</span></tt></a>, <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>,
3934
 
etc.</p>
3935
 
<p><a class="reference internal" href="#sqlalchemy.schema.DDLElement" title="sqlalchemy.schema.DDLElement"><tt class="xref py py-class docutils literal"><span class="pre">DDLElement</span></tt></a> integrates closely with SQLAlchemy events,
3936
 
introduced in <a class="reference internal" href="event.html"><em>Events</em></a>.  An instance of one is
3937
 
itself an event receiving callable:</p>
3938
 
<div class="highlight-python"><div class="highlight"><pre><span class="n">event</span><span class="o">.</span><span class="n">listen</span><span class="p">(</span>
3939
 
    <span class="n">users</span><span class="p">,</span>
3940
 
    <span class="s">&#39;after_create&#39;</span><span class="p">,</span>
3941
 
    <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_if</span><span class="p">(</span><span class="n">dialect</span><span class="o">=</span><span class="s">&#39;postgresql&#39;</span><span class="p">)</span>
3942
 
<span class="p">)</span></pre></div>
3943
 
</div>
3944
 
<p>See also:</p>
3945
 
<blockquote>
3946
 
<div><p><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></p>
3947
 
<p><a class="reference internal" href="events.html#sqlalchemy.events.DDLEvents" title="sqlalchemy.events.DDLEvents"><tt class="xref py py-class docutils literal"><span class="pre">DDLEvents</span></tt></a></p>
3948
 
<p><a class="reference internal" href="event.html"><em>Events</em></a></p>
3949
 
<p><a class="reference internal" href="#schema-ddl-sequences"><em>Controlling DDL Sequences</em></a></p>
3950
 
</div></blockquote>
3951
 
<dl class="method">
3952
 
<dt id="sqlalchemy.schema.DDLElement.__call__">
3953
 
<tt class="descname">__call__</tt><big>(</big><em>target</em>, <em>bind</em>, <em>**kw</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.DDLElement.__call__" title="Permalink to this definition">¶</a></dt>
3954
 
<dd><p>Execute the DDL as a ddl_listener.</p>
3955
 
</dd></dl>
3956
 
 
3957
 
<dl class="method">
3958
 
<dt id="sqlalchemy.schema.DDLElement.against">
3959
 
<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>
3960
 
<dd><p>Return a copy of this DDL against a specific schema item.</p>
3961
 
</dd></dl>
3962
 
 
3963
 
<dl class="attribute">
3964
 
<dt id="sqlalchemy.schema.DDLElement.bind">
3965
 
<tt class="descname">bind</tt><a class="headerlink" href="#sqlalchemy.schema.DDLElement.bind" title="Permalink to this definition">¶</a></dt>
3966
 
<dd></dd></dl>
3967
 
 
3968
 
<dl class="attribute">
3969
 
<dt id="sqlalchemy.schema.DDLElement.callable_">
3970
 
<tt class="descname">callable_</tt><em class="property"> = None</em><a class="headerlink" href="#sqlalchemy.schema.DDLElement.callable_" title="Permalink to this definition">¶</a></dt>
3971
 
<dd></dd></dl>
3972
 
 
3973
 
<dl class="attribute">
3974
 
<dt id="sqlalchemy.schema.DDLElement.dialect">
3975
 
<tt class="descname">dialect</tt><em class="property"> = None</em><a class="headerlink" href="#sqlalchemy.schema.DDLElement.dialect" title="Permalink to this definition">¶</a></dt>
3976
 
<dd></dd></dl>
3977
 
 
3978
 
<dl class="method">
3979
 
<dt id="sqlalchemy.schema.DDLElement.execute">
3980
 
<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>
3981
 
<dd><p>Execute this DDL immediately.</p>
3982
 
<p>Executes the DDL statement in isolation using the supplied
3983
 
<a class="reference internal" href="connections.html#sqlalchemy.engine.Connectable" title="sqlalchemy.engine.Connectable"><tt class="xref py py-class docutils literal"><span class="pre">Connectable</span></tt></a> or
3984
 
<a class="reference internal" href="connections.html#sqlalchemy.engine.Connectable" title="sqlalchemy.engine.Connectable"><tt class="xref py py-class docutils literal"><span class="pre">Connectable</span></tt></a> assigned to the <tt class="docutils literal"><span class="pre">.bind</span></tt>
3985
 
property, if not supplied. If the DDL has a conditional <tt class="docutils literal"><span class="pre">on</span></tt>
3986
 
criteria, it will be invoked with None as the event.</p>
3987
 
<table class="docutils field-list" frame="void" rules="none">
3988
 
<col class="field-name" />
3989
 
<col class="field-body" />
3990
 
<tbody valign="top">
3991
 
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
3992
 
<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
3993
 
<a class="reference internal" href="connections.html#sqlalchemy.engine.Connectable" title="sqlalchemy.engine.Connectable"><tt class="xref py py-class docutils literal"><span class="pre">Connectable</span></tt></a> must be present in the
3994
 
<tt class="docutils literal"><span class="pre">.bind</span></tt> property.</li>
3995
 
<li><strong>target</strong> &#8211; Optional, defaults to None.  The target SchemaItem for the
3996
 
execute call.  Will be passed to the <tt class="docutils literal"><span class="pre">on</span></tt> callable if any,
3997
 
and may also provide string expansion data for the
3998
 
statement. See <tt class="docutils literal"><span class="pre">execute_at</span></tt> for more information.</li>
3999
 
</ul>
4000
 
</td>
4001
 
</tr>
4002
 
</tbody>
4003
 
</table>
4004
 
</dd></dl>
4005
 
 
4006
 
<dl class="method">
4007
 
<dt id="sqlalchemy.schema.DDLElement.execute_at">
4008
 
<tt class="descname">execute_at</tt><big>(</big><em>event_name</em>, <em>target</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.DDLElement.execute_at" title="Permalink to this definition">¶</a></dt>
4009
 
<dd><p>Link execution of this DDL to the DDL lifecycle of a SchemaItem.</p>
4010
 
<p class="deprecated">
4011
 
<span class="versionmodified">Deprecated since version 0.7: </span>See <a class="reference internal" href="events.html#sqlalchemy.events.DDLEvents" title="sqlalchemy.events.DDLEvents"><tt class="xref py py-class docutils literal"><span class="pre">DDLEvents</span></tt></a>, as well as <a class="reference internal" href="#sqlalchemy.schema.DDLElement.execute_if" title="sqlalchemy.schema.DDLElement.execute_if"><tt class="xref py py-meth docutils literal"><span class="pre">DDLElement.execute_if()</span></tt></a>.</p>
4012
 
<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,
4013
 
executing it when that schema item is created or dropped. The DDL
4014
 
statement will be executed using the same Connection and transactional
4015
 
context as the Table create/drop itself. The <tt class="docutils literal"><span class="pre">.bind</span></tt> property of
4016
 
this statement is ignored.</p>
4017
 
<table class="docutils field-list" frame="void" rules="none">
4018
 
<col class="field-name" />
4019
 
<col class="field-body" />
4020
 
<tbody valign="top">
4021
 
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
4022
 
<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>;
4023
 
e.g. &#8216;before-create&#8217;, &#8216;after-create&#8217;, &#8216;before-drop&#8217; or &#8216;after-drop&#8217;</li>
4024
 
<li><strong>target</strong> &#8211; The Table or MetaData instance for which this DDLElement will
4025
 
be associated with.</li>
4026
 
</ul>
4027
 
</td>
4028
 
</tr>
4029
 
</tbody>
4030
 
</table>
4031
 
<p>A DDLElement instance can be linked to any number of schema items.</p>
4032
 
<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
4033
 
<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>
4034
 
<p>Caveat: Creating or dropping a Table in isolation will also trigger
4035
 
any DDL set to <tt class="docutils literal"><span class="pre">execute_at</span></tt> that Table&#8217;s MetaData.  This may change
4036
 
in a future release.</p>
4037
 
</dd></dl>
4038
 
 
4039
 
<dl class="method">
4040
 
<dt id="sqlalchemy.schema.DDLElement.execute_if">
4041
 
<tt class="descname">execute_if</tt><big>(</big><em>dialect=None</em>, <em>callable_=None</em>, <em>state=None</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.DDLElement.execute_if" title="Permalink to this definition">¶</a></dt>
4042
 
<dd><p>Return a callable that will execute this
4043
 
DDLElement conditionally.</p>
4044
 
<p>Used to provide a wrapper for event listening:</p>
4045
 
<div class="highlight-python"><div class="highlight"><pre><span class="n">event</span><span class="o">.</span><span class="n">listen</span><span class="p">(</span>
4046
 
            <span class="n">metadata</span><span class="p">,</span>
4047
 
            <span class="s">&#39;before_create&#39;</span><span class="p">,</span>
4048
 
            <span class="n">DDL</span><span class="p">(</span><span class="s">&quot;my_ddl&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">execute_if</span><span class="p">(</span><span class="n">dialect</span><span class="o">=</span><span class="s">&#39;postgresql&#39;</span><span class="p">)</span>
4049
 
        <span class="p">)</span></pre></div>
4050
 
</div>
4051
 
<table class="docutils field-list" frame="void" rules="none">
4052
 
<col class="field-name" />
4053
 
<col class="field-body" />
4054
 
<tbody valign="top">
4055
 
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
4056
 
<li><strong>dialect</strong> &#8211; <p>May be a string, tuple or a callable
4057
 
predicate.  If a string, it will be compared to the name of the
4058
 
executing database dialect:</p>
4059
 
<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="o">.</span><span class="n">execute_if</span><span class="p">(</span><span class="n">dialect</span><span class="o">=</span><span class="s">&#39;postgresql&#39;</span><span class="p">)</span></pre></div>
4060
 
</div>
4061
 
<p>If a tuple, specifies multiple dialect names:</p>
4062
 
<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="o">.</span><span class="n">execute_if</span><span class="p">(</span><span class="n">dialect</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>
4063
 
</div>
4064
 
</li>
4065
 
<li><strong>callable</strong> &#8211; <p>A callable, which will be invoked with
4066
 
four positional arguments as well as optional keyword
4067
 
arguments:</p>
4068
 
<blockquote>
4069
 
<div><table class="docutils field-list" frame="void" rules="none">
4070
 
<col class="field-name" />
4071
 
<col class="field-body" />
4072
 
<tbody valign="top">
4073
 
<tr class="field-odd field"><th class="field-name">ddl:</th><td class="field-body">This DDL element.</td>
4074
 
</tr>
4075
 
<tr class="field-even field"><th class="field-name">target:</th><td class="field-body">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 which is the
4076
 
target of this event. May be None if the DDL is executed
4077
 
explicitly.</td>
4078
 
</tr>
4079
 
<tr class="field-odd field"><th class="field-name">bind:</th><td class="field-body">The <a class="reference internal" href="connections.html#sqlalchemy.engine.Connection" title="sqlalchemy.engine.Connection"><tt class="xref py py-class docutils literal"><span class="pre">Connection</span></tt></a> being used for DDL execution</td>
4080
 
</tr>
4081
 
<tr class="field-even field"><th class="field-name">tables:</th><td class="field-body">Optional keyword argument - a list of Table objects which are to
4082
 
be created/ dropped within a MetaData.create_all() or drop_all()
4083
 
method call.</td>
4084
 
</tr>
4085
 
<tr class="field-odd field"><th class="field-name">state:</th><td class="field-body">Optional keyword argument - will be the <tt class="docutils literal"><span class="pre">state</span></tt> argument
4086
 
passed to this function.</td>
4087
 
</tr>
4088
 
<tr class="field-even field"><th class="field-name">checkfirst:</th><td class="field-body">Keyword argument, will be True if the &#8216;checkfirst&#8217; flag was
4089
 
set during the call to <tt class="docutils literal"><span class="pre">create()</span></tt>, <tt class="docutils literal"><span class="pre">create_all()</span></tt>,
4090
 
<tt class="docutils literal"><span class="pre">drop()</span></tt>, <tt class="docutils literal"><span class="pre">drop_all()</span></tt>.</td>
4091
 
</tr>
4092
 
</tbody>
4093
 
</table>
4094
 
</div></blockquote>
4095
 
<p>If the callable returns a true value, the DDL statement will be
4096
 
executed.</p>
4097
 
</li>
4098
 
<li><strong>state</strong> &#8211; any value which will be passed to the <a href="#id2"><span class="problematic" id="id3">callable_</span></a>
4099
 
as the <tt class="docutils literal"><span class="pre">state</span></tt> keyword argument.</li>
4100
 
</ul>
4101
 
</td>
4102
 
</tr>
4103
 
</tbody>
4104
 
</table>
4105
 
<p>See also:</p>
4106
 
<blockquote>
4107
 
<div><p><a class="reference internal" href="events.html#sqlalchemy.events.DDLEvents" title="sqlalchemy.events.DDLEvents"><tt class="xref py py-class docutils literal"><span class="pre">DDLEvents</span></tt></a></p>
4108
 
<p><a class="reference internal" href="event.html"><em>Events</em></a></p>
4109
 
</div></blockquote>
4110
 
</dd></dl>
4111
 
 
4112
 
<dl class="attribute">
4113
 
<dt id="sqlalchemy.schema.DDLElement.on">
4114
 
<tt class="descname">on</tt><em class="property"> = None</em><a class="headerlink" href="#sqlalchemy.schema.DDLElement.on" title="Permalink to this definition">¶</a></dt>
4115
 
<dd></dd></dl>
4116
 
 
4117
 
<dl class="attribute">
4118
 
<dt id="sqlalchemy.schema.DDLElement.target">
4119
 
<tt class="descname">target</tt><em class="property"> = None</em><a class="headerlink" href="#sqlalchemy.schema.DDLElement.target" title="Permalink to this definition">¶</a></dt>
4120
 
<dd></dd></dl>
4121
 
 
4122
 
</dd></dl>
4123
 
 
4124
 
<dl class="class">
4125
 
<dt id="sqlalchemy.schema.DDL">
4126
 
<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>
4127
 
<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>
4128
 
<p>A literal DDL statement.</p>
4129
 
<p>Specifies literal SQL DDL to be executed by the database.  DDL objects
4130
 
function as DDL event listeners, and can be subscribed to those events
4131
 
listed in <a class="reference internal" href="events.html#sqlalchemy.events.DDLEvents" title="sqlalchemy.events.DDLEvents"><tt class="xref py py-class docutils literal"><span class="pre">DDLEvents</span></tt></a>, using either <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
4132
 
<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> objects as targets.   Basic templating support allows
4133
 
a single DDL instance to handle repetitive tasks for multiple tables.</p>
4134
 
<p>Examples:</p>
4135
 
<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">event</span><span class="p">,</span> <span class="n">DDL</span>
4136
 
 
4137
 
<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>
4138
 
<span class="n">event</span><span class="o">.</span><span class="n">listen</span><span class="p">(</span><span class="n">tbl</span><span class="p">,</span> <span class="s">&#39;before_create&#39;</span><span class="p">,</span> <span class="n">DDL</span><span class="p">(</span><span class="s">&#39;DROP TRIGGER users_trigger&#39;</span><span class="p">))</span>
4139
 
 
4140
 
<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>
4141
 
<span class="n">event</span><span class="o">.</span><span class="n">listen</span><span class="p">(</span><span class="n">tbl</span><span class="p">,</span> <span class="s">&#39;after_create&#39;</span><span class="p">,</span> <span class="n">spow</span><span class="o">.</span><span class="n">execute_if</span><span class="p">(</span><span class="n">dialect</span><span class="o">=</span><span class="s">&#39;somedb&#39;</span><span class="p">))</span>
4142
 
 
4143
 
<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>
4144
 
<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>
4145
 
</div>
4146
 
<p>When operating on Table events, the following <tt class="docutils literal"><span class="pre">statement</span></tt>
4147
 
string substitions are available:</p>
4148
 
<div class="highlight-python"><pre>%(table)s  - the Table name, with any required quoting applied
4149
 
%(schema)s - the schema name, with any required quoting applied
4150
 
%(fullname)s - the Table name including schema, quoted if needed</pre>
4151
 
</div>
4152
 
<p>The DDL&#8217;s &#8220;context&#8221;, if any, will be combined with the standard
4153
 
substutions noted above.  Keys present in the context will override
4154
 
the standard substitutions.</p>
4155
 
<dl class="method">
4156
 
<dt id="sqlalchemy.schema.DDL.__init__">
4157
 
<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>
4158
 
<dd><p>Create a DDL statement.</p>
4159
 
<table class="docutils field-list" frame="void" rules="none">
4160
 
<col class="field-name" />
4161
 
<col class="field-body" />
4162
 
<tbody valign="top">
4163
 
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
4164
 
<li><strong>statement</strong> &#8211; <p>A string or unicode string to be executed.  Statements will be
4165
 
processed with Python&#8217;s string formatting operator.  See the
4166
 
<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>
4167
 
<p>A literal &#8216;%&#8217; in a statement must be escaped as &#8216;%%&#8217;.</p>
4168
 
<p>SQL bind parameters are not available in DDL statements.</p>
4169
 
</li>
4170
 
<li><strong>on</strong> &#8211; <p>Deprecated.  See <a class="reference internal" href="#sqlalchemy.schema.DDLElement.execute_if" title="sqlalchemy.schema.DDLElement.execute_if"><tt class="xref py py-meth docutils literal"><span class="pre">DDLElement.execute_if()</span></tt></a>.</p>
4171
 
<p>Optional filtering criteria.  May be a string, tuple or a callable
4172
 
predicate.  If a string, it will be compared to the name of the
4173
 
executing database dialect:</p>
4174
 
<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>
4175
 
</div>
4176
 
<p>If a tuple, specifies multiple dialect names:</p>
4177
 
<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>
4178
 
</div>
4179
 
<p>If a callable, it will be invoked with four positional arguments
4180
 
as well as optional keyword arguments:</p>
4181
 
<blockquote>
4182
 
<div><table class="docutils field-list" frame="void" rules="none">
4183
 
<col class="field-name" />
4184
 
<col class="field-body" />
4185
 
<tbody valign="top">
4186
 
<tr class="field-odd field"><th class="field-name">ddl:</th><td class="field-body">This DDL element.</td>
4187
 
</tr>
4188
 
<tr class="field-even field"><th class="field-name">event:</th><td class="field-body">The name of the event that has triggered this DDL, such as
4189
 
&#8216;after-create&#8217; Will be None if the DDL is executed explicitly.</td>
4190
 
</tr>
4191
 
<tr class="field-odd 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
4192
 
this event. May be None if the DDL is executed explicitly.</td>
4193
 
</tr>
4194
 
<tr class="field-even 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>
4195
 
</tr>
4196
 
<tr class="field-odd field"><th class="field-name">tables:</th><td class="field-body">Optional keyword argument - a list of Table objects which are to
4197
 
be created/ dropped within a MetaData.create_all() or drop_all()
4198
 
method call.</td>
4199
 
</tr>
4200
 
</tbody>
4201
 
</table>
4202
 
</div></blockquote>
4203
 
<p>If the callable returns a true value, the DDL statement will be
4204
 
executed.</p>
4205
 
</li>
4206
 
<li><strong>context</strong> &#8211; Optional dictionary, defaults to None.  These values will be
4207
 
available for use in string substitutions on the DDL statement.</li>
4208
 
<li><strong>bind</strong> &#8211; Optional. A <a class="reference internal" href="connections.html#sqlalchemy.engine.Connectable" title="sqlalchemy.engine.Connectable"><tt class="xref py py-class docutils literal"><span class="pre">Connectable</span></tt></a>, used by
4209
 
default when <tt class="docutils literal"><span class="pre">execute()</span></tt> is invoked without a bind argument.</li>
4210
 
</ul>
4211
 
</td>
4212
 
</tr>
4213
 
</tbody>
4214
 
</table>
4215
 
<p>See also:</p>
4216
 
<blockquote>
4217
 
<div><a class="reference internal" href="events.html#sqlalchemy.events.DDLEvents" title="sqlalchemy.events.DDLEvents"><tt class="xref py py-class docutils literal"><span class="pre">DDLEvents</span></tt></a>
4218
 
<tt class="xref py py-mod docutils literal"><span class="pre">sqlalchemy.event</span></tt></div></blockquote>
4219
 
</dd></dl>
4220
 
 
4221
 
</dd></dl>
4222
 
 
4223
 
<dl class="class">
4224
 
<dt id="sqlalchemy.schema.CreateTable">
4225
 
<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>
4226
 
<dd><p>Bases: <tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.schema._CreateDropBase</span></tt></p>
4227
 
<p>Represent a CREATE TABLE statement.</p>
4228
 
<dl class="method">
4229
 
<dt id="sqlalchemy.schema.CreateTable.__init__">
4230
 
<tt class="descname">__init__</tt><big>(</big><em>element</em>, <em>on=None</em>, <em>bind=None</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.CreateTable.__init__" title="Permalink to this definition">¶</a></dt>
4231
 
<dd><p>Create a <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.</p>
4232
 
<table class="docutils field-list" frame="void" rules="none">
4233
 
<col class="field-name" />
4234
 
<col class="field-body" />
4235
 
<tbody valign="top">
4236
 
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
4237
 
<li><strong>element</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> that&#8217;s the subject
4238
 
of the CREATE</li>
4239
 
<li><strong>on</strong> &#8211; See the description for &#8216;on&#8217; in <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>.</li>
4240
 
<li><strong>bind</strong> &#8211; See the description for &#8216;bind&#8217; in <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>.</li>
4241
 
</ul>
4242
 
</td>
4243
 
</tr>
4244
 
</tbody>
4245
 
</table>
4246
 
</dd></dl>
4247
 
 
4248
 
</dd></dl>
4249
 
 
4250
 
<dl class="class">
4251
 
<dt id="sqlalchemy.schema.DropTable">
4252
 
<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>
4253
 
<dd><p>Bases: <tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.schema._CreateDropBase</span></tt></p>
4254
 
<p>Represent a DROP TABLE statement.</p>
4255
 
</dd></dl>
4256
 
 
4257
 
<dl class="class">
4258
 
<dt id="sqlalchemy.schema.CreateColumn">
4259
 
<em class="property">class </em><tt class="descclassname">sqlalchemy.schema.</tt><tt class="descname">CreateColumn</tt><big>(</big><em>element</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.CreateColumn" title="Permalink to this definition">¶</a></dt>
4260
 
<dd><p>Bases: <tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.schema._DDLCompiles</span></tt></p>
4261
 
<p>Represent 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> as rendered in a CREATE TABLE statement,
4262
 
via 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.</p>
4263
 
<p>This is provided to support custom column DDL within the generation
4264
 
of CREATE TABLE statements, by using the
4265
 
compiler extension documented in <a class="reference internal" href="compiler.html"><em>Custom SQL Constructs and Compilation Extension</em></a>
4266
 
to extend <a class="reference internal" href="#sqlalchemy.schema.CreateColumn" title="sqlalchemy.schema.CreateColumn"><tt class="xref py py-class docutils literal"><span class="pre">CreateColumn</span></tt></a>.</p>
4267
 
<p>Typical integration is to examine the incoming <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>
4268
 
object, and to redirect compilation if a particular flag or condition
4269
 
is found:</p>
4270
 
<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">schema</span>
4271
 
<span class="kn">from</span> <span class="nn">sqlalchemy.ext.compiler</span> <span class="kn">import</span> <span class="n">compiles</span>
4272
 
 
4273
 
<span class="nd">@compiles</span><span class="p">(</span><span class="n">schema</span><span class="o">.</span><span class="n">CreateColumn</span><span class="p">)</span>
4274
 
<span class="k">def</span> <span class="nf">compile</span><span class="p">(</span><span class="n">element</span><span class="p">,</span> <span class="n">compiler</span><span class="p">,</span> <span class="o">**</span><span class="n">kw</span><span class="p">):</span>
4275
 
    <span class="n">column</span> <span class="o">=</span> <span class="n">element</span><span class="o">.</span><span class="n">element</span>
4276
 
 
4277
 
    <span class="k">if</span> <span class="s">&quot;special&quot;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">column</span><span class="o">.</span><span class="n">info</span><span class="p">:</span>
4278
 
        <span class="k">return</span> <span class="n">compiler</span><span class="o">.</span><span class="n">visit_create_column</span><span class="p">(</span><span class="n">element</span><span class="p">,</span> <span class="o">**</span><span class="n">kw</span><span class="p">)</span>
4279
 
 
4280
 
    <span class="n">text</span> <span class="o">=</span> <span class="s">&quot;</span><span class="si">%s</span><span class="s"> SPECIAL DIRECTIVE </span><span class="si">%s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span>
4281
 
            <span class="n">column</span><span class="o">.</span><span class="n">name</span><span class="p">,</span>
4282
 
            <span class="n">compiler</span><span class="o">.</span><span class="n">type_compiler</span><span class="o">.</span><span class="n">process</span><span class="p">(</span><span class="n">column</span><span class="o">.</span><span class="n">type</span><span class="p">)</span>
4283
 
        <span class="p">)</span>
4284
 
    <span class="n">default</span> <span class="o">=</span> <span class="n">compiler</span><span class="o">.</span><span class="n">get_column_default_string</span><span class="p">(</span><span class="n">column</span><span class="p">)</span>
4285
 
    <span class="k">if</span> <span class="n">default</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
4286
 
        <span class="n">text</span> <span class="o">+=</span> <span class="s">&quot; DEFAULT &quot;</span> <span class="o">+</span> <span class="n">default</span>
4287
 
 
4288
 
    <span class="k">if</span> <span class="ow">not</span> <span class="n">column</span><span class="o">.</span><span class="n">nullable</span><span class="p">:</span>
4289
 
        <span class="n">text</span> <span class="o">+=</span> <span class="s">&quot; NOT NULL&quot;</span>
4290
 
 
4291
 
    <span class="k">if</span> <span class="n">column</span><span class="o">.</span><span class="n">constraints</span><span class="p">:</span>
4292
 
        <span class="n">text</span> <span class="o">+=</span> <span class="s">&quot; &quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span>
4293
 
                    <span class="n">compiler</span><span class="o">.</span><span class="n">process</span><span class="p">(</span><span class="n">const</span><span class="p">)</span>
4294
 
                    <span class="k">for</span> <span class="n">const</span> <span class="ow">in</span> <span class="n">column</span><span class="o">.</span><span class="n">constraints</span><span class="p">)</span>
4295
 
    <span class="k">return</span> <span class="n">text</span></pre></div>
4296
 
</div>
4297
 
<p>The above construct can be 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> as follows:</p>
4298
 
<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">Table</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="n">Integer</span><span class="p">,</span> <span class="n">String</span>
4299
 
<span class="kn">from</span> <span class="nn">sqlalchemy</span> <span class="kn">import</span> <span class="n">schema</span>
4300
 
 
4301
 
<span class="n">metadata</span> <span class="o">=</span> <span class="n">MetaData</span><span class="p">()</span>
4302
 
 
4303
 
<span class="n">table</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">MetaData</span><span class="p">(),</span>
4304
 
        <span class="n">Column</span><span class="p">(</span><span class="s">&#39;x&#39;</span><span class="p">,</span> <span class="n">Integer</span><span class="p">,</span> <span class="n">info</span><span class="o">=</span><span class="p">{</span><span class="s">&quot;special&quot;</span><span class="p">:</span><span class="bp">True</span><span class="p">},</span> <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">),</span>
4305
 
        <span class="n">Column</span><span class="p">(</span><span class="s">&#39;y&#39;</span><span class="p">,</span> <span class="n">String</span><span class="p">(</span><span class="mi">50</span><span class="p">)),</span>
4306
 
        <span class="n">Column</span><span class="p">(</span><span class="s">&#39;z&#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">info</span><span class="o">=</span><span class="p">{</span><span class="s">&quot;special&quot;</span><span class="p">:</span><span class="bp">True</span><span class="p">})</span>
4307
 
    <span class="p">)</span>
4308
 
 
4309
 
<span class="n">metadata</span><span class="o">.</span><span class="n">create_all</span><span class="p">(</span><span class="n">conn</span><span class="p">)</span></pre></div>
4310
 
</div>
4311
 
<p>Above, the directives we&#8217;ve added to the <a class="reference internal" href="#sqlalchemy.schema.Column.info" title="sqlalchemy.schema.Column.info"><tt class="xref py py-attr docutils literal"><span class="pre">Column.info</span></tt></a> collection
4312
 
will be detected by our custom compilation scheme:</p>
4313
 
<div class="highlight-python"><pre>CREATE TABLE mytable (
4314
 
        x SPECIAL DIRECTIVE INTEGER NOT NULL,
4315
 
        y VARCHAR(50),
4316
 
        z SPECIAL DIRECTIVE VARCHAR(20),
4317
 
    PRIMARY KEY (x)
4318
 
)</pre>
4319
 
</div>
4320
 
<p class="versionadded">
4321
 
<span class="versionmodified">New in version 0.8: </span>The <a class="reference internal" href="#sqlalchemy.schema.CreateColumn" title="sqlalchemy.schema.CreateColumn"><tt class="xref py py-class docutils literal"><span class="pre">CreateColumn</span></tt></a> construct was added
4322
 
to support custom column creation styles.</p>
4323
 
</dd></dl>
4324
 
 
4325
 
<dl class="class">
4326
 
<dt id="sqlalchemy.schema.CreateSequence">
4327
 
<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>
4328
 
<dd><p>Bases: <tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.schema._CreateDropBase</span></tt></p>
4329
 
<p>Represent a CREATE SEQUENCE statement.</p>
4330
 
</dd></dl>
4331
 
 
4332
 
<dl class="class">
4333
 
<dt id="sqlalchemy.schema.DropSequence">
4334
 
<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>
4335
 
<dd><p>Bases: <tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.schema._CreateDropBase</span></tt></p>
4336
 
<p>Represent a DROP SEQUENCE statement.</p>
4337
 
</dd></dl>
4338
 
 
4339
 
<dl class="class">
4340
 
<dt id="sqlalchemy.schema.CreateIndex">
4341
 
<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>
4342
 
<dd><p>Bases: <tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.schema._CreateDropBase</span></tt></p>
4343
 
<p>Represent a CREATE INDEX statement.</p>
4344
 
</dd></dl>
4345
 
 
4346
 
<dl class="class">
4347
 
<dt id="sqlalchemy.schema.DropIndex">
4348
 
<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>
4349
 
<dd><p>Bases: <tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.schema._CreateDropBase</span></tt></p>
4350
 
<p>Represent a DROP INDEX statement.</p>
4351
 
</dd></dl>
4352
 
 
4353
 
<dl class="class">
4354
 
<dt id="sqlalchemy.schema.AddConstraint">
4355
 
<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>
4356
 
<dd><p>Bases: <tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.schema._CreateDropBase</span></tt></p>
4357
 
<p>Represent an ALTER TABLE ADD CONSTRAINT statement.</p>
4358
 
</dd></dl>
4359
 
 
4360
 
<dl class="class">
4361
 
<dt id="sqlalchemy.schema.DropConstraint">
4362
 
<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>
4363
 
<dd><p>Bases: <tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.schema._CreateDropBase</span></tt></p>
4364
 
<p>Represent an ALTER TABLE DROP CONSTRAINT statement.</p>
4365
 
</dd></dl>
4366
 
 
4367
 
<dl class="class">
4368
 
<dt id="sqlalchemy.schema.CreateSchema">
4369
 
<em class="property">class </em><tt class="descclassname">sqlalchemy.schema.</tt><tt class="descname">CreateSchema</tt><big>(</big><em>name</em>, <em>quote=None</em>, <em>**kw</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.CreateSchema" title="Permalink to this definition">¶</a></dt>
4370
 
<dd><p>Bases: <tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.schema._CreateDropBase</span></tt></p>
4371
 
<p>Represent a CREATE SCHEMA statement.</p>
4372
 
<p class="versionadded">
4373
 
<span class="versionmodified">New in version 0.7.4.</span></p>
4374
 
<p>The argument here is the string name of the schema.</p>
4375
 
<dl class="method">
4376
 
<dt id="sqlalchemy.schema.CreateSchema.__init__">
4377
 
<tt class="descname">__init__</tt><big>(</big><em>name</em>, <em>quote=None</em>, <em>**kw</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.CreateSchema.__init__" title="Permalink to this definition">¶</a></dt>
4378
 
<dd><p>Create a new <a class="reference internal" href="#sqlalchemy.schema.CreateSchema" title="sqlalchemy.schema.CreateSchema"><tt class="xref py py-class docutils literal"><span class="pre">CreateSchema</span></tt></a> construct.</p>
4379
 
</dd></dl>
4380
 
 
4381
 
</dd></dl>
4382
 
 
4383
 
<dl class="class">
4384
 
<dt id="sqlalchemy.schema.DropSchema">
4385
 
<em class="property">class </em><tt class="descclassname">sqlalchemy.schema.</tt><tt class="descname">DropSchema</tt><big>(</big><em>name</em>, <em>quote=None</em>, <em>cascade=False</em>, <em>**kw</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.DropSchema" title="Permalink to this definition">¶</a></dt>
4386
 
<dd><p>Bases: <tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.schema._CreateDropBase</span></tt></p>
4387
 
<p>Represent a DROP SCHEMA statement.</p>
4388
 
<p>The argument here is the string name of the schema.</p>
4389
 
<p class="versionadded">
4390
 
<span class="versionmodified">New in version 0.7.4.</span></p>
4391
 
<dl class="method">
4392
 
<dt id="sqlalchemy.schema.DropSchema.__init__">
4393
 
<tt class="descname">__init__</tt><big>(</big><em>name</em>, <em>quote=None</em>, <em>cascade=False</em>, <em>**kw</em><big>)</big><a class="headerlink" href="#sqlalchemy.schema.DropSchema.__init__" title="Permalink to this definition">¶</a></dt>
4394
 
<dd><p>Create a new <a class="reference internal" href="#sqlalchemy.schema.DropSchema" title="sqlalchemy.schema.DropSchema"><tt class="xref py py-class docutils literal"><span class="pre">DropSchema</span></tt></a> construct.</p>
4395
 
</dd></dl>
4396
 
 
4397
 
</dd></dl>
4398
 
 
4399
 
</div>
 
169
<div class="toctree-wrapper compound">
 
170
<ul>
 
171
<li class="toctree-l1"><a class="reference internal" href="metadata.html">Describing Databases with MetaData</a></li>
 
172
<li class="toctree-l1"><a class="reference internal" href="reflection.html">Reflecting Database Objects</a></li>
 
173
<li class="toctree-l1"><a class="reference internal" href="defaults.html">Column Insert/Update Defaults</a></li>
 
174
<li class="toctree-l1"><a class="reference internal" href="constraints.html">Defining Constraints and Indexes</a></li>
 
175
<li class="toctree-l1"><a class="reference internal" href="ddl.html">Customizing DDL</a></li>
 
176
</ul>
4400
177
</div>
4401
178
</div>
4402
179
 
4406
183
 
4407
184
<div id="docs-bottom-navigation" class="docs-navigation-links">
4408
185
        Previous:
4409
 
        <a href="pooling.html" title="previous chapter">Connection Pooling</a>
 
186
        <a href="types.html" title="previous chapter">Column and Data Types</a>
4410
187
        Next:
4411
 
        <a href="types.html" title="next chapter">Column and Data Types</a>
 
188
        <a href="metadata.html" title="next chapter">Describing Databases with MetaData</a>
4412
189
 
4413
190
    <div id="docs-copyright">
4414
191
        &copy; <a href="../copyright.html">Copyright</a> 2007-2013, the SQLAlchemy authors and contributors.
4415
 
        Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
 
192
        Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.2b1.
4416
193
    </div>
4417
194
</div>
4418
195