Opened 13 years ago

Closed 13 years ago

Last modified 13 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

Description

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.

Thanks!

Change History (3)

comment:1 by Ramiro Morales, 13 years ago

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 13 years ago by Ramiro Morales (previous) (diff)

comment:3 by Pavel White , 13 years ago

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

Ex:

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