Opened 10 years ago

Closed 10 years ago

Last modified 7 years ago

#8819 closed (fixed)

Regression: filter by extra(select = ..., order_by = ...) with distinct() fails ("Unknown column")

Reported by: kevin@… Owned by: Malcolm Tredinnick
Component: Core (Other) Version: master
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


Regression: adding an extra field to a QuerySet using .extra(select = ...) with extra(order_by = ...) AND .distinct() throws an OperationalError. This seems to be because the generated SQL lists the extra column twice: once correctly (as the derived field), and once just by itself at the end of the SELECT list.

Reduced test case below (using contrib.auth for simplified test case, though this is reproducible with any model).

import setup_django
from django.contrib.auth.models import User

a = User.objects.all().extra( select = { 'test_new_field': '1' }).distinct()
b = User.objects.all().extra( select = { 'test_new_field': '1' }, order_by = ['-test_new_field'] )
c = User.objects.all().extra( select = { 'test_new_field': '1' }, order_by = ['-test_new_field'] ).distinct()

print a[:1]
print b[:1]
print c[:1]

(In the code above, "setup_django" just sets the right settings file, etc.)

Expected: a, b, and c both evaluate without error.
Actual: a and b are fine, c raises OperationalError: (1054, "Unknown column 'test_new_field' in 'field list'")

I haven't tracked this down to a particular rev yet, but it's almost definitely within the past week or so.

Change History (3)

comment:1 Changed 10 years ago by Malcolm Tredinnick

milestone: 1.0
Owner: changed from nobody to Malcolm Tredinnick
Status: newassigned

comment:2 Changed 10 years ago by Malcolm Tredinnick

Resolution: fixed
Status: assignedclosed

(In [8898]) Fixed #8819 -- Don't include two copies of extra-select columns in the query.
This was triggered by r8794, but was, in fact, fairly fragile before then. The
current fix is the correct way we should be doing this.

comment:3 Changed 7 years ago by Jacob

milestone: 1.0

Milestone 1.0 deleted

Note: See TracTickets for help on using tickets.
Back to Top