[Sqlalchemy-commits] [2456] sqlalchemy/trunk: error raised if trying to auto-join on a self referen
Brought to you by:
zzzeek
From: <co...@sq...> - 2007-03-28 17:48:43
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head><meta http-equiv="content-type" content="text/html; charset=utf-8" /><style type="text/css"><!-- #msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; } #msg dt { float: left; width: 6em; font-weight: bold; } #msg dt:after { content:':';} #msg dl, #msg dt, #msg ul, #msg li, #header, #footer { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; } #msg dl a { font-weight: bold} #msg dl a:link { color:#fc3; } #msg dl a:active { color:#ff0; } #msg dl a:visited { color:#cc6; } h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; } #msg pre { overflow: auto; background: #ffc; border: 1px #fc0 solid; padding: 6px; } #msg ul, pre { overflow: auto; } #header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; } #patch { width: 100%; } #patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;} #patch .propset h4, #patch .binary h4 {margin:0;} #patch pre {padding:0;line-height:1.2em;margin:0;} #patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;} #patch .propset .diff, #patch .binary .diff {padding:10px 0;} #patch span {display:block;padding:0 10px;} #patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;} #patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;} #patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;} #patch .lines, .info {color:#888;background:#fff;} --></style> <title>[2456] sqlalchemy/trunk: error raised if trying to auto-join on a self referential</title> </head> <body> <div id="msg"> <dl> <dt>Revision</dt> <dd>2456</dd> <dt>Author</dt> <dd>zzzeek</dd> <dt>Date</dt> <dd>2007-03-28 13:48:02 -0400 (Wed, 28 Mar 2007)</dd> </dl> <h3>Log Message</h3> <pre>error raised if trying to auto-join on a self referential</pre> <h3>Modified Paths</h3> <ul> <li><a href="#sqlalchemytrunklibsqlalchemyormquerypy">sqlalchemy/trunk/lib/sqlalchemy/orm/query.py</a></li> <li><a href="#sqlalchemytrunktestormgenerativepy">sqlalchemy/trunk/test/orm/generative.py</a></li> </ul> </div> <div id="patch"> <h3>Diff</h3> <a id="sqlalchemytrunklibsqlalchemyormquerypy"></a> <div class="modfile"><h4>Modified: sqlalchemy/trunk/lib/sqlalchemy/orm/query.py (2455 => 2456)</h4> <pre class="diff"><span> <span class="info">--- sqlalchemy/trunk/lib/sqlalchemy/orm/query.py 2007-03-28 15:53:18 UTC (rev 2455) +++ sqlalchemy/trunk/lib/sqlalchemy/orm/query.py 2007-03-28 17:48:02 UTC (rev 2456) </span><span class="lines">@@ -442,6 +442,8 @@ </span><span class="cx"> keys = [] </span><span class="cx"> for key in prop: </span><span class="cx"> p = mapper.props[key] </span><ins>+ if p._is_self_referential(): + raise exceptions.InvalidRequestError("Self-referential query on '%s' property must be constructed manually using an Alias object for the related table." % (str(p))) </ins><span class="cx"> keys.append(key) </span><span class="cx"> mapper = p.mapper </span><span class="cx"> else: </span><span class="lines">@@ -450,6 +452,8 @@ </span><span class="cx"> mapper = self._joinpoint </span><span class="cx"> for key in keys: </span><span class="cx"> prop = mapper.props[key] </span><ins>+ if prop._is_self_referential(): + raise exceptions.InvalidRequestError("Self-referential query on '%s' property must be constructed manually using an Alias object for the related table." % str(prop)) </ins><span class="cx"> if outerjoin: </span><span class="cx"> if prop.secondary: </span><span class="cx"> clause = clause.outerjoin(prop.secondary, prop.get_join(mapper, primary=True, secondary=False)) </span></span></pre></div> <a id="sqlalchemytrunktestormgenerativepy"></a> <div class="modfile"><h4>Modified: sqlalchemy/trunk/test/orm/generative.py (2455 => 2456)</h4> <pre class="diff"><span> <span class="info">--- sqlalchemy/trunk/test/orm/generative.py 2007-03-28 15:53:18 UTC (rev 2455) +++ sqlalchemy/trunk/test/orm/generative.py 2007-03-28 17:48:02 UTC (rev 2456) </span><span class="lines">@@ -1,10 +1,10 @@ </span><del>-from testbase import PersistTest, AssertMixin </del><ins>+from testbase import PersistTest, AssertMixin, ORMTest </ins><span class="cx"> import testbase </span><span class="cx"> import tables </span><span class="cx"> </span><span class="cx"> from sqlalchemy import * </span><ins>+from sqlalchemy import exceptions </ins><span class="cx"> </span><del>- </del><span class="cx"> class Foo(object): </span><span class="cx"> pass </span><span class="cx"> </span><span class="lines">@@ -233,6 +233,30 @@ </span><span class="cx"> res = self.query.filter(and_(table1.c.ID==table2.c.T1ID,table2.c.T1ID==1)).distinct() </span><span class="cx"> self.assertEqual(res.count(), 1) </span><span class="cx"> </span><ins>+class SelfRefTest(ORMTest): + def define_tables(self, metadata): + global t1 + t1 = Table('t1', metadata, + Column('id', Integer, primary_key=True), + Column('parent_id', Integer, ForeignKey('t1.id')) + ) + def test_noautojoin(self): + class T(object):pass + mapper(T, t1, properties={'children':relation(T)}) + sess = create_session() + try: + sess.query(T).join('children').select_by(id=7) + assert False + except exceptions.InvalidRequestError, e: + assert str(e) == "Self-referential query on 'T.children (T)' property must be constructed manually using an Alias object for the related table.", str(e) </ins><span class="cx"> </span><ins>+ try: + sess.query(T).join(['children']).select_by(id=7) + assert False + except exceptions.InvalidRequestError, e: + assert str(e) == "Self-referential query on 'T.children (T)' property must be constructed manually using an Alias object for the related table.", str(e) + + + </ins><span class="cx"> if __name__ == "__main__": </span><span class="cx"> testbase.main() </span></span></pre> </div> </div> </body> </html> |