我正在使用 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 byJSON
, including the same behaviors for indexing operations. It also adds additional operators specific toJSONB
, includingJSONB.Comparator.has_key()
,JSONB.Comparator.has_all()
,JSONB.Comparator.has_any()
,JSONB.Comparator.contains()
, andJSONB.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/