You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug @henadzit Hello bro, I am trying to perform a fuzzy search on a JSON field (json_field) using the icontains filter in Tortoise ORM. However, I am encountering an error when attempting to filter the JSON array.
To Reproduce
awaitGoods.create(json_field=[{"goodsId": 111, "goodsName": "111"}, {"goodsId": 222, "goodsName": "222"}])
result=awaitGoods.filter(json_field__filter=[{"goodsName__icontains": "11"}])
result=awaitGoods.filter(json_field__filter={"0__goodsName__icontains": "11"}) # it work, but can't determine the index
@sleep1223 I'm afraid you cannot use icontains in this way. There are a few alternatives:
Use RawSQL. Below is one of the ways to do it.
await Goods.annotate(goods_name_match=RawSQL(
"(SELECT 1 FROM jsonb_array_elements(json_field) AS elem WHERE elem->>'goodsName' ILIKE '111')"
)).filter(goods_name_match=1)
But be aware that this option is vulnerable to SQL injections.
If case-insensitive search is not a requirement, you can do the following:
Describe the bug
@henadzit Hello bro, I am trying to perform a fuzzy search on a JSON field (json_field) using the icontains filter in Tortoise ORM. However, I am encountering an error when attempting to filter the JSON array.
To Reproduce
Expected behavior
Additional context
Add any other context about the problem here.
The text was updated successfully, but these errors were encountered: