#35958: QuerySet.distinct() crashes with "SELECT DISTINCT ON expressions must 
match
initial ORDER BY expressions"
-------------------------------------+-------------------------------------
     Reporter:  adofosam             |                    Owner:  (none)
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  5.1
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  distinct             |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

 * summary:
     Issue with the distinct() function when specifying a field to run it
     on
     =>
     QuerySet.distinct() crashes with "SELECT DISTINCT ON expressions must
     match initial ORDER BY expressions"


Old description:

> When I attempt to run the following code, I get a error.
>
> Code:
> """
> user = User.objects.get(id=user_id)
> list = List.objects.filter(user=user).order_by('email').distinct('email')
> """
>
> Error (Context: my app is called "lists"):
> """
> django.db.utils.ProgrammingError: SELECT DISTINCT ON expressions must
> match initial ORDER BY expressions
> LINE 1: SELECT COUNT(*) FROM (SELECT DISTINCT ON ("lists_list"...
> """
>
> I'm using a postgresql database and my list model is as follows:
> """
> class EventList(models.Model): # List for Marketting
>     id = models.UUIDField(primary_key=True, default=uuid.uuid4,
> editable=False, unique=True)
>     user= models.ForeignKey(User, on_delete=models.CASCADE)
>     email = models.EmailField()
> """

New description:

 When I attempt to run the following code, I get an error.

 Code:
 {{{#!python
 user = User.objects.get(id=user_id)
 list = List.objects.filter(user=user).order_by('email').distinct('email')
 }}}

 Error (Context: my app is called "lists"):
 {{{
 django.db.utils.ProgrammingError: SELECT DISTINCT ON expressions must
 match initial ORDER BY expressions
 LINE 1: SELECT COUNT(*) FROM (SELECT DISTINCT ON ("lists_list"...
 }}}

 I'm using a PostgreSQL database and my model is:
 {{{#!python
 class EventList(models.Model):
     id = models.UUIDField(primary_key=True, default=uuid.uuid4,
 editable=False, unique=True)
     user= models.ForeignKey(User, on_delete=models.CASCADE)
     email = models.EmailField()
 }}}

--
-- 
Ticket URL: <https://code.djangoproject.com/ticket/35958#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/django-updates/0107019383b63b75-aa2a27a7-c5c7-4124-baf8-3a5eef42fd65-000000%40eu-central-1.amazonses.com.

Reply via email to