From: Mike Bayer Date: Mon, 5 Jul 2021 18:37:58 +0000 (-0400) Subject: Add additional support to honor _proxy_key in Core selects X-Git-Tag: rel_1_4_21~14^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4e1b2ee4cb345f499bdfbff6c393efbebd8e1e72;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Add additional support to honor _proxy_key in Core selects Fixed ORM regression where ad-hoc label names generated for hybrid properties and potentially other similar types of ORM-enabled expressions would usually be propagated outwards through subqueries, allowing the name to be retained in the final keys of the result set even when selecting from subqueries. Additional state is now tracked in this case that isn't lost when a hybrid is selected out of a Core select / subquery. as we have removed things like column.label() from ORM, since we now have to export the cols with the same names as what we will render, experiment with giving a greater role to the _proxy_key annotation so that a desired name can be carried through more transarently. Fixes: #6718 Change-Id: Icb313244c13ea1a8f58d3e05d07aa3e1039e15bf --- diff --git a/doc/build/changelog/unreleased_14/6718.rst b/doc/build/changelog/unreleased_14/6718.rst new file mode 100644 index 0000000000..05e20b927d --- /dev/null +++ b/doc/build/changelog/unreleased_14/6718.rst @@ -0,0 +1,11 @@ +.. change:: + :tags: orm, regression + :tickets: 6718 + + Fixed ORM regression where ad-hoc label names generated for hybrid + properties and potentially other similar types of ORM-enabled expressions + would usually be propagated outwards through subqueries, allowing the name + to be retained in the final keys of the result set even when selecting from + subqueries. Additional state is now tracked in this case that isn't lost + when a hybrid is selected out of a Core select / subquery. + diff --git a/lib/sqlalchemy/orm/context.py b/lib/sqlalchemy/orm/context.py index 78026efb1a..a6efac9cff 100644 --- a/lib/sqlalchemy/orm/context.py +++ b/lib/sqlalchemy/orm/context.py @@ -2890,7 +2890,7 @@ class _ORMColumnEntity(_ColumnEntity): ezero._adapter if ezero.is_aliased_class else None, ) - if column._annotations: + if column._annotations and not column._expression_label: # annotated columns perform more slowly in compiler and # result due to the __eq__() method, so use deannotated column = column._deannotate() diff --git a/lib/sqlalchemy/sql/elements.py b/lib/sqlalchemy/sql/elements.py index 709106b6b9..f06aee74f7 100644 --- a/lib/sqlalchemy/sql/elements.py +++ b/lib/sqlalchemy/sql/elements.py @@ -887,6 +887,21 @@ class ColumnElement( else: return getattr(self, "name", "_no_label") + @util.memoized_property + def _expression_label(self): + """a suggested label to use in the case that the column has no name, + which should be used if possible as the explicit 'AS