Opened 11 years ago

Closed 11 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

Description

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 by orblivion, 11 years ago

Cc: orblivion added

comment:2 by Sam Solomon <sssbox@…>, 11 years ago

Cc: sssbox@… added

comment:3 by Aymeric Augustin, 11 years ago

Resolution: duplicate
Status: newclosed

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

comment:4 by ubercore, 11 years ago

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 by ubercore, 11 years ago

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