0

How could I make this query be case insensitive?

result = db.session.query(countries).filter_by(name=country,capital=capital).first()

Im using Flask SQL-Alchemy by the way. I can do db.func.lower() but I havent found a way to use it properly in my line yet.

davidism
  • 121,510
  • 29
  • 395
  • 339
magnus
  • 66
  • 1
  • 6

1 Answers1

2

You should use filter instead of filter_by:

from sqlalchemy import func

result = db.session.query(countries).filter(
   func.lower(countries.name) == name.lower(),
   func.lower(countries.capital) == capital.lower()
).first()

If countries is a table (not model) you should use countries.c.name syntax.

Similar topic: Case Insensitive Flask-SQLAlchemy Query.

jorzel
  • 1,216
  • 1
  • 8
  • 12
  • thanks alot for the answer! you were correct. im using a table btw! so using the syntax your showed made it work. btw, in your code there is filter_by. this is the code that ended up working for me: `result = db.session.query(countries).filter( db.func.lower(countries.c.name) == country.lower(), db.func.lower(countries.c.capital) == capital.lower() ).first()` – magnus Dec 17 '21 at 15:27
  • IT was typo and mistake of . Corrected – jorzel Dec 17 '21 at 15:57