~ubuntu-branches/debian/sid/sqlalchemy/sid

« back to all changes in this revision

Viewing changes to doc/orm/relationships.html

  • Committer: Package Import Robot
  • Author(s): Piotr Ożarowski
  • Date: 2014-06-27 20:17:13 UTC
  • mfrom: (1.4.28)
  • Revision ID: package-import@ubuntu.com-20140627201713-g6p1kq8q1qenztrv
Tags: 0.9.6-1
* New upstream release
* Remove Python 3.X build tag files, thanks to Matthias Urlichs for the
  patch (closes: #747852)
* python-fdb isn't in the Debian archive yet so default dialect for firebird://
  URLs is changed to obsolete kinterbasdb, thanks to Russell Stuart for the
  patch (closes: #752145)

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
    <script type="text/javascript">
33
33
      var DOCUMENTATION_OPTIONS = {
34
34
          URL_ROOT:    '../',
35
 
          VERSION:     '0.9.4',
 
35
          VERSION:     '0.9.6',
36
36
          COLLAPSE_MODINDEX: false,
37
37
          FILE_SUFFIX: '.html'
38
38
      };
44
44
        <script type="text/javascript" src="../_static/doctools.js"></script>
45
45
    <!-- end iterate through sphinx environment script_files -->
46
46
 
 
47
    <script type="text/javascript" src="../_static/detectmobile.js"></script>
47
48
    <script type="text/javascript" src="../_static/init.js"></script>
48
49
    <link rel="index" title="Index" href="../genindex.html" />
49
50
    <link rel="search" title="Search" href="../search.html" />
69
70
 
70
71
 
71
72
 
 
73
 
72
74
<div id="docs-container">
73
75
 
74
76
 
75
77
 
76
78
 
 
79
 
 
80
<div id="docs-top-navigation-container" class="body-background">
77
81
<div id="docs-header">
 
82
    <div id="docs-version-header">
 
83
        Release: <span class="version-num">0.9.6</span> | Release Date: June 23, 2014
 
84
    </div>
 
85
 
78
86
    <h1>SQLAlchemy 0.9 Documentation</h1>
79
87
 
80
 
    <div id="docs-search">
81
 
    Search:
82
 
    <form class="search" action="../search.html" method="get">
83
 
      <input type="text" name="q" size="18" /> <input type="submit" value="Search" />
84
 
      <input type="hidden" name="check_keywords" value="yes" />
85
 
      <input type="hidden" name="area" value="default" />
86
 
    </form>
87
 
    </div>
88
 
 
89
 
    <div id="docs-version-header">
90
 
        Release: <span class="version-num">0.9.4</span> | Release Date: March 28, 2014
91
 
 
92
 
 
93
 
    </div>
94
 
 
95
 
</div>
96
 
 
97
 
<div id="docs-top-navigation">
98
 
    <div id="docs-top-page-control" class="docs-navigation-links">
 
88
</div>
 
89
</div>
 
90
 
 
91
<div id="docs-body-container">
 
92
 
 
93
    <div id="fixed-sidebar" class="withsidebar">
 
94
 
 
95
 
 
96
        <div id="docs-sidebar-popout">
 
97
            <h3><a href="../index.html">SQLAlchemy 0.9 Documentation</a></h3>
 
98
 
 
99
            <p id="sidebar-paginate">
 
100
                    <a href="index.html" title="SQLAlchemy ORM">Up</a> |
 
101
 
 
102
                    <a href="mapper_config.html" title="Mapper Configuration">Prev</a> |
 
103
                    <a href="collections.html" title="Collection Configuration and Techniques">Next</a>
 
104
            </p>
 
105
 
 
106
            <p id="sidebar-topnav">
 
107
                <a href="../index.html">Contents</a> |
 
108
                <a href="../genindex.html">Index</a>
 
109
            </p>
 
110
 
 
111
            <div id="sidebar-search">
 
112
                <form class="search" action="../search.html" method="get">
 
113
                  <input type="text" name="q" size="12" /> <input type="submit" value="Search" />
 
114
                  <input type="hidden" name="check_keywords" value="yes" />
 
115
                  <input type="hidden" name="area" value="default" />
 
116
                </form>
 
117
            </div>
 
118
 
 
119
        </div>
 
120
 
 
121
        <div id="docs-sidebar">
 
122
 
 
123
        <h3><a href="#">            
 
124
                Relationship Configuration
 
125
            
 
126
        </a></h3>
99
127
        <ul>
100
 
            <li>Prev:
101
 
            <a href="mapper_config.html" title="previous chapter">Mapper Configuration</a>
102
 
            </li>
103
 
            <li>Next:
104
 
            <a href="collections.html" title="next chapter">Collection Configuration and Techniques</a>
105
 
            </li>
106
 
 
107
 
        <li>
108
 
            <a href="../contents.html">Table of Contents</a> |
109
 
            <a href="../genindex.html">Index</a>
110
 
            | <a href="../_sources/orm/relationships.txt">view source
111
 
        </li>
112
 
        </ul>
113
 
    </div>
114
 
 
115
 
    <div id="docs-navigation-banner">
116
 
        <a href="../index.html">SQLAlchemy 0.9 Documentation</a>
117
 
                » <a href="index.html" title="SQLAlchemy ORM">SQLAlchemy ORM</a>
118
 
        » 
119
 
                Relationship Configuration
120
 
            
121
 
 
122
 
        <h2>
123
 
            
124
 
                Relationship Configuration
125
 
            
126
 
        </h2>
127
 
    </div>
128
 
 
129
 
</div>
130
 
 
131
 
<div id="docs-body-container">
132
 
 
133
 
    <div id="docs-sidebar">
134
 
    <h3><a href="../index.html">Table of Contents</a></h3>
135
 
    <ul>
136
128
<li><a class="reference internal" href="#">Relationship Configuration</a><ul>
137
129
<li><a class="reference internal" href="#basic-relational-patterns">Basic Relational Patterns</a><ul>
138
130
<li><a class="reference internal" href="#one-to-many">One To Many</a></li>
161
153
<li><a class="reference internal" href="#specifying-alternate-join-conditions">Specifying Alternate Join Conditions</a></li>
162
154
<li><a class="reference internal" href="#creating-custom-foreign-conditions">Creating Custom Foreign Conditions</a></li>
163
155
<li><a class="reference internal" href="#using-custom-operators-in-join-conditions">Using custom operators in join conditions</a></li>
 
156
<li><a class="reference internal" href="#non-relational-comparisons-materialized-path">Non-relational Comparisons / Materialized Path</a></li>
164
157
<li><a class="reference internal" href="#self-referential-many-to-many-relationship">Self-Referential Many-to-Many Relationship</a></li>
165
158
<li><a class="reference internal" href="#composite-secondary-joins">Composite &#8220;Secondary&#8221; Joins</a></li>
166
159
<li><a class="reference internal" href="#relationship-to-non-primary-mapper">Relationship to Non Primary Mapper</a></li>
175
168
</ul>
176
169
 
177
170
 
178
 
    <h4>Previous Topic</h4>
179
 
    <p>
180
 
    <a href="mapper_config.html" title="previous chapter">Mapper Configuration</a>
181
 
    </p>
182
 
    <h4>Next Topic</h4>
183
 
    <p>
184
 
    <a href="collections.html" title="next chapter">Collection Configuration and Techniques</a>
185
 
    </p>
186
 
 
187
 
 
188
 
    <h4>Quick Search</h4>
189
 
    <p>
190
 
    <form class="search" action="../search.html" method="get">
191
 
      <input type="text" name="q" size="18" /> <input type="submit" value="Search" />
192
 
      <input type="hidden" name="check_keywords" value="yes" />
193
 
      <input type="hidden" name="area" value="default" />
194
 
    </form>
195
 
    </p>
 
171
 
 
172
 
 
173
        </div>
196
174
 
197
175
    </div>
198
176
 
 
177
    
 
178
 
199
179
    <div id="docs-body" class="withsidebar" >
200
180
        
201
181
<span class="target" id="module-sqlalchemy.orm"></span><div class="section" id="relationship-configuration">
1104
1084
<a class="reference internal" href="#sqlalchemy.orm.relationship" title="sqlalchemy.orm.relationship"><tt class="xref py py-func docutils literal"><span class="pre">relationship()</span></tt></a> argument.</p>
1105
1085
<p>The custom criteria we use in a <a class="reference internal" href="#sqlalchemy.orm.relationship.params.primaryjoin" title="sqlalchemy.orm.relationship"><tt class="xref py py-paramref docutils literal"><span class="pre">primaryjoin</span></tt></a>
1106
1086
is generally only significant when SQLAlchemy is rendering SQL in
1107
 
order to load or represent this relationship. That is, it&#8217;s  used in
 
1087
order to load or represent this relationship. That is, it&#8217;s used in
1108
1088
the SQL statement that&#8217;s emitted in order to perform a per-attribute
1109
1089
lazy load, or when a join is constructed at query time, such as via
1110
1090
<a class="reference internal" href="query.html#sqlalchemy.orm.query.Query.join" title="sqlalchemy.orm.query.Query.join"><tt class="xref py py-meth docutils literal"><span class="pre">Query.join()</span></tt></a>, or via the eager &#8220;joined&#8221; or &#8220;subquery&#8221; styles of
1235
1215
custom operators.</p>
1236
1216
</div>
1237
1217
</div>
 
1218
<div class="section" id="non-relational-comparisons-materialized-path">
 
1219
<h3>Non-relational Comparisons / Materialized Path<a class="headerlink" href="#non-relational-comparisons-materialized-path" title="Permalink to this headline">¶</a></h3>
 
1220
<div class="admonition warning">
 
1221
<p class="first admonition-title">Warning</p>
 
1222
<p class="last">this section details an experimental feature.</p>
 
1223
</div>
 
1224
<p>Using custom expressions means we can produce unorthodox join conditions that
 
1225
don&#8217;t obey the usual primary/foreign key model.  One such example is the
 
1226
materialized path pattern, where we compare strings for overlapping path tokens
 
1227
in order to produce a tree structure.</p>
 
1228
<p>Through careful use of <a class="reference internal" href="#sqlalchemy.orm.foreign" title="sqlalchemy.orm.foreign"><tt class="xref py py-func docutils literal"><span class="pre">foreign()</span></tt></a> and <a class="reference internal" href="#sqlalchemy.orm.remote" title="sqlalchemy.orm.remote"><tt class="xref py py-func docutils literal"><span class="pre">remote()</span></tt></a>, we can build
 
1229
a relationship that effectively produces a rudimentary materialized path
 
1230
system.   Essentially, when <a class="reference internal" href="#sqlalchemy.orm.foreign" title="sqlalchemy.orm.foreign"><tt class="xref py py-func docutils literal"><span class="pre">foreign()</span></tt></a> and <a class="reference internal" href="#sqlalchemy.orm.remote" title="sqlalchemy.orm.remote"><tt class="xref py py-func docutils literal"><span class="pre">remote()</span></tt></a> are
 
1231
on the <em>same</em> side of the comparison expression, the relationship is considered
 
1232
to be &#8220;one to many&#8221;; when they are on <em>different</em> sides, the relationship
 
1233
is considered to be &#8220;many to one&#8221;.   For the comparison we&#8217;ll use here,
 
1234
we&#8217;ll be dealing with collections so we keep things configured as &#8220;one to many&#8221;:</p>
 
1235
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Element</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
 
1236
    <span class="n">__tablename__</span> <span class="o">=</span> <span class="s">&#39;element&#39;</span>
 
1237
 
 
1238
    <span class="n">path</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="n">primary_key</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
 
1239
 
 
1240
    <span class="n">descendants</span> <span class="o">=</span> <span class="n">relationship</span><span class="p">(</span><span class="s">&#39;Element&#39;</span><span class="p">,</span>
 
1241
                           <span class="n">primaryjoin</span><span class="o">=</span>
 
1242
                                <span class="n">remote</span><span class="p">(</span><span class="n">foreign</span><span class="p">(</span><span class="n">path</span><span class="p">))</span><span class="o">.</span><span class="n">like</span><span class="p">(</span>
 
1243
                                        <span class="n">path</span><span class="o">.</span><span class="n">concat</span><span class="p">(</span><span class="s">&#39;/%&#39;</span><span class="p">)),</span>
 
1244
                           <span class="n">viewonly</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span>
 
1245
                           <span class="n">order_by</span><span class="o">=</span><span class="n">path</span><span class="p">)</span></pre></div>
 
1246
</div>
 
1247
<p>Above, if given an <tt class="docutils literal"><span class="pre">Element</span></tt> object with a path attribute of <tt class="docutils literal"><span class="pre">&quot;/foo/bar2&quot;</span></tt>,
 
1248
we seek for a load of <tt class="docutils literal"><span class="pre">Element.descendants</span></tt> to look like:</p>
 
1249
<div class="highlight-python"><pre>SELECT element.path AS element_path
 
1250
FROM element
 
1251
WHERE element.path LIKE ('/foo/bar2' || '/%') ORDER BY element.path</pre>
 
1252
</div>
 
1253
<div class="versionadded">
 
1254
<p><span>New in version 0.9.5: </span>Support has been added to allow a single-column
 
1255
comparison to itself within a primaryjoin condition, as well as for
 
1256
primaryjoin conditions that use <tt class="xref py py-meth docutils literal"><span class="pre">Operators.like()</span></tt> as the comparison
 
1257
operator.</p>
 
1258
</div>
 
1259
</div>
1238
1260
<div class="section" id="self-referential-many-to-many-relationship">
1239
1261
<span id="self-referential-many-to-many"></span><h3>Self-Referential Many-to-Many Relationship<a class="headerlink" href="#self-referential-many-to-many-relationship" title="Permalink to this headline">¶</a></h3>
1240
1262
<p>Many to many relationships can be customized by one or both of <a class="reference internal" href="#sqlalchemy.orm.relationship.params.primaryjoin" title="sqlalchemy.orm.relationship"><tt class="xref py py-paramref docutils literal"><span class="pre">primaryjoin</span></tt></a>
2186
2208
</div>
2187
2209
<div class="admonition seealso">
2188
2210
<p class="first admonition-title">See also</p>
2189
 
<p><a class="reference internal" href="#self-referential"><em>Adjacency List Relationships</em></a> - in-depth explaination of how
 
2211
<p><a class="reference internal" href="#self-referential"><em>Adjacency List Relationships</em></a> - in-depth explanation of how
2190
2212
<a class="reference internal" href="#sqlalchemy.orm.relationship.params.remote_side" title="sqlalchemy.orm.relationship"><tt class="xref py py-paramref docutils literal"><span class="pre">remote_side</span></tt></a>
2191
2213
is used to configure self-referential relationships.</p>
2192
2214
<p class="last"><a class="reference internal" href="#sqlalchemy.orm.remote" title="sqlalchemy.orm.remote"><tt class="xref py py-func docutils literal"><span class="pre">remote()</span></tt></a> - an annotation function that accomplishes the same