Opened 12 years ago

Closed 12 years ago

Last modified 11 years ago

#16058 closed Bug (invalid)

Values_list with distinct return duplicate values

Reported by: jonasnockert Owned by: nobody
Component: Database layer (models, ORM) Version: 1.3
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


I'm using Django 1.3 with the following model (and as far as I can tell the generated sqlite3 schema looks okay as well):

class Article(Model):
    product_number = PositiveIntegerField(_("Product number"), db_index=True)
    product_variant = PositiveIntegerField(_("Variant"), db_index=True)
    class Meta:
        unique_together = (('product_number', 'product_variant'),)
        ordering = ['-date_added']

I'm making the following query which returns duplicate results:

>>> Article.objects.filter(product_number=99668).values_list('product_number', flat=True).distinct()
[99668, 99668]

It looks like a bug from here but I'm also a bit sceptical since other people seem to be using this query construct successfully.


Change History (3)

comment:1 Changed 12 years ago by Ramiro Morales

Resolution: invalid
Status: newclosed

I suspect is is because the (default, implicit) ordering Article model queries have and/or the unique_together condition. You are seeing duplicate results because .distinct() is acting on all the columns included in the low level SQL query.

Take a look at:

  • The note in the distinct() method and
  • the suggestion in the last paragraph of the values() method (using .unique().values_list(...) if possible)
Last edited 12 years ago by Ramiro Morales (previous) (diff)

comment:3 Changed 11 years ago by Pavel White

You can use .sorted_by() for clear sorted meta oredering.


>>> Article.objects.filter(product_number=99668).sorted_by().values_list('product_number', flat=True).distinct()
Note: See TracTickets for help on using tickets.
Back to Top