【问题标题】:Why can I update a PostgreSQL column string but not an integer in Sequel from a JSONB value?为什么我可以从 JSONB 值更新 PostgreSQL 列字符串但不能更新 Sequel 中的整数?
【发布时间】:2016-03-20 10:22:26
【问题描述】:

我正在使用 Sequel,并且我有一个带有 JSONB 散列的表作为列,我想从该散列中获取一个值并将其全部存储在表列中。这适用于字符串目标列:

j = Sequel.pg_jsonb_op(:metadata)
DB[:litigation_cached_inputs].update(description: j.get_text('Summary'))

我的诉讼缓存输入表正在更新,以将描述字符串列设置为元数据 JSONB 列中摘要哈希键的值。

当我尝试做同样的事情,但目标列的 (nature_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

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

【问题讨论】:

    标签: ruby json postgresql sequel


    【解决方案1】:

    需要将值强制转换,不能将jsonb直接转换为integer,需要中间转换为textj.extract('NatureOfSuitID').cast(String).cast(Integer)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-12
      • 1970-01-01
      • 2020-07-27
      • 2016-07-14
      • 2015-07-16
      • 2015-11-10
      相关资源
      最近更新 更多