I'm using Flask-SQLAlchemy to query from a database of users; however, while
user = models.User.query.filter_by(username="ganye").first()
will return
<User u'ganye'>
doing
user = models.User.query.filter_by(username="GANYE").first()
returns
None
I'm wondering if there's a way to query the database in a case insensitive way, so that the second example will still return
<User u'ganye'>
You can do it by using either the
lower
orupper
functions in your filter:Another option is to do searching using
ilike
instead oflike
:Improving on @plaes's answer, this one will make the query shorter if you specify just the column(s) you need:
The above example is very useful in case one needs to use Flask's jsonify for AJAX purposes and then in your javascript access it using data.result: