Is there any way to rearrange the table's fields in Rails migrations?

81 views Asked by At

After the table creation, I want to rearrange its fields in a different order, and take care about its stored data.

Is there any way to do this?

2

There are 2 answers

2
errata On

You can use after in a change column statement:

def up
  change_column :your_table, :some_column, :integer, after: :other_column
end
0
pramod On

You have to create a separate migration to change column.

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

Please refer api