diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2016-09-28 18:01:57 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2016-09-28 18:08:30 -0400 |
commit | 800a18aff2927433163afec3b7a4671eabe1c2e3 (patch) | |
tree | d96a7cda2bf6d576a526d52c70c8a63e6b1a0e56 | |
parent | f09056323e2c7b51ecdf02f8c31e7529382ed0f8 (diff) | |
download | sqlalchemy-800a18aff2927433163afec3b7a4671eabe1c2e3.tar.gz |
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
-rw-r--r-- | doc/build/changelog/changelog_11.rst | 8 | ||||
-rw-r--r-- | lib/sqlalchemy/dialects/postgresql/base.py | 3 | ||||
-rw-r--r-- | test/dialect/postgresql/test_compiler.py | 28 |
3 files changed, 31 insertions, 8 deletions
diff --git a/doc/build/changelog/changelog_11.rst b/doc/build/changelog/changelog_11.rst index f9b99b348..af22fd6bc 100644 --- a/doc/build/changelog/changelog_11.rst +++ b/doc/build/changelog/changelog_11.rst @@ -22,6 +22,14 @@ :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 09d36349f..a9f11aae0 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 88110ba2d..ac8bb4815 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")) |