#30011: Count with filter annotation bug on filter
-------------------------------------+-------------------------------------
     Reporter:  Taqi Abbas           |                    Owner:  nobody
         Type:  Uncategorized        |                   Status:  new
    Component:  Database layer       |                  Version:  2.1
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  Annotation, Count,   |             Triage Stage:
  Filter                             |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Raphael Kimmig):

 I've got a somewhat smaller repro here, the bug seems to require both a
 filtered annotation and a nested filter.

 {{{
 from django.db.models import Count, Q
 from django.test import TestCase
 from django.db import models, OperationalError


 class Thing(models.Model):
     pass


 class RelatedThing(models.Model):
     thing = models.ForeignKey(
         Thing, on_delete=models.CASCADE,
     )


 class TestCountExtraArg(TestCase):
     def setUp(self):
         t = Thing.objects.create()
         t.relatedthing_set.create()

     def test_issue_not_triggered_without_filter_in_(self):
         queryset = Thing.objects.annotate(
             related_count=Count("relatedthing")
         )
         result = queryset.filter(id__in=queryset.values("id"))

         self.assertEqual(result[0].related_count, 1, result)

     def test_issue_not_triggered_without_nested_query(self):
         queryset = Thing.objects.annotate(
             related_count=Count("relatedthing", filter=Q(id__gt=0))
         )
         result = queryset.filter(id__in=list(queryset.values_list("id",
 flat=True)))
         self.assertEqual(result[0].related_count, 1, result)

     def test_triggering_issue(self):
         queryset = Thing.objects.annotate(
             related_count=Count("relatedthing", filter=Q(id__gt=0))
         )
         result = queryset.filter(id__in=queryset.values("id"))

         with self.assertRaises(OperationalError):
             self.assertEqual(result[0].related_count, 1, result)
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/30011#comment:3>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/065.e5d1f7c0f5d663b22a27e88602a4c27b%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to