python - SQLAlchemy 'Comparator' 对象没有属性 'has_key'

标签 python json postgresql sqlalchemy

我正在使用 JSON SQLAlchemy 列,并且我的查询语句需要“has_key”属性,但显然我的“Comparator”对象没有此属性。

Transaction.query.filter(Transaction.fields.has_key('issuername')).all()

其中 Transaction.fields 是一个列(JSON)。我收到错误“AttributeError:与 Transaction.fields 关联的“InstrumentedAttribute”对象和“Comparator”对象都没有属性“has_key””。 My SQLAlchemy 最新版本为 1.09。我构建这个查询是错误的吗?

最佳答案

has_key 运算符特定于 JSONB

The JSONB type includes all operations provided by JSON, including the same behaviors for indexing operations. It also adds additional operators specific to JSONB, including JSONB.Comparator.has_key(), JSONB.Comparator.has_all(), JSONB.Comparator.has_any(), JSONB.Comparator.contains(), and JSONB.Comparator.contained_by().

对于 JSON 列,您可以使用 -> PostgreSQL 运算符(通过键获取 JSON 对象字段)

Transaction.query.filter(Transaction.fields.op('->')('issuername')!=None).all()

关于python - SQLAlchemy 'Comparator' 对象没有属性 'has_key',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34027532/

相关文章:

python - 适用于 PostgreSQL 和 MySQL 的纯 python SQL 解决方案?

python - 区分 PySide 中的信号源

javascript - 如何添加名称已作为另一个属性存在的 JSON 项?

json - 使用 JSONP 从服务器返回响应

javascript - 当对象的键中有冒号时如何访问对象属性?

sql - 如何在 Postgresql 中向字符串添加任意数量的数字或从字符串中删除数字

python - numpy 从二维矩阵获取子矩阵

python - Pygame display.info 给出了错误的分辨率大小

python - 即使我清楚地为其分配了不同的值,方法属性也不会自行更新

postgresql - PostgreSQL 是否索引空值?