Code

Opened 7 years ago

Closed 6 years ago

Last modified 5 years ago

#4002 closed (invalid)

Attributes added by extra() aren't visible to filter()

Reported by: exogen@… Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Keywords: extra filter
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

Attributes added with the extra() method are visible by order_by, but not to filter. An exception is raised:

<type 'exceptions.TypeError'>: Cannot resolve keyword 'test' into field

...but order_by('test') works fine.

Attachments (1)

models.py (2.5 KB) - added by exogen@… 7 years ago.
extra / filter shortcoming demo

Download all attachments as: .zip

Change History (9)

comment:1 Changed 7 years ago by Simon G. <dev@…>

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

Can you attach a short test/example code to help us track this down?

comment:2 Changed 7 years ago by exogen@…

I added a models.py with a function that demonstrates the behavior. Drop it into any example app, syncdb, import and run the function.

Changed 7 years ago by exogen@…

extra / filter shortcoming demo

comment:3 Changed 7 years ago by mtredinnick

  • Keywords filter, qs-rf added; filter removed

comment:4 Changed 7 years ago by adrian

Good catch.

comment:5 Changed 6 years ago by mtredinnick

  • Keywords qs-rf-fixed added; qs-rf removed

Fixed on queryset-refactor branch in [6762]. Will be closed when that branch is merged into trunk.

comment:6 Changed 6 years ago by mtredinnick

  • Keywords filter added; filter, qs-rf-fixed removed
  • Resolution set to invalid
  • Status changed from new to closed

Upon further reflection, this isn't a valid request. The SQL standard requires (and MySQL and PostgreSQL, at least, enforce) that the where-clause only refers to table columns, not aggregates or other computed columns. So you can't refer to extra_select things in the filtering phase. (The tests I committed above show the failure on those two backends. It's really just a fluke that SQLite works here.)

When we implement "GROUP BY" and "HAVING" support, the sort of thing you're trying to do here will be possible.

I'm going to commit a patch that reverts [6762] shortly.

comment:7 Changed 6 years ago by mtredinnick

(In [6961]) queryset-refactor: Reverted [6762] because it generates invalid SQL. It only worked accidentally with SQLite.

Refs #4002.

Add Comment

Modify Ticket

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


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

 
Note: See TracTickets for help on using tickets.