有没有办法重新安排Rails迁移中的表格字段?

时间:2015-06-08 01:16:30

标签: ruby-on-rails migration

创建表格后,我想以不同的顺序重新排列其字段,并注意其存储的数据。

有没有办法做到这一点?

2 个答案:

答案 0 :(得分:2)

您可以在更改列声明中使用after:

def up
  change_column :your_table, :some_column, :integer, after: :other_column
end

答案 1 :(得分:0)

您必须创建单独的迁移以更改列。

rails g migration change_data_type_for_fieldname

class ChangeDataTypeForFieldname < ActiveRecord::Migration
  def up
    #change column type form int to  bigint
    change_column :my_table, :my_column, :bigint
  end
  def down
    #change column type form bigint to  int
    change_column :my_table, :my_column, :int
  end
end

请参阅api