[Django] #33808: Count manytomany__in produces wrong SQL

18 views
Skip to first unread message

Django

unread,
Jun 24, 2022, 10:54:40 AM6/24/22
to django-...@googlegroups.com
#33808: Count manytomany__in produces wrong SQL
-------------------------------------+-------------------------------------
Reporter: | Owner: nobody
OrsoBruno96 |
Type: Bug | Status: new
Component: Database | Version: 3.2
layer (models, ORM) | Keywords: database, count,
Severity: Normal | query, orm
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
You can find in attachment a minimal working example. I created the zip
with

{{{
django-admin startproject mwe
./manage.py startapp testapp
}}}


Then I only edited the file `testapp/models.py` `testapp/tests.py` and
`mwe/settings.py` (to add the app to the installed apps) and ran
`./manage.py makemigrations testapp`.

Running the tests with the usual `./manage.py test` triggers the problem.
I printed the query produced by django, simply adding a `print` in
`django/db/backends/sqlite/base.py`. The query is the following:

{{{
SELECT DISTINCT "testapp_firstmodel"."id",
COUNT("testapp_secondmodel_related_field2"."secondmodel_id" IN
"testapp_secondmodel_related_field1"."secondmodel_id") AS "howmany"
FROM "testapp_firstmodel"
LEFT OUTER JOIN "testapp_secondmodel_related_field1" ON
("testapp_firstmodel"."id" =
"testapp_secondmodel_related_field1"."firstmodel_id")
LEFT OUTER JOIN "testapp_secondmodel_related_field2" ON
("testapp_firstmodel"."id" =
"testapp_secondmodel_related_field2"."firstmodel_id")
GROUP BY "testapp_firstmodel"."id" LIMIT 21
}}}

Adding a layer of parenthesis in the query inside the `Count` fixes the
issue.


{{{
SELECT DISTINCT "testapp_firstmodel"."id",
COUNT("testapp_secondmodel_related_field2"."secondmodel_id" IN
("testapp_secondmodel_related_field1"."secondmodel_id")) AS "howmany"
FROM "testapp_firstmodel"
LEFT OUTER JOIN "testapp_secondmodel_related_field1" ON
("testapp_firstmodel"."id" =
"testapp_secondmodel_related_field1"."firstmodel_id")
LEFT OUTER JOIN "testapp_secondmodel_related_field2" ON
("testapp_firstmodel"."id" =
"testapp_secondmodel_related_field2"."firstmodel_id")
GROUP BY "testapp_firstmodel"."id" LIMIT 21
}}}

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

Django

unread,
Jun 24, 2022, 10:55:07 AM6/24/22
to django-...@googlegroups.com
#33808: Count manytomany__in produces wrong SQL
-------------------------------------+-------------------------------------
Reporter: Fabio Zoratti | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: database, count, | Triage Stage:
query, orm | Unreviewed
Has patch: 0 | Needs documentation: 0

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

* Attachment "mwe.zip" added.

Minimal django project reproducing the issue

Django

unread,
Jun 24, 2022, 4:12:13 PM6/24/22
to django-...@googlegroups.com
#33808: Count manytomany__in produces wrong SQL
-------------------------------------+-------------------------------------
Reporter: Fabio Zoratti | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: database, count, | Triage Stage:
query, orm | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Fabio Zoratti):

I used `git bisect` to find the commit that broke the code. The commit is

{{{

commit 3a505c70e7b228bf1212c067a8f38271ca86ce09 (HEAD, refs/bisect/bad)
Author: Simon Charette <chare...@gmail.com>
Date: Wed Mar 6 01:24:41 2019 -0500

Refs #27149, #29542 -- Simplified subquery parentheses wrapping logic.
}}}

The diff clearly shows that some parenthesis are being removed from the
query

{{{
git show 3a505c70e7b228bf1212c067a8f38271ca86ce09
diff --git a/django/db/models/lookups.py b/django/db/models/lookups.py
index e78ffdf390..910291094e 100644
--- a/django/db/models/lookups.py
+++ b/django/db/models/lookups.py
@@ -89,8 +89,7 @@ class Lookup:
value = self.apply_bilateral_transforms(value)
value = value.resolve_expression(compiler.query)
if hasattr(value, 'as_sql'):
- sql, params = compiler.compile(value)
- return '(' + sql + ')', params
+ return compiler.compile(value)
else:
return self.get_db_prep_lookup(value, connection)

}}}

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

Reply all
Reply to author
Forward
0 new messages