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/base.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/base.py')
-rw-r--r-- | lib/sqlalchemy/dialects/postgresql/base.py | 32 |
1 files changed, 19 insertions, 13 deletions
diff --git a/lib/sqlalchemy/dialects/postgresql/base.py b/lib/sqlalchemy/dialects/postgresql/base.py index 3c33d9ee8..3a458ebed 100644 --- a/lib/sqlalchemy/dialects/postgresql/base.py +++ b/lib/sqlalchemy/dialects/postgresql/base.py @@ -2147,22 +2147,28 @@ class PGCompiler(compiler.SQLCompiler): cols = insert_statement.table.c for c in cols: col_key = c.key + if col_key in set_parameters: value = set_parameters.pop(col_key) - if coercions._is_literal(value): - value = elements.BindParameter(None, value, type_=c.type) + elif c in set_parameters: + value = set_parameters.pop(c) + else: + continue - else: - if ( - isinstance(value, elements.BindParameter) - and value.type._isnull - ): - value = value._clone() - value.type = c.type - value_text = self.process(value.self_group(), use_schema=False) - - key_text = self.preparer.quote(col_key) - action_set_ops.append("%s = %s" % (key_text, value_text)) + if coercions._is_literal(value): + value = elements.BindParameter(None, value, type_=c.type) + + else: + if ( + isinstance(value, elements.BindParameter) + and value.type._isnull + ): + value = value._clone() + value.type = c.type + value_text = self.process(value.self_group(), use_schema=False) + + key_text = self.preparer.quote(col_key) + action_set_ops.append("%s = %s" % (key_text, value_text)) # check for names that don't match columns if set_parameters: |