#34464: QuerySet.count() regression on Django 4.2 with values() and annotate()
-------------------------------------+-------------------------------------
               Reporter:  Ian        |          Owner:  nobody
                   Type:  Bug        |         Status:  new
              Component:  Database   |        Version:  4.2
  layer (models, ORM)                |
               Severity:  Normal     |       Keywords:  postgresql
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 After upgrading to Django 4.2 we noticed incorrect results on some queries
 we use in reporting.

 We are using PostgreSQL as our database.
 Using psycopg2 or the new psycopg 3 support does not change the result.

 Sample model:

 {{{
 #!python
 class SampleModel(models.Model):
     sample_datetime_field = models.DateTimeField()
 }}}

 Example code to reproduce the issue:

 {{{
 #!python
 from bug_test.models import SampleModel
 from django.db.models import Count
 from django.db.models.functions import TruncHour
 from django.utils import timezone

 # Prepopulate 10 hours of sample data

 now = timezone.now()

 for i in range(10):
     field_value = now - timedelta(hours=1 + i)

     for i in range(5):
         SampleModel.objects.create(sample_datetime_field=field_value)

 # Affected query example

 qs =
 SampleModel.objects.all().annotate(hour=TruncHour('sample_datetime_field')).\
 values('hour').annotate(hour_count=Count('*')).filter(hour_count__gte=5)

 print(f"Count: {qs.count()}")  # Incorrectly returns 0. Returns correct
 result on Django 4.1
 print(f"Len: {len(qs)}")  # Returns correct result of 10
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/34464>
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/01070187557a565a-d16028a8-2a67-48a8-90d8-22775287db52-000000%40eu-central-1.amazonses.com.

Reply via email to