Code

Opened 2 years ago

Last modified 2 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.

Attachments (0)

Change History (4)

comment:1 Changed 2 years ago by lrekucki

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

comment:2 Changed 2 years ago by lukeplant

  • Triage Stage changed from Unreviewed to Accepted

comment:3 follow-up: Changed 2 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 2 years ago by anonymous

Expected:

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

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as new
The owner will be changed from nobody to anonymous. Next status will be 'assigned'
as The resolution will be set. Next status will be 'closed'
Author


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

 
Note: See TracTickets for help on using tickets.