[Django] #32848: Bad error with invalid lookup on annotated field.

4 views
Skip to first unread message

Django

unread,
Jun 15, 2021, 8:40:00 AM6/15/21
to django-...@googlegroups.com
#32848: Bad error with invalid lookup on annotated field.
------------------------------------------+------------------------
Reporter: Gordon Wrigley | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 3.2
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
------------------------------------------+------------------------
This on Django 3.1.12 with Postgres and Python 3.8.6

(This is a little contrived)

`MyModel.objects.annotate(bob=F("my_duration")).values("bob")`

works fine, then if we add a bad lookup

`MyModel.objects.annotate(bob=F("my_duration")).values("bob__fred")`

we get an error saying "Cannot resolve keyword 'bob' into field." which is
a misleading error.

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

Django

unread,
Jun 15, 2021, 8:40:10 AM6/15/21
to django-...@googlegroups.com
#32848: Bad error with invalid lookup on annotated field.
--------------------------------+--------------------------------------

Reporter: Gordon Wrigley | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 3.1
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+--------------------------------------
Changes (by Gordon Wrigley):

* version: 3.2 => 3.1


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

Django

unread,
Jun 16, 2021, 4:09:57 AM6/16/21
to django-...@googlegroups.com
#32848: Bad error with invalid lookup on annotated field.
--------------------------------+--------------------------------------

Reporter: Gordon Wrigley | Owner: nobody
Type: Uncategorized | Status: closed
Component: Uncategorized | Version: 3.1
Severity: Normal | Resolution: needsinfo

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+--------------------------------------
Changes (by Carlton Gibson):

* status: new => closed
* resolution: => needsinfo


Comment:

Hi Gordon. Thanks.

Hmmm. "Cannot resolve keyword 'bob' into field." is exactly what I'm
expecting here. 🤔

I'm going to say `needsinfo`: If you want to suggest a patch implementing
the logic for a better error message here then happy to look at it — but
I'm initially sceptical that backing out of the lookup resolution in order
to check if an annotation was applied is going to be worth the complexity.
However, if something clean is available then it's worth considering.

I hope that makes sense.

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

Django

unread,
Jun 16, 2021, 4:19:14 AM6/16/21
to django-...@googlegroups.com
#32848: Bad error with invalid lookup on annotated field.
--------------------------------+--------------------------------------

Reporter: Gordon Wrigley | Owner: nobody
Type: Uncategorized | Status: closed
Component: Uncategorized | Version: 3.1
Severity: Normal | Resolution: needsinfo
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+--------------------------------------

Comment (by Gordon Wrigley):

If you do it without the annotation, so
`MyModel.objects.values("my_duration__fred")` you "Cannot resolve keyword
'fred' into field." which is what I'd expect. The field "bob" is legit,
it's the lookup 'fred' that is bogus, so that's what I'd expect the error
on.

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

Django

unread,
Jun 16, 2021, 9:56:11 AM6/16/21
to django-...@googlegroups.com
#32848: Bad error with invalid lookup on annotated field.
--------------------------------+--------------------------------------

Reporter: Gordon Wrigley | Owner: nobody
Type: Uncategorized | Status: closed
Component: Uncategorized | Version: 3.1
Severity: Normal | Resolution: needsinfo
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+--------------------------------------

Comment (by João Freires):

I don't think that this ticket is closed. When we try to filter a Queryset
with an annotation, the resolution error is more accurate.
`MyModel.objects.annotate(bob=F("my_duration")).filter('bob__fred')`
Threw an accurate error message: "FieldError: Unsupported lookup
'fred'..."

But with:


`MyModel.objects.annotate(bob=F("my_duration")).values('bob__fred')`

I got: "FieldError: Cannot resolve keyword 'bob' into field."

I didn't try to evaluate how difficult is to do the same resolution in
Queryset's filter and values.
But I think that is good to keep the pattern and the two message errors
give a correct explanation.

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

Django

unread,
Jun 17, 2021, 1:08:58 AM6/17/21
to django-...@googlegroups.com
#32848: Bad error with invalid lookup on annotated field.
--------------------------------+--------------------------------------

Reporter: Gordon Wrigley | Owner: nobody
Type: Uncategorized | Status: closed
Component: Uncategorized | Version: 3.1
Severity: Normal | Resolution: needsinfo
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+--------------------------------------

Comment (by Carlton Gibson):

Very happy to reopen if someone wants to provide a patch showing it's
feasible at a moderate complexity level!

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

Reply all
Reply to author
Forward
0 new messages