From: Mike Bayer Date: Fri, 9 May 2014 02:44:06 +0000 (-0400) Subject: - Fixed bug where :meth:`.Table.update` and :meth:`.Table.delete` X-Git-Tag: rel_0_8_7~34 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=93e11a9d7d58f777099902edfd338a21b4060320;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - Fixed bug where :meth:`.Table.update` and :meth:`.Table.delete` would produce an empty WHERE clause when an empty :func:`.and_()` or :func:`.or_()` or other blank expression were applied. This is now consistent with that of :func:`.select`. fixes #3045 --- diff --git a/doc/build/changelog/changelog_08.rst b/doc/build/changelog/changelog_08.rst index 27734f0f4e..a9d6cc2dab 100644 --- a/doc/build/changelog/changelog_08.rst +++ b/doc/build/changelog/changelog_08.rst @@ -11,6 +11,16 @@ .. changelog:: :version: 0.8.7 + .. change:: + :tags: bug, sql + :tickets: 3045 + :versions: 0.9.5 + + Fixed bug where :meth:`.Table.update` and :meth:`.Table.delete` + would produce an empty WHERE clause when an empty :func:`.and_()` + or :func:`.or_()` or other blank expression were applied. This is + now consistent with that of :func:`.select`. + .. change:: :tags: bug, postgresql :pullreq: bitbucket:13 diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py index a363c54662..f87d9d7539 100644 --- a/lib/sqlalchemy/sql/compiler.py +++ b/lib/sqlalchemy/sql/compiler.py @@ -1536,7 +1536,9 @@ class SQLCompiler(engine.Compiled): text += " " + extra_from_text if update_stmt._whereclause is not None: - text += " WHERE " + self.process(update_stmt._whereclause) + t = self.process(update_stmt._whereclause) + if t: + text += " WHERE " + t limit_clause = self.update_limit_clause(update_stmt) if limit_clause: @@ -1864,8 +1866,9 @@ class SQLCompiler(engine.Compiled): delete_stmt, delete_stmt._returning) if delete_stmt._whereclause is not None: - text += " WHERE " - text += delete_stmt._whereclause._compiler_dispatch(self) + t = delete_stmt._whereclause._compiler_dispatch(self) + if t: + text += " WHERE " + t if self.returning and not self.returning_precedes_values: text += " " + self.returning_clause( diff --git a/test/sql/test_compiler.py b/test/sql/test_compiler.py index 21cc7b4b45..c88d586962 100644 --- a/test/sql/test_compiler.py +++ b/test/sql/test_compiler.py @@ -829,6 +829,15 @@ class SelectTest(fixtures.TestBase, AssertsCompiledSQL): ) + def test_where_empty(self): + self.assert_compile( + select([table1.c.myid]).where(and_()), + "SELECT mytable.myid FROM mytable" + ) + self.assert_compile( + select([table1.c.myid]).where(or_()), + "SELECT mytable.myid FROM mytable" + ) def test_multiple_col_binds(self): self.assert_compile( diff --git a/test/sql/test_delete.py b/test/sql/test_delete.py index b567315153..64173bb008 100644 --- a/test/sql/test_delete.py +++ b/test/sql/test_delete.py @@ -1,6 +1,6 @@ #! coding:utf-8 -from sqlalchemy import Column, Integer, String, Table, delete, select +from sqlalchemy import Column, Integer, String, Table, delete, select, and_, or_ from sqlalchemy.dialects import mysql from sqlalchemy.testing import AssertsCompiledSQL, fixtures @@ -39,6 +39,18 @@ class DeleteTest(_DeleteTestBase, fixtures.TablesTest, AssertsCompiledSQL): 'WHERE mytable.myid = :myid_1 ' 'AND mytable.name = :name_1') + def test_where_empty(self): + table1 = self.tables.mytable + + self.assert_compile( + table1.delete().where(and_()), + "DELETE FROM mytable" + ) + self.assert_compile( + table1.delete().where(or_()), + "DELETE FROM mytable" + ) + def test_prefix_with(self): table1 = self.tables.mytable diff --git a/test/sql/test_update.py b/test/sql/test_update.py index b8e2d837eb..1422ce5a2d 100644 --- a/test/sql/test_update.py +++ b/test/sql/test_update.py @@ -177,6 +177,17 @@ class UpdateTest(_UpdateFromTestBase, fixtures.TablesTest, AssertsCompiledSQL): 'mytable.myid = hoho(:hoho_1) AND ' 'mytable.name = :param_2 || mytable.name || :param_3') + def test_where_empty(self): + table1 = self.tables.mytable + self.assert_compile( + table1.update().where(and_()), + "UPDATE mytable SET myid=:myid, name=:name, description=:description" + ) + self.assert_compile( + table1.update().where(or_()), + "UPDATE mytable SET myid=:myid, name=:name, description=:description" + ) + def test_prefix_with(self): table1 = self.tables.mytable