mattq
mattq

Reputation: 89

Rails, Postgres - Change column type from integer to string

I have a Rails 3.2 app that stores zip codes in a user_profiles table. I originally used integer as the data type for zip codes; however, this is obviously wrong since you can't use numbers that start with zero (and some US zip codes do start with zero). Therefore, I need to convert the zip code column AND my production data to string instead. I think this is done with a simple change_column migration, but I don't know the exact code to use when using Postgres. Any help would be much appreciated! My original migration to add the zip code column is below:

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

Upvotes: 2

Views: 1837

Answers (1)

joshua.paling
joshua.paling

Reputation: 13952

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

Have you tried that?

Upvotes: 6

Related Questions