Opened 3 years ago

Closed 3 years ago

Last modified 3 years 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



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

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)

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


Test fails

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

expected result

testcase passes


found on 1.4.3, same problem exists in 1.5.0

Change History (3)

comment:1 Changed 3 years 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 3 years 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 3 years 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.

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