加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 用于将列类型从文本更改为json的Rails迁移(Pos

发布时间:2020-12-17 02:49:32 所属栏目:百科 来源:网络整理
导读:我一直在努力将Postgres数据库中的列类型从text更改为json.这是我试过的…… class ChangeNotesTypeInPlaces ActiveRecord::Migration[5.0] def up execute 'ALTER TABLE places ALTER COLUMN notes TYPE json USING (notes::json)' end def down execute 'A
我一直在努力将Postgres数据库中的列类型从text更改为json.这是我试过的……

class ChangeNotesTypeInPlaces < ActiveRecord::Migration[5.0]
  def up
    execute 'ALTER TABLE places ALTER COLUMN notes TYPE json USING (notes::json)'
  end

  def down
    execute 'ALTER TABLE places ALTER COLUMN notes TYPE text USING (notes::text)'
  end
end

也…

class ChangeNotesTypeInPlaces < ActiveRecord::Migration[5.0]
  def up
    change_column :places,:notes,'json USING CAST(notes AS json)'
  end

  def down
    change_column :places,'text USING CAST(notes AS text)'
  end
end

这两个都返回相同的错误……

PG::InvalidTextRepresentation: ERROR:  invalid input syntax for type json

解决方法

使用Rails 5.1.x和PostgreSQL 9.4,在将文本列(包含有效的json)转换为jsonb列时,这对我有用:

class ChangeTextColumnsToJson < ActiveRecord::Migration[5.1]
  def change
    change_column :table_name,:column_name,:jsonb,using: 'column_name::text::jsonb'
  end
end

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读