繁体   English   中英

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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM