diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2020-11-23 20:13:20 -0500 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2020-11-23 20:13:20 -0500 |
commit | 584cabbf7e79948e38b29df5af63c3c712566f31 (patch) | |
tree | c4e8451d7f4b794826d20ecf18b8252099fadf25 /lib/sqlalchemy/dialects/postgresql/dml.py | |
parent | b4e40b35627f1c26b84234d16a36ce2850a798b9 (diff) | |
download | sqlalchemy-584cabbf7e79948e38b29df5af63c3c712566f31.tar.gz |
Support Column objects in the SET clause for upsert
Established support for :class:`_schema.Column` objects as well as ORM
instrumented attributes as keys in the ``set_`` dictionary passed to the
:meth:`_postgresql.Insert.on_conflict_do_update` and
:meth:`_sqlite.Insert.on_conflict_do_update` methods, which match to the
:class:`_schema.Column` objects in the ``.c`` collection of the target
:class:`_schema.Table`. Previously, only string column names were
expected; a column expression would be assumed to be an out-of-table
expression that would render fully along with a warning.
Fixes: #5722
Change-Id: Ice73b501d721c28d978a0277a83cedc6aff756a9
Diffstat (limited to 'lib/sqlalchemy/dialects/postgresql/dml.py')
-rw-r--r-- | lib/sqlalchemy/dialects/postgresql/dml.py | 21 |
1 files changed, 14 insertions, 7 deletions
diff --git a/lib/sqlalchemy/dialects/postgresql/dml.py b/lib/sqlalchemy/dialects/postgresql/dml.py index 50fd09528..78cad974f 100644 --- a/lib/sqlalchemy/dialects/postgresql/dml.py +++ b/lib/sqlalchemy/dialects/postgresql/dml.py @@ -7,6 +7,8 @@ from . import ext from ... import util +from ...sql import coercions +from ...sql import roles from ...sql import schema from ...sql.base import _generative from ...sql.dml import Insert as StandardInsert @@ -77,12 +79,16 @@ class Insert(StandardInsert): conditional target index. :param set\_: - Required argument. A dictionary or other mapping object - with column names as keys and expressions or literals as values, - specifying the ``SET`` actions to take. - If the target :class:`_schema.Column` specifies a ". - key" attribute distinct - from the column name, that key should be used. + A dictionary or other mapping object + where the keys are either names of columns in the target table, + or :class:`_schema.Column` objects or other ORM-mapped columns + matching that of the target table, and expressions or literals + as values, specifying the ``SET`` actions to take. + + .. versionadded:: 1.4 The + :paramref:`_postgresql.Insert.on_conflict_do_update.set_` + parameter supports :class:`_schema.Column` objects from the target + :class:`_schema.Table` as keys. .. warning:: This dictionary does **not** take into account Python-specified default UPDATE values or generation functions, @@ -229,6 +235,7 @@ class OnConflictDoUpdate(OnConflictClause): if not isinstance(set_, dict) or not set_: raise ValueError("set parameter must be a non-empty dictionary") self.update_values_to_set = [ - (key, value) for key, value in set_.items() + (coercions.expect(roles.DMLColumnRole, key), value) + for key, value in set_.items() ] self.update_whereclause = where |