Joh*_*ohn 5 ruby migration postgresql schema ruby-on-rails
我想使用 Rails 更改数据库中多个列的数据类型。我已尝试以下代码,但收到错误“G::DuplicateColumn: ERROR: 关系“town_health_records”的列“geography”已存在”
我尝试创建一个新的迁移文件并运行 rake db:migrate ,如下所示:
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
Run Code Online (Sandbox Code Playgroud)
我只需要将以下各列的数据类型更改为字符串:
: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
Run Code Online (Sandbox Code Playgroud)
尝试t.change代替t.string. 您现在正在做的是尝试声明另一个名为 geography 的列,这就是您看到该错误的原因。
查看该方法的API 文档change_table。
因此,您的迁移文件应如下所示:
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
Run Code Online (Sandbox Code Playgroud)