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
?
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
?
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.
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`?