-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[MySQL] [playhouse] [JSONField] contains() on the JSONField causes error: Incorrect arguments to ESCAPE #2609
Comments
The query seems to work fine for the Reg.insert_many(
[
{"id": 1, "name": "bar \\ 1", "message": ["a \\ b"]},
{"id": 2, "name": "bar", "message": ["a \\\\ b"]},
]
).execute()
key = "\\"
# This query against the name column works fine.
query = Reg.select(Reg.name).where(Reg.name.contains(key))
print('name like')
for r in query:
print(r.name)
# This query against the message (json) column does not.
query = Reg.select(Reg.name).where(Reg.message.contains(key))
print('message like')
for r in query: # peewee.OperationalError: (1210, 'Incorrect arguments to ESCAPE')
print(r.name) Furthermore, I think using # This works just fine:
query = Reg.select(Reg.name).where(fn.json_extract(Reg.message, '$[0]').contains(key))
# SELECT `t1`.`name` FROM `reg` AS `t1`
# WHERE (json_extract(`t1`.`message`, '$[0]') LIKE '%\\%' ESCAPE '\')
for r in query: # works fine.
print(r.name) Basically, I think you're misusing the JSON data-type and should instead either:
|
@coleifer Using the |
In addition to this if we even used the #!/usr/bin/env python
import json
from peewee import *
db = MySQLDatabase(
"db", host="localhost", port=3306, user="user", password="password"
)
class JsonTextField(TextField):
def db_value(self, value):
if value is not None:
return json.dumps(value)
def python_value(self, value):
if value is not None:
return json.loads(value)
class t1(Model):
id = IntegerField(primary_key=True)
name = CharField(255)
message = JsonTextField()
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)
for r in query:
print(r) It produces the same error: SELECT `t1`.`name` FROM `t1` AS `t1` WHERE ((`t1`.`name` LIKE '%\\\\%' ESCAPE '\\') OR (`t1`.`message` LIKE '\"%\\\\\\\\%\"' ESCAPE '\"\\\\\"')) |
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. |
3.15.2 released now, which contains this fix. |
Having the following script that replicates the issue:
It produces invalid query:
We are currently using the version 3.14.4, however the same issue is in the latest.
The text was updated successfully, but these errors were encountered: