[MySQL] [playhouse] [JSONField] contains() on the JSONField causes error: Incorrect arguments to ESCAPE
See original GitHub issueHaving 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:
- Created a year ago
- Comments:5 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
3.15.2 released now, which contains this fix.
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.