首页 > 解决方案 > sqlalchemy 中的数组过滤

问题描述

我想以这种方式过滤我的数组字段:我有数组 my_array {'one', 'two', 'three'} 并且我想按值列表过滤数据 ['one', 'two']。因此,如果我的数组中有“一”和“二”,那么我会得到这些数据。我试图用 sqlalchemy 'contains' 来实现这一点。

 result = session.query(Alert).filter(Alert.tag_list.contains(tag_list)).filter(
                Alert.date_create >= datetime.datetime.utcnow() - datetime.timedelta(minutes=c.ALERT_TIME_LIVE)).order_by(Alert.person_id).limit(
            per_page).offset((page - 1) * per_page).all()

我的数据库类型from sqlalchemy.dialects.postgresql import ARRAYtag_list = Column(ARRAY(String(50), dimensions=1)). 但是当我尝试完成我的查询时,即使我的数组中存在两个标签,我也会收到空列表。 在此处输入图像描述

在我的图片中我有标签'test',但是当使用'contains(['test','test1'])'它返回[]。

标签: pythonpostgresqlsqlalchemy

解决方案


推荐阅读