]> 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:45:01 +0000 (22:45 -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 27734f0f4e52b68624bb8409533eb5be1d2be15d..a9d6cc2dab32533ee0ca78344197f7d5c42a96b8 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 a363c546621454161f32740e0708017697d5d637..f87d9d753933ede750de4ab4eb17cced8314e42b 100644 (file)
@@ -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(
index 21cc7b4b45eda8df56f9362a664aaa7a3092bdbe..c88d58696266041988b9c2cb866bdde28862bc77 100644 (file)
@@ -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(
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 b8e2d837ebec48b04e2b54c583cc2807878a7a5b..1422ce5a2da8ecc72e8a22e326b0c491e3cc00ef 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