Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have the following model:

class Post(db.Model):
    id = db.Column(db.Integer, primary_key = True)
    title = db.Column(db.String(250))
    content = db.Column(db.String(5000))

I'd like to run a query that selects all posts, case insensitive, that match '%' + [some_phrase] + '%'. That is, select all rows that have titles that contain some phrase, case insensitive. From the research I've done, it looks like I need to use Postgres's ILIKE query for it to match case insensitive. How can I execute a query like this with SQLAlchemy? Something along the lines of

Post.query.select.where(Post.title.ilike('%[some_phrase]%'))?

Thanks.

share|improve this question
    
possible duplicate of Case Insensitive Flask-SQLAlchemy Query –  Mark Hildreth Dec 4 '13 at 0:15

1 Answer 1

up vote 8 down vote accepted

I think it should work

Post.query.filter(Post.title.ilike('%some_phrase%'))

http://docs.sqlalchemy.org/en/latest/orm/internals.html?highlight=ilike#sqlalchemy.orm.attributes.QueryableAttribute.ilike

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.