From dfce8c35d3f95c401957f4d0ddaf8c7f49f52ece Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Thu, 18 Mar 2021 11:59:16 -0400 Subject: Raise at Core / ORM concrete inh level for label overlap Fixed regression where the :class:`.ConcreteBase` would fail to map at all when a mapped column name overlapped with the discriminator column name, producing an assertion error. The use case here did not function correctly in 1.3 as the polymorphic union would produce a query that ignored the discriminator column entirely, while emitting duplicate column warnings. As 1.4's architecture cannot easily reproduce this essentially broken behavior of 1.3 at the ``select()`` level right now, the use case now raises an informative error message instructing the user to use the ``.ConcreteBase._concrete_discriminator_name`` attribute to resolve the conflict. To assist with this configuration, ``.ConcreteBase._concrete_discriminator_name`` may be placed on the base class only where it will be automatically used by subclasses; previously this was not the case. Fixes: #6090 Change-Id: I8b7d01e4c9ea0dc97f30b8cd658b3505b24312a7 --- lib/sqlalchemy/sql/elements.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) (limited to 'lib/sqlalchemy/sql') diff --git a/lib/sqlalchemy/sql/elements.py b/lib/sqlalchemy/sql/elements.py index 29023c9fe..26c03b57b 100644 --- a/lib/sqlalchemy/sql/elements.py +++ b/lib/sqlalchemy/sql/elements.py @@ -4330,12 +4330,21 @@ class Label(roles.LabeledColumnExprRole, ColumnElement): disallow_is_literal=True, name_is_truncatable=isinstance(name, _truncated_label), ) - # TODO: want to remove this assertion at some point. all - # _make_proxy() implementations will give us back the key that - # is our "name" in the first place. based on this we can - # safely return our "self.key" as the key here, to support a new - # case where the key and name are separate. - assert key == self.name + + # there was a note here to remove this assertion, which was here + # to determine if we later could support a use case where + # the key and name of a label are separate. But I don't know what + # that case was. For now, this is an unexpected case that occurs + # when a label name conflicts with other columns and select() + # is attempting to disambiguate an explicit label, which is not what + # the user would want. See issue #6090. + if key != self.name: + raise exc.InvalidRequestError( + "Label name %s is being renamed to an anonymous label due " + "to disambiguation " + "which is not supported right now. Please use unique names " + "for explicit labels." % (self.name) + ) e._propagate_attrs = selectable._propagate_attrs e._proxies.append(self) -- cgit v1.2.1