]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Enable include_table for ON CONFLICT whereclauses
authorMike Bayer <mike_mp@zzzcomputing.com>
Wed, 28 Sep 2016 22:01:57 +0000 (18:01 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 28 Sep 2016 22:08:30 +0000 (18:08 -0400)
Fixed issue in new PG "on conflict" construct where columns including
those of the "excluded" namespace would not be table-qualified
in the WHERE clauses in the statement.

Change-Id: Idfefc93e7e7b0d84805e23d5436d822d606f6a0a
Fixes: #3807
doc/build/changelog/changelog_11.rst
lib/sqlalchemy/dialects/postgresql/base.py
test/dialect/postgresql/test_compiler.py

index f9b99b3481a17a8352e31a75a134163031886f83..af22fd6bc82adfe7b212988999b35b54adeaaea8 100644 (file)
 .. changelog::
     :version: 1.1.0
 
+    .. change::
+        :tags: bug, postgresql
+        :tickets: 3807
+
+        Fixed issue in new PG "on conflict" construct where columns including
+        those of the "excluded" namespace would not be table-qualified
+        in the WHERE clauses in the statement.
+
     .. change::
         :tags: bug, sql, mysql
         :tickets: 3803
index 09d36349fa4fda2acd93569fd3625255873c2960..a9f11aae01c6f4cd92917a1186bbb87540f290d4 100644 (file)
@@ -1431,7 +1431,6 @@ class PGCompiler(compiler.SQLCompiler):
                 target_text += ' WHERE %s' % \
                     self.process(
                         clause.inferred_target_whereclause,
-                        include_table=False,
                         use_schema=False
                     )
         else:
@@ -1471,7 +1470,7 @@ class PGCompiler(compiler.SQLCompiler):
             action_text += ' WHERE %s' % \
                 self.process(
                     clause.update_whereclause,
-                    include_table=False,
+                    include_table=True,
                     use_schema=False
                 )
 
index 88110ba2df12635e7eedf232a57e7574cefb5498..ac8bb4815ba4fb4fb2b42621e8c62f96f66691bd 100644 (file)
@@ -1205,7 +1205,7 @@ class InsertOnConflictTest(fixtures.TestBase, AssertsCompiledSQL):
         self.assert_compile(i,
                             'INSERT INTO mytable (name) VALUES '
                             "(%(name)s) ON CONFLICT (name) "
-                            "WHERE name > %(name_1)s "
+                            "WHERE mytable.name > %(name_1)s "
                             'DO UPDATE SET name = excluded.name')
 
     def test_do_update_unnamed_index_target(self):
@@ -1224,7 +1224,7 @@ class InsertOnConflictTest(fixtures.TestBase, AssertsCompiledSQL):
         self.assert_compile(i,
                             'INSERT INTO mytable (name) VALUES '
                             "(%(name)s) ON CONFLICT (name) "
-                            "WHERE name > %(name_1)s "
+                            "WHERE mytable.name > %(name_1)s "
                             'DO UPDATE SET name = excluded.name')
 
     def test_do_update_unnamed_exclude_constraint_target(self):
@@ -1237,7 +1237,7 @@ class InsertOnConflictTest(fixtures.TestBase, AssertsCompiledSQL):
         self.assert_compile(i,
                             'INSERT INTO mytable (name) VALUES '
                             "(%(name)s) ON CONFLICT (name, description) "
-                            "WHERE description != %(description_1)s "
+                            "WHERE mytable.description != %(description_1)s "
                             'DO UPDATE SET name = excluded.name')
 
     def test_do_update_add_whereclause(self):
@@ -1253,10 +1253,26 @@ class InsertOnConflictTest(fixtures.TestBase, AssertsCompiledSQL):
         self.assert_compile(i,
                             'INSERT INTO mytable (name) VALUES '
                             "(%(name)s) ON CONFLICT (name, description) "
-                            "WHERE description != %(description_1)s "
+                            "WHERE mytable.description != %(description_1)s "
                             'DO UPDATE SET name = excluded.name '
-                            "WHERE name != %(name_1)s "
-                            "AND description != %(description_2)s")
+                            "WHERE mytable.name != %(name_1)s "
+                            "AND mytable.description != %(description_2)s")
+
+    def test_do_update_add_whereclause_references_excluded(self):
+        i = insert(
+            self.table1, values=dict(name='foo'))
+        i = i.on_conflict_do_update(
+            constraint=self.excl_constr_anon,
+            set_=dict(name=i.excluded.name),
+            where=(
+                (self.table1.c.name != i.excluded.name))
+        )
+        self.assert_compile(i,
+                            'INSERT INTO mytable (name) VALUES '
+                            "(%(name)s) ON CONFLICT (name, description) "
+                            "WHERE mytable.description != %(description_1)s "
+                            'DO UPDATE SET name = excluded.name '
+                            "WHERE mytable.name != excluded.name")
 
     def test_quote_raw_string_col(self):
         t = table('t', column("FancyName"), column("other name"))