Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #13156 -- Ensure that exists() queries are as fast as they can …

…be. Thanks to Jerome Leclanche for the report.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12810 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 9f6a82fafbd84865a06e162c14b6a5d5e110babe 1 parent d58020f
Russell Keith-Magee authored March 20, 2010
7  django/db/models/sql/query.py
@@ -377,10 +377,13 @@ def get_count(self, using):
377 377
     def has_results(self, using):
378 378
         q = self.clone()
379 379
         q.add_extra({'a': 1}, None, None, None, None, None)
380  
-        q.add_fields(())
  380
+        q.select = []
  381
+        q.select_fields = []
  382
+        q.default_cols = False
  383
+        q.select_related = False
381 384
         q.set_extra_mask(('a',))
382 385
         q.set_aggregate_mask(())
383  
-        q.clear_ordering()
  386
+        q.clear_ordering(True)
384 387
         q.set_limits(high=1)
385 388
         compiler = q.get_compiler(using=using)
386 389
         return bool(compiler.execute_sql(SINGLE))
10  tests/regressiontests/queries/models.py
@@ -277,6 +277,16 @@ def __unicode__(self):
277 277
 
278 278
 
279 279
 __test__ = {'API_TESTS':"""
  280
+>>> # Regression for #13156 -- exists() queries have minimal SQL
  281
+>>> from django.db import connection
  282
+>>> settings.DEBUG = True
  283
+>>> Tag.objects.exists()
  284
+False
  285
+>>> # Ok - so the exist query worked - but did it include too many columns?
  286
+>>> "id" not in connection.queries[-1]['sql'] and "name" not in connection.queries[-1]['sql']
  287
+True
  288
+>>> settings.DEBUG = False
  289
+
280 290
 >>> generic = NamedCategory.objects.create(name="Generic")
281 291
 >>> t1 = Tag.objects.create(name='t1', category=generic)
282 292
 >>> t2 = Tag.objects.create(name='t2', parent=t1, category=generic)

0 notes on commit 9f6a82f

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