]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- Fixed bug in new "label resolution" feature of :ticket:`2992` where
authorMike Bayer <mike_mp@zzzcomputing.com>
Mon, 23 Mar 2015 16:33:48 +0000 (12:33 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Mon, 23 Mar 2015 16:33:48 +0000 (12:33 -0400)
a label that was anonymous, then labeled again with a name, would
fail to be locatable via a textual label.  This situation occurs
naturally when a mapped :func:`.column_property` is given an
explicit label in a query.
fixes #3340

doc/build/changelog/changelog_10.rst
lib/sqlalchemy/sql/compiler.py
lib/sqlalchemy/sql/elements.py
test/sql/test_text.py

index 9f68d05eb1edd2f45205f805689ad24b3a0a0e3c..2caa48a5e8db7c1c0526f152f32e3c4e557e8cc1 100644 (file)
 .. changelog::
     :version: 1.0.0b4
 
+    .. change::
+        :tags: bug, sql
+        :tickets: 3340
+
+        Fixed bug in new "label resolution" feature of :ticket:`2992` where
+        a label that was anonymous, then labeled again with a name, would
+        fail to be locatable via a textual label.  This situation occurs
+        naturally when a mapped :func:`.column_property` is given an
+        explicit label in a query.
+
     .. change::
         :tags: bug, sql
         :tickets: 3335
index 8e709a474e02cef3915367396bd72e6bb6c07ed3..755193552d07ce275a84590032cf249afa34aa83 100644 (file)
@@ -560,7 +560,6 @@ class SQLCompiler(Compiled):
 
         selectable = self.stack[-1]['selectable']
         with_cols, only_froms = selectable._label_resolve_dict
-
         try:
             if within_columns_clause:
                 col = only_froms[element.element]
index 7d64c2c4ace43312e6d81f7aa996113e7cb25729..ca8ec1f5517dd8c7b0be99353415e8639efab0dc 100644 (file)
@@ -3040,10 +3040,12 @@ class Label(ColumnElement):
 
         if name:
             self.name = name
+            self._resolve_label = self.name
         else:
             self.name = _anonymous_label(
                 '%%(%d %s)s' % (id(self), getattr(element, 'name', 'anon'))
             )
+
         self.key = self._label = self._key_label = self.name
         self._element = element
         self._type = type_
@@ -3094,7 +3096,7 @@ class Label(ColumnElement):
         self.element = clone(self.element, **kw)
         self.__dict__.pop('_allow_label_resolve', None)
         if anonymize_labels:
-            self.name = _anonymous_label(
+            self.name = self._resolve_label = _anonymous_label(
                 '%%(%d %s)s' % (
                     id(self), getattr(self.element, 'name', 'anon'))
             )
index 4483597ac017a53c09666299d432dbcab6932595..1c3cb0cb46ab96c5a685eb074b2cc520a7d51877 100644 (file)
@@ -574,6 +574,15 @@ class OrderByLabelResolutionTest(fixtures.TestBase, AssertsCompiledSQL):
             "FROM mytable AS mytable_1 ORDER BY mytable_1.name"
         )
 
+    def test_order_by_named_label_from_anon_label(self):
+        s1 = select([table1.c.myid.label(None).label("foo"), table1.c.name])
+        stmt = s1.order_by("foo")
+        self.assert_compile(
+            stmt,
+            "SELECT mytable.myid AS foo, mytable.name "
+            "FROM mytable ORDER BY foo"
+        )
+
     def test_order_by_outermost_label(self):
         # test [ticket:3335], assure that order_by("foo")
         # catches the label named "foo" in the columns clause only,