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 Initial Version

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

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 return 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 return 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 (0)

Note: See TracTickets for help on using tickets.
Back to Top