diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index 9a19a9ea95..ffc9d3b13f 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -377,10 +377,13 @@ class Query(object): def has_results(self, using): q = self.clone() 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_aggregate_mask(()) - q.clear_ordering() + q.clear_ordering(True) q.set_limits(high=1) compiler = q.get_compiler(using=using) return bool(compiler.execute_sql(SINGLE)) diff --git a/tests/regressiontests/queries/models.py b/tests/regressiontests/queries/models.py index 43c843756c..06c4524d9d 100644 --- a/tests/regressiontests/queries/models.py +++ b/tests/regressiontests/queries/models.py @@ -277,6 +277,16 @@ class Plaything(models.Model): __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") >>> t1 = Tag.objects.create(name='t1', category=generic) >>> t2 = Tag.objects.create(name='t2', parent=t1, category=generic)