SQLAlchemy jsonb列如何根据键上的id列表执行过滤器

2024-10-03 09:13:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一份身份证清单,比如:

tracker_ids = [69]

我需要根据追踪器的id获取所有的信息对象

数据如下:

^{pr2}$

我尝试了下面的方法,但它产生了一个错误。在

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.

Tags: 数据对象方法信息ididsdatatype
1条回答
网友
1楼 · 发布于 2024-10-03 09:13:42

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

相关问题 更多 >