简体   繁体   English

使用SQLAlchemy进行PostgreSQL ILIKE查询

[英]PostgreSQL ILIKE query with SQLAlchemy

I'd like to run a query that selects all posts, case insensitive, that have titles that match '%' + [some_phrase] + '%' . 我想运行一个查询,选择所有帖子,不区分大小写,标题匹配'%' + [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. 从我所做的研究来看,我看起来需要使用Postgres的ILIKE查询来匹配不区分大小写。 How can I execute a query like this with SQLAlchemy? 如何使用SQLAlchemy执行这样的查询?

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

对于python 3.6而不是'%' + some_phrase + '%'你可以写

Post.query.filter(Post.title.ilike(f'%{some_phrase}%'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM