问题
I created a migration that simply adds a new column, but I want it to be added before the created_at and updated_at columns, is there a way to specify in which position the new column is created ?
I searched and only found someone saying to use the :after option in add_column, but it doesn't do anything. then I looked in the rails api docs and found no such option.
回答1:
After option works well for me
add_column :table_name, :column_name, :type, :after => :column_name
Also look at this question.
回答2:
From my research, the :after
option does not work for SQLite or Postgres, as these DBMSs do not allow such operation. However, the option works for MySQL.
References:
SQLite: https://www.sqlite.org/lang_altertable.html - this basically tells you to create a new table with correct column ordering, then copy data over.
Postgres: https://wiki.postgresql.org/wiki/Alter_column_position
来源:https://stackoverflow.com/questions/9046413/how-to-change-the-position-of-a-new-column-in-rails-3-x-migration