Opened 2 years ago

Closed 2 years ago

#33746 closed Bug (duplicate)

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

Reported by: Juanmi Taboada Owned by: nobody
Component: Database layer (models, ORM) Version: 4.0
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

Description

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.

Change History (1)

comment:1 by Carlton Gibson, 2 years ago

Resolution: duplicate
Status: newclosed

Duplicate of #25705

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