]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- fix to correlation of subqueries when the column list of the select statement
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 28 Dec 2006 00:27:58 +0000 (00:27 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 28 Dec 2006 00:27:58 +0000 (00:27 +0000)
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.

CHANGES
lib/sqlalchemy/sql.py
test/sql/select.py

diff --git a/CHANGES b/CHANGES
index df9c4c020a49b6800db9ce82d3fbb12e903c3a0f..bbb69fcd45dc40010d78e5edc78534498a8bcdb5 100644 (file)
--- 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"])
index 1ca6eb93d5ddd62ec38941fad862a3aecf8c8929..207276f5ff184c7537568d2e2fb1eee1b8507737 100644 (file)
@@ -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
index c422c63fbe664b081c54db89d2f53172252ae7e8..5d91276e23fbf2262ad034c367b50bff611f703f 100644 (file)
@@ -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")