﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
33746	"[ORM] queryset.query return a string that makes MySQLDB to fail with error ""not enough arguments for format string"""	Juanmi Taboada	nobody	"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."	Bug	closed	Database layer (models, ORM)	4.0	Normal	duplicate			Unreviewed	0	0	0	0	0	0
