[Django] #31581: Queryset bug when using Q()?

4 views
Skip to first unread message

Django

unread,
May 13, 2020, 8:53:38 AM5/13/20
to django-...@googlegroups.com
#31581: Queryset bug when using Q()?
-------------------------------------+-------------------------------------
Reporter: Javier | Owner: nobody
Buzzi |
Type: Bug | Status: new
Component: Database | Version: 3.0
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
I ran this on all the "recent" versions of python and i see the issue on
all of them. At this point, im not 100% sure if it is a problem or not, i
ran across this trying to show some counts in the Admin that had some
search_fields that traversed the model backwards and forwards in order to
match the thing properly.

This is my mocked models of the issue:

{{{
class ModelA(models.Model):
somecriteria = models.CharField(max_length=50)


class ModelC(models.Model):
unimportant = models.CharField(max_length=50)


class ModelB(models.Model):
somerelation = models.ForeignKey(ModelA, on_delete=models.CASCADE)
m2m = models.ManyToManyField(
ModelC,
through='ModelBC'
)


class ModelBC(models.Model):
b = models.ForeignKey(ModelB, on_delete=models.CASCADE)
c = models.ForeignKey(ModelC, on_delete=models.CASCADE)
}}}

The "bug" is shown when i do:

{{{
queryset =
ModelB.objects.select_related('a').annotate(num_c=Count('m2m__id')).values('num_c')
queryset = queryset.filter(Q(modelbc__c__unimportant='1') |
Q(somerelation__somecriteria='1'))
}}}

When i do:

{{{
queryset =
ModelB.objects.select_related('a').annotate(num_c=Count('m2m__id')).values('num_c')
queryset = queryset.filter(modelbc__c__unimportant='1',
somerelation__somecriteria='1')
}}}

it works as expected.

My testing arena can be found here:
https://gist.github.com/kingbuzzman/9fe5470e31a421aa88b2a64e5447e147 see
the comments, for the way to run it.

--
Ticket URL: <https://code.djangoproject.com/ticket/31581>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
May 13, 2020, 8:56:40 AM5/13/20
to django-...@googlegroups.com
#31581: Queryset bug when using Q()? -- doubles up the counts by doing an extra
join
-------------------------------------+-------------------------------------
Reporter: Javier Buzzi | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 3.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

--
Ticket URL: <https://code.djangoproject.com/ticket/31581#comment:1>

Django

unread,
May 13, 2020, 8:57:50 AM5/13/20
to django-...@googlegroups.com
#31581: Queryset bug when using Q()? -- doubles up the counts by doing an extra
join
-------------------------------------+-------------------------------------
Reporter: Javier Buzzi | Owner: nobody
Type: Bug | Status: new

Component: Database layer | Version: 3.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Javier Buzzi:

Old description:

New description:

I ran this on all the "recent" versions of python and i see the issue on
all of them. At this point, im not 100% sure if it is a problem or not, i
ran across this trying to show some counts in the Admin that had some
search_fields that traversed the model backwards and forwards in order to
match the thing properly.

This "bug" can be seen from 1.11-3.0. Havent checked 3.1

When i do:

it works as expected.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/31581#comment:2>

Django

unread,
May 13, 2020, 8:59:55 AM5/13/20
to django-...@googlegroups.com
#31581: Queryset bug when using Q()? -- doubles up the counts by doing an extra
join
-------------------------------------+-------------------------------------
Reporter: Javier Buzzi | Owner: nobody
Type: Bug | Status: new

Component: Database layer | Version: 3.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Javier Buzzi:

Old description:

> I ran this on all the "recent" versions of python and i see the issue on


> all of them. At this point, im not 100% sure if it is a problem or not, i
> ran across this trying to show some counts in the Admin that had some
> search_fields that traversed the model backwards and forwards in order to
> match the thing properly.
>

> This "bug" can be seen from 1.11-3.0. Havent checked 3.1
>

New description:

I ran this on all the "recent" versions of python and i see the issue on
all of them. At this point, im not 100% sure if it is a problem or not, i
ran across this trying to show some counts in the Admin that had some
search_fields that traversed the model backwards and forwards in order to
match the thing properly.

This "bug" can be seen from 1.11-3.0. Haven't checked 3.1 because i cant
find it in pip.

When i do:

it works as expected.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/31581#comment:3>

Django

unread,
May 13, 2020, 9:03:21 AM5/13/20
to django-...@googlegroups.com
#31581: Queryset bug when using Q()? -- doubles up the counts by doing an extra
join.

-------------------------------------+-------------------------------------
Reporter: Javier Buzzi | Owner: nobody
Type: Bug | Status: closed

Component: Database layer | Version: 3.0
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* status: new => closed
* resolution: => invalid


Comment:

These queryset are different so you cannot expect the same results:
- `Q(condition1) | Q(condition2)` means "condition1 **OR** condition2",
- `.filter(condition1, condition2)` means "condition1 **AND**
condition2".

--
Ticket URL: <https://code.djangoproject.com/ticket/31581#comment:4>

Django

unread,
May 13, 2020, 9:30:28 AM5/13/20
to django-...@googlegroups.com
#31581: Queryset bug when using Q()? -- doubles up the counts by doing an extra
join.
-------------------------------------+-------------------------------------
Reporter: Javier Buzzi | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 3.0
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Javier Buzzi):

My apologies, i just realized my mistake. Looks like im going to have to
wrap the admin filters in order for it to work correctly, and thats fine.
The ORM is working as expected. Sorry to waste anyones time.

--
Ticket URL: <https://code.djangoproject.com/ticket/31581#comment:5>

Reply all
Reply to author
Forward
0 new messages