summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2014-05-08 22:44:06 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2014-05-08 22:44:06 -0400
commit55eacc8dbea3c3f98197bde9034fd6558fb2bc09 (patch)
treef4fedb84a3cd05166f40c34f6b2e06e5839fa619
parentc8873b31f0c87ba0d1a7518b36af7151dec34be4 (diff)
downloadsqlalchemy-55eacc8dbea3c3f98197bde9034fd6558fb2bc09.tar.gz
- 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
-rw-r--r--doc/build/changelog/changelog_08.rst10
-rw-r--r--lib/sqlalchemy/sql/compiler.py9
-rw-r--r--test/sql/test_compiler.py9
-rw-r--r--test/sql/test_delete.py14
-rw-r--r--test/sql/test_update.py11
5 files changed, 49 insertions, 4 deletions
diff --git a/doc/build/changelog/changelog_08.rst b/doc/build/changelog/changelog_08.rst
index 19f99bbde..656858720 100644
--- a/doc/build/changelog/changelog_08.rst
+++ b/doc/build/changelog/changelog_08.rst
@@ -12,6 +12,16 @@
: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
:versions: 0.9.5
diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py
index a7465204a..cd01ea5e5 100644
--- a/lib/sqlalchemy/sql/compiler.py
+++ b/lib/sqlalchemy/sql/compiler.py
@@ -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(
diff --git a/test/sql/test_compiler.py b/test/sql/test_compiler.py
index 1be76c696..917c7d89d 100644
--- a/test/sql/test_compiler.py
+++ b/test/sql/test_compiler.py
@@ -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(
diff --git a/test/sql/test_delete.py b/test/sql/test_delete.py
index b56731515..64173bb00 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 829739bcc..a08d5f672 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