diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2022-11-07 18:40:03 -0500 |
---|---|---|
committer | mike bayer <mike_mp@zzzcomputing.com> | 2022-11-11 16:20:00 +0000 |
commit | 8e91cfe529b9b0150c16e52e22e4590bfbbe79fd (patch) | |
tree | dc8328ae669164a8fe7cf9c8a821ba92a9057921 /lib/sqlalchemy/sql | |
parent | e3a8d198917f4246365e09fa975d55c64082cd2e (diff) | |
download | sqlalchemy-8e91cfe529b9b0150c16e52e22e4590bfbbe79fd.tar.gz |
establish consistency for RETURNING column labels
The RETURNING clause now renders columns using the routine as that of the
:class:`.Select` to generate labels, which will include disambiguating
labels, as well as that a SQL function surrounding a named column will be
labeled using the column name itself. This is a more comprehensive change
than a similar one made for the 1.4 series that adjusted the function label
issue only.
includes 1.4's changelog for the backported version which also
fixes an Oracle issue independently of the 2.0 series.
Fixes: #8770
Change-Id: I2ab078a214a778ffe1720dbd864ae4c105a0691d
Diffstat (limited to 'lib/sqlalchemy/sql')
-rw-r--r-- | lib/sqlalchemy/sql/compiler.py | 23 | ||||
-rw-r--r-- | lib/sqlalchemy/sql/dml.py | 4 | ||||
-rw-r--r-- | lib/sqlalchemy/sql/selectable.py | 8 |
3 files changed, 30 insertions, 5 deletions
diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py index 3e62cb350..97397e9cf 100644 --- a/lib/sqlalchemy/sql/compiler.py +++ b/lib/sqlalchemy/sql/compiler.py @@ -3760,7 +3760,6 @@ class SQLCompiler(Compiled): "_label_select_column is only relevant within " "the columns clause of a SELECT or RETURNING" ) - if isinstance(column, elements.Label): if col_expr is not column: result_expr = _CompileLabel( @@ -4416,9 +4415,27 @@ class SQLCompiler(Compiled): populate_result_map: bool, **kw: Any, ) -> str: + columns = [ - self._label_returning_column(stmt, c, populate_result_map, **kw) - for c in base._select_iterables(returning_cols) + self._label_returning_column( + stmt, + column, + populate_result_map, + fallback_label_name=fallback_label_name, + column_is_repeated=repeated, + name=name, + proxy_name=proxy_name, + **kw, + ) + for ( + name, + proxy_name, + fallback_label_name, + column, + repeated, + ) in stmt._generate_columns_plus_names( + True, cols=base._select_iterables(returning_cols) + ) ] return "RETURNING " + ", ".join(columns) diff --git a/lib/sqlalchemy/sql/dml.py b/lib/sqlalchemy/sql/dml.py index 5145a4a16..2d3e3598b 100644 --- a/lib/sqlalchemy/sql/dml.py +++ b/lib/sqlalchemy/sql/dml.py @@ -59,6 +59,7 @@ from .selectable import FromClause from .selectable import HasCTE from .selectable import HasPrefixes from .selectable import Join +from .selectable import SelectLabelStyle from .selectable import TableClause from .selectable import TypedReturnsRows from .sqltypes import NullType @@ -399,6 +400,9 @@ class UpdateBase( ] = util.EMPTY_DICT named_with_column = False + _label_style: SelectLabelStyle = ( + SelectLabelStyle.LABEL_STYLE_DISAMBIGUATE_ONLY + ) table: _DMLTableElement _return_defaults = False diff --git a/lib/sqlalchemy/sql/selectable.py b/lib/sqlalchemy/sql/selectable.py index 9de015774..488dfe721 100644 --- a/lib/sqlalchemy/sql/selectable.py +++ b/lib/sqlalchemy/sql/selectable.py @@ -2193,7 +2193,9 @@ class SelectsRows(ReturnsRows): _label_style: SelectLabelStyle = LABEL_STYLE_NONE def _generate_columns_plus_names( - self, anon_for_dupe_key: bool + self, + anon_for_dupe_key: bool, + cols: Optional[_SelectIterable] = None, ) -> List[_ColumnsPlusNames]: """Generate column names as rendered in a SELECT statement by the compiler. @@ -2204,7 +2206,9 @@ class SelectsRows(ReturnsRows): _column_naming_convention as well. """ - cols = self._all_selected_columns + + if cols is None: + cols = self._all_selected_columns key_naming_convention = SelectState._column_naming_convention( self._label_style |