1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>
examples.join_conditions.cast
—
SQLAlchemy 0.9 Documentation
</title>
<!-- begin iterate through SQLA + sphinx environment css_files -->
<link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="../../../_static/docs.css" type="text/css" />
<link rel="stylesheet" href="../../../_static/sphinx_paramlinks.css" type="text/css" />
<link rel="stylesheet" href="../../../_static/changelog.css" type="text/css" />
<!-- end iterate through SQLA + sphinx environment css_files -->
<!-- begin layout.mako headers -->
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '../../../',
VERSION: '0.9.8',
COLLAPSE_MODINDEX: false,
FILE_SUFFIX: '.html'
};
</script>
<!-- begin iterate through sphinx environment script_files -->
<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>
<!-- end iterate through sphinx environment script_files -->
<script type="text/javascript" src="../../../_static/detectmobile.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.9 Documentation" href="../../../index.html" />
<link rel="up" title="Module code" href="../../index.html" />
<!-- end layout.mako headers -->
</head>
<body>
<div id="docs-container">
<div id="docs-top-navigation-container" class="body-background">
<div id="docs-header">
<div id="docs-version-header">
Release: <span class="version-num">0.9.8</span> | Release Date: October 13, 2014
</div>
<h1>SQLAlchemy 0.9 Documentation</h1>
</div>
</div>
<div id="docs-body-container">
<div id="fixed-sidebar" class="">
<div id="index-nav">
<form class="search" action="../../../search.html" method="get">
<input type="text" name="q" size="12" /> <input type="submit" value="Search" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p>
<a href="../../../index.html">Contents</a> |
<a href="../../../genindex.html">Index</a>
</p>
</div>
</div>
<div id="docs-body" class="" >
<h1>Source code for examples.join_conditions.cast</h1><div class="highlight"><pre>
<span class="sd">"""Illustrate a :func:`.relationship` that joins two columns where those</span>
<span class="sd">columns are not of the same type, and a CAST must be used on the SQL</span>
<span class="sd">side in order to match them.</span>
<span class="sd">When complete, we'd like to see a load of the relationship to look like::</span>
<span class="sd"> -- load the primary row, a_id is a string</span>
<span class="sd"> SELECT a.id AS a_id_1, a.a_id AS a_a_id</span>
<span class="sd"> FROM a</span>
<span class="sd"> WHERE a.a_id = '2'</span>
<span class="sd"> -- then load the collection using CAST, b.a_id is an integer</span>
<span class="sd"> SELECT b.id AS b_id, b.a_id AS b_a_id</span>
<span class="sd"> FROM b</span>
<span class="sd"> WHERE CAST('2' AS INTEGER) = b.a_id</span>
<span class="sd">The relationship is essentially configured as follows::</span>
<span class="sd"> class B(Base):</span>
<span class="sd"> # ...</span>
<span class="sd"> a = relationship(A,</span>
<span class="sd"> primaryjoin=cast(A.a_id, Integer) == foreign(B.a_id),</span>
<span class="sd"> backref="bs")</span>
<span class="sd">Where above, we are making use of the :func:`.cast` function in order</span>
<span class="sd">to produce CAST, as well as the :func:`.foreign` :term:`annotation` function</span>
<span class="sd">in order to note to the ORM that ``B.a_id`` should be treated like the</span>
<span class="sd">"foreign key" column.</span>
<span class="sd">"""</span>
<span class="kn">from</span> <span class="nn">sqlalchemy</span> <span class="kn">import</span> <span class="o">*</span>
<span class="kn">from</span> <span class="nn">sqlalchemy.orm</span> <span class="kn">import</span> <span class="o">*</span>
<span class="kn">from</span> <span class="nn">sqlalchemy.ext.declarative</span> <span class="kn">import</span> <span class="n">declarative_base</span>
<span class="n">Base</span> <span class="o">=</span> <span class="n">declarative_base</span><span class="p">()</span>
<span class="k">class</span> <span class="nc">StringAsInt</span><span class="p">(</span><span class="n">TypeDecorator</span><span class="p">):</span>
<span class="sd">"""Coerce string->integer type.</span>
<span class="sd"> This is needed only if the relationship() from</span>
<span class="sd"> int to string is writable, as SQLAlchemy will copy</span>
<span class="sd"> the string parent values into the integer attribute</span>
<span class="sd"> on the child during a flush.</span>
<span class="sd"> """</span>
<span class="n">impl</span> <span class="o">=</span> <span class="n">Integer</span>
<span class="k">def</span> <span class="nf">process_bind_param</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">dialect</span><span class="p">):</span>
<span class="k">if</span> <span class="n">value</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
<span class="n">value</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
<span class="k">return</span> <span class="n">value</span>
<span class="k">class</span> <span class="nc">A</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
<span class="sd">"""Parent. The referenced column is a string type."""</span>
<span class="n">__tablename__</span> <span class="o">=</span> <span class="s">'a'</span>
<span class="nb">id</span> <span class="o">=</span> <span class="n">Column</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">a_id</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="k">class</span> <span class="nc">B</span><span class="p">(</span><span class="n">Base</span><span class="p">):</span>
<span class="sd">"""Child. The column we reference 'A' with is an integer."""</span>
<span class="n">__tablename__</span> <span class="o">=</span> <span class="s">'b'</span>
<span class="nb">id</span> <span class="o">=</span> <span class="n">Column</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">a_id</span> <span class="o">=</span> <span class="n">Column</span><span class="p">(</span><span class="n">StringAsInt</span><span class="p">)</span>
<span class="n">a</span> <span class="o">=</span> <span class="n">relationship</span><span class="p">(</span><span class="s">"A"</span><span class="p">,</span>
<span class="c"># specify primaryjoin. The string form is optional</span>
<span class="c"># here, but note that Declarative makes available all</span>
<span class="c"># of the built-in functions we might need, including</span>
<span class="c"># cast() and foreign().</span>
<span class="n">primaryjoin</span><span class="o">=</span><span class="s">"cast(A.a_id, Integer) == foreign(B.a_id)"</span><span class="p">,</span>
<span class="n">backref</span><span class="o">=</span><span class="s">"bs"</span><span class="p">)</span>
<span class="c"># we demonstrate with SQLite, but the important part</span>
<span class="c"># is the CAST rendered in the SQL output.</span>
<span class="n">e</span> <span class="o">=</span> <span class="n">create_engine</span><span class="p">(</span><span class="s">'sqlite://'</span><span class="p">,</span> <span class="n">echo</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<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">e</span><span class="p">)</span>
<span class="n">s</span> <span class="o">=</span> <span class="n">Session</span><span class="p">(</span><span class="n">e</span><span class="p">)</span>
<span class="n">s</span><span class="o">.</span><span class="n">add_all</span><span class="p">([</span>
<span class="n">A</span><span class="p">(</span><span class="n">a_id</span><span class="o">=</span><span class="s">"1"</span><span class="p">),</span>
<span class="n">A</span><span class="p">(</span><span class="n">a_id</span><span class="o">=</span><span class="s">"2"</span><span class="p">,</span> <span class="n">bs</span><span class="o">=</span><span class="p">[</span><span class="n">B</span><span class="p">(),</span> <span class="n">B</span><span class="p">()]),</span>
<span class="n">A</span><span class="p">(</span><span class="n">a_id</span><span class="o">=</span><span class="s">"3"</span><span class="p">,</span> <span class="n">bs</span><span class="o">=</span><span class="p">[</span><span class="n">B</span><span class="p">()]),</span>
<span class="p">])</span>
<span class="n">s</span><span class="o">.</span><span class="n">commit</span><span class="p">()</span>
<span class="n">b1</span> <span class="o">=</span> <span class="n">s</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="n">B</span><span class="p">)</span><span class="o">.</span><span class="n">filter_by</span><span class="p">(</span><span class="n">a_id</span><span class="o">=</span><span class="s">"2"</span><span class="p">)</span><span class="o">.</span><span class="n">first</span><span class="p">()</span>
<span class="k">print</span><span class="p">(</span><span class="n">b1</span><span class="o">.</span><span class="n">a</span><span class="p">)</span>
<span class="n">a1</span> <span class="o">=</span> <span class="n">s</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="n">A</span><span class="p">)</span><span class="o">.</span><span class="n">filter_by</span><span class="p">(</span><span class="n">a_id</span><span class="o">=</span><span class="s">"2"</span><span class="p">)</span><span class="o">.</span><span class="n">first</span><span class="p">()</span>
<span class="k">print</span><span class="p">(</span><span class="n">a1</span><span class="o">.</span><span class="n">bs</span><span class="p">)</span></pre></div>
</div>
</div>
<div id="docs-bottom-navigation" class="docs-navigation-links">
<div id="docs-copyright">
© <a href="../../../copyright.html">Copyright</a> 2007-2014, the SQLAlchemy authors and contributors.
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.2b1.
</div>
</div>
</div>
</body>
</html>
|