From 37a61a1b497cb44d0098d188255238ddf4e8e38c Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Fri, 19 Jan 2007 02:19:38 +0000 Subject: [PATCH] - another fix to subquery correlation so that a subquery which has only one FROM element will *not* correlate that single element, since at least one FROM element is required in a query. --- CHANGES | 3 +++ lib/sqlalchemy/sql.py | 8 +++++++- test/sql/select.py | 5 ++++- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/CHANGES b/CHANGES index d8cff55dec..c85fef9676 100644 --- a/CHANGES +++ b/CHANGES @@ -10,6 +10,9 @@ 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. + - another fix to subquery correlation so that a subquery which has only one FROM + element will *not* correlate that single element, since at least one FROM element is + required in a query. - default "timezone" setting is now False. this corresponds to Python's datetime behavior as well as Postgres' timestamp/time types (which is the only timezone-sensitive dialect at the moment) [ticket:414] diff --git a/lib/sqlalchemy/sql.py b/lib/sqlalchemy/sql.py index 323173cc56..dbd119fd1e 100644 --- a/lib/sqlalchemy/sql.py +++ b/lib/sqlalchemy/sql.py @@ -1580,7 +1580,13 @@ class Select(_SelectBaseMixin, FromClause): else: return None - froms = property(lambda self: self.__froms.difference(self.__hide_froms).difference(self.__correlated), doc="""a collection containing all elements of the FROM clause""") + def _calc_froms(self): + f = self.__froms.difference(self.__hide_froms) + if (len(f) > 1): + return f.difference(self.__correlated) + else: + return f + froms = property(_calc_froms, doc="""a collection containing all elements of the FROM clause""") def accept_visitor(self, visitor): for f in self.froms: diff --git a/test/sql/select.py b/test/sql/select.py index f117366529..bdc79c400b 100644 --- a/test/sql/select.py +++ b/test/sql/select.py @@ -121,7 +121,10 @@ sq2.sq_myothertable_otherid, sq2.sq_myothertable_othername FROM \ (SELECT sq.mytable_myid AS sq_mytable_myid, sq.mytable_name AS sq_mytable_name, \ sq.mytable_description AS sq_mytable_description, sq.myothertable_otherid AS sq_myothertable_otherid, \ sq.myothertable_othername AS sq_myothertable_othername FROM (" + sqstring + ") AS sq) AS sq2") - + + def testdontovercorrelate(self): + self.runtest(select([table1], from_obj=[table1, table1.select()]), """SELECT mytable.myid, mytable.name, mytable.description FROM mytable, (SELECT mytable.myid AS myid, mytable.name AS name, mytable.description AS description FROM mytable)""") + def testwheresubquery(self): # TODO: this tests that you dont get a "SELECT column" without a FROM but its not working yet. #self.runtest( -- 2.47.2