﻿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
7142	QuerySet: Distinct + Values + order by doesn't behave as one would expect	bo	nobody	"best shown by example .. effects the Queryset Refactor 

{{{

## a simple model
class Thing(models.Model):
   is_ok = models.BooleanField()
   name = models.CharField()


## This works just fine
q = Thing.objects.filter(is_ok = True).values('name').distinct()
##
##  SQL
##
##  SELECT DISTINCT name from thing where is_ok = 1
##

## this fails
#

q = Thing.objects.filter(is_ok = True).extra(select={'short_name' : ""SUBSTRING(name, 1, 3) ""}.values('short_name').distinct()

#
# it does return a dict of just short_name
# 
#  q  = [{ 'short_name': 'bla'}, { 'short_name': 'bla2'}, ...]
#
#  however the SQL generated is
#
#
# SELECT DISTINCT name, (SUBSTRING(name, 1, 3)) as short_name from thing where is_ok = 1
#
#  Note that 'Name' is still included in the query thus 'DISTINCT' 
#  is NOT distinct on 'short_name' but the name - short_name combo
#
# it should be
#
# SELECT DISTINCT (SUBSTRING(name, 1, 3)) as short_name from thing where is_ok = 1
#
}}}"		closed	Database layer (models, ORM)	dev		invalid	qsrf-cleanup distinct order_by values		Design decision needed	0	0	0	0	0	0
