[Django] #33746: [ORM] queryset.query return a string that makes MySQLDB to fail with error "not enough arguments for format string"

16 views
Skip to first unread message

Django

unread,
May 27, 2022, 2:23:05 AM5/27/22
to django-...@googlegroups.com
#33746: [ORM] queryset.query return a string that makes MySQLDB to fail with error
"not enough arguments for format string"
-------------------------------------+-------------------------------------
Reporter: Juanmi | Owner: nobody
Taboada |
Type: Bug | Status: new
Component: Database | Version: 4.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 |
-------------------------------------+-------------------------------------
Recently I faced an error when getting a query with **queryset.query**.

The resultant string had inside "**%**" (percentage character) for **LIKES
clauses** and **MySQLDB** module at is actual version is rendering the
query using:
{{{
query = query % args
}}}


Example of the wrong a QUERY I got from **queryset.query** rendering:
{{{
SELECT DISTINCT products_becaseeker.beca_id,
`products_becaseeker`.`status` FROM `products_becaseeker` INNER JOIN
`products_becapublic` ON (`products_becaseeker`.`beca_id` =
`products_becapublic`.`beca_id`) WHERE
(`products_becapublic`.`convenor_id` = 246 OR
`products_becapublic`.`convenor_id` = 34 OR
LOWER(JSON_UNQUOTE(`products_becaseeker`.`convenor_others`)) LIKE
LOWER(%"246"%) OR
LOWER(JSON_UNQUOTE(`products_becaseeker`.`convenor_others`)) LIKE
LOWER(%"34"%)) AND MATCH(products_becaseeker.keywords) AGAINST
(\'(comunidad* madrid*) ("comunidad de madrid") ("comunidad de madrid")
(comunidad madrid)\' IN BOOLEAN MODE) ORDER BY products_becaseeker.status
DESC
}}}
the offending part is:
{{{
... LIKE LOWER(%"246"%) ...
... LIKE LOWER(%"34"%) ...
}}}

I believe the right string to be render in **MySQLDB** should be:
{{{
... LIKE LOWER("%%246%%") ...
... LIKE LOWER("%%34%%") ...
}}}

The error I verified it is happening in **Django 4.0.2** together with
**Python 3.9** at **MySQLdb/cursors.py, line 201**

The short quick solution was to replace those with the right ones:
{{{
.replace('(%"', '("%%').replace('"%)', '%%")')
}}}

and it worked like a charm, but I believe the query rendered should be
aware about how MySQLDB is rendering the query.

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

Django

unread,
May 27, 2022, 3:25:06 AM5/27/22
to django-...@googlegroups.com
#33746: [ORM] queryset.query return a string that makes MySQLDB to fail with error
"not enough arguments for format string"
-------------------------------------+-------------------------------------
Reporter: Juanmi Taboada | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 4.0
(models, ORM) |
Severity: Normal | Resolution: duplicate
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 Carlton Gibson):

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


Comment:

Duplicate of #25705

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

Reply all
Reply to author
Forward
0 new messages