]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Fixed issue in MySQL :meth:`.mysql.Insert.on_duplicate_key_update` construct
authorCristian Sabaila <cristian.sabaila@geotogether.com>
Tue, 2 Nov 2021 22:16:49 +0000 (00:16 +0200)
committerCristian Sabaila <cristian.sabaila@geotogether.com>
Tue, 2 Nov 2021 22:18:56 +0000 (00:18 +0200)
where referencing a column within a composed expression would render
the ``VALUES`` keyword to the column name of the argument.

lib/sqlalchemy/dialects/mysql/base.py
test/dialect/mysql/test_compiler.py
test/dialect/mysql/test_on_duplicate.py

index 4827df12f1cef06b60d246662465fa6c9130d88a..684f35c5f8b92446b73c5158f277abc563f9a749 100644 (file)
@@ -1302,7 +1302,7 @@ class MySQLCompiler(compiler.SQLCompiler):
                         and obj.table is on_duplicate.inserted_alias
                     ):
                         obj = literal_column(
-                            "VALUES(" + self.preparer.quote(column.name) + ")"
+                            "VALUES(" + self.preparer.quote(obj.name) + ")"
                         )
                         return obj
                     else:
index 708039f943e302746313fc1ac03f61987d7a8bac..a6ead333fa5062609c051fe3ff2577b3bfee43cf 100644 (file)
@@ -1110,12 +1110,12 @@ class InsertOnDuplicateTest(fixtures.TestBase, AssertsCompiledSQL):
         )
         stmt = stmt.on_duplicate_key_update(
             bar=func.coalesce(stmt.inserted.bar),
-            baz=stmt.inserted.baz + "some literal",
+            baz=stmt.inserted.baz + "some literal" + stmt.inserted.bar,
         )
         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))"
+            "baz = (concat(VALUES(bar), %s, VALUES(baz)))"
         )
         self.assert_compile(
             stmt,
index 65d5b8364e7df44ac00101fdcbe174b91f5e01de..4c8307d9077bd8aca6823fed4fe6fac353063bd1 100644 (file)
@@ -100,13 +100,18 @@ class OnDuplicateTest(fixtures.TablesTest):
         conn.execute(insert(foos).values(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")
+            bar=func.concat(stmt.inserted.bar, "_foo"),
+            baz=func.concat(stmt.inserted.bar, foos.baz),
         )
         result = conn.execute(stmt)
         eq_(result.inserted_primary_key, (None,))
         eq_(
             conn.execute(foos.select().where(foos.c.id == 1)).fetchall(),
-            [(1, "ab_foo", "bz", False)],
+            [(1, "ab_foo", "ab_bz", False)],
+        )
+        eq_(
+            conn.execute(foos.select().where(foos.c.id == 1)).fetchall(),
+            [(2, "b_foo", "b_bz", False)],
         )
 
     def test_on_duplicate_key_update_preserve_order(self, connection):