From: Mike Bayer Date: Wed, 21 Sep 2011 21:28:12 +0000 (-0400) Subject: - Fixed bug whereby with_only_columns() method of X-Git-Tag: rel_0_6_9~29 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=de5aaa27f20cdf56fc071e09e23027bdf5489ea4;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - Fixed bug whereby with_only_columns() method of Select would fail if a selectable were passed. [ticket:2270]. However, the FROM behavior is still incorrect here, so you need 0.7 in any case for this use case to be usable. --- diff --git a/CHANGES b/CHANGES index 98a34e9d26..6a53e9d057 100644 --- a/CHANGES +++ b/CHANGES @@ -69,6 +69,12 @@ CHANGES when used with certain dialects. This bug is not in 0.7. + - Fixed bug whereby with_only_columns() method of + Select would fail if a selectable were passed. + [ticket:2270]. However, the FROM behavior is + still incorrect here, so you need 0.7 in + any case for this use case to be usable. + - schema - Added an informative error message when ForeignKeyConstraint refers to a column name in diff --git a/lib/sqlalchemy/sql/expression.py b/lib/sqlalchemy/sql/expression.py index 0035b57cd5..8df45d1580 100644 --- a/lib/sqlalchemy/sql/expression.py +++ b/lib/sqlalchemy/sql/expression.py @@ -4267,12 +4267,13 @@ class Select(_SelectBaseMixin, FromClause): with the given columns. """ - - self._raw_columns = [ - isinstance(c, _ScalarSelect) and - c.self_group(against=operators.comma_op) or c - for c in [_literal_as_column(c) for c in columns] - ] + rc = [] + for c in columns: + c = _literal_as_column(c) + if isinstance(c, _ScalarSelect): + c = c.self_group(against=operators.comma_op) + rc.append(c) + self._raw_columns = rc @_generative def where(self, whereclause): diff --git a/test/sql/test_selectable.py b/test/sql/test_selectable.py index dd0bf52756..4209e33288 100644 --- a/test/sql/test_selectable.py +++ b/test/sql/test_selectable.py @@ -125,6 +125,18 @@ class SelectableTest(TestBase, AssertsExecutionResults, AssertsCompiledSQL): sel3 = visitors.ReplacingCloningVisitor().traverse(sel2) assert sel3.corresponding_column(col) is sel3.c.foo + def test_with_only_generative(self): + s1 = table1.select().as_scalar() + s2 = table1.select().as_scalar() + self.assert_compile( + s1.with_only_columns([s2]), + # this is the wrong SQL - 0.7 does it correctly. + # but the test here at the moment is just that with_only_columns() + # doesn't try to evaluate the selectable as a boolean. + "SELECT (SELECT table1.col1, table1.col2, " + "table1.col3, table1.colx FROM table1) AS anon_1 FROM table1" + ) + def test_select_on_table(self): sel = select([table1, table2], use_labels=True)