[Django] #28616: DISTINCT ON and update() does the wrong thing

12 views
Skip to first unread message

Django

unread,
Sep 19, 2017, 7:24:22 AM9/19/17
to django-...@googlegroups.com
#28616: DISTINCT ON and update() does the wrong thing
-------------------------------------+-------------------------------------
Reporter: Daniel | Owner: nobody
Keller |
Type: Bug | Status: new
Component: Database | Version: 1.11
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
If I have a model like

{{{
from django.db import models
from django.utils import timezone

class Foo(models.Model):
num = models.IntegerField(default=0)
date = models.DateTimeField(default=timezone.now)
flag = models.BooleanField(default=False)
}}}

and (with Postgresql) I do
{{{
Foo.objects.order_by('num', '-date').distinct('num').only('pk')
}}}

I get a query like
{{{
SELECT DISTINCT ON ("app_foo"."num") "app_foo"."id" AS Col1 FROM "app_foo"
ORDER BY "app_foo"."num" ASC, "app_foo"."date" DESC; args=()
}}}
which returns the latest `Foo` for each `num`.

BUT, if I do
{{{
Foo.objects.order_by('num', '-date').distinct('num').update(flag=True)
}}}

then it executes
{{{
UPDATE "app_foo" SET "flag" = true; args=(True)
}}}
which updates ''everything''.

I don't necessarily think that this behaviour should be supported, but it
would be nice to at least get a `NotImplementedError`.

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

Django

unread,
Sep 22, 2017, 2:07:59 AM9/22/17
to django-...@googlegroups.com
#28616: DISTINCT ON and update() does the wrong thing
-------------------------------------+-------------------------------------
Reporter: Daniel Keller | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* cc: felixxm (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/28616#comment:1>

Django

unread,
Sep 26, 2017, 8:34:56 PM9/26/17
to django-...@googlegroups.com
#28616: DISTINCT ON and update() does the wrong thing
-------------------------------------+-------------------------------------
Reporter: Daniel Keller | Owner: nobody
Type: Bug | Status: new

Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/28616#comment:2>

Django

unread,
Oct 4, 2017, 12:50:51 PM10/4/17
to django-...@googlegroups.com
#28616: DISTINCT ON and update() does the wrong thing
-------------------------------------+-------------------------------------
Reporter: Daniel Keller | Owner: messfish
Type: Bug | Status: assigned

Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by messfish):

* status: new => assigned
* owner: nobody => messfish


--
Ticket URL: <https://code.djangoproject.com/ticket/28616#comment:3>

Django

unread,
Jun 4, 2022, 4:36:06 AM6/4/22
to django-...@googlegroups.com
#28616: DISTINCT ON and update() does the wrong thing
-------------------------------------+-------------------------------------
Reporter: Daniel Keller | Owner: Anvesh
| Mishra

Type: Bug | Status: assigned
Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Anvesh Mishra):

* owner: nobody => Anvesh Mishra


* status: new => assigned

Django

unread,
Jun 4, 2022, 3:25:04 PM6/4/22
to django-...@googlegroups.com
#28616: DISTINCT ON and update() does the wrong thing
-------------------------------------+-------------------------------------
Reporter: Daniel Keller | Owner: Anvesh
| Mishra
Type: Bug | Status: assigned
Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Anvesh Mishra):

Should we add a support for this behavior or stick to
`NotImplementedError` ?

--
Ticket URL: <https://code.djangoproject.com/ticket/28616#comment:4>

Django

unread,
Nov 11, 2022, 7:14:46 AM11/11/22
to django-...@googlegroups.com
#28616: DISTINCT ON and update() does the wrong thing
-------------------------------------+-------------------------------------
Reporter: Daniel Keller | Owner: (none)
Type: Bug | Status: new

Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Anvesh Mishra):

* cc: Anvesh Mishra (added)
* owner: Anvesh Mishra => (none)
* status: assigned => new


--
Ticket URL: <https://code.djangoproject.com/ticket/28616#comment:5>

Django

unread,
Dec 9, 2022, 5:46:49 AM12/9/22
to django-...@googlegroups.com
#28616: DISTINCT ON and update() does the wrong thing
-------------------------------------+-------------------------------------
Reporter: Daniel Keller | Owner: Anvesh
| Mishra
Type: Bug | Status: assigned
Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Anvesh Mishra):

* owner: (none) => Anvesh Mishra


* status: new => assigned


Comment:

On looking into it further it seems like the problem comes because of how
the `QuerySet.update()` works
{{{ #!python
query = self.query.chain(sql.UpdateQuery)
}}}

The Query.chain() changes the class of the current object to UpdateQuery
which removes the previous query as a new object is created i.e
{{{
SELECT DISTINCT ON ("distinct_foo"."num") "distinct_foo"."id",
"distinct_foo"."num", "distinct_foo"."date", "distinct_foo"."flag" FROM
"distinct_foo" ORDER BY "distinct_foo"."num" ASC, "distinct_foo"."date"
DESC
}}}
and then replaces it with


{{{
UPDATE "app_foo" SET "flag" = true; args=(True)
}}}

instead of combining both.

--
Ticket URL: <https://code.djangoproject.com/ticket/28616#comment:6>

Reply all
Reply to author
Forward
0 new messages