Opened 5 years ago

Last modified 5 years ago

#30130 closed Bug

Django .values().distinct() returns a lot more records than .values().distinct().count() — at Version 2

Reported by: James Lin Owned by: nobody
Component: Database layer (models, ORM) Version: 2.1
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by James Lin)

I have a table virtualmachineresources, which has 100k+ rows, it has columns machine and cluster, some rows the cluster field is empty. it has repeating rows of machine + with/without cluster, hence I want to use the distinct() method.

using .values().distinct().count(), it returned 2k rows

In [6]: VirtualMachineResources.objects.all().values('machine', 'cluster')
   ...: .distinct().count()                                               
Out[6]: 2247

When I loop through the distinct query

for resource in VirtualMachineResources.objects.all().values('machine', 'cluster').distinct(): 
    print(resource['machine'], resource['cluster'])

I observed it returned 100k rows, with repeating rows that the same 'machine` with/without the cluster.

Here is the corresponding stackoverflow question https://stackoverflow.com/questions/54354462/django-distinct-returns-more-records-than-count

Change History (2)

comment:1 by James Lin, 5 years ago

Description: modified (diff)

comment:2 by James Lin, 5 years ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top