Filtering a database using __icontains fails for JSONB fields
See original GitHub issueDescribe the bug
In the previous version of the library (tortoise-orm==0.16.4
namely), filtering of a database using __icontains
on a JSONB field of a model would convert the field to VARCHAR
and then search for the requested substring. However, for tortoise-orm==0.16.8
that does not work and fails with an error:
function upper(jsonb) does not exist
HINT: No function matches the given name and argument types.
You might need to add explicit type casts.
To Reproduce
The code to execute the query:
phrases = ["a red apple", "a book", "the Sun", "a big store"]
query = Q()
for phrase in phrases:
query = query & Q(poem__icontains=phrase)
collection = collection.filter(query)
total_filtered = await collection.count()
where collection
is a table that has the poem
field, which in turn is of a JSONB type.
For tortoise-orm==0.16.8
the raw query is (and it fails with the error above):
SELECT COUNT(*) FROM "collection" WHERE UPPER("phrases") LIKE UPPER(\'%a red apple%\') ESCAPE \'\\\
Expected behavior
SELECT COUNT(*) FROM "collection" WHERE UPPER(CAST("phrases" AS VARCHAR)) LIKE UPPER('%a red apple%')
Additional context
PostgreSQL is used for database handling.
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Filtering and calculating over JSONB object - Stack Overflow
First expand brand and status into separate rows using cross join lateral and then use count filter conditional aggregation.
Read more >Using Postgres JSONB Fields in Django - pganalyze
With Django's ORM, creating JSONB fields, using them in your models, inserting data into them, and querying from them is all possible.
Read more >Documentation: 15: 8.14. JSON Types - PostgreSQL
The json data type stores an exact copy of the input text, which processing functions must reparse on each execution; while jsonb data...
Read more >SODA Filter Specifications (Reference) - Oracle Help Center
A filter can use QBE operators, which are predefined JSON fields whose names start with a dollar-sign character ( $ ). The JSON...
Read more >PostgreSQL specific model fields - Django documentation
from django.contrib.postgres.fields import ArrayField from django.db import ... <Post: Second post>]> >>> Post.objects.filter(tags__contains=['django']) ...
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 Free
Top 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
yes, works great. thank you both @lntuition and @grigi for the fast replies and action!
Ah, I found
CAST
was removed at 42adca39. I think we should revert it. Thank you for your report !