Ruby on rails 如何更改rails中的列数据类型?
我想使用Rails更改数据库中若干列的数据类型。我尝试了以下代码,但得到一个错误“G::DuplicateColumn:error:关系“town\u health\u records”的列“geography”已经存在 我尝试创建一个新的迁移文件并运行rake db:migrate,如下所示:Ruby on rails 如何更改rails中的列数据类型?,ruby-on-rails,ruby,postgresql,migration,schema,Ruby On Rails,Ruby,Postgresql,Migration,Schema,我想使用Rails更改数据库中若干列的数据类型。我尝试了以下代码,但得到一个错误“G::DuplicateColumn:error:关系“town\u health\u records”的列“geography”已经存在 我尝试创建一个新的迁移文件并运行rake db:migrate,如下所示: class UpdateColumns < ActiveRecord::Migration def change change_table :town_health_records d
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
尝试
t.change
而不是t.string
。您现在正在做的是尝试声明另一个名为geography的列,这就是为什么您会看到这个错误
查看change\u表
方法的
因此,您的迁移文件应该如下所示:
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
class UpdateColumns
添加迁移:
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
或者回滚,然后再次创建表如果要更改多个列,请使用change\u table。另外,请确保将类名更改为更易于维护的名称,如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
class ChangeTownHealthRecordsColumns
如果您对此主题感兴趣,您可以在本文中阅读更多内容:。这为您提供了所需的信息作为旁白,您应该在使用PostgreSQL时忘记
:string
,而只使用:text
(除非您有充分的理由限制数据库中的字段大小)varchar(n)
是一个稍微贵一点的text
版本,它有一个大小限制,您显然不关心这个限制,因为您没有任何:limit
s。rails的方法是添加一个新的迁移来实现更改,而不是进行回滚。
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