Author: russellm
Date: 2007-01-03 22:00:16 -0600 (Wed, 03 Jan 2007)
New Revision: 4283

Modified:
  django/trunk/django/db/models/query.py
  django/trunk/tests/modeltests/or_lookups/models.py
Log:
Fixed #2473 -- Added special case for '__in=[]' (empty set) queries, because 
'WHERE attr IN ()' is invalid SQL on many backends. Thanks, Gary Wilson.


Modified: django/trunk/django/db/models/query.py
===================================================================
--- django/trunk/django/db/models/query.py      2007-01-03 22:40:06 UTC (rev 
4282)
+++ django/trunk/django/db/models/query.py      2007-01-04 04:00:16 UTC (rev 
4283)
@@ -641,7 +641,15 @@
    except KeyError:
        pass
    if lookup_type == 'in':
-        return '%s%s IN (%s)' % (table_prefix, field_name, ','.join(['%s' for 
v in value]))
+        in_string = ','.join(['%s' for id in value])
+        if in_string:
+            return '%s%s IN (%s)' % (table_prefix, field_name, in_string)
+        else:
+            # Most backends do not accept an empty string inside the IN
+            # expression, i.e. cannot do "WHERE ... IN ()".  Since there are
+            # also some backends that do not accept "WHERE false", we instead
+            # use an expression that always evaluates to False.
+            return '0=1'
    elif lookup_type == 'range':
        return '%s%s BETWEEN %%s AND %%s' % (table_prefix, field_name)
    elif lookup_type in ('year', 'month', 'day'):

Modified: django/trunk/tests/modeltests/or_lookups/models.py
===================================================================
--- django/trunk/tests/modeltests/or_lookups/models.py  2007-01-03 22:40:06 UTC 
(rev 4282)
+++ django/trunk/tests/modeltests/or_lookups/models.py  2007-01-04 04:00:16 UTC 
(rev 4283)
@@ -69,6 +69,21 @@
>>> Article.objects.filter(Q(pk=1) | Q(pk=2) | Q(pk=3))
[<Article: Hello>, <Article: Goodbye>, <Article: Hello and goodbye>]

+# You could also use "in" to accomplish the same as above.
+>>> Article.objects.filter(pk__in=[1,2,3])
+[<Article: Hello>, <Article: Goodbye>, <Article: Hello and goodbye>]
+
+>>> Article.objects.filter(pk__in=[1,2,3,4])
+[<Article: Hello>, <Article: Goodbye>, <Article: Hello and goodbye>]
+
+# Passing "in" an empty list returns no results ...
+>>> Article.objects.filter(pk__in=[])
+[]
+
+# ... but can return results if we OR it with another query.
+>>> Article.objects.filter(Q(pk__in=[]) | Q(headline__icontains='goodbye'))
+[<Article: Goodbye>, <Article: Hello and goodbye>]
+
# Q arg objects are ANDed
>>> Article.objects.filter(Q(headline__startswith='Hello'), 
Q(headline__contains='bye'))
[<Article: Hello and goodbye>]


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django 
updates" group.
To post to this group, send email to django-updates@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to