diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2020-03-04 17:44:40 -0500 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2020-03-04 17:46:27 -0500 |
| commit | 57b2aae0d9efe91c2338e5a762e04366f86c2651 (patch) | |
| tree | 8c44e58a44befcf032b02f79e1641516ff4e1f9a /test | |
| parent | 72cb8e8cbfe1a1b70e9085e46b4515287188547b (diff) | |
| download | sqlalchemy-57b2aae0d9efe91c2338e5a762e04366f86c2651.tar.gz | |
Render VALUES within composed MySQL on duplicate key expressions
Fixed issue in MySQL :meth:`.mysql.Insert.on_duplicate_key_update` construct
where using a SQL function or other composed expression for a column argument
would not properly render the ``VALUES`` keyword surrounding the column
itself.
Fixes: #5173
Change-Id: I16d39c2fdb8bbb7f3d1b2ffdd20e1bf69359ab75
Diffstat (limited to 'test')
| -rw-r--r-- | test/dialect/mysql/test_compiler.py | 25 | ||||
| -rw-r--r-- | test/dialect/mysql/test_on_duplicate.py | 17 |
2 files changed, 42 insertions, 0 deletions
diff --git a/test/dialect/mysql/test_compiler.py b/test/dialect/mysql/test_compiler.py index d59c0549f..e74c37d63 100644 --- a/test/dialect/mysql/test_compiler.py +++ b/test/dialect/mysql/test_compiler.py @@ -928,3 +928,28 @@ class InsertOnDuplicateTest(fixtures.TestBase, AssertsCompiledSQL): "ON DUPLICATE KEY UPDATE bar = %s" ) self.assert_compile(stmt, expected_sql) + + def test_update_sql_expr(self): + stmt = insert(self.table).values( + [{"id": 1, "bar": "ab"}, {"id": 2, "bar": "b"}] + ) + stmt = stmt.on_duplicate_key_update( + bar=func.coalesce(stmt.inserted.bar), + baz=stmt.inserted.baz + "some literal", + ) + expected_sql = ( + "INSERT INTO foos (id, bar) VALUES (%s, %s), (%s, %s) ON " + "DUPLICATE KEY UPDATE bar = coalesce(VALUES(bar)), " + "baz = (concat(VALUES(baz), %s))" + ) + self.assert_compile( + stmt, + expected_sql, + checkparams={ + "id_m0": 1, + "bar_m0": "ab", + "id_m1": 2, + "bar_m1": "b", + "baz_1": "some literal", + }, + ) diff --git a/test/dialect/mysql/test_on_duplicate.py b/test/dialect/mysql/test_on_duplicate.py index 077e5ba98..439648153 100644 --- a/test/dialect/mysql/test_on_duplicate.py +++ b/test/dialect/mysql/test_on_duplicate.py @@ -77,6 +77,23 @@ class OnDuplicateTest(fixtures.TablesTest): [(1, "b", "bz", None)], ) + def test_on_duplicate_key_update_expression(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( + bar=func.concat(stmt.inserted.bar, "_foo") + ) + result = conn.execute(stmt) + eq_(result.inserted_primary_key, [2]) + eq_( + conn.execute(foos.select().where(foos.c.id == 1)).fetchall(), + [(1, "ab_foo", "bz", False)], + ) + def test_on_duplicate_key_update_preserve_order(self): foos = self.tables.foos with testing.db.connect() as conn: |
