Rails 4 migration: how to reorder columns Rails 4 migration: how to reorder columns ruby ruby

Rails 4 migration: how to reorder columns


When using MySQL, you can call change_column, but you have to repeat the column type (just copy and paste it from your other migration):

def up  change_column :your_table, :some_column, :integer, after: :other_columnend

Or if you have to reorder multiple columns in one table:

def up  change_table :your_table do |t|    t.change :some_column, :integer, after: :other_column    # ...  endend

change_column calls ALTER TABLE under the hood. From the MySQL documentation:

You can also use FIRST and AFTER in CHANGE or MODIFY operations toreorder columns within a table.

Note that this approach doesn't work with PostgreSQL. (see Alter column positions)