背景:我們使用PaperTrail來保存我們不斷變化的模型的歷史。現在我想查詢一個屬於某個客戶的項目。 PaperTrail可選擇存儲object_changes
,我需要查詢此字段以瞭解何時使用此ID創建或更改爲此ID。查詢整數成員的jsonb數組
我的表看起來簡化這樣的:
item_type | object_changes
----------|----------------------------------------------------------
"Item" | {"customer_id": [null, 5], "other": [null, "change"]}
"Item" | {"customer_id": [4, 5], "other": ["unrelated", "change"]}
"Item" | {"customer_id": [5, 6], "other": ["asht", "asht"]}
如何查詢元素從或ID 5(所以上面的所有行)改變了嗎?我想:
SELECT * FROM versions WHERE object_changes->'customer_id' ? 5;
這讓我:
ERROR: operator does not exist: jsonb ? integer LINE 1: ...T * FROM versions WHERE object_changes->'customer_id' ? 5; ^ HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
您可能還喜歡'where_object_changes'方法。它應該是爲'object_changes'列做一個'where'子句的簡便方法。 –
那麼你有你的答案嗎? –