Opened 13 years ago

Closed 6 years ago

#17930 closed Bug (fixed)

Error in Queryset with operator | (union queryset) + slice

Reported by: vini.gracindo@… Owned by: Ian Foote
Component: Database layer (models, ORM) Version: 1.3
Severity: Normal Keywords: queryset union, slice
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Łukasz Rekucki)

class Example:
    name = models.CharField(max_length = 30)
    public = models.BooleanField()
>>> Example.objects.create('example 1', False)
<Example: Example object>
>>> Example.objects.create('example 2', False)
<Example: Example object>
>>> Example.objects.create('example 3', True)
<Example: Example object>
>>> Example.objects.create('example 4', True)
<Example: Example object>
>>> Example.objects.create('example 5', False)
<Example: Example object>
>>> query = Example.objects.filter(public = True)
>>> if(query.count() < 3):
...     query = query | Example.objects.filter(public = False).order_by('?')[:1]
...
>>> query.count()
5
>>>

When using the union of querysets to slice it "ignores" the slice and takes all objects where public = false.

Change History (9)

comment:1 by Łukasz Rekucki, 13 years ago

Description: modified (diff)

comment:2 by Luke Plant, 12 years ago

Triage Stage: UnreviewedAccepted

comment:3 by Anssi Kääriäinen, 12 years ago

What should the expected output be? Maybe (pseudoquery):

select * from example
where (original_query_conditions) or pk in (select pk from example where public = false order by random() limit 1)

Or, in this special case this could be written as:

select * from example
where (original_query_conditions) or pk = (select pk from example where public = false order by random() limit 1)

in reply to:  3 comment:4 by anonymous, 12 years ago

Expected:

>> query = query | Example.objects.filter(public = False).order_by('?')[:1]
>> print query.count()
3

comment:5 by Ian Foote, 6 years ago

Owner: changed from nobody to Ian Foote
Status: newassigned

comment:6 by Ian Foote, 6 years ago

Has patch: set
Last edited 6 years ago by Tim Graham (previous) (diff)

comment:7 by Simon Charette, 6 years ago

Patch needs improvement: set

comment:8 by Tim Graham, 6 years ago

Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin

comment:9 by Tim Graham <timograham@…>, 6 years ago

Resolution: fixed
Status: assignedclosed

In e1fc07c:

Fixed #17930 -- Allowed ORing (|) with sliced QuerySets.

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