我有一个ID列表,如:
tracker_ids = [69]
我需要基于tracker_id获取所有APInformation对象。
数据如下:
{ 'tracker_id' : 69, 'cpu_core_avg': 89.890', 'is_threshold': true,'datetime':1539053379040 }
{ 'tracker_id' : 70, 'cpu_core_avg': 65.0', 'is_threshold': false, 'datetime':1539053379040 }
{ 'tracker_id' : 69, 'cpu_core_avg': 34.9', 'is_threshold': false,'datetime':1539053379040 }
我尝试了以下操作,但会引发错误。
session.query(APInformation).\
filter(APInformation.data['tracker_id'].in_(tracker_ids),
APInformation.data['datetime'].astext.cast(BigInteger) > 1539053379040).\
all()
它引发的错误:
ProgrammingError: (psycopg2.ProgrammingError) operator does not exist: jsonb = integer
LINE 3: ...oring_apinfomation".data -> 'tracker_id') IN (69)
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
答案 0 :(得分:2)
您必须先对jsonb值进行强制转换,然后再将其与IN谓词一起使用,就像对 datetime 值进行的操作一样:
session.query(APInformation).\
filter(APInformation.data['tracker_id'].astext.cast(Integer).in_(tracker_ids),
APInformation.data['datetime'].astext.cast(BigInteger) > 1539053379040).\
all()