From 927abc3b33f10464ed04db3d7a454faeb6e729f2 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Fri, 13 May 2022 16:08:34 -0400 Subject: render col name in on conflict set clause, not given key Fixed bug where the PostgreSQL :meth:`_postgresql.Insert.on_conflict` method and the SQLite :meth:`_sqlite.Insert.on_conflict` method would both fail to correctly accommodate a column with a separate ".key" when specifying the column using its key name in the dictionary passed to ``set_``, as well as if the :attr:`_sqlite.Insert.excluded` or :attr:`_postgresql.Insert.excluded` collection were used as the dictionary directly. Fixes: #8014 Change-Id: I67226aeedcb2c683e22405af64720cc1f990f274 --- test/dialect/test_sqlite.py | 47 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) (limited to 'test/dialect/test_sqlite.py') diff --git a/test/dialect/test_sqlite.py b/test/dialect/test_sqlite.py index 8e7632c90..fb4331998 100644 --- a/test/dialect/test_sqlite.py +++ b/test/dialect/test_sqlite.py @@ -2736,7 +2736,7 @@ class RegexpTest(fixtures.TestBase, testing.AssertsCompiledSQL): ) -class OnConflictTest(fixtures.TablesTest): +class OnConflictTest(AssertsCompiledSQL, fixtures.TablesTest): __only_on__ = ("sqlite >= 3.24.0",) __backend__ = True @@ -2750,6 +2750,13 @@ class OnConflictTest(fixtures.TablesTest): Column("name", String(50)), ) + Table( + "users_w_key", + metadata, + Column("id", Integer, primary_key=True), + Column("name", String(50), key="name_keyed"), + ) + class SpecialType(sqltypes.TypeDecorator): impl = String cache_ok = True @@ -2794,6 +2801,44 @@ class OnConflictTest(fixtures.TablesTest): ValueError, insert(self.tables.users).on_conflict_do_update ) + @testing.combinations("control", "excluded", "dict") + def test_set_excluded(self, scenario): + """test #8014, sending all of .excluded to set""" + + if scenario == "control": + users = self.tables.users + + stmt = insert(users) + self.assert_compile( + stmt.on_conflict_do_update(set_=stmt.excluded), + "INSERT INTO users (id, name) VALUES (?, ?) ON CONFLICT " + "DO UPDATE SET id = excluded.id, name = excluded.name", + ) + else: + users_w_key = self.tables.users_w_key + + stmt = insert(users_w_key) + + if scenario == "excluded": + self.assert_compile( + stmt.on_conflict_do_update(set_=stmt.excluded), + "INSERT INTO users_w_key (id, name) VALUES (?, ?) " + "ON CONFLICT " + "DO UPDATE SET id = excluded.id, name = excluded.name", + ) + else: + self.assert_compile( + stmt.on_conflict_do_update( + set_={ + "id": stmt.excluded.id, + "name_keyed": stmt.excluded.name_keyed, + } + ), + "INSERT INTO users_w_key (id, name) VALUES (?, ?) " + "ON CONFLICT " + "DO UPDATE SET id = excluded.id, name = excluded.name", + ) + def test_on_conflict_do_no_call_twice(self): users = self.tables.users -- cgit v1.2.1