From 1f7969ae50d4be92f330d2cf6a2df9aba8b307bf Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Wed, 11 Nov 2020 11:13:27 -0500 Subject: Warn / raise for returning() / return_defaults() combinations A warning is emmitted if a returning() method such as :meth:`_sql.Insert.returning` is called multiple times, as this does not yet support additive operation. Version 1.4 will support additive operation for this. Additionally, any combination of the :meth:`_sql.Insert.returning` and :meth:`_sql.Insert.return_defaults` methods now raises an error as these methods are mutually exclusive; previously the operation would fail silently. Fixes: #5691 Change-Id: Id95e0f9da48bba0b59439cb26564f0daa684c8e3 --- lib/sqlalchemy/sql/dml.py | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) (limited to 'lib/sqlalchemy/sql') diff --git a/lib/sqlalchemy/sql/dml.py b/lib/sqlalchemy/sql/dml.py index 5726cddc0..ddb85224a 100644 --- a/lib/sqlalchemy/sql/dml.py +++ b/lib/sqlalchemy/sql/dml.py @@ -207,6 +207,8 @@ class UpdateBase( _hints = util.immutabledict() named_with_column = False + _return_defaults = None + is_dml = True @classmethod @@ -343,11 +345,10 @@ class UpdateBase( for server_flag, updated_timestamp in connection.execute(stmt): print(server_flag, updated_timestamp) - The given collection of column expressions should be derived from - the table that is - the target of the INSERT, UPDATE, or DELETE. While - :class:`_schema.Column` - objects are typical, the elements can also be expressions:: + The given collection of column expressions should be derived from the + table that is the target of the INSERT, UPDATE, or DELETE. While + :class:`_schema.Column` objects are typical, the elements can also be + expressions:: stmt = table.insert().returning( (table.c.first_name + " " + table.c.last_name). @@ -383,6 +384,16 @@ class UpdateBase( """ + if self._return_defaults: + raise exc.InvalidRequestError( + "return_defaults() is already configured on this statement" + ) + if self._returning: + util.warn( + "The returning() method does not currently support multiple " + "additive calls. The existing RETURNING clause being " + "replaced by new columns." + ) self._returning = cols def _exported_columns_iterator(self): @@ -760,6 +771,10 @@ class ValuesBase(UpdateBase): :attr:`_engine.CursorResult.inserted_primary_key_rows` """ + if self._returning: + raise exc.InvalidRequestError( + "RETURNING is already configured on this statement" + ) self._return_defaults = cols or True -- cgit v1.2.1