1

I know this may seem like a duplicate, but the answer to the question asked that is basically identical to this one did not work for me.

```
from sqlalchemy import or_, func as F

query = request.args.get("query")

search_books = SavedBooks.query.filter(SavedBooks.authors.any(f'{query}')).all()
search_books = 
SavedBooks.query.filter(F.array_to_string(SavedBooks.authors,',').ilike(f'{query}')).all()
search_books = SavedBooks.query.filter(SavedBooks.authors.like(any_(f'{query}'))).all()
 ```

Of these three search_books options, the first returns the author if the query string is an exact match only. The second does the exact same as the first, the ilike seems to not make a difference, the third is an option that has some type of syntax error, but I suppose would work. Any suggestions?

Edit: This is the error I get when trying out the different queries.

sqlalchemy.exc.ProgrammingError: (psycopg2.errors.UndefinedFunction) operator does not exist: character varying[] ~~ unknown LINE 3: ....isbn13 ILIKE '%eastmond%' OR saved_books.authors LIKE ANY (... ^ HINT: No operator matches the given name and argument types. You might need to add explicit type casts.

Edit:

I am aware of this related post, How to use ilike sqlalchemy on postgresql array field?. I used it to formulate my question. Anyways, I was not able to query the author column of type array(string) with ilike, so I went with a workaround by creating a new column that was a copy of the authors column but in normal string format (I called authors_string). Then i just queried that column with ilike and it worked just fined. Make sure you remove the brackets from authors before you commit it into authors_string. I did that by using this. authors_string = str(authors).strip('[]')

Kuda
  • 95
  • 1
  • 9

1 Answers1

1

this should work..

search_books = SavedBooks.query.filter(SavedBooks.authors.ilike(query))).all()
Ruta Deshpande
  • 170
  • 1
  • 10