]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- repair type expressions for columns when we aren't using select.apply_labels(),
authorMike Bayer <mike_mp@zzzcomputing.com>
Sun, 2 Sep 2012 00:15:33 +0000 (20:15 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sun, 2 Sep 2012 00:15:33 +0000 (20:15 -0400)
label should be the column name.

lib/sqlalchemy/sql/compiler.py
test/sql/test_type_expressions.py

index 297cd9adb0154c54e069bd9bc8602d177772ea07..2fc14c84c5f7898c02721eb5f5d531be5121ef26 100644 (file)
@@ -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)
index 1445ee4f96d58a52c7c96585a690b3fb2a3f08cb..1a331d5703942b4868fa1a7ac38ee4bfe88f6750 100644 (file)
@@ -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(),