72
74
<div id="docs-container">
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
78
86
<h1>SQLAlchemy 0.9 Documentation</h1>
80
<div id="docs-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" />
89
<div id="docs-version-header">
90
Release: <span class="version-num">0.9.4</span> | Release Date: March 28, 2014
97
<div id="docs-top-navigation">
98
<div id="docs-top-page-control" class="docs-navigation-links">
91
<div id="docs-body-container">
93
<div id="fixed-sidebar" class="withsidebar">
96
<div id="docs-sidebar-popout">
97
<h3><a href="../index.html">SQLAlchemy 0.9 Documentation</a></h3>
99
<p id="sidebar-paginate">
100
<a href="index.html" title="SQLAlchemy ORM">Up</a> |
102
<a href="mapper_config.html" title="Mapper Configuration">Prev</a> |
103
<a href="collections.html" title="Collection Configuration and Techniques">Next</a>
106
<p id="sidebar-topnav">
107
<a href="../index.html">Contents</a> |
108
<a href="../genindex.html">Index</a>
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" />
121
<div id="docs-sidebar">
124
Relationship Configuration
101
<a href="mapper_config.html" title="previous chapter">Mapper Configuration</a>
104
<a href="collections.html" title="next chapter">Collection Configuration and Techniques</a>
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
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>
119
Relationship Configuration
124
Relationship Configuration
131
<div id="docs-body-container">
133
<div id="docs-sidebar">
134
<h3><a href="../index.html">Table of Contents</a></h3>
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 “Secondary” Joins</a></li>
166
159
<li><a class="reference internal" href="#relationship-to-non-primary-mapper">Relationship to Non Primary Mapper</a></li>
178
<h4>Previous Topic</h4>
180
<a href="mapper_config.html" title="previous chapter">Mapper Configuration</a>
184
<a href="collections.html" title="next chapter">Collection Configuration and Techniques</a>
188
<h4>Quick Search</h4>
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" />
199
179
<div id="docs-body" class="withsidebar" >
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’s used in
1087
order to load or represent this relationship. That is, it’s used in
1108
1088
the SQL statement that’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 “joined” or “subquery” styles of
1235
1215
custom operators.</p>
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>
1224
<p>Using custom expressions means we can produce unorthodox join conditions that
1225
don’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 “one to many”; when they are on <em>different</em> sides, the relationship
1233
is considered to be “many to one”. For the comparison we’ll use here,
1234
we’ll be dealing with collections so we keep things configured as “one to many”:</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">'element'</span>
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>
1240
<span class="n">descendants</span> <span class="o">=</span> <span class="n">relationship</span><span class="p">(</span><span class="s">'Element'</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">'/%'</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>
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">"/foo/bar2"</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
1251
WHERE element.path LIKE ('/foo/bar2' || '/%') ORDER BY element.path</pre>
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
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>
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