【发布时间】:2017-05-03 07:27:22
【问题描述】:
使用 Postgres 数据库,将文本字段 staff_ids 添加到 branches 表中:
add_column :branches, :staff_ids, :text
在控制器中将此字段添加到 branch_params 列表中:
:staff_ids => []
数据已保存在此列中,例如["","32","52"]。查询此字段时,我收到一条错误消息,提示 staff_ids 应该是一个数组
Branch.where("? = ANY(staff_ids)", '20')
ActiveRecord::StatementInvalid: PG::WrongObjectType: ERROR: op ANY/ALL (array) requires array on right side
实际上,我在添加staff_ids 字段时忘记在迁移中添加array: true 选项。
现在添加了另一个迁移来更改此列并尝试添加array: true 选项:
change_column :branches, :staff_ids, :text, array: true
但迁移失败并出现错误:
PG::DatatypeMismatch: ERROR: column "staff_ids" cannot be cast automatically to type text[]
现在要么我想更新 where 子句,以便它根据 staff_ids 过滤分支而不添加数组:true 要么修复上面提到的迁移。
有什么建议/想法吗?
【问题讨论】:
-
您可以尝试在数据库级别使用 SQL 修复它,但您可能想要修复迁移?..
-
想要在迁移中修复它以便在暂存和生产环境中执行它
-
您可以在迁移中运行该答案中的代码。