加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 如何将字符串列更改为bigint?

发布时间:2020-12-16 21:32:59 所属栏目:百科 来源:网络整理
导读:在rails迁移中.如何将字符串类型列更改为bigint? 我有: t.change :ip_number_from,:integer,:limit = 8 我明白了: PG::Error: ERROR: column "ip_number_from" cannot be cast to type bigint 我甚至尝试过两种选择: change_column :ip_to_countries,:ip
在rails迁移中.如何将字符串类型列更改为bigint?

我有:

t.change :ip_number_from,:integer,:limit => 8

我明白了:

PG::Error: ERROR:  column "ip_number_from" cannot be cast to type bigint

我甚至尝试过两种选择:

change_column :ip_to_countries,:ip_number_from,:limit => 8
change_column :ip_to_countries,:bigint

还是一样的错误.

解决方法

Postgres告诉您该列中存在不知道如何转换的现有数据,因此它需要一个ALTER语句,该列为列提供USING子句以指定如何转换现有值.

不幸的是,您需要下拉特定于数据库的代码才能完成此任务,或者使用与此处建议的解决方案类似的内容:

http://webjazz.blogspot.co.uk/2010/03/how-to-alter-columns-in-postgresql.html

编辑:以下是您在迁移中直接在SQL中执行此操作的方法:

execute <<-SQL
  ALTER TABLE ip_to_countries
  ALTER COLUMN ip_number_from TYPE bigint USING ip_number_from::bigint
SQL

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读