I was a little addicted when I tried to search by string using json_contains
.
Must be enclosed in " "
. In fact, you need to use json_contains (fuga,'"piyo"')
when you select directly with mysql.
success.py
from sqlalchemy import func
search_str = 'piyo'
db.session.query(Hoge).filter(func.json_contains(Hoge.fuga, f'"{search_str}"'))
fail.py
from sqlalchemy import func
search_str = 'piyo'
db.session.query(Hoge).filter(func.json_contains(Hoge.fuga, search_str))
sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (3141, 'Invalid JSON text in argument 2 to function json_contains: "Invalid value." at position 0.')
Recommended Posts