小编Geo*_*iev的帖子

PostgreSQL 无法将类型 json 转换为字符变化[]

在我的 Rails 5.0.5 应用程序中,我需要将json列转换为string, array: true.

我的列中的值json如下:

[ "200px-RR5219-0015R.png", "2017_03_25_2235.doc", "137555.jpg" ]
Run Code Online (Sandbox Code Playgroud)

我尝试过这个迁移:

[ "200px-RR5219-0015R.png", "2017_03_25_2235.doc", "137555.jpg" ]
Run Code Online (Sandbox Code Playgroud)

并得到这个错误:

ActiveRecord::StatementInvalid: PG::DatatypeMismatch: ERROR:  column "attachments" cannot be cast automatically to type character varying[]
HINT:  You might need to specify "USING attachments::character varying[]".
: ALTER TABLE "tasks" ALTER COLUMN "attachments" TYPE character varying[]
Run Code Online (Sandbox Code Playgroud)

然后我编辑了迁移:

class ChangeTaskAttachmentsTypeToString < ActiveRecord::Migration[5.0]
  def change
    change_column :tasks, :attachments, :string, array: true
  end
end
Run Code Online (Sandbox Code Playgroud)

最后得到这个错误:

PG::CannotCoerce: ERROR:  cannot cast type json to …
Run Code Online (Sandbox Code Playgroud)

postgresql ruby-on-rails rails-migrations postgresql-9.3 ruby-on-rails-5

3
推荐指数
1
解决办法
6859
查看次数