python - 使用 array.contains() 对 postgresql 数组列进行查询过滤
问题描述
我有一个数组列(我需要,我知道这不是典型的约定),我需要在查询中对其进行过滤。我正在尝试使用 contains(),
db.session.query(table).filter((table.name.ilike
('%'+name+'%')),
table.languages.contains(language)).limit(200)
但我不断收到此错误:
NotImplementedError: ARRAY.contains() not implemented for the base
ARRAY
type; please use the dialect-specific ARRAY type
我是 Flask-Sqlalchemy 的新手,所以我不确定如何正确使用它。任何帮助深表感谢!
解决方案
这个期望意味着你应该使用dialect-specific ARRAY type
来代替基本的 ARRAY 类型。所以在你的模型定义中,使用sqlalchemy.dialects.postgresql.ARRAY
toARRAY
from sqlalchemy.dialects.postgresql import ARRAY
class table(db.Model):
...
languages = db.Column(ARRAY) # notice this ARRAY import from sqlalchemy.dialects.postgresql
现在,您可以使用db.session.query(table).filter(table.languages.contains([language])).limit(200)
.
推荐阅读
- emacs - 如何在 f90 模式的 emacs 中自定义颜色?
- c++ - 如何制作半透明颜色的 QGraphicsItem?
- reactjs - 导入组合组件时出现打字稿导入错误
- python - 如何判断我的 AWS EC2 实例是否支持 Amazon-EBS?
- javascript - 有没有办法在 JavaScript 中水平输出
- java - 带有 java 参数的 NSIS 快捷方式
- python - 网页抓取:在 BeautifulSoup 中使用 findAll 的问题
- vue.js - VueJs:如何根据另一个元素更新一个元素的宽度?
- opencv - 另一个:CMake 错误:变量设置为 NOTFOUND:CUDA_nppi_LIBRARY (ADVANCED)
- python - Amazon Elastic Beanstalk 如何采用自定义 python 命令?