]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- Fixed bug where :meth:`.Table.update` and :meth:`.Table.delete`
authorMike Bayer <mike_mp@zzzcomputing.com>
Fri, 9 May 2014 02:44:06 +0000 (22:44 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 9 May 2014 02:44:06 +0000 (22:44 -0400)
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

doc/build/changelog/changelog_08.rst
lib/sqlalchemy/sql/compiler.py
test/sql/test_compiler.py
test/sql/test_delete.py
test/sql/test_update.py

index 19f99bbdedcf91e09dff5bbce0d2f0959a175445..656858720c87b70ab22dc9e122f4ebedf9984ea3 100644 (file)
 .. 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
index a7465204a67907927e99e14308158d64d490d521..cd01ea5e5656904241a8612b5b6fc44e99a36bc3 100644 (file)
@@ -1839,7 +1839,9 @@ class SQLCompiler(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:
@@ -2261,8 +2263,9 @@ class SQLCompiler(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(
index 1be76c69626818f6171fb0da2c11a5cfd63968fb..917c7d89da19e26b0409d1c98260949f8a63ad32 100644 (file)
@@ -962,6 +962,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(
index b567315153add4b7bba55d9a5ca2b561d8ea21dc..64173bb008c8445ea77a1835c7fc45db0f358d69 100644 (file)
@@ -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
 
index 829739bcc62d6baa6cce1d7cea4afd8f56287d0b..a08d5f672cc518b4a4b2328cd0dca96dc4c1e32d 100644 (file)
@@ -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