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

« back to all changes in this revision

Viewing changes to doc/orm/extensions/declarative.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
      };
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>
189
189
<div class="section" id="synopsis">
190
190
<h2>Synopsis<a class="headerlink" href="#synopsis" title="Permalink to this headline">¶</a></h2>
191
191
<p>SQLAlchemy object-relational configuration involves the
192
 
combination of <a class="reference internal" href="../../core/schema.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="../mapper_config.html#sqlalchemy.orm.mapper" title="sqlalchemy.orm.mapper"><tt class="xref py py-func docutils literal"><span class="pre">mapper()</span></tt></a>, and class
 
192
combination of <a class="reference internal" href="../../core/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="../mapper_config.html#sqlalchemy.orm.mapper" title="sqlalchemy.orm.mapper"><tt class="xref py py-func docutils literal"><span class="pre">mapper()</span></tt></a>, and class
193
193
objects to define a mapped class.
194
194
<a class="reference internal" href="#module-sqlalchemy.ext.declarative" title="sqlalchemy.ext.declarative"><tt class="xref py py-mod docutils literal"><span class="pre">declarative</span></tt></a> allows all three to be
195
195
expressed at once within the class declaration. As much as
208
208
</div>
209
209
<p>Above, the <a class="reference internal" href="#sqlalchemy.ext.declarative.declarative_base" title="sqlalchemy.ext.declarative.declarative_base"><tt class="xref py py-func docutils literal"><span class="pre">declarative_base()</span></tt></a> callable returns a new base class from
210
210
which all mapped classes should inherit. When the class definition is
211
 
completed, a new <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> and <a class="reference internal" href="../mapper_config.html#sqlalchemy.orm.mapper" title="sqlalchemy.orm.mapper"><tt class="xref py py-func docutils literal"><span class="pre">mapper()</span></tt></a> will have been generated.</p>
 
211
completed, a new <a class="reference internal" href="../../core/metadata.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> and <a class="reference internal" href="../mapper_config.html#sqlalchemy.orm.mapper" title="sqlalchemy.orm.mapper"><tt class="xref py py-func docutils literal"><span class="pre">mapper()</span></tt></a> will have been generated.</p>
212
212
<p>The resulting table and mapper are accessible via
213
213
<tt class="docutils literal"><span class="pre">__table__</span></tt> and <tt class="docutils literal"><span class="pre">__mapper__</span></tt> attributes on the
214
214
<tt class="docutils literal"><span class="pre">SomeClass</span></tt> class:</p>
221
221
</div>
222
222
<div class="section" id="defining-attributes">
223
223
<h2>Defining Attributes<a class="headerlink" href="#defining-attributes" title="Permalink to this headline">¶</a></h2>
224
 
<p>In the previous example, the <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a> objects are
 
224
<p>In the previous example, the <a class="reference internal" href="../../core/metadata.html#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a> objects are
225
225
automatically named with the name of the attribute to which they are
226
226
assigned.</p>
227
227
<p>To name columns explicitly with a name distinct from their mapped attribute,
233
233
    <span class="nb">id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="s">&quot;some_table_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></pre></div>
234
234
</div>
235
235
<p>Attributes may be added to the class after its construction, and they will be
236
 
added to the underlying <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> and
 
236
added to the underlying <a class="reference internal" href="../../core/metadata.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> and
237
237
<a class="reference internal" href="../mapper_config.html#sqlalchemy.orm.mapper" title="sqlalchemy.orm.mapper"><tt class="xref py py-func docutils literal"><span class="pre">mapper()</span></tt></a> definitions as appropriate:</p>
238
238
<div class="highlight-python"><div class="highlight"><pre><span class="n">SomeClass</span><span class="o">.</span><span class="n">data</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="s">&#39;data&#39;</span><span class="p">,</span> <span class="n">Unicode</span><span class="p">)</span>
239
239
<span class="n">SomeClass</span><span class="o">.</span><span class="n">related</span> <span class="o">=</span> <span class="n">relationship</span><span class="p">(</span><span class="n">RelatedInfo</span><span class="p">)</span></pre></div>
241
241
<p>Classes which are constructed using declarative can interact freely
242
242
with classes that are mapped explicitly with <a class="reference internal" href="../mapper_config.html#sqlalchemy.orm.mapper" title="sqlalchemy.orm.mapper"><tt class="xref py py-func docutils literal"><span class="pre">mapper()</span></tt></a>.</p>
243
243
<p>It is recommended, though not required, that all tables
244
 
share the same underlying <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.MetaData" title="sqlalchemy.schema.MetaData"><tt class="xref py py-class docutils literal"><span class="pre">MetaData</span></tt></a> object,
245
 
so that string-configured <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.ForeignKey" title="sqlalchemy.schema.ForeignKey"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></tt></a>
 
244
share the same underlying <a class="reference internal" href="../../core/metadata.html#sqlalchemy.schema.MetaData" title="sqlalchemy.schema.MetaData"><tt class="xref py py-class docutils literal"><span class="pre">MetaData</span></tt></a> object,
 
245
so that string-configured <a class="reference internal" href="../../core/constraints.html#sqlalchemy.schema.ForeignKey" title="sqlalchemy.schema.ForeignKey"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></tt></a>
246
246
references can be resolved without issue.</p>
247
247
</div>
248
248
<div class="section" id="accessing-the-metadata">
249
249
<h2>Accessing the MetaData<a class="headerlink" href="#accessing-the-metadata" title="Permalink to this headline">¶</a></h2>
250
250
<p>The <a class="reference internal" href="#sqlalchemy.ext.declarative.declarative_base" title="sqlalchemy.ext.declarative.declarative_base"><tt class="xref py py-func docutils literal"><span class="pre">declarative_base()</span></tt></a> base class contains a
251
 
<a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.MetaData" title="sqlalchemy.schema.MetaData"><tt class="xref py py-class docutils literal"><span class="pre">MetaData</span></tt></a> object where newly defined
252
 
<a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> objects are collected. This object is
 
251
<a class="reference internal" href="../../core/metadata.html#sqlalchemy.schema.MetaData" title="sqlalchemy.schema.MetaData"><tt class="xref py py-class docutils literal"><span class="pre">MetaData</span></tt></a> object where newly defined
 
252
<a class="reference internal" href="../../core/metadata.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> objects are collected. This object is
253
253
intended to be accessed directly for
254
 
<a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.MetaData" title="sqlalchemy.schema.MetaData"><tt class="xref py py-class docutils literal"><span class="pre">MetaData</span></tt></a>-specific operations. Such as, to issue
 
254
<a class="reference internal" href="../../core/metadata.html#sqlalchemy.schema.MetaData" title="sqlalchemy.schema.MetaData"><tt class="xref py py-class docutils literal"><span class="pre">MetaData</span></tt></a>-specific operations. Such as, to issue
255
255
CREATE statements for all tables:</p>
256
256
<div class="highlight-python"><div class="highlight"><pre><span class="n">engine</span> <span class="o">=</span> <span class="n">create_engine</span><span class="p">(</span><span class="s">&#39;sqlite://&#39;</span><span class="p">)</span>
257
257
<span class="n">Base</span><span class="o">.</span><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></pre></div>
258
258
</div>
259
259
<p><a class="reference internal" href="#sqlalchemy.ext.declarative.declarative_base" title="sqlalchemy.ext.declarative.declarative_base"><tt class="xref py py-func docutils literal"><span class="pre">declarative_base()</span></tt></a> can also receive a pre-existing
260
 
<a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.MetaData" title="sqlalchemy.schema.MetaData"><tt class="xref py py-class docutils literal"><span class="pre">MetaData</span></tt></a> object, which allows a
 
260
<a class="reference internal" href="../../core/metadata.html#sqlalchemy.schema.MetaData" title="sqlalchemy.schema.MetaData"><tt class="xref py py-class docutils literal"><span class="pre">MetaData</span></tt></a> object, which allows a
261
261
declarative setup to be associated with an already
262
 
existing traditional collection of <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a>
 
262
existing traditional collection of <a class="reference internal" href="../../core/metadata.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a>
263
263
objects:</p>
264
264
<div class="highlight-python"><div class="highlight"><pre><span class="n">mymetadata</span> <span class="o">=</span> <span class="n">MetaData</span><span class="p">()</span>
265
265
<span class="n">Base</span> <span class="o">=</span> <span class="n">declarative_base</span><span class="p">(</span><span class="n">metadata</span><span class="o">=</span><span class="n">mymetadata</span><span class="p">)</span></pre></div>
304
304
evaluated as Python expressions.  The full namespace available within
305
305
this evaluation includes all classes mapped for this declarative base,
306
306
as well as the contents of the <tt class="docutils literal"><span class="pre">sqlalchemy</span></tt> package, including
307
 
expression functions like <a class="reference internal" href="../../core/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> and
308
 
<a class="reference internal" href="../../core/expression_api.html#sqlalchemy.sql.expression.func" title="sqlalchemy.sql.expression.func"><tt class="xref py py-attr docutils literal"><span class="pre">func</span></tt></a>:</p>
 
307
expression functions like <a class="reference internal" href="../../core/sqlelement.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> and
 
308
<a class="reference internal" href="../../core/sqlelement.html#sqlalchemy.sql.expression.func" title="sqlalchemy.sql.expression.func"><tt class="xref py py-attr docutils literal"><span class="pre">func</span></tt></a>:</p>
309
309
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">User</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
310
310
    <span class="c"># ....</span>
311
311
    <span class="n">addresses</span> <span class="o">=</span> <span class="n">relationship</span><span class="p">(</span><span class="s">&quot;Address&quot;</span><span class="p">,</span>
333
333
                         <span class="n">primaryjoin</span><span class="o">=</span><span class="s">&quot;address.Address.user_id==&quot;</span>
334
334
                                        <span class="s">&quot;User.id&quot;</span><span class="p">)</span></pre></div>
335
335
</div>
336
 
<p class="versionadded">
337
 
<span class="versionmodified">New in version 0.8: </span>module-qualified paths can be used when specifying string arguments
 
336
<div class="versionadded">
 
337
<p><span>New in version 0.8: </span>module-qualified paths can be used when specifying string arguments
338
338
with Declarative, in order to specify specific modules.</p>
 
339
</div>
339
340
<p>Two alternatives also exist to using string-based attributes.  A lambda
340
341
can also be used, which will be evaluated after all mappers have been
341
342
configured:</p>
357
358
with declarative as with traditional mappings. The
358
359
<tt class="docutils literal"><span class="pre">secondary</span></tt> argument to
359
360
<a class="reference internal" href="../relationships.html#sqlalchemy.orm.relationship" title="sqlalchemy.orm.relationship"><tt class="xref py py-func docutils literal"><span class="pre">relationship()</span></tt></a> is as usual passed a
360
 
<a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> object, which is typically declared in the
361
 
traditional way.  The <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> usually shares
362
 
the <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.MetaData" title="sqlalchemy.schema.MetaData"><tt class="xref py py-class docutils literal"><span class="pre">MetaData</span></tt></a> object used by the declarative base:</p>
 
361
<a class="reference internal" href="../../core/metadata.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> object, which is typically declared in the
 
362
traditional way.  The <a class="reference internal" href="../../core/metadata.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> usually shares
 
363
the <a class="reference internal" href="../../core/metadata.html#sqlalchemy.schema.MetaData" title="sqlalchemy.schema.MetaData"><tt class="xref py py-class docutils literal"><span class="pre">MetaData</span></tt></a> object used by the declarative base:</p>
363
364
<div class="highlight-python"><div class="highlight"><pre><span class="n">keywords</span> <span class="o">=</span> <span class="n">Table</span><span class="p">(</span>
364
365
    <span class="s">&#39;keywords&#39;</span><span class="p">,</span> <span class="n">Base</span><span class="o">.</span><span class="n">metadata</span><span class="p">,</span>
365
366
    <span class="n">Column</span><span class="p">(</span><span class="s">&#39;author_id&#39;</span><span class="p">,</span> <span class="n">Integer</span><span class="p">,</span> <span class="n">ForeignKey</span><span class="p">(</span><span class="s">&#39;authors.id&#39;</span><span class="p">)),</span>
380
381
    <span class="n">keywords</span> <span class="o">=</span> <span class="n">relationship</span><span class="p">(</span><span class="s">&quot;Keyword&quot;</span><span class="p">,</span> <span class="n">secondary</span><span class="o">=</span><span class="s">&quot;keywords&quot;</span><span class="p">)</span></pre></div>
381
382
</div>
382
383
<p>As with traditional mapping, its generally not a good idea to use
383
 
a <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> as the &#8220;secondary&#8221; argument which is also mapped to
 
384
a <a class="reference internal" href="../../core/metadata.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> as the &#8220;secondary&#8221; argument which is also mapped to
384
385
a class, unless the <a class="reference internal" href="../relationships.html#sqlalchemy.orm.relationship" title="sqlalchemy.orm.relationship"><tt class="xref py py-func docutils literal"><span class="pre">relationship()</span></tt></a> is declared with <tt class="docutils literal"><span class="pre">viewonly=True</span></tt>.
385
386
Otherwise, the unit-of-work system may attempt duplicate INSERT and
386
387
DELETE statements against the underlying table.</p>
396
397
arguments are specified using the <tt class="docutils literal"><span class="pre">__table_args__</span></tt> class attribute.
397
398
This attribute accommodates both positional as well as keyword
398
399
arguments that are normally sent to the
399
 
<a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> constructor.
 
400
<a class="reference internal" href="../../core/metadata.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> constructor.
400
401
The attribute can be specified in one of two forms. One is as a
401
402
dictionary:</p>
402
403
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">MyClass</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
426
427
<div class="section" id="using-a-hybrid-approach-with-table">
427
428
<h2>Using a Hybrid Approach with __table__<a class="headerlink" href="#using-a-hybrid-approach-with-table" title="Permalink to this headline">¶</a></h2>
428
429
<p>As an alternative to <tt class="docutils literal"><span class="pre">__tablename__</span></tt>, a direct
429
 
<a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> construct may be used.  The
430
 
<a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a> objects, which in this case require
 
430
<a class="reference internal" href="../../core/metadata.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> construct may be used.  The
 
431
<a class="reference internal" href="../../core/metadata.html#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a> objects, which in this case require
431
432
their names, will be added to the mapping just like a regular mapping
432
433
to a table:</p>
433
434
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">MyClass</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
453
454
</div>
454
455
<p>Some configuration schemes may find it more appropriate to use <tt class="docutils literal"><span class="pre">__table__</span></tt>,
455
456
such as those which already take advantage of the data-driven nature of
456
 
<a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> to customize and/or automate schema definition.</p>
 
457
<a class="reference internal" href="../../core/metadata.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> to customize and/or automate schema definition.</p>
457
458
<p>Note that when the <tt class="docutils literal"><span class="pre">__table__</span></tt> approach is used, the object is immediately
458
 
usable as a plain <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> within the class declaration body itself,
 
459
usable as a plain <a class="reference internal" href="../../core/metadata.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> within the class declaration body itself,
459
460
as a Python class is only another syntactical block.  Below this is illustrated
460
461
by using the <tt class="docutils literal"><span class="pre">id</span></tt> column in the <tt class="docutils literal"><span class="pre">primaryjoin</span></tt> condition of a
461
462
<a class="reference internal" href="../relationships.html#sqlalchemy.orm.relationship" title="sqlalchemy.orm.relationship"><tt class="xref py py-func docutils literal"><span class="pre">relationship()</span></tt></a>:</p>
488
489
</div>
489
490
<div class="section" id="using-reflection-with-declarative">
490
491
<h2>Using Reflection with Declarative<a class="headerlink" href="#using-reflection-with-declarative" title="Permalink to this headline">¶</a></h2>
491
 
<p>It&#8217;s easy to set up a <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> that uses <tt class="docutils literal"><span class="pre">autoload=True</span></tt>
 
492
<p>It&#8217;s easy to set up a <a class="reference internal" href="../../core/metadata.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> that uses <tt class="docutils literal"><span class="pre">autoload=True</span></tt>
492
493
in conjunction with a mapped class:</p>
493
494
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">MyClass</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
494
495
    <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">Base</span><span class="o">.</span><span class="n">metadata</span><span class="p">,</span>
517
518
 
518
519
<span class="n">Base</span><span class="o">.</span><span class="n">prepare</span><span class="p">(</span><span class="n">e</span><span class="p">)</span></pre></div>
519
520
</div>
520
 
<p class="versionadded">
521
 
<span class="versionmodified">New in version 0.8: </span>Added <a class="reference internal" href="#sqlalchemy.ext.declarative.DeferredReflection" title="sqlalchemy.ext.declarative.DeferredReflection"><tt class="xref py py-class docutils literal"><span class="pre">DeferredReflection</span></tt></a>.</p>
 
521
<div class="versionadded">
 
522
<p><span>New in version 0.8: </span>Added <a class="reference internal" href="#sqlalchemy.ext.declarative.DeferredReflection" title="sqlalchemy.ext.declarative.DeferredReflection"><tt class="xref py py-class docutils literal"><span class="pre">DeferredReflection</span></tt></a>.</p>
 
523
</div>
522
524
</div>
523
525
<div class="section" id="mapper-configuration">
524
526
<h2>Mapper Configuration<a class="headerlink" href="#mapper-configuration" title="Permalink to this headline">¶</a></h2>
577
579
                                                <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
578
580
    <span class="n">primary_language</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">String</span><span class="p">(</span><span class="mi">50</span><span class="p">))</span></pre></div>
579
581
</div>
580
 
<p class="versionchanged">
581
 
<span class="versionmodified">Changed in version 0.7: </span>joined table inheritance favors the subclass
 
582
<div class="versionchanged">
 
583
<p><span>Changed in version 0.7: </span>joined table inheritance favors the subclass
582
584
column over that of the superclass, such as querying above
583
585
for <tt class="docutils literal"><span class="pre">Engineer.id</span></tt>.  Prior to 0.7 this was the reverse.</p>
584
586
</div>
 
587
</div>
585
588
<div class="section" id="single-table-inheritance">
586
589
<span id="declarative-single-table"></span><h3>Single Table Inheritance<a class="headerlink" href="#single-table-inheritance" title="Permalink to this headline">¶</a></h3>
587
590
<p>Single table inheritance is defined as a subclass that does not have
645
648
<p>In a situation like this, Declarative can&#8217;t be sure
646
649
of the intent, especially if the <tt class="docutils literal"><span class="pre">start_date</span></tt> columns had, for example,
647
650
different types.   A situation like this can be resolved by using
648
 
<a class="reference internal" href="#sqlalchemy.ext.declarative.declared_attr" title="sqlalchemy.ext.declarative.declared_attr"><tt class="xref py py-class docutils literal"><span class="pre">declared_attr</span></tt></a> to define the <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a> conditionally, taking
 
651
<a class="reference internal" href="#sqlalchemy.ext.declarative.declared_attr" title="sqlalchemy.ext.declarative.declared_attr"><tt class="xref py py-class docutils literal"><span class="pre">declared_attr</span></tt></a> to define the <a class="reference internal" href="../../core/metadata.html#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a> conditionally, taking
649
652
care to return the <strong>existing column</strong> via the parent <tt class="docutils literal"><span class="pre">__table__</span></tt> if it
650
653
already exists:</p>
651
654
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">sqlalchemy.ext.declarative</span> <span class="kn">import</span> <span class="n">declared_attr</span>
674
677
</div>
675
678
<p>Above, when <tt class="docutils literal"><span class="pre">Manager</span></tt> is mapped, the <tt class="docutils literal"><span class="pre">start_date</span></tt> column is
676
679
already present on the <tt class="docutils literal"><span class="pre">Person</span></tt> class.  Declarative lets us return
677
 
that <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a> as a result in this case, where it knows to skip
 
680
that <a class="reference internal" href="../../core/metadata.html#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a> as a result in this case, where it knows to skip
678
681
re-assigning the same column. If the mapping is mis-configured such
679
682
that the <tt class="docutils literal"><span class="pre">start_date</span></tt> column is accidentally re-assigned to a
680
683
different table (such as, if we changed <tt class="docutils literal"><span class="pre">Manager</span></tt> to be joined
681
684
inheritance without fixing <tt class="docutils literal"><span class="pre">start_date</span></tt>), an error is raised which
682
 
indicates an existing <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a> is trying to be re-assigned to
683
 
a different owning <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a>.</p>
684
 
<p class="versionadded">
685
 
<span class="versionmodified">New in version 0.8: </span><a class="reference internal" href="#sqlalchemy.ext.declarative.declared_attr" title="sqlalchemy.ext.declarative.declared_attr"><tt class="xref py py-class docutils literal"><span class="pre">declared_attr</span></tt></a> can be used on a non-mixin
686
 
class, and the returned <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a> or other mapped attribute
 
685
indicates an existing <a class="reference internal" href="../../core/metadata.html#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a> is trying to be re-assigned to
 
686
a different owning <a class="reference internal" href="../../core/metadata.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a>.</p>
 
687
<div class="versionadded">
 
688
<p><span>New in version 0.8: </span><a class="reference internal" href="#sqlalchemy.ext.declarative.declared_attr" title="sqlalchemy.ext.declarative.declared_attr"><tt class="xref py py-class docutils literal"><span class="pre">declared_attr</span></tt></a> can be used on a non-mixin
 
689
class, and the returned <a class="reference internal" href="../../core/metadata.html#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a> or other mapped attribute
687
690
will be applied to the mapping as any other attribute.  Previously,
688
691
the resulting attribute would be ignored, and also result in a warning
689
692
being emitted when a subclass was created.</p>
690
 
<p class="versionadded">
691
 
<span class="versionmodified">New in version 0.8: </span><a class="reference internal" href="#sqlalchemy.ext.declarative.declared_attr" title="sqlalchemy.ext.declarative.declared_attr"><tt class="xref py py-class docutils literal"><span class="pre">declared_attr</span></tt></a>, when used either with a
 
693
</div>
 
694
<div class="versionadded">
 
695
<p><span>New in version 0.8: </span><a class="reference internal" href="#sqlalchemy.ext.declarative.declared_attr" title="sqlalchemy.ext.declarative.declared_attr"><tt class="xref py py-class docutils literal"><span class="pre">declared_attr</span></tt></a>, when used either with a
692
696
mixin or non-mixin declarative class, can return an existing
693
 
<a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a> already assigned to the parent <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a>,
694
 
to indicate that the re-assignment of the <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a> should be
 
697
<a class="reference internal" href="../../core/metadata.html#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a> already assigned to the parent <a class="reference internal" href="../../core/metadata.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a>,
 
698
to indicate that the re-assignment of the <a class="reference internal" href="../../core/metadata.html#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a> should be
695
699
skipped, however should still be mapped on the target class,
696
700
in order to resolve duplicate column conflicts.</p>
 
701
</div>
697
702
<p>The same concept can be used with mixin classes (see
698
703
<a class="reference internal" href="#declarative-mixins"><em>Mixin and Custom Base Classes</em></a>):</p>
699
704
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Person</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
717
722
Because we&#8217;re using single table inheritance, we&#8217;re sure that in this case,
718
723
<tt class="docutils literal"><span class="pre">cls.__table__</span></tt> refers to <tt class="docutils literal"><span class="pre">People.__table__</span></tt>.  If we were mixing joined-
719
724
and single-table inheritance, we might want our mixin to check more carefully
720
 
if <tt class="docutils literal"><span class="pre">cls.__table__</span></tt> is really the <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> we&#8217;re looking for.</p>
 
725
if <tt class="docutils literal"><span class="pre">cls.__table__</span></tt> is really the <a class="reference internal" href="../../core/metadata.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> we&#8217;re looking for.</p>
721
726
</div>
722
727
</div>
723
728
<div class="section" id="concrete-table-inheritance">
738
743
</div>
739
744
<p>Usage of an abstract base class is a little less straightforward as it
740
745
requires usage of <a class="reference internal" href="../mapper_config.html#sqlalchemy.orm.util.polymorphic_union" title="sqlalchemy.orm.util.polymorphic_union"><tt class="xref py py-func docutils literal"><span class="pre">polymorphic_union()</span></tt></a>,
741
 
which needs to be created with the <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> objects
 
746
which needs to be created with the <a class="reference internal" href="../../core/metadata.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> objects
742
747
before the class is built:</p>
743
748
<div class="highlight-python"><div class="highlight"><pre><span class="n">engineers</span> <span class="o">=</span> <span class="n">Table</span><span class="p">(</span><span class="s">&#39;engineers&#39;</span><span class="p">,</span> <span class="n">Base</span><span class="o">.</span><span class="n">metadata</span><span class="p">,</span>
744
749
                <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>
773
778
<p>Helper classes provides a simpler pattern for concrete inheritance.
774
779
With these objects, the <tt class="docutils literal"><span class="pre">__declare_last__</span></tt> helper is used to configure the
775
780
&#8220;polymorphic&#8221; loader for the mapper after all subclasses have been declared.</p>
776
 
<p class="versionadded">
777
 
<span class="versionmodified">New in version 0.7.3.</span></p>
 
781
<div class="versionadded">
 
782
<p><span>New in version 0.7.3.</span></p>
 
783
</div>
778
784
<p>An abstract base can be declared using the
779
785
<a class="reference internal" href="#sqlalchemy.ext.declarative.AbstractConcreteBase" title="sqlalchemy.ext.declarative.AbstractConcreteBase"><tt class="xref py py-class docutils literal"><span class="pre">AbstractConcreteBase</span></tt></a> class:</p>
780
786
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">sqlalchemy.ext.declarative</span> <span class="kn">import</span> <span class="n">AbstractConcreteBase</span>
910
916
and <tt class="docutils literal"><span class="pre">b.c.id</span></tt> are two distinct Python objects, referencing their
911
917
parent tables <tt class="docutils literal"><span class="pre">a</span></tt> and <tt class="docutils literal"><span class="pre">b</span></tt> respectively.</p>
912
918
<p>In the case of the mixin column, it seems that only one
913
 
<a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a> object is explicitly created, yet the ultimate
 
919
<a class="reference internal" href="../../core/metadata.html#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a> object is explicitly created, yet the ultimate
914
920
<tt class="docutils literal"><span class="pre">created_at</span></tt> column above must exist as a distinct Python object
915
921
for each separate destination class.  To accomplish this, the declarative
916
 
extension creates a <strong>copy</strong> of each <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a> object encountered on
 
922
extension creates a <strong>copy</strong> of each <a class="reference internal" href="../../core/metadata.html#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a> object encountered on
917
923
a class that is detected as a mixin.</p>
918
924
<p>This copy mechanism is limited to simple columns that have no foreign
919
 
keys, as a <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.ForeignKey" title="sqlalchemy.schema.ForeignKey"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></tt></a> itself contains references to columns
 
925
keys, as a <a class="reference internal" href="../../core/constraints.html#sqlalchemy.schema.ForeignKey" title="sqlalchemy.schema.ForeignKey"><tt class="xref py py-class docutils literal"><span class="pre">ForeignKey</span></tt></a> itself contains references to columns
920
926
which can&#8217;t be properly recreated at this level.  For columns that
921
927
have foreign keys, as well as for the variety of mapper-level constructs
922
928
that require destination-explicit context, the
935
941
</div>
936
942
<p>Where above, the <tt class="docutils literal"><span class="pre">address_id</span></tt> class-level callable is executed at the
937
943
point at which the <tt class="docutils literal"><span class="pre">User</span></tt> class is constructed, and the declarative
938
 
extension can use the resulting <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a> object as returned by
 
944
extension can use the resulting <a class="reference internal" href="../../core/metadata.html#sqlalchemy.schema.Column" title="sqlalchemy.schema.Column"><tt class="xref py py-class docutils literal"><span class="pre">Column</span></tt></a> object as returned by
939
945
the method without the need to copy it.</p>
940
 
<p class="versionchanged">
941
 
<span class="versionmodified">Changed in version >: </span>0.6.5
 
946
<div class="versionchanged">
 
947
<p><span>Changed in version &gt;: </span>0.6.5
942
948
Rename 0.6.5 <tt class="docutils literal"><span class="pre">sqlalchemy.util.classproperty</span></tt>
943
949
into <a class="reference internal" href="#sqlalchemy.ext.declarative.declared_attr" title="sqlalchemy.ext.declarative.declared_attr"><tt class="xref py py-class docutils literal"><span class="pre">declared_attr</span></tt></a>.</p>
 
950
</div>
944
951
<p>Columns generated by <a class="reference internal" href="#sqlalchemy.ext.declarative.declared_attr" title="sqlalchemy.ext.declarative.declared_attr"><tt class="xref py py-class docutils literal"><span class="pre">declared_attr</span></tt></a> can also be
945
952
referenced by <tt class="docutils literal"><span class="pre">__mapper_args__</span></tt> to a limited degree, currently
946
953
by <tt class="docutils literal"><span class="pre">polymorphic_on</span></tt> and <tt class="docutils literal"><span class="pre">version_id_col</span></tt>, by specifying the
1071
1078
</div>
1072
1079
<p>Above, the <tt class="docutils literal"><span class="pre">HasStringCollection</span></tt> mixin produces a <a class="reference internal" href="../relationships.html#sqlalchemy.orm.relationship" title="sqlalchemy.orm.relationship"><tt class="xref py py-func docutils literal"><span class="pre">relationship()</span></tt></a>
1073
1080
which refers to a newly generated class called <tt class="docutils literal"><span class="pre">StringAttribute</span></tt>.  The
1074
 
<tt class="docutils literal"><span class="pre">StringAttribute</span></tt> class is generated with it&#8217;s own <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a>
 
1081
<tt class="docutils literal"><span class="pre">StringAttribute</span></tt> class is generated with it&#8217;s own <a class="reference internal" href="../../core/metadata.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a>
1075
1082
definition which is local to the parent class making usage of the
1076
1083
<tt class="docutils literal"><span class="pre">HasStringCollection</span></tt> mixin.  It also produces an <a class="reference internal" href="associationproxy.html#sqlalchemy.ext.associationproxy.association_proxy" title="sqlalchemy.ext.associationproxy.association_proxy"><tt class="xref py py-func docutils literal"><span class="pre">association_proxy()</span></tt></a>
1077
1084
object which proxies references to the <tt class="docutils literal"><span class="pre">strings</span></tt> attribute onto the <tt class="docutils literal"><span class="pre">value</span></tt>
1092
1099
<a class="reference internal" href="#sqlalchemy.ext.declarative.declared_attr" title="sqlalchemy.ext.declarative.declared_attr"><tt class="xref py py-class docutils literal"><span class="pre">declared_attr</span></tt></a> decorator must be used so that a distinct
1093
1100
<a class="reference internal" href="associationproxy.html#sqlalchemy.ext.associationproxy.association_proxy" title="sqlalchemy.ext.associationproxy.association_proxy"><tt class="xref py py-func docutils literal"><span class="pre">association_proxy()</span></tt></a> object is created for each of the <tt class="docutils literal"><span class="pre">TypeA</span></tt>
1094
1101
and <tt class="docutils literal"><span class="pre">TypeB</span></tt> classes.</p>
1095
 
<p class="versionadded">
1096
 
<span class="versionmodified">New in version 0.8: </span><a class="reference internal" href="#sqlalchemy.ext.declarative.declared_attr" title="sqlalchemy.ext.declarative.declared_attr"><tt class="xref py py-class docutils literal"><span class="pre">declared_attr</span></tt></a> is usable with non-mapped
 
1102
<div class="versionadded">
 
1103
<p><span>New in version 0.8: </span><a class="reference internal" href="#sqlalchemy.ext.declarative.declared_attr" title="sqlalchemy.ext.declarative.declared_attr"><tt class="xref py py-class docutils literal"><span class="pre">declared_attr</span></tt></a> is usable with non-mapped
1097
1104
attributes, including user-defined attributes as well as
1098
1105
<a class="reference internal" href="associationproxy.html#sqlalchemy.ext.associationproxy.association_proxy" title="sqlalchemy.ext.associationproxy.association_proxy"><tt class="xref py py-func docutils literal"><span class="pre">association_proxy()</span></tt></a>.</p>
1099
1106
</div>
 
1107
</div>
1100
1108
<div class="section" id="controlling-table-inheritance-with-mixins">
1101
1109
<h3>Controlling table inheritance with mixins<a class="headerlink" href="#controlling-table-inheritance-with-mixins" title="Permalink to this headline">¶</a></h3>
1102
1110
<p>The <tt class="docutils literal"><span class="pre">__tablename__</span></tt> attribute in conjunction with the hierarchy of
1218
1226
</div>
1219
1227
<div class="section" id="creating-indexes-with-mixins">
1220
1228
<h3>Creating Indexes with Mixins<a class="headerlink" href="#creating-indexes-with-mixins" title="Permalink to this headline">¶</a></h3>
1221
 
<p>To define a named, potentially multicolumn <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Index" title="sqlalchemy.schema.Index"><tt class="xref py py-class docutils literal"><span class="pre">Index</span></tt></a> that applies to all
1222
 
tables derived from a mixin, use the &#8220;inline&#8221; form of <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Index" title="sqlalchemy.schema.Index"><tt class="xref py py-class docutils literal"><span class="pre">Index</span></tt></a> and
 
1229
<p>To define a named, potentially multicolumn <a class="reference internal" href="../../core/constraints.html#sqlalchemy.schema.Index" title="sqlalchemy.schema.Index"><tt class="xref py py-class docutils literal"><span class="pre">Index</span></tt></a> that applies to all
 
1230
tables derived from a mixin, use the &#8220;inline&#8221; form of <a class="reference internal" href="../../core/constraints.html#sqlalchemy.schema.Index" title="sqlalchemy.schema.Index"><tt class="xref py py-class docutils literal"><span class="pre">Index</span></tt></a> and
1223
1231
establish it as part of <tt class="docutils literal"><span class="pre">__table_args__</span></tt>:</p>
1224
1232
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">MyMixin</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
1225
1233
    <span class="n">a</span> <span class="o">=</span>  <span class="n">Column</span><span class="p">(</span><span class="n">Integer</span><span class="p">)</span>
1249
1257
        <span class="s">&quot;&quot;</span>
1250
1258
        <span class="c"># do something with mappings</span></pre></div>
1251
1259
</div>
1252
 
<p class="versionadded">
1253
 
<span class="versionmodified">New in version 0.7.3.</span></p>
 
1260
<div class="versionadded">
 
1261
<p><span>New in version 0.7.3.</span></p>
 
1262
</div>
1254
1263
</div>
1255
1264
<div class="section" id="abstract">
1256
1265
<span id="declarative-abstract"></span><h3><tt class="docutils literal"><span class="pre">__abstract__</span></tt><a class="headerlink" href="#abstract" title="Permalink to this headline">¶</a></h3>
1272
1281
    <span class="s">&quot;&quot;</span></pre></div>
1273
1282
</div>
1274
1283
<p>One possible use of <tt class="docutils literal"><span class="pre">__abstract__</span></tt> is to use a distinct
1275
 
<a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.MetaData" title="sqlalchemy.schema.MetaData"><tt class="xref py py-class docutils literal"><span class="pre">MetaData</span></tt></a> for different bases:</p>
 
1284
<a class="reference internal" href="../../core/metadata.html#sqlalchemy.schema.MetaData" title="sqlalchemy.schema.MetaData"><tt class="xref py py-class docutils literal"><span class="pre">MetaData</span></tt></a> for different bases:</p>
1276
1285
<div class="highlight-python"><div class="highlight"><pre><span class="n">Base</span> <span class="o">=</span> <span class="n">declarative_base</span><span class="p">()</span>
1277
1286
 
1278
1287
<span class="k">class</span> <span class="nc">DefaultBase</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
1284
1293
    <span class="n">metadata</span> <span class="o">=</span> <span class="n">MetaData</span><span class="p">()</span></pre></div>
1285
1294
</div>
1286
1295
<p>Above, classes which inherit from <tt class="docutils literal"><span class="pre">DefaultBase</span></tt> will use one
1287
 
<a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.MetaData" title="sqlalchemy.schema.MetaData"><tt class="xref py py-class docutils literal"><span class="pre">MetaData</span></tt></a> as the registry of tables, and those which inherit from
 
1296
<a class="reference internal" href="../../core/metadata.html#sqlalchemy.schema.MetaData" title="sqlalchemy.schema.MetaData"><tt class="xref py py-class docutils literal"><span class="pre">MetaData</span></tt></a> as the registry of tables, and those which inherit from
1288
1297
<tt class="docutils literal"><span class="pre">OtherBase</span></tt> will use a different one. The tables themselves can then be
1289
1298
created perhaps within distinct databases:</p>
1290
1299
<div class="highlight-python"><div class="highlight"><pre><span class="n">DefaultBase</span><span class="o">.</span><span class="n">metadata</span><span class="o">.</span><span class="n">create_all</span><span class="p">(</span><span class="n">some_engine</span><span class="p">)</span>
1291
1300
<span class="n">OtherBase</span><span class="o">.</span><span class="n">metadata_create_all</span><span class="p">(</span><span class="n">some_other_engine</span><span class="p">)</span></pre></div>
1292
1301
</div>
1293
 
<p class="versionadded">
1294
 
<span class="versionmodified">New in version 0.7.3.</span></p>
 
1302
<div class="versionadded">
 
1303
<p><span>New in version 0.7.3.</span></p>
 
1304
</div>
1295
1305
</div>
1296
1306
</div>
1297
1307
<div class="section" id="class-constructor">
1306
1316
<h2>Sessions<a class="headerlink" href="#sessions" title="Permalink to this headline">¶</a></h2>
1307
1317
<p>Note that <tt class="docutils literal"><span class="pre">declarative</span></tt> does nothing special with sessions, and is
1308
1318
only intended as an easier way to configure mappers and
1309
 
<a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> objects.  A typical application
 
1319
<a class="reference internal" href="../../core/metadata.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> objects.  A typical application
1310
1320
setup using <tt class="xref py py-class docutils literal"><span class="pre">scoped_session</span></tt> might look like:</p>
1311
1321
<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;postgresql://scott:tiger@localhost/test&#39;</span><span class="p">)</span>
1312
1322
<span class="n">Session</span> <span class="o">=</span> <span class="n">scoped_session</span><span class="p">(</span><span class="n">sessionmaker</span><span class="p">(</span><span class="n">autocommit</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span>
1321
1331
<h2>API Reference<a class="headerlink" href="#api-reference" title="Permalink to this headline">¶</a></h2>
1322
1332
<dl class="function">
1323
1333
<dt id="sqlalchemy.ext.declarative.declarative_base">
1324
 
<tt class="descclassname">sqlalchemy.ext.declarative.</tt><tt class="descname">declarative_base</tt><big>(</big><em>bind=None</em>, <em>metadata=None</em>, <em>mapper=None</em>, <em>cls=&lt;type 'object'&gt;</em>, <em>name='Base'</em>, <em>constructor=&lt;function __init__ at 0x10aadd938&gt;</em>, <em>class_registry=None</em>, <em>metaclass=&lt;class 'sqlalchemy.ext.declarative.api.DeclarativeMeta'&gt;</em><big>)</big><a class="headerlink" href="#sqlalchemy.ext.declarative.declarative_base" title="Permalink to this definition">¶</a></dt>
 
1334
<tt class="descclassname">sqlalchemy.ext.declarative.</tt><tt class="descname">declarative_base</tt><big>(</big><em>bind=None</em>, <em>metadata=None</em>, <em>mapper=None</em>, <em>cls=&lt;type 'object'&gt;</em>, <em>name='Base'</em>, <em>constructor=&lt;function __init__ at 0x10d468140&gt;</em>, <em>class_registry=None</em>, <em>metaclass=&lt;class 'sqlalchemy.ext.declarative.api.DeclarativeMeta'&gt;</em><big>)</big><a class="headerlink" href="#sqlalchemy.ext.declarative.declarative_base" title="Permalink to this definition">¶</a></dt>
1325
1335
<dd><p>Construct a base class for declarative class definitions.</p>
1326
1336
<p>The new base class will be given a metaclass that produces
1327
 
appropriate <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> objects and makes
 
1337
appropriate <a class="reference internal" href="../../core/metadata.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> objects and makes
1328
1338
the appropriate <a class="reference internal" href="../mapper_config.html#sqlalchemy.orm.mapper" title="sqlalchemy.orm.mapper"><tt class="xref py py-func docutils literal"><span class="pre">mapper()</span></tt></a> calls based on the
1329
1339
information provided declaratively in the class and any subclasses
1330
1340
of the class.</p>
1338
1348
the <tt class="docutils literal"><span class="pre">bind</span></tt> attribute on the <tt class="xref py py-class docutils literal"><span class="pre">MetaData</span></tt>
1339
1349
instance.</li>
1340
1350
<li><strong>metadata</strong> &#8211; An optional <tt class="xref py py-class docutils literal"><span class="pre">MetaData</span></tt> instance.  All
1341
 
<a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> objects implicitly declared by
 
1351
<a class="reference internal" href="../../core/metadata.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> objects implicitly declared by
1342
1352
subclasses of the base will share this MetaData.  A MetaData instance
1343
1353
will be created if none is provided.  The
1344
1354
<tt class="xref py py-class docutils literal"><span class="pre">MetaData</span></tt> instance will be available via the
1370
1380
</tr>
1371
1381
</tbody>
1372
1382
</table>
 
1383
<div class="admonition seealso">
 
1384
<p class="first admonition-title">See also</p>
 
1385
<p class="last"><a class="reference internal" href="#sqlalchemy.ext.declarative.as_declarative" title="sqlalchemy.ext.declarative.as_declarative"><tt class="xref py py-func docutils literal"><span class="pre">as_declarative()</span></tt></a></p>
 
1386
</div>
 
1387
</dd></dl>
 
1388
 
 
1389
<dl class="function">
 
1390
<dt id="sqlalchemy.ext.declarative.as_declarative">
 
1391
<tt class="descclassname">sqlalchemy.ext.declarative.</tt><tt class="descname">as_declarative</tt><big>(</big><em>**kw</em><big>)</big><a class="headerlink" href="#sqlalchemy.ext.declarative.as_declarative" title="Permalink to this definition">¶</a></dt>
 
1392
<dd><p>Class decorator for <a class="reference internal" href="#sqlalchemy.ext.declarative.declarative_base" title="sqlalchemy.ext.declarative.declarative_base"><tt class="xref py py-func docutils literal"><span class="pre">declarative_base()</span></tt></a>.</p>
 
1393
<p>Provides a syntactical shortcut to the <tt class="docutils literal"><span class="pre">cls</span></tt> argument
 
1394
sent to <a class="reference internal" href="#sqlalchemy.ext.declarative.declarative_base" title="sqlalchemy.ext.declarative.declarative_base"><tt class="xref py py-func docutils literal"><span class="pre">declarative_base()</span></tt></a>, allowing the base class
 
1395
to be converted in-place to a &#8220;declarative&#8221; base:</p>
 
1396
<div class="highlight-python"><pre>from sqlalchemy.ext.declarative import as_declarative
 
1397
 
 
1398
@as_declarative()
 
1399
class Base(object)
 
1400
    @declared_attr
 
1401
    def __tablename__(cls):
 
1402
        return cls.__name__.lower()
 
1403
    id = Column(Integer, primary_key=True)
 
1404
 
 
1405
class MyMappedClass(Base):
 
1406
    # ...</pre>
 
1407
</div>
 
1408
<p>All keyword arguments passed to <a class="reference internal" href="#sqlalchemy.ext.declarative.as_declarative" title="sqlalchemy.ext.declarative.as_declarative"><tt class="xref py py-func docutils literal"><span class="pre">as_declarative()</span></tt></a> are passed
 
1409
along to <a class="reference internal" href="#sqlalchemy.ext.declarative.declarative_base" title="sqlalchemy.ext.declarative.declarative_base"><tt class="xref py py-func docutils literal"><span class="pre">declarative_base()</span></tt></a>.</p>
 
1410
<div class="versionadded">
 
1411
<p><span>New in version 0.8.3.</span></p>
 
1412
</div>
 
1413
<div class="admonition seealso">
 
1414
<p class="first admonition-title">See also</p>
 
1415
<p class="last"><a class="reference internal" href="#sqlalchemy.ext.declarative.declarative_base" title="sqlalchemy.ext.declarative.declarative_base"><tt class="xref py py-func docutils literal"><span class="pre">declarative_base()</span></tt></a></p>
 
1416
</div>
1373
1417
</dd></dl>
1374
1418
 
1375
1419
<dl class="class">
1376
1420
<dt id="sqlalchemy.ext.declarative.declared_attr">
1377
1421
<em class="property">class </em><tt class="descclassname">sqlalchemy.ext.declarative.</tt><tt class="descname">declared_attr</tt><big>(</big><em>fget</em>, <em>*arg</em>, <em>**kw</em><big>)</big><a class="headerlink" href="#sqlalchemy.ext.declarative.declared_attr" title="Permalink to this definition">¶</a></dt>
1378
 
<dd><p>Mark a class-level method as representing the definition of
 
1422
<dd><p>Bases: <tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.orm.interfaces._MappedAttribute</span></tt>, <tt class="xref py py-class docutils literal"><span class="pre">__builtin__.property</span></tt></p>
 
1423
<p>Mark a class-level method as representing the definition of
1379
1424
a mapped property or special declarative member name.</p>
1380
1425
<p>&#64;declared_attr turns the attribute into a scalar-like
1381
1426
property that can be invoked from the uninstantiated class.
1414
1459
        <span class="k">else</span><span class="p">:</span>
1415
1460
            <span class="k">return</span> <span class="p">{</span><span class="s">&quot;polymorphic_identity&quot;</span><span class="p">:</span><span class="n">cls</span><span class="o">.</span><span class="n">__name__</span><span class="p">}</span></pre></div>
1416
1461
</div>
1417
 
<p class="versionchanged">
1418
 
<span class="versionmodified">Changed in version 0.8: </span><a class="reference internal" href="#sqlalchemy.ext.declarative.declared_attr" title="sqlalchemy.ext.declarative.declared_attr"><tt class="xref py py-class docutils literal"><span class="pre">declared_attr</span></tt></a> can be used with
 
1462
<div class="versionchanged">
 
1463
<p><span>Changed in version 0.8: </span><a class="reference internal" href="#sqlalchemy.ext.declarative.declared_attr" title="sqlalchemy.ext.declarative.declared_attr"><tt class="xref py py-class docutils literal"><span class="pre">declared_attr</span></tt></a> can be used with
1419
1464
non-ORM or extension attributes, such as user-defined attributes
1420
1465
or <a class="reference internal" href="associationproxy.html#sqlalchemy.ext.associationproxy.association_proxy" title="sqlalchemy.ext.associationproxy.association_proxy"><tt class="xref py py-func docutils literal"><span class="pre">association_proxy()</span></tt></a> objects, which will be assigned
1421
1466
to the class at class construction time.</p>
 
1467
</div>
1422
1468
</dd></dl>
1423
1469
 
1424
1470
<dl class="function">
1486
1532
<dl class="class">
1487
1533
<dt id="sqlalchemy.ext.declarative.AbstractConcreteBase">
1488
1534
<em class="property">class </em><tt class="descclassname">sqlalchemy.ext.declarative.</tt><tt class="descname">AbstractConcreteBase</tt><a class="headerlink" href="#sqlalchemy.ext.declarative.AbstractConcreteBase" title="Permalink to this definition">¶</a></dt>
1489
 
<dd><p>A helper class for &#8216;concrete&#8217; declarative mappings.</p>
 
1535
<dd><p>Bases: <tt class="xref py py-class docutils literal"><span class="pre">sqlalchemy.ext.declarative.api.ConcreteBase</span></tt></p>
 
1536
<p>A helper class for &#8216;concrete&#8217; declarative mappings.</p>
1490
1537
<p><a class="reference internal" href="#sqlalchemy.ext.declarative.AbstractConcreteBase" title="sqlalchemy.ext.declarative.AbstractConcreteBase"><tt class="xref py py-class docutils literal"><span class="pre">AbstractConcreteBase</span></tt></a> will use the <a class="reference internal" href="../mapper_config.html#sqlalchemy.orm.util.polymorphic_union" title="sqlalchemy.orm.util.polymorphic_union"><tt class="xref py py-func docutils literal"><span class="pre">polymorphic_union()</span></tt></a>
1491
1538
function automatically, against all tables mapped as a subclass
1492
1539
to this class.   The function is called via the
1552
1599
<dd><p>A helper class for construction of mappings based on
1553
1600
a deferred reflection step.</p>
1554
1601
<p>Normally, declarative can be used with reflection by
1555
 
setting a <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> object using autoload=True
 
1602
setting a <a class="reference internal" href="../../core/metadata.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> object using autoload=True
1556
1603
as the <tt class="docutils literal"><span class="pre">__table__</span></tt> attribute on a declarative class.
1557
 
The caveat is that the <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> must be fully
 
1604
The caveat is that the <a class="reference internal" href="../../core/metadata.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a> must be fully
1558
1605
reflected, or at the very least have a primary key column,
1559
1606
at the point at which a normal declarative mapping is
1560
1607
constructed, meaning the <a class="reference internal" href="../../core/connections.html#sqlalchemy.engine.Engine" title="sqlalchemy.engine.Engine"><tt class="xref py py-class docutils literal"><span class="pre">Engine</span></tt></a> must be available
1561
1608
at class declaration time.</p>
1562
1609
<p>The <a class="reference internal" href="#sqlalchemy.ext.declarative.DeferredReflection" title="sqlalchemy.ext.declarative.DeferredReflection"><tt class="xref py py-class docutils literal"><span class="pre">DeferredReflection</span></tt></a> mixin moves the construction
1563
1610
of mappers to be at a later point, after a specific
1564
 
method is called which first reflects all <a class="reference internal" href="../../core/schema.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a>
 
1611
method is called which first reflects all <a class="reference internal" href="../../core/metadata.html#sqlalchemy.schema.Table" title="sqlalchemy.schema.Table"><tt class="xref py py-class docutils literal"><span class="pre">Table</span></tt></a>
1565
1612
objects created so far.   Classes can define it as such:</p>
1566
1613
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">sqlalchemy.ext.declarative</span> <span class="kn">import</span> <span class="n">declarative_base</span>
1567
1614
<span class="kn">from</span> <span class="nn">sqlalchemy.ext.declarative</span> <span class="kn">import</span> <span class="n">DeferredReflection</span>
1607
1654
<div class="highlight-python"><div class="highlight"><pre><span class="n">ReflectedOne</span><span class="o">.</span><span class="n">prepare</span><span class="p">(</span><span class="n">engine_one</span><span class="p">)</span>
1608
1655
<span class="n">ReflectedTwo</span><span class="o">.</span><span class="n">prepare</span><span class="p">(</span><span class="n">engine_two</span><span class="p">)</span></pre></div>
1609
1656
</div>
1610
 
<p class="versionadded">
1611
 
<span class="versionmodified">New in version 0.8.</span></p>
 
1657
<div class="versionadded">
 
1658
<p><span>New in version 0.8.</span></p>
 
1659
</div>
1612
1660
</dd></dl>
1613
1661
 
1614
1662
</div>
1626
1674
 
1627
1675
    <div id="docs-copyright">
1628
1676
        &copy; <a href="../../copyright.html">Copyright</a> 2007-2013, the SQLAlchemy authors and contributors.
1629
 
        Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
 
1677
        Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.2b1.
1630
1678
    </div>
1631
1679
</div>
1632
1680