﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
28616	DISTINCT ON and update() does the wrong thing	Daniel Keller	Anvesh Mishra	"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`."	Bug	assigned	Database layer (models, ORM)	1.11	Normal			Mariusz Felisiak Anvesh Mishra	Accepted	0	0	0	0	0	0
