9

I'm looking to put a column at the front of my table I know you can do

add_column :customer, :first_name, after: :last_name

but is there a way to do :before?

2 Answers2

5

You're able to insert a column at the front of your table by using the :first option:

add_column :table_name, :column_name, :column_type, first: true

You can still use :after to handle all other positioning cases.

Zoran
  • 4,196
  • 2
  • 22
  • 33
2

These are mysql-specific options by the way.

https://github.com/rails/rails/blob/80e66cc/activerecord/lib/active_record/connection_adapters/mysql/schema_creation.rb#L50-L55 lists the available options, looks like they only support before and first.

For what it's worth, PostgreSQL only supports adding columns to the end of the table.

Related question: alter table add ... before `code`?

Community
  • 1
  • 1
Joe Van Dyk
  • 6,828
  • 8
  • 57
  • 73