﻿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
32433	Delete distinct produces an unexpected and potentially harmful SQL	egism	nobody	"I was looking for a way to delete the first Comment of each Post (a sample domain). Since I know that every new Post starts with a system generated comment I decided to go with:

`Comment.objects.order_by('post_id', 'created_at').distinct('post_id').delete()`

Before proceeding I tested it with:

`Comment.objects.order_by('post_id', 'created_at').distinct('post_id').count()`

Made sure the result actually contains what I needed and proceeded with the `delete()`. The result was rather surprising. I was notified that the whole table was wiped clean. I then checked the actual SQL that was executed and it was a simple `DELETE FROM comments;`.

As an ORM user, I would say it is the worst outcome possible and I would at least expect an error in such a case or ideally a SQL of what I was trying to achieve. At the same time, `count` and `delete` produces an inconsistent result which is even more mistaking.

Potential solutions:
* raise an error with a decent explanation
* produce a desired SQL according to the query

Since I have never submitted a change to Django, I have a very limited knowledge of the ORM and its intricacies. I could give it a try and issue a patch for this with some guidance.

"	Uncategorized	new	Database layer (models, ORM)	3.1	Normal		orm, delete, distinct, postgresql		Unreviewed	0	0	0	0	0	0
