/SQLAlchemy-0.7.8/doc/orm/inheritance.html
HTML | 752 lines | 675 code | 77 blank | 0 comment | 0 complexity | 8b05ee4e03b6544f02751f3d8ce6cf0f MD5 | raw file
Large files files are truncated, but you can click here to view the full file
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
-
- <title>
-
-
- Mapping Class Inheritance Hierarchies
- —
- SQLAlchemy 0.7 Documentation
- </title>
-
- <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
- <link rel="stylesheet" href="../_static/docs.css" type="text/css" />
- <script type="text/javascript">
- var DOCUMENTATION_OPTIONS = {
- URL_ROOT: '../',
- VERSION: '0.7.8',
- COLLAPSE_MODINDEX: false,
- FILE_SUFFIX: '.html'
- };
- </script>
- <script type="text/javascript" src="../_static/jquery.js"></script>
- <script type="text/javascript" src="../_static/underscore.js"></script>
- <script type="text/javascript" src="../_static/doctools.js"></script>
- <script type="text/javascript" src="../_static/init.js"></script>
- <link rel="index" title="Index" href="../genindex.html" />
- <link rel="search" title="Search" href="../search.html" />
- <link rel="copyright" title="Copyright" href="../copyright.html" />
- <link rel="top" title="SQLAlchemy 0.7 Documentation" href="../index.html" />
- <link rel="up" title="SQLAlchemy ORM" href="index.html" />
- <link rel="next" title="Using the Session" href="session.html" />
- <link rel="prev" title="Collection Configuration and Techniques" href="collections.html" />
- </head>
- <body>
-
- <div id="docs-container">
- <div id="docs-header">
- <h1>SQLAlchemy 0.7 Documentation</h1>
- <div id="docs-search">
- Search:
- <form class="search" action="../search.html" method="get">
- <input type="text" name="q" size="18" /> <input type="submit" value="Search" />
- <input type="hidden" name="check_keywords" value="yes" />
- <input type="hidden" name="area" value="default" />
- </form>
- </div>
- <div id="docs-version-header">
- Release: <span class="version-num">0.7.8</span> | Release Date: June 16, 2012
- </div>
- </div>
- <div id="docs-top-navigation">
- <div id="docs-top-page-control" class="docs-navigation-links">
- <ul>
- <li>Prev:
- <a href="collections.html" title="previous chapter">Collection Configuration and Techniques</a>
- </li>
- <li>Next:
- <a href="session.html" title="next chapter">Using the Session</a>
- </li>
- <li>
- <a href="../contents.html">Table of Contents</a> |
- <a href="../genindex.html">Index</a>
- | <a href="../_sources/orm/inheritance.txt">view source
- </li>
- </ul>
- </div>
- <div id="docs-navigation-banner">
- <a href="../index.html">SQLAlchemy 0.7 Documentation</a>
- ť <a href="index.html" title="SQLAlchemy ORM">SQLAlchemy ORM</a>
- ť
- Mapping Class Inheritance Hierarchies
-
- <h2>
-
- Mapping Class Inheritance Hierarchies
-
- </h2>
- </div>
- </div>
- <div id="docs-body-container">
- <div id="docs-sidebar">
- <h3><a href="../index.html">Table of Contents</a></h3>
- <ul>
- <li><a class="reference internal" href="#">Mapping Class Inheritance Hierarchies</a><ul>
- <li><a class="reference internal" href="#joined-table-inheritance">Joined Table Inheritance</a><ul>
- <li><a class="reference internal" href="#basic-control-of-which-tables-are-queried">Basic Control of Which Tables are Queried</a></li>
- <li><a class="reference internal" href="#advanced-control-of-which-tables-are-queried">Advanced Control of Which Tables are Queried</a></li>
- <li><a class="reference internal" href="#creating-joins-to-specific-subtypes">Creating Joins to Specific Subtypes</a></li>
- </ul>
- </li>
- <li><a class="reference internal" href="#single-table-inheritance">Single Table Inheritance</a></li>
- <li><a class="reference internal" href="#concrete-table-inheritance">Concrete Table Inheritance</a><ul>
- <li><a class="reference internal" href="#concrete-inheritance-with-declarative">Concrete Inheritance with Declarative</a></li>
- </ul>
- </li>
- <li><a class="reference internal" href="#using-relationships-with-inheritance">Using Relationships with Inheritance</a><ul>
- <li><a class="reference internal" href="#relationships-with-concrete-inheritance">Relationships with Concrete Inheritance</a></li>
- </ul>
- </li>
- <li><a class="reference internal" href="#using-inheritance-with-declarative">Using Inheritance with Declarative</a></li>
- </ul>
- </li>
- </ul>
- <h4>Previous Topic</h4>
- <p>
- <a href="collections.html" title="previous chapter">Collection Configuration and Techniques</a>
- </p>
- <h4>Next Topic</h4>
- <p>
- <a href="session.html" title="next chapter">Using the Session</a>
- </p>
- <h4>Quick Search</h4>
- <p>
- <form class="search" action="../search.html" method="get">
- <input type="text" name="q" size="18" /> <input type="submit" value="Search" />
- <input type="hidden" name="check_keywords" value="yes" />
- <input type="hidden" name="area" value="default" />
- </form>
- </p>
- </div>
- <div id="docs-body" class="withsidebar" >
-
- <div class="section" id="mapping-class-inheritance-hierarchies">
- <span id="inheritance-toplevel"></span><h1>Mapping Class Inheritance Hierarchies<a class="headerlink" href="#mapping-class-inheritance-hierarchies" title="Permalink to this headline">ś</a></h1>
- <p>SQLAlchemy supports three forms of inheritance: <em>single table inheritance</em>,
- where several types of classes are stored in one table, <em>concrete table
- inheritance</em>, where each type of class is stored in its own table, and <em>joined
- table inheritance</em>, where the parent/child classes are stored in their own
- tables that are joined together in a select. Whereas support for single and
- joined table inheritance is strong, concrete table inheritance is a less
- common scenario with some particular problems so is not quite as flexible.</p>
- <p>When mappers are configured in an inheritance relationship, SQLAlchemy has the
- ability to load elements “polymorphically”, meaning that a single query can
- return objects of multiple types.</p>
- <div class="admonition note">
- <p class="first admonition-title">Note</p>
- <p class="last">This section currently uses classical mappings to illustrate inheritance
- configurations, and will soon be updated to standardize on Declarative.
- Until then, please refer to <a class="reference internal" href="extensions/declarative.html#declarative-inheritance"><em>Inheritance Configuration</em></a> for information on
- how common inheritance mappings are constructed declaratively.</p>
- </div>
- <p>For the following sections, assume this class relationship:</p>
- <div class="highlight-python+sql"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Employee</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
- <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">name</span>
- <span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
- <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">__class__</span><span class="o">.</span><span class="n">__name__</span> <span class="o">+</span> <span class="s">" "</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span>
- <span class="k">class</span> <span class="nc">Manager</span><span class="p">(</span><span class="n">Employee</span><span class="p">):</span>
- <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">manager_data</span><span class="p">):</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">name</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">manager_data</span> <span class="o">=</span> <span class="n">manager_data</span>
- <span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
- <span class="k">return</span> <span class="p">(</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">__class__</span><span class="o">.</span><span class="n">__name__</span> <span class="o">+</span> <span class="s">" "</span> <span class="o">+</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">+</span> <span class="s">" "</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">manager_data</span>
- <span class="p">)</span>
- <span class="k">class</span> <span class="nc">Engineer</span><span class="p">(</span><span class="n">Employee</span><span class="p">):</span>
- <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">engineer_info</span><span class="p">):</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">name</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">engineer_info</span> <span class="o">=</span> <span class="n">engineer_info</span>
- <span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
- <span class="k">return</span> <span class="p">(</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">__class__</span><span class="o">.</span><span class="n">__name__</span> <span class="o">+</span> <span class="s">" "</span> <span class="o">+</span>
- <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">+</span> <span class="s">" "</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">engineer_info</span>
- <span class="p">)</span></pre></div>
- </div>
- <div class="section" id="joined-table-inheritance">
- <h2>Joined Table Inheritance<a class="headerlink" href="#joined-table-inheritance" title="Permalink to this headline">ś</a></h2>
- <p>In joined table inheritance, each class along a particular classes’ list of
- parents is represented by a unique table. The total set of attributes for a
- particular instance is represented as a join along all tables in its
- inheritance path. Here, we first define a table to represent the <tt class="docutils literal"><span class="pre">Employee</span></tt>
- class. This table will contain a primary key column (or columns), and a column
- for each attribute that’s represented by <tt class="docutils literal"><span class="pre">Employee</span></tt>. In this case it’s just
- <tt class="docutils literal"><span class="pre">name</span></tt>:</p>
- <div class="highlight-python"><div class="highlight"><pre><span class="n">employees</span> <span class="o">=</span> <span class="n">Table</span><span class="p">(</span><span class="s">'employees'</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">'employee_id'</span><span class="p">,</span> <span class="n">Integer</span><span class="p">,</span> <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">),</span>
- <span class="n">Column</span><span class="p">(</span><span class="s">'name'</span><span class="p">,</span> <span class="n">String</span><span class="p">(</span><span class="mi">50</span><span class="p">)),</span>
- <span class="n">Column</span><span class="p">(</span><span class="s">'type'</span><span class="p">,</span> <span class="n">String</span><span class="p">(</span><span class="mi">30</span><span class="p">),</span> <span class="n">nullable</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
- <span class="p">)</span></pre></div>
- </div>
- <p>The table also has a column called <tt class="docutils literal"><span class="pre">type</span></tt>. It is strongly advised in both
- single- and joined- table inheritance scenarios that the root table contains a
- column whose sole purpose is that of the <strong>discriminator</strong>; it stores a value
- which indicates the type of object represented within the row. The column may
- be of any desired datatype. While there are some “tricks” to work around the
- requirement that there be a discriminator column, they are more complicated to
- configure when one wishes to load polymorphically.</p>
- <p>Next we define individual tables for each of <tt class="docutils literal"><span class="pre">Engineer</span></tt> and <tt class="docutils literal"><span class="pre">Manager</span></tt>,
- which contain columns that represent the attributes unique to the subclass
- they represent. Each table also must contain a primary key column (or
- columns), and in most cases a foreign key reference to the parent table. It is
- standard practice that the same column is used for both of these roles, and
- that the column is also named the same as that of the parent table. However
- this is optional in SQLAlchemy; separate columns may be used for primary key
- and parent-relationship, the column may be named differently than that of the
- parent, and even a custom join condition can be specified between parent and
- child tables instead of using a foreign key:</p>
- <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">'engineers'</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">'employee_id'</span><span class="p">,</span> <span class="n">Integer</span><span class="p">,</span>
- <span class="n">ForeignKey</span><span class="p">(</span><span class="s">'employees.employee_id'</span><span class="p">),</span>
- <span class="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">),</span>
- <span class="n">Column</span><span class="p">(</span><span class="s">'engineer_info'</span><span class="p">,</span> <span class="n">String</span><span class="p">(</span><span class="mi">50</span><span class="p">)),</span>
- <span class="p">)</span>
- <span class="n">managers</span> <span class="o">=</span> <span class="n">Table</span><span class="p">(</span><span class="s">'managers'</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">'employee_id'</span><span class="p">,</span> <span class="n">Integer</span><span class="p">,</span>
-