【问题标题】:How do you change column data type in rails?你如何更改rails中的列数据类型?
【发布时间】:2013-12-30 03:56:22
【问题描述】:

我想使用 Rails 更改数据库中多个列的数据类型。我尝试了以下代码,但出现错误“G::DuplicateColumn:错误:关系“town_health_records”的列“地理”已存在”

我已尝试创建一个新的迁移文件并运行 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

我只需要将以下列的数据类型更改为字符串:

: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

【问题讨论】:

  • stackoverflow.com/questions/2799774/… 这将为您提供所需的信息
  • 顺便说一句,在使用 PostgreSQL 时你应该忘记:string,而只使用:text(除非你有充分的理由限制数据库内的字段大小)。 varchar(n)text 的一个稍微贵一点的版本,它有一个你显然不关心的大小限制,因为你没有任何 :limits。

标签: ruby-on-rails ruby postgresql migration schema


【解决方案1】:

尝试t.change 而不是t.string。您现在正在尝试声明另一个名为 geography 的列,这就是您看到该错误的原因。

查看API Documentation 中的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

【讨论】:

    【解决方案2】:

    添加迁移:

    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
    

    或者回滚然后重新创建表

    【讨论】:

    • rails 方法是添加一个新的迁移来实现更改而不是回滚。
    【解决方案3】:

    如果您要更改多个列,请使用 change_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
    

    如果您对此主题感兴趣,可以阅读这篇文章:https://kolosek.com/rails-change-database-column

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-24
      • 2020-07-27
      • 1970-01-01
      • 2016-11-25
      • 2012-02-20
      • 1970-01-01
      • 2011-11-02
      相关资源
      最近更新 更多