Commit 801a84ae authored by Paweł Marczewski's avatar Paweł Marczewski Committed by Tim Graham
Browse files

Fixed #24835 -- Fixed QuerySet.exists() after an annotation with Count()

QuerySet.exists() incorrectly handled query.group_by = True
case (grouping by all select fields), causing GROUP BY
expressions to be wiped along with select fields.
parent 4df7e848
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -489,6 +489,9 @@ class Query(object):
    def has_results(self, using):
        q = self.clone()
        if not q.distinct:
            if q.group_by is True:
                q.add_fields((f.attname for f in self.model._meta.concrete_fields), False)
                q.set_group_by()
            q.clear_select_clause()
        q.clear_ordering(True)
        q.set_limits(high=1)
+3 −0
Original line number Diff line number Diff line
@@ -14,3 +14,6 @@ Bugfixes

* Fixed crash during :djadmin:`makemigrations` if a migrations module either
  is missing ``__init__.py`` or is a file (:ticket:`24848`).

* Fixed ``exists()`` returning incorrect results after annotation with
  ``Count()`` (:ticket:`24835`).
+4 −0
Original line number Diff line number Diff line
@@ -325,6 +325,10 @@ class NonAggregateAnnotationTestCase(TestCase):
            lambda a: (a['age'], a['age_count'])
        )

    def test_annotate_exists(self):
        authors = Author.objects.annotate(c=Count('id')).filter(c__gt=1)
        self.assertFalse(authors.exists())

    def test_column_field_ordering(self):
        """
        Test that columns are aligned in the correct order for