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

mat*_*ttq 2 postgresql ruby-on-rails

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

class AddZipCodeToUserProfiles < ActiveRecord::Migration
  def change
    add_column :user_profiles, :zip_code, :integer
  end
end
Run Code Online (Sandbox Code Playgroud)

jos*_*ing 5

class ChangeZipCodeToString < ActiveRecord::Migration
  def change
    change_column :user_profiles, :zip_code, :string
  end
end
Run Code Online (Sandbox Code Playgroud)

你试过吗?