Code

#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: []

Attachments (0)

Change History (5)

comment:1 Changed 17 months ago by orblivion

  • Cc orblivion added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 17 months ago by Sam Solomon <sssbox@…>

  • Cc sssbox@… added

comment:3 Changed 17 months ago by aaugustin

  • Resolution set to duplicate
  • Status changed from new to closed

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

comment:4 Changed 14 months ago by ubercore

  • Needs tests set
  • Resolution duplicate deleted
  • Status changed from closed to new

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 14 months ago by ubercore

  • Resolution set to worksforme
  • Status changed from new to closed

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

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.