Rails,Postgres - 将列类型从整数更改为字符串

时间:2015-08-10 22:39:23

标签: ruby-on-rails postgresql

我有一个Rails 3.2应用程序,用于在user_profiles表中存储邮政编码。我最初使用整数作为邮政编码的数据类型;然而,这显然是错误的,因为你不能使用以零开头的数字(并且一些美国邮政编码确实从零开始)。因此,我需要将邮政编码列和我的生产数据转换为字符串。我认为这是通过简单的change_column迁移完成的,但我不知道使用Postgres时要使用的确切代码。任何帮助将非常感激!我添加邮政编码列的原始迁移如下:

class AddZipCodeToUserProfiles < ActiveRecord::Migration
  def change
    add_column :user_profiles, :zip_code, :integer
  end
end

1 个答案:

答案 0 :(得分:5)

class ChangeZipCodeToString < ActiveRecord::Migration
  def change
    change_column :user_profiles, :zip_code, :string
  end
end

你试过吗?