Dim*_*tri 3 ruby postgresql sequel sequel-gem
我有一张桌子transactions
id | bigint | NOT NULL DEFAULT nextval('transactions_id_seq'::regclass)
transaction_id | bigint | NOT NULL
middleware_id | text |
opname | optype | NOT NULL
created_at | timestamp without time zone | NOT NULL
finished_at | timestamp without time zone |
request | jsonb | NOT NULL
answer | jsonb |
Run Code Online (Sandbox Code Playgroud)
其中请求可以包含以下数据:
{ plugin_id: string, item_src_id: string, item_dst_id: string, payload: {}}
或
{ plugin_id: string, item_id: string, payload: {}}
item_id我可以使用纯 SQL来选择事务列表:
SELECT id, request
FROM transactions
WHERE 'BEX456'
IN (request->>'item_id', request->>'item_src_id', request->>'item_dst_id')
Run Code Online (Sandbox Code Playgroud)
但是,当我在 Sequel 中使用该请求时,它会警告我
SEQUEL 弃用警告:调用具有多个参数或第一个参数/元素是字符串的数组的数据集过滤方法已被弃用,并将在 Sequel 5 中删除。
对于该请求:
$db[:transactions].
where("? IN (request->>'item_id', request->>'item_src_id', request->>'item_dst_id')", data[:item_id]).all
Run Code Online (Sandbox Code Playgroud)
我可以Sequel.lit在 where 操作中使用该字符串,但我的问题是 -我可以使用本机 Sequel 运算符来选择 field 内的 json 吗?
这对我有用:
$db[:transactions].where(
data[:item_id] => [
Sequel.lit("request->>'item_id'"),
Sequel.lit("request->>'item_dst_id'"),
Sequel.lit("request->>'item_src_id'") ] ).first
Run Code Online (Sandbox Code Playgroud)
pg_json_opsPostgres 的 JSON 运算符在扩展中可用。
预先加载:
Sequel.extension :pg_json_ops
Run Code Online (Sandbox Code Playgroud)
然后,根据您的具体情况:
# request ->> 'item_id'
:request.pg_jsonb.get_text('item_id') # WITH core extensions
Sequel.pg_jsonb_op(:request).get_text('item_id') # or WITHOUT
Run Code Online (Sandbox Code Playgroud)
您也拥有 Ruby 的力量:
["item_id", "item_dst_id", "item_src_id"].map { |key| :request.pg_jsonb.get_text(key) }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1407 次 |
| 最近记录: |