0

In django, to query JSONB, I do:

    cursor.execute("""\
    SELECT * FROM "somemodel_some_model" 
    WHERE UPPER(("somemodel_some_model"."data" 
    #>> array[0,'fieldX'])::text[]) 
    LIKE UPPER(% %s %)
    """,[my_string])

.. and I get:

IndexError: list index out of range

I know the above cant be true because when I use the ORM to achive this using:

obj=Some_Model.objects.filter(data__0__fieldX__icontains=search_term)

.. I get the results. To investigate this, I drop to SQL and do the following:

SELECT * FROM "somemodel_some_model"
WHERE UPPER(("somemodel_some_model"."data" 
#>> array[0, 'fieldX'])::text[]) 
LIKE UPPER(%my_search_string%)

..but, I get:

django.db.utils.ProgrammingError: syntax error at or near "%"

So, the question is, do I need to escape %? Seems odd

0

2 Answers 2

1

When using cursor.execute like syntax should be LIKE %% string %% or if you use column and LIKE %% || column || %%

Sign up to request clarification or add additional context in comments.

Comments

0

try this:

 cursor.execute("SELECT * FROM somemodel_some_model WHERE UPER((somemodel_some_model.data #>> array[0,'fieldX'])::text[])  LIKE UPPER(concat('%', %s, '%')) ",[my_string])

because if you use UPPER(% %s %)

resulting query would be like UPPER(% 'your_string' %) (I don't know if django quotes literals) or UPPER(% your_string %) - both cases not valid for SQL, So you either gave to construct [%my_string%] in django, or use concat as in example above

3 Comments

unfortunately, when I use LIKE UPPER(concat('%', %s, '%')) it still gives the same error (I presume there is a typo in your statement with a missing ))
yes - there were a typo. are you sure in your """?.. there's no need in "somemodel_some_model" - you can use somemodel_some_model, same for tablename
I am not totally sure I understand your last comment, but, when I use somemodel_some_model for my other queries, it seems to execute perfectly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.