Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #10181 -- Handle an EmptyResultSet exception case properly in n…

…ested querysets.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@9951 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 851461aa72cc94e7000c3c7713e1d9fb130e0fda 1 parent a605a8f
Malcolm Tredinnick authored March 02, 2009
2  django/db/models/sql/where.py
@@ -50,7 +50,7 @@ def add(self, data, connector):
50 50
         if hasattr(obj, "process"):
51 51
             try:
52 52
                 obj, params = obj.process(lookup_type, value)
53  
-            except EmptyShortCircuit:
  53
+            except (EmptyShortCircuit, EmptyResultSet):
54 54
                 # There are situations where we want to short-circuit any
55 55
                 # comparisons and make sure that nothing is returned. One
56 56
                 # example is when checking for a NULL pk value, or the
13  tests/regressiontests/queries/models.py
@@ -1048,11 +1048,14 @@ def __unicode__(self):
1048 1048
 [<Annotation: a1>]
1049 1049
 
1050 1050
 Nested queries should not evaluate the inner query as part of constructing the
1051  
-SQL. This test verifies this: if the inner query is evaluated, the outer "in"
1052  
-lookup will raise an EmptyResultSet exception (as the inner query returns
1053  
-nothing).
1054  
->>> print Annotation.objects.filter(notes__in=Note.objects.filter(note="xyzzy")).query
1055  
-SELECT ...
  1051
+SQL (so we should see a nested query here, indicated by two "SELECT" calls).
  1052
+>>> Annotation.objects.filter(notes__in=Note.objects.filter(note="xyzzy")).query.as_sql()[0].count('SELECT')
  1053
+2
  1054
+
  1055
+Bug #10181 -- Avoid raising an EmptyResultSet if an inner query is provably
  1056
+empty (and hence, not executed).
  1057
+>>> Tag.objects.filter(id__in=Tag.objects.filter(id__in=[]))
  1058
+[]
1056 1059
 
1057 1060
 Bug #9997 -- If a ValuesList or Values queryset is passed as an inner query, we
1058 1061
 make sure it's only requesting a single value and use that as the thing to

0 notes on commit 851461a

Please sign in to comment.
Something went wrong with that request. Please try again.