python - 如何在 SQLAlchemy 中比较 Python 列表和 Postgres 数组?

标签 python postgresql sqlalchemy

我的 PSQL 表类型数组中有一个整数类型的列,我们称该列为 tags。我有一个具有整数类型的 Python 列表,我们称它为 categories

如果标签列有一个或多个与类别数组匹配的整数,我如何运行 SQLAlchemy 查询以便返回表中的任何行?

最佳答案

假设 categories 列表不是很大,这是使用 any(...) 对于每个类别:

q = db.session.query(MyTable)
clauses = [MyTable.tags.any(cat_id) for cat_id in categories]
q = q.filter(db.or_(*clauses))
for r in q.all():
    print(r)

关于python - 如何在 SQLAlchemy 中比较 Python 列表和 Postgres 数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30561075/

相关文章:

python - sqlalchemy 整数列大小

python - Sqlalchemy 以相同的顺序获得结果

python - 如何测试 Python 3.4 asyncio 代码?

python - 类型错误 : unsupported operand type(s) for/: 'Image' and 'int'

python - python 2.7 中的等效 urllib.parse.unquote()

sqlite - SQLite 中有关纪元的问题

python - 使用 psycopg2 将行插入 psql db

python 包导入模块使用 __init__.py

postgresql - postgres 选择每个实体的最新记录,字段值除外

python - 通过排除表来处理 Automap 错误