[Django] #19434: Some QuerySet methods not aware of fields added on "extra"

21 views
Skip to first unread message

Django

unread,
Dec 5, 2012, 5:16:34 PM12/5/12
to django-...@googlegroups.com
#19434: Some QuerySet methods not aware of fields added on "extra"
----------------------------------------------+--------------------
Reporter: hcarvalhoalves | Owner: nobody
Type: Bug | Status: new
Component: Database layer (models, ORM) | Version: 1.4
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------------------+--------------------
Some examples to illustrate the issue:

{{{
(Pdb) CUSTOM_SQL = "SELECT some_function(some_field) FROM some_table"

# This works
(Pdb) qs = qs.extra(select={'some_alias': CUSTOM_SQL},
order_by=['some_alias'])

# This fails
(Pdb) qs.filter(some_alias__gt=0)
*** FieldError: Cannot resolve keyword 'some_alias' into field. Choices
are: ...

# This works
(Pdb) qs.values()
[{'some_alias': 1}, {'some_alias': 2}]

# This fails
(Pdb) qs.values('some_alias')
*** FieldError: Cannot resolve keyword 'some_alias' into field. Choices
are: ...
}}}

There's a situation where half of the API supports `extra` queries
correctly, the other half fails with a `FieldError`, and there's no clear
indication to what should be the right behavior.

--
Ticket URL: <https://code.djangoproject.com/ticket/19434>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Dec 5, 2012, 5:48:28 PM12/5/12
to django-...@googlegroups.com
#19434: Some QuerySet methods not aware of fields added on "extra"
-------------------------------------+-------------------------------------

Reporter: hcarvalhoalves | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.4
(models, ORM) | Resolution:
Severity: Normal | Triage Stage:
Keywords: | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by hcarvalhoalves):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Comment:

Could be worked around if the ORM supported a `HAVING` clause, so `filter`
''et al'' wouldn't need to be aware of aliases, but ticket #8784 asking
for it's inclusion was marked WONTFIX. I have no idea what the solution
could be then.

--
Ticket URL: <https://code.djangoproject.com/ticket/19434#comment:1>

Django

unread,
Dec 8, 2012, 1:57:54 PM12/8/12
to django-...@googlegroups.com
#19434: Some QuerySet methods not aware of fields added on "extra"
-------------------------------------+-------------------------------------

Reporter: hcarvalhoalves | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.4
(models, ORM) | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by aaugustin):

* stage: Unreviewed => Accepted


Comment:

Yes, it isn't possible to filter on a field added in a
`.extra(select={...})`. I've hit this problem too.

--
Ticket URL: <https://code.djangoproject.com/ticket/19434#comment:2>

Django

unread,
Feb 18, 2013, 3:34:19 PM2/18/13
to django-...@googlegroups.com
#19434: Some QuerySet methods not aware of fields added on "extra"
-------------------------------------+-------------------------------------

Reporter: hcarvalhoalves | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: master

(models, ORM) | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: | Needs documentation: 0
Has patch: 1 | Patch needs improvement: 0

Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by fhahn):

* cc: flo@… (added)
* has_patch: 0 => 1
* version: 1.4 => master


Comment:

I've started a patch that adds support for filtering fields added by extra
and created a pull request: https://github.com/django/django/pull/735

I think something similar has been done to provide support for filtering
annotations/aggregations

{{{
qs.extra(select={'num_plus_one': 'num + 1'}
qs.filter(num_plus_one=2)
}}}

will result in following query:

{{{
SELECT (num+1) AS "num_plus_on" ...
WHERE (num+1) = 2 ....
}}}

In order to avoid quoting of the lvalue of the term in the WHERE part I
modified WhereNode.add to accept a QueryWrapper (contains the extra sql).
There's probably a much better way to add an unqouted lvalue to the
WhereNode, any suggestions/feedback are very appreciated.


{{{ qs.values('some_alias') }}} worked for me (without modification), but
I've added a test to check it.

--
Ticket URL: <https://code.djangoproject.com/ticket/19434#comment:3>

Django

unread,
Jun 4, 2013, 10:53:26 AM6/4/13
to django-...@googlegroups.com
#19434: Some QuerySet methods not aware of fields added on "extra"
-------------------------------------+-------------------------------------

Reporter: hcarvalhoalves | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: master
(models, ORM) | Resolution:
Severity: Normal | Triage Stage: Ready for
Keywords: | checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timo):

* stage: Accepted => Ready for checkin


Comment:

I've updated the patch with some cosmetic tweaks as well as to merge
cleanly to master (confirmed all tests pass as well). Would like someone
more familiar with the code to +1 before it gets committed.

https://github.com/django/django/pull/1242

--
Ticket URL: <https://code.djangoproject.com/ticket/19434#comment:4>

Django

unread,
Jun 4, 2013, 6:43:09 PM6/4/13
to django-...@googlegroups.com
#19434: Some QuerySet methods not aware of fields added on "extra"
-------------------------------------+-------------------------------------

Reporter: hcarvalhoalves | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: master
(models, ORM) | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: | Needs documentation: 0
Has patch: 1 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by timo):

* stage: Ready for checkin => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/19434#comment:5>

Django

unread,
Oct 8, 2013, 9:10:26 AM10/8/13
to django-...@googlegroups.com
#19434: Some QuerySet methods not aware of fields added on "extra"
-------------------------------------+-------------------------------------

Reporter: hcarvalhoalves | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: master
(models, ORM) | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: | Needs documentation: 0
Has patch: 1 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------

Comment (by timo):

#21159 reported the inability to `distinct()` on `extra` columns.

--
Ticket URL: <https://code.djangoproject.com/ticket/19434#comment:6>

Django

unread,
Feb 8, 2014, 5:22:07 AM2/8/14
to django-...@googlegroups.com
#19434: Some QuerySet methods not aware of fields added on "extra"
-------------------------------------+-------------------------------------

Reporter: hcarvalhoalves | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: master
(models, ORM) | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: | Needs documentation: 0
Has patch: 1 | Patch needs improvement: 1

Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by timo):

* needs_better_patch: 0 => 1


Comment:

Patch no longer merges cleanly.

--
Ticket URL: <https://code.djangoproject.com/ticket/19434#comment:7>

Django

unread,
Aug 4, 2015, 7:41:53 PM8/4/15
to django-...@googlegroups.com
#19434: Some QuerySet methods not aware of fields added on "extra"
-------------------------------------+-------------------------------------
Reporter: hcarvalhoalves | Owner: nobody
Type: Bug | Status: closed

Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution: wontfix
Keywords: QuerySet.extra | Triage Stage: Accepted

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* keywords: => QuerySet.extra
* status: new => closed
* resolution: => wontfix


Comment:

We are no longer fixing bugs with `QuerySet.extra()` per
[https://groups.google.com/d/topic/django-
developers/FojuU0syO8Y/discussion discussion on django-developers].

--
Ticket URL: <https://code.djangoproject.com/ticket/19434#comment:8>

Reply all
Reply to author
Forward
0 new messages