Opened 5 years ago

Last modified 5 years ago

#17930 new Bug

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

Reported by: vini.gracindo@… Owned by: nobody
Component: Database layer (models, ORM) Version: 1.3
Severity: Normal Keywords: queryset union, slice
Cc: Triage Stage: Accepted
Has patch: no 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()

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

Change History (4)

comment:1 Changed 5 years ago by Łukasz Rekucki

Description: modified (diff)

comment:2 Changed 5 years ago by Luke Plant

Triage Stage: UnreviewedAccepted

comment:3 Changed 5 years ago by Anssi Kääriäinen

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)

comment:4 in reply to:  3 Changed 5 years ago by anonymous


>> query = query | Example.objects.filter(public = False).order_by('?')[:1]
>> print query.count()
Note: See TracTickets for help on using tickets.
Back to Top