From: Mike Bayer Date: Thu, 28 Dec 2006 00:27:58 +0000 (+0000) Subject: - fix to correlation of subqueries when the column list of the select statement X-Git-Tag: rel_0_3_4~67 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5369b3df8c67ee357fd919e5f5d164e55a87d5be;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - fix to correlation of subqueries when the column list of the select statement is constructed with individual calls to append_column(); this fixes an ORM bug whereby nested select statements were not getting correlated with the main select generated by the Query object. --- diff --git a/CHANGES b/CHANGES index df9c4c020a..bbb69fcd45 100644 --- a/CHANGES +++ b/CHANGES @@ -3,6 +3,10 @@ with new instance - fixes to postgres reflection to better handle when schema names are present; thanks to jason (at) ncsmags.com [ticket:402] +- fix to correlation of subqueries when the column list of the select statement +is constructed with individual calls to append_column(); this fixes an ORM +bug whereby nested select statements were not getting correlated with the +main select generated by the Query object. 0.3.3 - string-based FROM clauses fixed, i.e. select(..., from_obj=["sometext"]) diff --git a/lib/sqlalchemy/sql.py b/lib/sqlalchemy/sql.py index 1ca6eb93d5..207276f5ff 100644 --- a/lib/sqlalchemy/sql.py +++ b/lib/sqlalchemy/sql.py @@ -1460,14 +1460,17 @@ class Select(_SelectBaseMixin, FromClause): if columns is not None: for c in columns: self.append_column(c) - + + for f in from_obj: + self.append_from(f) + + # whereclauses must be appended after the columns/FROM, since it affects + # the correlation of subqueries. see test/sql/select.py SelectTest.testwheresubquery if whereclause is not None: self.append_whereclause(whereclause) if having is not None: self.append_having(having) - for f in from_obj: - self.append_from(f) class _CorrelatedVisitor(ClauseVisitor): """visits a clause, locates any Select clauses, and tells them that they should diff --git a/test/sql/select.py b/test/sql/select.py index c422c63fbe..5d91276e23 100644 --- a/test/sql/select.py +++ b/test/sql/select.py @@ -149,6 +149,14 @@ sq.myothertable_othername AS sq_myothertable_othername FROM (" + sqstring + ") A select([users, s.c.street], from_obj=[s]), """SELECT users.user_id, users.user_name, users.password, s.street FROM users, (SELECT addresses.street AS street FROM addresses WHERE addresses.user_id = users.user_id) AS s""") + # test constructing the outer query via append_column(), which occurs in the ORM's Query object + s = select([], exists([1], table2.c.otherid==table1.c.myid), from_obj=[table1]) + s.append_column(table1) + self.runtest( + s, + "SELECT mytable.myid, mytable.name, mytable.description FROM mytable WHERE EXISTS (SELECT 1 FROM myothertable WHERE myothertable.otherid = mytable.myid)" + ) + def testcolumnsubquery(self): s = select([table1.c.myid], scalar=True, correlate=False) self.runtest(select([table1, s]), "SELECT mytable.myid, mytable.name, mytable.description, (SELECT mytable.myid AS myid FROM mytable) FROM mytable")