summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2015-03-23 12:33:48 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2015-03-23 12:33:48 -0400
commit04545727d4117db51786189e591b1777bde1a40b (patch)
tree16aa1a338c6529096db92d0d1efec1f057a28088
parentdb853306c40437a8ad3dc9f510865820a941c3b5 (diff)
downloadsqlalchemy-04545727d4117db51786189e591b1777bde1a40b.tar.gz
- 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. fixes #3340
-rw-r--r--doc/build/changelog/changelog_10.rst10
-rw-r--r--lib/sqlalchemy/sql/compiler.py1
-rw-r--r--lib/sqlalchemy/sql/elements.py4
-rw-r--r--test/sql/test_text.py9
4 files changed, 22 insertions, 2 deletions
diff --git a/doc/build/changelog/changelog_10.rst b/doc/build/changelog/changelog_10.rst
index 9f68d05eb..2caa48a5e 100644
--- a/doc/build/changelog/changelog_10.rst
+++ b/doc/build/changelog/changelog_10.rst
@@ -20,6 +20,16 @@
.. 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
Fixed bug in new "label resolution" feature of :ticket:`2992` where
diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py
index 8e709a474..755193552 100644
--- a/lib/sqlalchemy/sql/compiler.py
+++ b/lib/sqlalchemy/sql/compiler.py
@@ -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]
diff --git a/lib/sqlalchemy/sql/elements.py b/lib/sqlalchemy/sql/elements.py
index 7d64c2c4a..ca8ec1f55 100644
--- a/lib/sqlalchemy/sql/elements.py
+++ b/lib/sqlalchemy/sql/elements.py
@@ -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'))
)
diff --git a/test/sql/test_text.py b/test/sql/test_text.py
index 4483597ac..1c3cb0cb4 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,