question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Filtering a database using __icontains fails for JSONB fields

See original GitHub issue

Describe 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:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
odimkocommented, Apr 27, 2020

yes, works great. thank you both @lntuition and @grigi for the fast replies and action!

1reaction
lntuitioncommented, Apr 24, 2020

Ah, I found CAST was removed at 42adca39. I think we should revert it. Thank you for your report !

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found