首页 文章

导轨4,postgres和阵列

提问于
浏览
2

我一直在关注使用rails 4和postgres存储数组的一些指南,但我不断遇到这个错误:

PG::DatatypeMismatch: ERROR:  column "run_times" is of type character varying[] but expression is of type character varying at character 35
HINT:  You will need to rewrite or cast the expression.

这是rails正在执行的SQL:

UPDATE "models" SET "run_times" = $1, "updated_at" = $2 WHERE "models"."id" = $3  [["run_times", "{0,2}"], ["updated_at", "2015-07-07 17:44:47.480675"], ["id", 1]]

这是由控制器使用(作为示例)生成的:

@model.run_times = ["0", "2"]

有什么我想念的吗?

有趣的是,这似乎适用于rails控制台:

@model = Model.find(1)
@model.run_times = ["123", "456"]
@model.save

这是我使用的迁移:

class AddRunTimesToModel < ActiveRecord::Migration
  def change
    add_column :models, :run_times, :text, array: true, default: '{}'
  end
end

3 回答

  • 0

    我相信通常的建议是在PostgreSQL中将基于字符串的数组定义为文本,而不是字符串 . 我会“关闭”迁移,并将类型更改为文本数组 .

  • 0

    好吧,这就是我需要做的事情:

    • 而不是 permit(:run_times) 它需要 permit(run_times: [])
      rails迁移中的

    • 类型是字符串,而不是文本

    • 迁移后重启服务器

  • 0

    既然你传入了字符串,那么只需要仔细检查你的run_times列是否在你的模型表的架构中设置了这样?

    t.string :run_times, array: true, default: '{}'
    

相关问题