【发布时间】:2015-07-07 17:46:03
【问题描述】:
我一直在关注一些关于使用 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
【问题讨论】:
标签: ruby-on-rails postgresql ruby-on-rails-4