Opened 6 years ago

Closed 6 years ago

#19407 closed Uncategorized (worksforme)

queryset.none() acts like queryset.all() when using a values_list in a filter

Reported by: orblivion Owned by: nobody
Component: Database layer (models, ORM) Version: 1.4
Severity: Normal Keywords:
Cc: orblivion, sssbox@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: no UI/UX: no


I'm using PostgreSQL 9.1.6

This does not work as expceted:

In : User.objects.filter(id__in = Profile.objects.none().values_list('user_id', flat = True))
Out: [<User: xxx>, <User: yyy>...]

However without values_list, the similar works as expected:

In : User.objects.filter(profile__in = Profile.objects.none())
Out: []

Casting values_list to list works as expected:

In : User.objects.filter(id__in = list(Profile.objects.none().values_list('user_id', flat = True)))
Out: []

In fact values_list claims to be EmptyQuerySet of [], as expected:

In : Profile.objects.none().values_list('user_id', flat = True)
Out: []

And a manually empty QuerySet works:

In : User.objects.filter(id__in = Profile.objects.filter(blah__in = []).values_list('user_id', flat = True))
Out: []

Change History (5)

comment:1 Changed 6 years ago by orblivion

Cc: orblivion added

comment:2 Changed 6 years ago by Sam Solomon <sssbox@…>

Cc: sssbox@… added

comment:3 Changed 6 years ago by Aymeric Augustin

Resolution: duplicate
Status: newclosed

This looks a lot like #17712, please reopen if it's a different issue.

comment:4 Changed 6 years ago by ubercore

Needs tests: set
Resolution: duplicate
Status: closednew

This actually isn't, afaict, a dupe of #17712. The key to this issue is using in with a ValuesListQuerySet, when the ValuesListQuerySet evaluates to an empty list. The WHERE clause is updated with the subquery, but the subquery itself doesn't get a WHERE clause, so it returns all rows:

WHERE "table_name"."lookup_field" IN (SELECT U0."lookup_field" FROM "other_table" U0)

The expected behavior would be:

WHERE "table_name"."lookup_field" IN (SELECT U0."lookup_field" FROM "other_table" U0 where "lookup_field" = 'lookup_value')

comment:5 Changed 6 years ago by ubercore

Resolution: worksforme
Status: newclosed

Sorry, different problem, but this does appear to be fixed. Re-closing.

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