summaryrefslogtreecommitdiff
path: root/test/dialect/test_sqlite.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2020-11-23 20:13:20 -0500
committerMike Bayer <mike_mp@zzzcomputing.com>2020-11-23 20:13:20 -0500
commit584cabbf7e79948e38b29df5af63c3c712566f31 (patch)
treec4e8451d7f4b794826d20ecf18b8252099fadf25 /test/dialect/test_sqlite.py
parentb4e40b35627f1c26b84234d16a36ce2850a798b9 (diff)
downloadsqlalchemy-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 'test/dialect/test_sqlite.py')
-rw-r--r--test/dialect/test_sqlite.py47
1 files changed, 47 insertions, 0 deletions
diff --git a/test/dialect/test_sqlite.py b/test/dialect/test_sqlite.py
index 456bad7bd..f8b50f888 100644
--- a/test/dialect/test_sqlite.py
+++ b/test/dialect/test_sqlite.py
@@ -2921,6 +2921,53 @@ class OnConflictTest(fixtures.TablesTest):
[(10, "I'm a name")],
)
+ def test_on_conflict_do_update_column_keys(self, connection):
+ users = self.tables.users
+
+ conn = connection
+ conn.execute(users.insert(), dict(id=1, name="name1"))
+
+ i = insert(users)
+ i = i.on_conflict_do_update(
+ index_elements=users.primary_key.columns,
+ set_={users.c.id: 10, users.c.name: "I'm a name"},
+ ).values(id=1, name="name4")
+
+ result = conn.execute(i)
+ eq_(result.inserted_primary_key, (1,))
+
+ eq_(
+ conn.execute(users.select().where(users.c.id == 10)).fetchall(),
+ [(10, "I'm a name")],
+ )
+
+ def test_on_conflict_do_update_clauseelem_keys(self, connection):
+ users = self.tables.users
+
+ class MyElem(object):
+ def __init__(self, expr):
+ self.expr = expr
+
+ def __clause_element__(self):
+ return self.expr
+
+ conn = connection
+ conn.execute(users.insert(), dict(id=1, name="name1"))
+
+ i = insert(users)
+ i = i.on_conflict_do_update(
+ index_elements=users.primary_key.columns,
+ set_={MyElem(users.c.id): 10, MyElem(users.c.name): "I'm a name"},
+ ).values({MyElem(users.c.id): 1, MyElem(users.c.name): "name4"})
+
+ result = conn.execute(i)
+ eq_(result.inserted_primary_key, (1,))
+
+ eq_(
+ conn.execute(users.select().where(users.c.id == 10)).fetchall(),
+ [(10, "I'm a name")],
+ )
+
def test_on_conflict_do_update_multivalues(self, connection):
users = self.tables.users