]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Allow stringify compiler to render unnamed column
authorMike Bayer <mike_mp@zzzcomputing.com>
Fri, 2 Sep 2016 15:48:15 +0000 (11:48 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 2 Sep 2016 15:48:15 +0000 (11:48 -0400)
Stringify of expression with unnamed :class:`.Column` objects, as
occurs in lots of situations including ORM error reporting,
will now render the name in string context as "<name unknown>"
rather than raising a compile error.

Change-Id: I76f637c5eb4cfdb1b526964cb001565b97e296da
Fixes: #3789
doc/build/changelog/changelog_11.rst
lib/sqlalchemy/sql/compiler.py
test/sql/test_compiler.py

index 88f1ebb244f5568a70076edcf767f3ccd7a5554c..c038966c5dbfb2f016f2addea766efb3a5f2f6a4 100644 (file)
 .. changelog::
     :version: 1.1.0
 
+    .. change::
+        :tags: bug, sql
+        :tickets: 3789
+
+        Stringify of expression with unnamed :class:`.Column` objects, as
+        occurs in lots of situations including ORM error reporting,
+        will now render the name in string context as "<name unknown>"
+        rather than raising a compile error.
+
     .. change::
         :tags: bug, sql
         :tickets: 3786
index 85d5ff6da8bcfb604c85991cb5bf616ffb8743ab..a7954f10a297da1530bb032297350a2c99a431c6 100644 (file)
@@ -660,12 +660,15 @@ class SQLCompiler(Compiled):
             return label.element._compiler_dispatch(
                 self, within_columns_clause=False, **kw)
 
+    def _fallback_column_name(self, column):
+        raise exc.CompileError("Cannot compile Column object until "
+                               "its 'name' is assigned.")
+
     def visit_column(self, column, add_to_result_map=None,
                      include_table=True, **kwargs):
         name = orig_name = column.name
         if name is None:
-            raise exc.CompileError("Cannot compile Column object until "
-                                   "its 'name' is assigned.")
+            name = self._fallback_column_name(column)
 
         is_literal = column.is_literal
         if not is_literal and isinstance(name, elements._truncated_label):
@@ -2203,6 +2206,9 @@ class StrSQLCompiler(SQLCompiler):
 
     """
 
+    def _fallback_column_name(self, column):
+        return "<name unknown>"
+
     def visit_getitem_binary(self, binary, operator, **kw):
         return "%s[%s]" % (
             self.process(binary.left, **kw),
@@ -2210,7 +2216,6 @@ class StrSQLCompiler(SQLCompiler):
         )
 
     def returning_clause(self, stmt, returning_cols):
-
         columns = [
             self._label_select_column(None, c, True, False, {})
             for c in elements._select_iterables(returning_cols)
index a0db9864ec702a5855c6722fff33d3aa49017da8..6896c98578998207fdef264e1569da07ecae51ca 100644 (file)
@@ -2432,14 +2432,16 @@ class SelectTest(fixtures.TestBase, AssertsCompiledSQL):
         assert_raises_message(
             exc.CompileError,
             "Cannot compile Column object until its 'name' is assigned.",
-            str, sel2
+            sel2.compile,
+            dialect=default.DefaultDialect()
         )
 
         sel3 = select([my_str]).as_scalar()
         assert_raises_message(
             exc.CompileError,
             "Cannot compile Column object until its 'name' is assigned.",
-            str, sel3
+            sel3.compile,
+            dialect=default.DefaultDialect()
         )
 
         my_str.name = 'foo'
@@ -2709,6 +2711,13 @@ class StringifySpecialTest(fixtures.TestBase):
             "FROM mytable WHERE mytable.myid = :myid_1"
         )
 
+    def test_unnamed_column(self):
+        stmt = Column(Integer) == 5
+        eq_ignore_whitespace(
+            str(stmt),
+            '"<name unknown>" = :param_1'
+        )
+
     def test_cte(self):
         # stringify of these was supported anyway by defaultdialect.
         stmt = select([table1.c.myid]).cte()