From e0396633e72bc09bd7cec715101d516ea87fa840 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sat, 5 Oct 2019 18:27:44 -0400 Subject: create second level deduping when use_labels is turned on As of #4753 we allow duplicate columns. This creates some new problems that there can be duplicate columns in a subquery which are then not addressible on the outside because they are ambiguous (Postgresql has this behavior at least). Additionally it creates situations where we are making an anon label of an anon label which is leaking into the query. New logic for generating anon labels handles this situation and also alters the .c collection of a subquery such that we are only getting the first column from the derived selectable that has that name, the subsequent ones have a new deduping label with two underscores and are not exposed in .c. The dedupe logic when rendering the columns will handle duplicate label names for different columns, vs. the same column repeated, as separate cases. Fixes: #4892 Change-Id: I929fbd8da14bcc239e0481c24bbd9b5ce826e8fa --- lib/sqlalchemy/sql/elements.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'lib/sqlalchemy/sql/elements.py') diff --git a/lib/sqlalchemy/sql/elements.py b/lib/sqlalchemy/sql/elements.py index 8ee157b6f..b6462b334 100644 --- a/lib/sqlalchemy/sql/elements.py +++ b/lib/sqlalchemy/sql/elements.py @@ -878,7 +878,13 @@ class ColumnElement( while self._is_clone_of is not None: self = self._is_clone_of - return _anonymous_label("%%(%d %s)s" % (id(self), seed or "anon")) + # as of 1.4 anonymous label for ColumnElement uses hash(), not id(), + # as the identifier, because a column and its annotated version are + # the same thing in a SQL statement + if isinstance(seed, _anonymous_label): + return _anonymous_label("%s%%(%d %s)s" % (seed, hash(self), "")) + + return _anonymous_label("%%(%d %s)s" % (hash(self), seed or "anon")) @util.memoized_property def anon_label(self): @@ -900,6 +906,10 @@ class ColumnElement( def _label_anon_label(self): return self._anon_label(getattr(self, "_label", None)) + @util.memoized_property + def _dedupe_label_anon_label(self): + return self._anon_label(getattr(self, "_label", "anon") + "_") + class WrapsColumnExpression(object): """Mixin that defines a :class:`.ColumnElement` as a wrapper with special -- cgit v1.2.1