Make case-sensitive SQLAlchemy case-insensitive

I am trying to query my PostgreSQL database using row lists. I want to return all rows whose column matches this row, and I would like it to be case insensitive in order to find more things.

fruits = ['apple', 'orange', 'pear', 'grape', 'watermelon', 'asian pear']

In this case, the "Asian pear" can be capitalized in the database.

obs = session.query(datamodel).filter(datamodel.fruitname._in(fruits)).all()

I know about func.lower (), and I use this for individual requests, but I'm not sure where to put it when using.

I would use func.lower like this in one question:

obs =session.query(datamodel).filter(func.lower(datamodel.fruitname)==func.lower(fruits[5]))).first()
+4
source share
1 answer

Stupid for me ...

When writing this question, I realized this ... this is the answer ...

session.query(datamodel).filter(func.lower(datamodel.fruitname).in_(fruits)).all()
+5
source

Source: https://habr.com/ru/post/1530903/


All Articles