From: Mike Bayer Date: Wed, 28 Sep 2016 22:01:57 +0000 (-0400) Subject: Enable include_table for ON CONFLICT whereclauses X-Git-Tag: rel_1_1_0~16 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=800a18aff2927433163afec3b7a4671eabe1c2e3;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Enable include_table for ON CONFLICT whereclauses 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 --- diff --git a/doc/build/changelog/changelog_11.rst b/doc/build/changelog/changelog_11.rst index f9b99b3481..af22fd6bc8 100644 --- a/doc/build/changelog/changelog_11.rst +++ b/doc/build/changelog/changelog_11.rst @@ -21,6 +21,14 @@ .. 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 diff --git a/lib/sqlalchemy/dialects/postgresql/base.py b/lib/sqlalchemy/dialects/postgresql/base.py index 09d36349fa..a9f11aae01 100644 --- a/lib/sqlalchemy/dialects/postgresql/base.py +++ b/lib/sqlalchemy/dialects/postgresql/base.py @@ -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 ) diff --git a/test/dialect/postgresql/test_compiler.py b/test/dialect/postgresql/test_compiler.py index 88110ba2df..ac8bb4815b 100644 --- a/test/dialect/postgresql/test_compiler.py +++ b/test/dialect/postgresql/test_compiler.py @@ -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"))