question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[MySQL] [playhouse] [JSONField] contains() on the JSONField causes error: Incorrect arguments to ESCAPE

See original GitHub issue

Having the following script that replicates the issue:

#!/usr/bin/env python

from peewee import *
from playhouse import mysql_ext

db = MySQLDatabase(
    "db", host="localhost", port=3306, user="user", password="password"
)

class t1(Model):
    id = IntegerField(primary_key=True)
    name = CharField(255) 
    message = mysql_ext.JSONField() 

    class Meta:
        database = db
        db_table = "t1"

db.connect()
db.create_tables([t1])
t1.truncate_table()
t1.insert_many(
    [
        {"id": 1, "name": "bar", "message": ["a \\ b"]},
        {"id": 2, "name": "bar", "message": ["a \\\\ b"]},
    ]
).execute()

key = "\\"

query = t1.select(t1.name).where(t1.name.contains(key) | t1.message.contains(key))

print(query) # SELECT `t1`.`name` FROM `t1` AS `t1` WHERE ((`t1`.`name` LIKE '%\\%' ESCAPE '\') OR (`t1`.`message` LIKE '"%\\\\%"' ESCAPE '"\\"'))

for r in query: # peewee.OperationalError: (1210, 'Incorrect arguments to ESCAPE')
    print(r)
    

It produces invalid query:

SELECT `t1`.`name` FROM `t1` AS `t1` WHERE ((`t1`.`name` LIKE '%\\\\%' ESCAPE '\\') OR (`t1`.`message` LIKE '\"%\\\\%\"' ESCAPE '\"\\\\\"'))

We are currently using the version 3.14.4, however the same issue is in the latest.

pip freeze | grep peewee
peewee==3.15.1

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
coleifercommented, Aug 26, 2022

3.15.2 released now, which contains this fix.

1reaction
coleifercommented, Aug 26, 2022

Aha, thank you for your persistence – I spotted the bug. The escape sequence is being handled by the Json converter, which dumps these strings as json and creates invalid escapes.

This should be fixed in master now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

MySQL 8.0 Reference Manual :: 11.5 The JSON Data Type
The JSON_TYPE() function expects a JSON argument and attempts to parse it into a JSON value. It returns the value's JSON type if...
Read more >
Insert single escape character into MySQL JSON field
The problem is I can't see how we get a single backslash into the system. Using two \'s causes the Invalid JSON error....
Read more >
Insert escaped double quotes into MySQL JSON field
Show activity on this post. into a MySQL JSON field, but I always get the error: SQL Error (3140): Invalid JSON text: "Missing...
Read more >
Playhouse, extensions to Peewee
If the passphrase is incorrect, an error will be raised when first ... Using the binary json field, you can test whether your...
Read more >
JSON Field model cause error during migrate #24 - GitHub
JSONField() which is caused from models.py. Even the migrations file defined the field as models.TextField() . Because if my understanding is ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found