Rails:将现有的整数列更改为数组数据类型 [英] Rails: Change existing integer column to array data type

查看:79
本文介绍了Rails:将现有的整数列更改为数组数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将整数列(已具有值)更改为整数数组列?当我尝试迁移时:

How do I change an integer column (which already has values) to an integer array column? When I try to migrate this:

def change
  change_column :cards, :value, :integer, array: true, default: []
end

我收到此错误:

PG::DatatypeMismatch: ERROR:  column "value" cannot be cast automatically to type integer[]
HINT:  You might need to specify "USING value::integer[]"

所以我尝试了这个:

change_column :cards, :value, :integer, array: true, default: [], using: 'value::integer[]'

然后我收到此错误:

PG::CannotCoerce: ERROR:  cannot cast type integer to integer[]

推荐答案

深入研究Postgres转换功能&尝试了几种无效的方法后,我找到了答案.

After digging around for postgres casting functions & trying several things that didn't work, I found the answer.

使用:'ARRAY [value] :: INTEGER []'

提示很接近,但恰好足以令人困惑.

The hint was close, but off by just enough to be confusing.

这篇关于Rails:将现有的整数列更改为数组数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆