summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2010-09-18 13:34:04 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2010-09-18 13:34:04 -0400
commitc5c8cdf3b4d7dc456cfef29ea04b2b7300060c7a (patch)
treea5f05fd65195e395800bf74c825b3fa9c35f389a
parent003149c504149849c679a60a1f346a0f0393dce0 (diff)
downloadsqlalchemy-c5c8cdf3b4d7dc456cfef29ea04b2b7300060c7a.tar.gz
- as_scalar(), label() can be called on a selectable
which contains a Column that is not yet named. [ticket:1862]
-rw-r--r--CHANGES6
-rw-r--r--lib/sqlalchemy/sql/expression.py13
-rw-r--r--test/sql/test_compiler.py21
3 files changed, 30 insertions, 10 deletions
diff --git a/CHANGES b/CHANGES
index 87ca1c63e..7d72f8769 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 70d5f13fc..5df3b8794 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 355e5dc70..4c712ce38 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'),