Столбец SQLAlchemy jsonb - Как выполнить фильтр на основе списка идентификаторов для ключа - PullRequest
0 голосов
/ 09 октября 2018

У меня есть список идентификаторов, таких как:

tracker_ids = [69]

Мне нужно получить все объекты APInformation на основе tracker_id.

данные выглядят так:

{ '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.

1 Ответ

0 голосов
/ 09 октября 2018

Вы должны привести значение 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()
...