【问题标题】:Rails Postgres migrations: invalid type name "jsonb USING content::jsonb" or invalid type name "jsonb USING CAST(content AS jsonb)Rails Postgres 迁移:无效类型名称“jsonb USING content::jsonb”或无效类型名称“jsonb USING CAST(content AS jsonb)
【发布时间】:2021-06-11 22:38:54
【问题描述】:

红宝石 2.7.3 导轨 6.1.3.1 PG 1.2.3 PostgreSQL 12.7

我正在将 Rails 4 应用程序迁移到 Rails 6。在进行大量 gem 更改后,我的迁移现在在更改表时失败,表更改将列更改为 JSONB 类型

现在失败的迁移的原始内容是

change_column :pages, :content, 'jsonb USING CAST(content AS jsonb)', null: false, default: '[]'

我尝试摆脱这样的演员:

change_column :pages, :content, 'jsonb', null: false, default: '[]'

然后抛出错误

PG::DatatypeMismatch: ERROR:  column "content" cannot be cast automatically to type jsonb
HINT:  You might need to specify "USING content::jsonb".

如果我将它切换到这种风格,它会以与第一种风格相同的方式爆炸

    change_column :pages, :content, 'jsonb USING content::jsonb', null: false, default: '[]'

抛出错误

PG::SyntaxError: ERROR:  syntax error at or near "USING"
LINE 1: SELECT 'jsonb USING CAST(content AS jsonb)'::regtype::oid
               ^
CONTEXT:  invalid type name "jsonb USING CAST(content AS jsonb)"

对这里的正确语法有什么想法吗?

【问题讨论】:

    标签: ruby-on-rails postgresql rails-migrations


    【解决方案1】:

    您可以尝试使用using 进行强制转换(我知道,使用两次听起来很奇怪!)

    change_column :pages, :content, :jsonb, using: "content::JSONB", null: false, default: '[]'
    

    看看是否可行。

    【讨论】:

      猜你喜欢
      • 2015-06-06
      • 1970-01-01
      • 1970-01-01
      • 2019-05-25
      • 2020-07-01
      • 1970-01-01
      • 2019-02-26
      • 1970-01-01
      • 2016-10-25
      相关资源
      最近更新 更多