From: Mike Bayer Date: Sun, 2 Sep 2012 00:15:33 +0000 (-0400) Subject: - repair type expressions for columns when we aren't using select.apply_labels(), X-Git-Tag: rel_0_8_0b1~184 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=9070e5c2a5e43c32788be316182cd2017d830012;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - repair type expressions for columns when we aren't using select.apply_labels(), label should be the column name. --- diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py index 297cd9adb0..2fc14c84c5 100644 --- a/lib/sqlalchemy/sql/compiler.py +++ b/lib/sqlalchemy/sql/compiler.py @@ -1038,7 +1038,11 @@ class SQLCompiler(engine.Compiled): isinstance(column, sql.Function)): result_expr = _CompileLabel(col_expr, column.anon_label) elif col_expr is not column: - result_expr = _CompileLabel(col_expr, column.anon_label) + # TODO: are we sure "column" has a .name and .key here ? + # assert isinstance(column, sql.ColumnClause) + result_expr = _CompileLabel(col_expr, + sql._as_truncated(column.name), + alt_names=(column.key,)) else: result_expr = col_expr @@ -1067,7 +1071,6 @@ class SQLCompiler(engine.Compiled): iswrapper=False, fromhints=None, compound_index=0, positional_names=None, **kwargs): - entry = self.stack and self.stack[-1] or {} existingfroms = entry.get('from', None) diff --git a/test/sql/test_type_expressions.py b/test/sql/test_type_expressions.py index 1445ee4f96..1a331d5703 100644 --- a/test/sql/test_type_expressions.py +++ b/test/sql/test_type_expressions.py @@ -1,4 +1,4 @@ -from sqlalchemy import Table, Column, String, func, MetaData, select, TypeDecorator +from sqlalchemy import Table, Column, String, func, MetaData, select, TypeDecorator, cast from test.lib import fixtures, AssertsCompiledSQL, testing from test.lib.testing import eq_ @@ -24,7 +24,14 @@ class SelectTest(fixtures.TestBase, AssertsCompiledSQL): self.assert_compile( select([table]), - "SELECT test_table.x, lower(test_table.y) AS y_1 FROM test_table" + "SELECT test_table.x, lower(test_table.y) AS y FROM test_table" + ) + + def test_anonymous_expr(self): + table = self._fixture() + self.assert_compile( + select([cast(table.c.y, String)]), + "SELECT CAST(test_table.y AS VARCHAR) AS anon_1 FROM test_table" ) def test_select_cols_use_labels(self): @@ -73,8 +80,8 @@ class SelectTest(fixtures.TestBase, AssertsCompiledSQL): table = self._fixture() self.assert_compile( select([table]).where(table.c.y == "hi"), - "SELECT test_table.x, lower(test_table.y) AS y_1 FROM " - "test_table WHERE test_table.y = lower(:y_2)" + "SELECT test_table.x, lower(test_table.y) AS y FROM " + "test_table WHERE test_table.y = lower(:y_1)" ) class RoundTripTestBase(object): @@ -121,6 +128,17 @@ class RoundTripTestBase(object): "Y1" ) + def test_targeting_by_string(self): + testing.db.execute( + self.tables.test_table.insert(), + {"x": "X1", "y": "Y1"}, + ) + row = testing.db.execute(select([self.tables.test_table])).first() + eq_( + row["y"], + "Y1" + ) + def test_targeting_apply_labels(self): testing.db.execute( self.tables.test_table.insert(),