Opened 3 years ago

Last modified 3 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 lrekucki)

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 (4)

comment:1 Changed 3 years ago by lrekucki

  • Description modified (diff)
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 3 years ago by lukeplant

  • Triage Stage changed from Unreviewed to Accepted

comment:3 follow-up: Changed 3 years ago by akaariai

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 3 years ago by anonymous

Expected:

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