Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 为什么我可以从JSONB值更新PostgreSQL列字符串而不是Sequel中的整数?_Ruby_Json_Postgresql_Sequel - Fatal编程技术网

Ruby 为什么我可以从JSONB值更新PostgreSQL列字符串而不是Sequel中的整数?

Ruby 为什么我可以从JSONB值更新PostgreSQL列字符串而不是Sequel中的整数?,ruby,json,postgresql,sequel,Ruby,Json,Postgresql,Sequel,我正在使用Sequel,我有一个表,其中有一个JSONB哈希作为列,我想从该哈希中获取一个值,并将其自身存储在一个表列中。这适用于字符串目标列: j = Sequel.pg_jsonb_op(:metadata) DB[:litigation_cached_inputs].update(description: j.get_text('Summary')) 在这里,我的诉讼\u缓存的\u输入表正在更新,以将描述字符串列设置为元数据JSONB列中摘要哈希键的值 当我尝试做同样的事情时,但是如果目

我正在使用Sequel,我有一个表,其中有一个JSONB哈希作为列,我想从该哈希中获取一个值,并将其自身存储在一个表列中。这适用于字符串目标列:

j = Sequel.pg_jsonb_op(:metadata)
DB[:litigation_cached_inputs].update(description: j.get_text('Summary'))
在这里,我的诉讼\u缓存的\u输入表正在更新,以将描述字符串列设置为元数据JSONB列中摘要哈希键的值

当我尝试做同样的事情时,但是如果目标列的(
nature\u of_suit
)类型是整数(与此相反,
description
列是字符串类型),我会得到错误:

[42] pry(main)> DB[:litigation_cached_inputs].update(nature_of_suit: j.extract('NatureOfSuitID'))
2015-12-14T14:31:23-05:00 [DEBUG] 633 :   Sequel::Postgres::Database (3.3ms)  UPDATE "litigation_cached_inputs" SET "nature_of_suit" = jsonb_extract_path("metadata", 'NatureOfSuitID')
Sequel::DatabaseError: PG::DatatypeMismatch: ERROR:  column "nature_of_suit" is of type integer but expression is of type jsonb

当我想提取一个字符串而不是一个整数时,为什么这样做会起作用?

您需要强制转换值,而不能将
jsonb
直接强制转换为
integer
,您需要对
text
进行中间强制转换:
j.extract('NatureOfSuitID')。强制转换(string)。强制转换(integer)
您需要强制转换值,而且您不能将
jsonb
直接转换为
integer
,您需要对
text
进行中间转换:
j.extract('NatureOfSuitID')。转换(字符串)。转换(整数)