I am trying to make filter in postgre sql table that have varchar and integer column. And I need to apply filter. For example I have a category table as below:
id name rank
1 test1 1
2 test2 2
3 test3 3
And I want to apply filter in slqalchemy in flask as below:
filter_text = "%{}%".format(request.args.get('search[value]'))
filter_int = request.args.get(['search[value]')
if(filter_text):
categories = Category.query.filter(
(Category.category_name.like(filter_text)) |
(Category.rank == filter_int)
).all()
else:
categories = Category.query.all()
I get search[value] from the input field which can be both integer and alphabet or both. When I run code and pass alphabet I get error while I pass search['value'] = 't':
sqlalchemy.exc.DataError: (psycopg2.errors.InvalidTextRepresentation) invalid input syntax for integer: "t"
I know the issue is due to alphabet passed in rank column which is integer. But I want to search in all column using same single input field.