Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#32848 closed Uncategorized (needsinfo)

Bad error with invalid lookup on annotated field.

Reported by: Gordon Wrigley Owned by: nobody
Component: Uncategorized Version: 3.1
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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.

Change History (5)

comment:1 by Gordon Wrigley, 3 years ago

Version: 3.23.1

comment:2 by Carlton Gibson, 3 years ago

Resolution: needsinfo
Status: newclosed

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.

comment:3 by Gordon Wrigley, 3 years ago

If you do it without the annotation, so MyModel.objects.values("my_duration__fred") you get "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.

Last edited 3 years ago by Gordon Wrigley (previous) (diff)

comment:4 by João Freires, 3 years ago

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 name 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.

Last edited 3 years ago by João Freires (previous) (diff)

comment:5 by Carlton Gibson, 3 years ago

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

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