#26434: Inconsistent results of QuerySet count() method using PostgreSQL backend
prior and post the QuerySet evaluation
-------------------------------------+-------------------------------------
     Reporter:  kamandol             |      Owner:  nobody
         Type:  Bug                  |     Status:  new
    Component:  Database layer       |    Version:  1.9
  (models, ORM)                      |   Keywords:  postgresql queryset
     Severity:  Normal               |  count annotate aggreagate order_by
 Triage Stage:  Unreviewed           |  Has patch:  0
Easy pickings:  0                    |      UI/UX:  0
-------------------------------------+-------------------------------------
 Using a PostgreSQL backend database, in some cases with QuerySets
 involving '''annotations''', '''values''' or '''values_list''' and
 ordering('''order_by''') clauses the resulting QuerySet '''count()'''
 method fails to predict the real row result unless that QuerySet is
 evaluated.

 For instance:

 {{{
 #!python
 q = BundleVersion.objects.order_by('-id').values_list("port__id",
 "asset_bundle__id").annotate(max=models.Max("id"))
 }}}

 This QuerySet using PostgreSQL would, in fact, group the results by
 '''BundleVersion.id''' instead of the tuple ("port!__id",
 "asset_bundle!__id") due to the '''order_by''' clause using a column not
 in the '''values_list'''. This is a documented behavior.

 The problem is that if '''q''' is not evaluated yet, calling
 '''q.count()''' will return an amount of rows as if the grouping was done
 on the tuple ("port!__id", "asset_bundle!__id"). '''After''' evaluating
 '''q''', or calling '''len(q)''', the result of '''q.count()''' will be
 the expected, as if the grouping was done using ''''BundleVersion.id''''.

--
Ticket URL: <https://code.djangoproject.com/ticket/26434>
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 django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/051.2d817457d399892b795b650906123d9b%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to