Commits

Mike Bayer  committed 43fe922

- [bug] column.label(None) now produces an
anonymous label, instead of returning the
column object itself, consistent with the behavior
of label(column, None). [ticket:2168]

  • Participants
  • Parent commits bbb2972

Comments (0)

Files changed (5)

     "inspector" object as the first argument.  
     [ticket:2418]
 
+  - [bug] column.label(None) now produces an 
+    anonymous label, instead of returning the
+    column object itself, consistent with the behavior
+    of label(column, None).  [ticket:2168]
+
   - [bug] Removed warning when Index is created
     with no columns; while this might not be what 
     the user intended, it is a valid use case 

File lib/sqlalchemy/orm/query.py

         # can be located in the result even
         # if the expression's identity has been changed
         # due to adaption.
-        if not column._label:
-            column = column.label(None)
+
+        if not column._label and not getattr(column, 'is_literal', False):
+            column = column.label(self._label_name)
 
         query._entities.append(self)
 

File lib/sqlalchemy/sql/expression.py

         else:
             return name
 
-    def label(self, name):
-        # currently, anonymous labels don't occur for 
-        # ColumnClause.   The use at the moment
-        # is that they do not generate nicely for 
-        # is_literal clauses.   We would like to change
-        # this so that label(None) acts as would be expected.
-        # See [ticket:2168].
-        if name is None:
-            return self
-        else:
-            return super(ColumnClause, self).label(name)
-
 
     def _bind_param(self, operator, obj):
         return _BindParamClause(self.name, obj,

File test/orm/test_query.py

         self.assert_compile(
             q3,
             "SELECT anon_1.users_id AS anon_1_users_id, anon_1.users_name AS anon_1_users_name,"
-            " anon_1.anon_2 AS anon_1_anon_2 FROM (SELECT users.id AS users_id, users.name AS"
-            " users_name, :param_1 AS anon_2 FROM users UNION SELECT users.id AS users_id, "
+            " anon_1.param_1 AS anon_1_param_1 FROM (SELECT users.id AS users_id, users.name AS"
+            " users_name, :param_1 AS param_1 FROM users UNION SELECT users.id AS users_id, "
             "users.name AS users_name, 'y' FROM users) AS anon_1"
         )
 
             ['User', 'foo']
         )
 
-        for q in (q3.order_by(User.id, "anon_1_anon_2"), q6.order_by(User.id, "foo")):
+        for q in (q3.order_by(User.id, "anon_1_param_1"), q6.order_by(User.id, "foo")):
             eq_(q.all(),
                 [
                     (User(id=7, name=u'jack'), u'x'), 

File test/sql/test_selectable.py

         eq_(c1._label, "t1_c1")
 
 class AnonLabelTest(fixtures.TestBase):
-    """Test behaviors that we hope to change with [ticket:2168]."""
+    """Test behaviors fixed by [ticket:2168]."""
 
     def test_anon_labels_named_column(self):
         c1 = column('x')
 
-        # surprising
-        assert c1.label(None) is c1
-        eq_(str(select([c1.label(None)])), "SELECT x")
+        assert c1.label(None) is not c1
+        eq_(str(select([c1.label(None)])), "SELECT x AS x_1")
 
     def test_anon_labels_literal_column(self):
         c1 = literal_column('x')
-        assert c1.label(None) is c1
-        eq_(str(select([c1.label(None)])), "SELECT x")
+        assert c1.label(None) is not c1
+        eq_(str(select([c1.label(None)])), "SELECT x AS x_1")
 
     def test_anon_labels_func(self):
         c1 = func.count('*')