Skip to content

Commit

Permalink
Fixed #13156 -- Ensure that exists() queries are as fast as they can …
Browse files Browse the repository at this point in the history
…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
freakboy3742 committed Mar 20, 2010
1 parent d58020f commit 9f6a82f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
7 changes: 5 additions & 2 deletions django/db/models/sql/query.py
Expand Up @@ -377,10 +377,13 @@ def get_count(self, using):
def has_results(self, using): def has_results(self, using):
q = self.clone() q = self.clone()
q.add_extra({'a': 1}, None, None, None, None, None) q.add_extra({'a': 1}, None, None, None, None, None)
q.add_fields(()) q.select = []
q.select_fields = []
q.default_cols = False
q.select_related = False
q.set_extra_mask(('a',)) q.set_extra_mask(('a',))
q.set_aggregate_mask(()) q.set_aggregate_mask(())
q.clear_ordering() q.clear_ordering(True)
q.set_limits(high=1) q.set_limits(high=1)
compiler = q.get_compiler(using=using) compiler = q.get_compiler(using=using)
return bool(compiler.execute_sql(SINGLE)) return bool(compiler.execute_sql(SINGLE))
Expand Down
10 changes: 10 additions & 0 deletions tests/regressiontests/queries/models.py
Expand Up @@ -277,6 +277,16 @@ def __unicode__(self):




__test__ = {'API_TESTS':""" __test__ = {'API_TESTS':"""
>>> # Regression for #13156 -- exists() queries have minimal SQL
>>> from django.db import connection
>>> settings.DEBUG = True
>>> Tag.objects.exists()
False
>>> # Ok - so the exist query worked - but did it include too many columns?
>>> "id" not in connection.queries[-1]['sql'] and "name" not in connection.queries[-1]['sql']
True
>>> settings.DEBUG = False
>>> generic = NamedCategory.objects.create(name="Generic") >>> generic = NamedCategory.objects.create(name="Generic")
>>> t1 = Tag.objects.create(name='t1', category=generic) >>> t1 = Tag.objects.create(name='t1', category=generic)
>>> t2 = Tag.objects.create(name='t2', parent=t1, category=generic) >>> t2 = Tag.objects.create(name='t2', parent=t1, category=generic)
Expand Down

0 comments on commit 9f6a82f

Please sign in to comment.