5

I'd like to change the data type for several columns in a database using Rails. I have tried the following code but I get an error "G::DuplicateColumn: ERROR: column "geography" of relation "town_health_records" already exists"

I have tried creating a new migration file and running rake db:migrate as shown below:

class UpdateColumns < ActiveRecord::Migration
  def change
    change_table :town_health_records do |t|
     t.string :geography
     t.string :total_pop_year_2005
     t.string :age_0_19_year_2005
     t.string :age_65_up_year_2005
     t.string :per_capita_income_year_2000
     t.string :persons_below_200pct_poverty_yr_2000
     t.float :pct_all_persons_below_200pct_poverty_year_2000
     t.float :pct_adequacy_prenatal_care_kotelchuck
     t.float :pct_c_sections_2005_2008
     t.integer :num_infant_deaths_2005_2008
     t.float :infant_mortality_rate_2005_2008
     t.float :pct_low_birthweight_2005_2008
     t.float :pct_multiple_births_2005_2008
     t.float :pct_publicly_financed_prenatal_care_2005_2008
     t.float :pct_teen_births_2005_2008


      t.timestamps
    end
  end
end

I only need to change the data type to string for the following columns :

:total_pop_year_2005
:age_0_19_year_2005
:age_65_up_year_2005
:per_capita_income_year_2000
:persons_below_200pct_poverty_yr_2000
John
  • 443
  • 8
  • 19
  • http://stackoverflow.com/questions/2799774/rails-migration-for-change-column this gives you the info you need – RustyToms Dec 11 '13 at 23:03
  • As an aside, you should forget about `:string` when using PostgreSQL and just use `:text` (unless you have a good reason to restrict the field size inside the database). `varchar(n)` is pretty much a slightly more expensive version of `text` that has a size limitation that you apparently don't care about since you don't have any `:limit`s. – mu is too short Dec 11 '13 at 23:36

3 Answers3

5

Try t.change instead of t.string. What you're doing right now is trying to declare another column named geography, which is why you're seeing that error.

Take a look at the API Documentation for the change_table method.

So, your migration file should look like:

class UpdateColumns < ActiveRecord::Migration
  def change
    change_table :town_health_records do |t|
      t.change :total_pop_year_2005, :string
      t.change :age_0_19_year_2005, :string
      ...
    end
  end
end
Zajn
  • 4,078
  • 24
  • 39
4

add a migration:

def change
  change_column :town_health_records, :total_pop_year_2005, :string
  change_column :town_health_records, :age_0_19_year_2005, :string
  change_column :town_health_records, :age_65_up_year_2005, :string
  change_column :town_health_records, :per_capita_income_year_2000, :string
  change_column :town_health_records, :persons_below_200pct_poverty_yr_2000, :string
end

or roll back and then create the table again

McGar
  • 205
  • 1
  • 10
0

If you are changing several columns use change_table. Plus, be sure to change your class name to something more maintainable like ChangeTownHealthRecordsColumns:

class ChangeTownHealthRecordsColumns < ActiveRecord::Migration
  def change
    change_table :town_health_records do |t|
      change_column :town_health_records, :total_pop_year_2005, :string
      change_column :town_health_records, :age_0_19_year_2005, :string
      change_column :town_health_records, :age_65_up_year_2005, :string
      change_column :town_health_records, :per_capita_income_year_2000, :string
      change_column :town_health_records, :persons_below_200pct_poverty_yr_2000, :string
    end
  end
end

If you are interested in this topic you can read more in this article: https://kolosek.com/rails-change-database-column.

Nesha Zoric
  • 6,218
  • 42
  • 34