Code

Opened 16 months ago

Closed 16 months ago

Last modified 16 months ago

#20101 closed Bug (fixed)

bitwise or on queryset fails when using exlude over manytomany

Reported by: harm Owned by: nobody
Component: Database layer (models, ORM) Version: 1.4
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

description

In the case below, when doing a bitwise or of 2 querysets, the results fails silently.

An item that should be in the result, is not.

steps to reproduce

run the following test case

models.py

from django.db import models

class Poll(models.Model):
    question = models.CharField(max_length=200)
    choices = models.ManyToManyField('Choice', related_name='polls')

        
class Choice(models.Model):
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)

tests.py

from django.test import TestCase
    
from models import Poll, Choice
    
class SimpleTest(TestCase):
    def test_or(self):
        """
        Tests that querysets can be or-ed
        """
        p = Poll.objects.create()

        c1 = Choice.objects.create()
        c2 = Choice.objects.create()
        c3 = Choice.objects.create()


        qs1 = Poll.objects.exclude(choices__in=[c1, c2])
        qs2 = Poll.objects.filter(choices__in=[c3])

        self.assertTrue(p in qs1)
        self.assertFalse(p in qs2)

        # here is the bug,  p is in qs1, but not in the orred result
        self.assertTrue(p in (qs1|qs2))

result

Test fails

======================================================================
FAIL: test_or (poll.tests.SimpleTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "mysite/poll/tests.py", line 31, in test_or
    self.assertTrue(p in (qs1|qs2))
AssertionError: False is not True

expected result

testcase passes

version

found on 1.4.3, same problem exists in 1.5.0

Attachments (0)

Change History (3)

comment:1 Changed 16 months ago by harm

  • Component changed from Uncategorized to Database layer (models, ORM)
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Type changed from Uncategorized to Bug

comment:2 Changed 16 months ago by Anssi Kääriäinen <akaariai@…>

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

In 80e68ee2ffb44d90c66b48e2db18ec4e16c025b4:

Added tests for already fixed #20101

The ticket dealt with a case where one query had .exclude() that
produced a subquery, the other query had a join to the same model that
was subqueried in the first query. This was already fixed in master, so
only test added.

comment:3 Changed 16 months ago by akaariai

This has been fixed in master, I don't know which commit contains the fix.

Backpatching the fix would likely require a lot of changes to stable/1.5.x. Even if a simple fix were found, the backpatching rules do not support backpatching as this isn't a regression nor critical failure in 1.5.

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.