Merge "Accommodate value of None for ON DUPLICATE KEY UPDATE"

This commit is contained in:
mike bayer
2019-06-10 18:46:26 +00:00
committed by Gerrit Code Review
3 changed files with 27 additions and 6 deletions
+6
View File
@@ -0,0 +1,6 @@
.. change::
:tags: bug, mysql
:tickets: 4715
Fixed bug where MySQL ON DUPLICATE KEY UPDATE would not accommodate setting
a column to the value NULL. Pull request courtesy Lukáš Banič.
+6 -6
View File
@@ -1232,15 +1232,15 @@ class MySQLCompiler(compiler.SQLCompiler):
c for c in self.statement.table.c if c.key not in ordered_keys
]
else:
# traverse in table column order
cols = self.statement.table.c
clauses = []
for column in cols:
val = on_duplicate.update.get(column.key)
if val is None:
continue
elif coercions._is_literal(val):
# traverses through all table columns to preserve table column order
for column in (col for col in cols if col.key in on_duplicate.update):
val = on_duplicate.update[column.key]
if coercions._is_literal(val):
val = elements.BindParameter(None, val, type_=column.type)
value_text = self.process(val.self_group(), use_schema=False)
elif isinstance(val, elements.BindParameter) and val.type._isnull:
+15
View File
@@ -62,6 +62,21 @@ class OnDuplicateTest(fixtures.TablesTest):
[(1, "ab", "bz", False)],
)
def test_on_duplicate_key_update_null(self):
foos = self.tables.foos
with testing.db.connect() as conn:
conn.execute(insert(foos, dict(id=1, bar="b", baz="bz")))
stmt = insert(foos).values(
[dict(id=1, bar="ab"), dict(id=2, bar="b")]
)
stmt = stmt.on_duplicate_key_update(updated_once=None)
result = conn.execute(stmt)
eq_(result.inserted_primary_key, [2])
eq_(
conn.execute(foos.select().where(foos.c.id == 1)).fetchall(),
[(1, "b", "bz", None)],
)
def test_on_duplicate_key_update_preserve_order(self):
foos = self.tables.foos
with testing.db.connect() as conn: