2015-12-01 61 views
2

我正在使用JSON SQLAlchemy列,我的查詢語句需要'has_key'屬性,但顯然我的'Comparator'對象沒有這個屬性。SQLAlchemy的比較器對象沒有屬性'has_key'

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

其中Transaction.fields是列(JSON)。我得到錯誤「AttributeError:與Transaction.fields關聯的'InstrumentedAttribute'對象和'Comparator'對象都沒有屬性'has_key'」。我的SQLAlchemy是最新版本1.09。我建立這個查詢是錯誤的?

回答

2

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() 
相關問題