label ordering rules intended for eager loading get in the way of valid use cases

Issue #3340 resolved
Mike Bayer repo owner created an issue
diff --git a/test/sql/test_text.py b/test/sql/test_text.py
index 4483597..1c3cb0c 100644
--- a/test/sql/test_text.py
+++ b/test/sql/test_text.py
@@ -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,

fails:

#!


 File "/Users/classic/dev/sqlalchemy/lib/sqlalchemy/util/langhelpers.py", line 1307, in warn_limited
    warnings.warn(msg, exc.SAWarning, stacklevel=2)
SAWarning: Can't resolve label reference 'foo'; converting to text() (this warning may be suppressed after 10 occurrences)

because of this:

diff --git a/lib/sqlalchemy/sql/elements.py b/lib/sqlalchemy/sql/elements.py
index 7d64c2c..db759eb 100644
--- a/lib/sqlalchemy/sql/elements.py
+++ b/lib/sqlalchemy/sql/elements.py
@@ -3040,6 +3040,8 @@ class Label(ColumnElement):

         if name:
             self.name = name
+            # shouldn't we do this??
+            # self._resolve_label = name
         else:
             self.name = _anonymous_label(
                 '%%(%d %s)s' % (id(self), getattr(element, 'name', 'anon'))

but doing "that" fails because test_columnadapter_anonymized is testing exactly that this label is no longer targetable.

But we have this situation for a column_property() and this should not fail:

from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class EMap(Base):
    __tablename__ = 'a'
    id = Column(Integer, primary_key=True)

    FName = Column(String(length=45))
    LName = Column(String(length=45))
    AssociateName = column_property(FName + " " + LName)

s = Session()


print s.query(EMap.AssociateName.label('foo')).group_by("foo")

Comments (1)

  1. Mike Bayer reporter
    • Fixed bug in new "label resolution" feature of 🎫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. fixes #3340

    → <<cset 04545727d411>>

  2. Log in to comment