]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- can now allow selects which correlate all FROM clauses
authorMike Bayer <mike_mp@zzzcomputing.com>
Fri, 28 Mar 2008 15:55:26 +0000 (15:55 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 28 Mar 2008 15:55:26 +0000 (15:55 +0000)
and have no FROM themselves.  These are typically
used in a scalar context, i.e. SELECT x, (SELECT x WHERE y)
FROM table.  Requires explicit correlate() call.

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

diff --git a/CHANGES b/CHANGES
index 101edcac0d9414bd856e33e3284f14a1d12ebe24..261135b9d0b38fb8dae828063a13ceae2ffd14d3 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -54,6 +54,11 @@ CHANGES
       mapper-config'ed order_by when using select_from()
 
 - sql
+    - can now allow selects which correlate all FROM clauses
+      and have no FROM themselves.  These are typically
+      used in a scalar context, i.e. SELECT x, (SELECT x WHERE y)
+      FROM table.  Requires explicit correlate() call.
+      
     - 'name' is no longer a required constructor argument for
       Column().  It (and .key) may now be deferred until the
       column is added to a Table.
index a3a25f57353dddafd6541c8322381ac7d143a5d2..f4611de6da0339b99b42d2c596a31dc562d4d95f 100644 (file)
@@ -3088,14 +3088,12 @@ class Select(_SelectBaseMixin, FromClause):
         for f in froms:
             froms.difference_update(f._hide_froms)
 
-        if len(froms) > 1:
+        if len(froms) > 1 or self.__correlate:
             if self.__correlate:
                 froms.difference_update(self.__correlate)
             if self._should_correlate and existing_froms is not None:
                 froms.difference_update(existing_froms)
 
-            if not froms:
-                raise exceptions.InvalidRequestError("Select statement '%s' is overcorrelated; returned no 'from' clauses" % str(self.__dont_correlate()))
         return froms
 
     froms = property(_get_display_froms, doc="""Return a list of all FromClause elements which will be applied to the FROM clause of the resulting statement.""")
index 24cff7702e5f818a38d7d4dd43a792928ea3c7d6..b8d1f4f68c4cf0390e624599df596ae40d62567f 100644 (file)
@@ -141,7 +141,16 @@ sq.myothertable_othername AS sq_myothertable_othername FROM (" + sqstring + ") A
 
     def test_dont_overcorrelate(self):
         self.assert_compile(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 test_intentional_full_correlate(self):
+        """test a subquery that has no FROM clause."""
+        
+        t = table('t', column('a'), column('b'))
+        s = select([t.c.a]).where(t.c.a==1).correlate(t).as_scalar()
 
+        s2 = select([t.c.a, s])
+        self.assert_compile(s2, """SELECT t.a, (SELECT t.a WHERE t.a = :t_a_1) AS anon_1 FROM t""")
+        
     def test_exists(self):
         self.assert_compile(exists([table1.c.myid], table1.c.myid==5).select(), "SELECT EXISTS (SELECT mytable.myid FROM mytable WHERE mytable.myid = :mytable_myid_1)", params={'mytable_myid':5})