Opened 6 years ago

Closed 6 years ago

Last modified 3 years ago

#8819 closed (fixed)

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

Reported by: kevin@… Owned by: mtredinnick
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: UI/UX:


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.

Attachments (0)

Change History (3)

comment:1 Changed 6 years ago by mtredinnick

  • milestone set to 1.0
  • Needs documentation unset
  • Needs tests unset
  • Owner changed from nobody to mtredinnick
  • Patch needs improvement unset
  • Status changed from new to assigned

comment:2 Changed 6 years ago by mtredinnick

  • Resolution set to fixed
  • Status changed from assigned to closed

(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 3 years ago by jacob

  • milestone 1.0 deleted

Milestone 1.0 deleted

Add Comment

Modify Ticket

Change Properties
<Author field>
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'

E-mail address and user name can be saved in the Preferences.

Note: See TracTickets for help on using tickets.