In a Rails Migration (MySQL), can you specify what position a new column should be?

This is now possible in Rails 2.3.6+ by passing the :after parameter

https://rails.lighthouseapp.com/projects/8994/tickets/3286-patch-add-support-for-mysql-column-positioning-to-migrations

To everyone that doesn't see the advantage in having this feature: do you never look at your database outside of the ORM? If I'm viewing in any sort of UI, I like having things like foreign keys, status columns, flags, etc, all grouped together. This doesn't impact the application, but definitely speeds up my ability to review data.


Sure you can.

  • Short answer:

    add_column :users, :gender, :string, :after => :column_name
    
  • Long answer:

Here is an example, let's say you want to add a column called "gender" after column "username" to "users" table.

  1. Type rails g migration AddGenderToUser gender:string
  2. Add "after => :username" in migration that was created so it looks like this:

    class AddSlugToDictionary < ActiveRecord::Migration
      def change
        add_column :users, :gender, :string, :after => :username
      end
    end
    

I created a patch that adds this additional functionality to the ActiveRecord Mysql adapter. It works for master and 2-3-stable.

https://rails.lighthouseapp.com/projects/8994/tickets/3286-patch-add-support-for-mysql-column-positioning-to-migrations

It might be mysql specific, but it doesn't make your migrations any less portable (other adapters would just ignore the extra positioning options).