PostgreSQL与SQLAlchemy的ILIKE查询

编程入门 行业动态 更新时间:2024-10-15 22:26:38
本文介绍了PostgreSQL与SQLAlchemy的ILIKE查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想运行一个查询,选择所有标题匹配'%'+ [some_phrase] +'%'的不区分大小写的帖子。也就是说,选择标题包含某个短语的所有行,不区分大小写。从我做的研究,看起来像我需要使用Postgres的ILIKE查询来匹配不区分大小写。如何使用SQLAlchemy执行这样的查询?

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

解决方案

我认为它应该工作

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

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

I'd like to run a query that selects all posts, case insensitive, that have titles 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?

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 think it should work

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

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

更多推荐

PostgreSQL与SQLAlchemy的ILIKE查询

本文发布于:2023-10-15 08:53:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1493821.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:PostgreSQL   SQLAlchemy   ILIKE

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!