SQLAlchemy jsonb 列 - 如何根据键上的 id 列表执行过滤器
SQLAlchemy jsonb column - How to perform filter based on list of ids on a key
我有一个 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.
您必须在将 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()
我有一个 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.
您必须在将 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()