Code

Opened 3 years ago

Closed 3 years ago

Last modified 2 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!

Attachments (0)

Change History (3)

comment:1 Changed 3 years ago by ramiro

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

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 3 years ago by ramiro (previous) (diff)

comment:3 Changed 2 years ago by Pavel White

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]

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.