Code

Ticket #2473: where_0=1.diff

File where_0=1.diff, 2.0 KB (added by Gary Wilson <gary.wilson@…>, 8 years ago)

used where 0=1 instead of where false

Line 
1=== modified file 'django/db/models/query.py'
2--- django/db/models/query.py   2006-12-19 04:35:09 +0000
3+++ django/db/models/query.py   2007-01-03 23:38:06 +0000
4@@ -641,7 +641,15 @@
5     except KeyError:
6         pass
7     if lookup_type == 'in':
8-        return '%s%s IN (%s)' % (table_prefix, field_name, ','.join(['%s' for v in value]))
9+        in_string = ','.join(['%s' for id in value])
10+        if in_string:
11+            return '%s%s IN (%s)' % (table_prefix, field_name, in_string)
12+        else:
13+            # Most backends do not accept an empty string inside the IN
14+            # expression, i.e. cannot do "WHERE ... IN ()".  Since there are
15+            # also some backends that do not accept "WHERE false", we instead
16+            # use an expression that always evaluates to False.
17+            return '0=1'
18     elif lookup_type == 'range':
19         return '%s%s BETWEEN %%s AND %%s' % (table_prefix, field_name)
20     elif lookup_type in ('year', 'month', 'day'):
21
22=== modified file 'tests/modeltests/or_lookups/models.py'
23--- tests/modeltests/or_lookups/models.py       2006-12-19 04:35:09 +0000
24+++ tests/modeltests/or_lookups/models.py       2006-12-21 17:32:31 +0000
25@@ -69,6 +69,21 @@
26 >>> Article.objects.filter(Q(pk=1) | Q(pk=2) | Q(pk=3))
27 [<Article: Hello>, <Article: Goodbye>, <Article: Hello and goodbye>]
28 
29+# You could also use "in" to accomplish the same as above.
30+>>> Article.objects.filter(pk__in=[1,2,3])
31+[<Article: Hello>, <Article: Goodbye>, <Article: Hello and goodbye>]
32+
33+>>> Article.objects.filter(pk__in=[1,2,3,4])
34+[<Article: Hello>, <Article: Goodbye>, <Article: Hello and goodbye>]
35+
36+# Passing "in" an empty list returns no results.
37+>>> Article.objects.filter(pk__in=[])
38+[]
39+
40+# But can return results if we OR it with another query.
41+>>> Article.objects.filter(Q(pk__in=[]) | Q(headline__icontains='goodbye'))
42+[<Article: Goodbye>, <Article: Hello and goodbye>]
43+
44 # Q arg objects are ANDed
45 >>> Article.objects.filter(Q(headline__startswith='Hello'), Q(headline__contains='bye'))
46 [<Article: Hello and goodbye>]
47