From: Mike Bayer Date: Sat, 18 Sep 2010 17:34:04 +0000 (-0400) Subject: - as_scalar(), label() can be called on a selectable X-Git-Tag: rel_0_6_5~57 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c5c8cdf3b4d7dc456cfef29ea04b2b7300060c7a;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - as_scalar(), label() can be called on a selectable which contains a Column that is not yet named. [ticket:1862] --- diff --git a/CHANGES b/CHANGES index 87ca1c63e5..7d72f8769f 100644 --- a/CHANGES +++ b/CHANGES @@ -98,7 +98,11 @@ CHANGES exported to the columns collection of an enclosing select() construct, or if any construct involving that column is compiled before its name is - assigned. [ticket:1862] + assigned. + + - as_scalar(), label() can be called on a selectable + which contains a Column that is not yet named. + [ticket:1862] - engine diff --git a/lib/sqlalchemy/sql/expression.py b/lib/sqlalchemy/sql/expression.py index 70d5f13fc0..5df3b87943 100644 --- a/lib/sqlalchemy/sql/expression.py +++ b/lib/sqlalchemy/sql/expression.py @@ -3685,8 +3685,7 @@ class _ScalarSelect(_Grouping): def __init__(self, element): self.element = element - cols = list(element.c) - self.type = cols[0].type + self.type = element._scalar_type() @property def columns(self): @@ -3737,7 +3736,10 @@ class CompoundSelect(_SelectBaseMixin, FromClause): self.selects.append(s.self_group(self)) _SelectBaseMixin.__init__(self, **kwargs) - + + def _scalar_type(self): + return self.selects[0]._scalar_type() + def self_group(self, against=None): return _FromGrouping(self) @@ -3910,6 +3912,11 @@ class Select(_SelectBaseMixin, FromClause): return froms + def _scalar_type(self): + elem = self._raw_columns[0] + cols = list(elem._select_iterable) + return cols[0].type + @property def froms(self): """Return the displayed list of FromClause elements.""" diff --git a/test/sql/test_compiler.py b/test/sql/test_compiler.py index 355e5dc70c..4c712ce380 100644 --- a/test/sql/test_compiler.py +++ b/test/sql/test_compiler.py @@ -1909,19 +1909,23 @@ sq.myothertable_othername AS sq_myothertable_othername FROM (" + sqstring + ") A lambda: sel1.c ) + # calling label or as_scalar doesn't compile + # anything. + sel2 = select([func.substr(my_str, 2, 3)]).label('my_substr') + assert_raises_message( exc.CompileError, "Cannot compile Column object until it's 'name' is assigned.", - lambda: select([func.substr(my_str, 2, 3)]).label('my_substr') + str, sel2 ) + sel3 = select([my_str]).as_scalar() assert_raises_message( - exc.InvalidRequestError, - "Cannot initialize a sub-selectable with this Column", - lambda: select([my_str]).as_scalar() + exc.CompileError, + "Cannot compile Column object until it's 'name' is assigned.", + str, sel3 ) - my_str.name = 'foo' self.assert_compile( @@ -1929,10 +1933,15 @@ sq.myothertable_othername AS sq_myothertable_othername FROM (" + sqstring + ") A "SELECT foo", ) self.assert_compile( - select([func.substr(my_str, 2, 3)]).label('my_substr'), + sel2, '(SELECT substr(foo, :substr_2, :substr_3) AS substr_1)', ) + self.assert_compile( + sel3, + "(SELECT foo)" + ) + def test_naming(self): f1 = func.hoho(table1.c.name) s1 = select([table1.c.myid, table1.c.myid.label('foobar'),