Search code examples
ruby-on-railsrails-migrations

Is it possible to re-order columns when doing a migration?


I am really picky with the order of columns that I have for my models, to the point I am deleting my db and changing my original migrations to make sure the order of columns is the way I want it.

Is there a better way then what I am doing?

I use the annotate gem also, and I like seeing my columns ordered in a specific way.


Solution

  • Changing the original migrations is not considered as a best practice especially if multiple people are working on the same project, and every one has to reset their database each time you change existing migrations.

    If you still feel strongly about the order of columns, one of the ways is to write a sql queries as part of migration and the support of ordering columns depends on the database.

    For mySQL (https://dev.mysql.com/doc/refman/5.7/en/alter-table.html):

    To add a column at a specific position within a table row, use FIRST or AFTER col_name. The default is to add the column last. You can also use FIRST and AFTER in CHANGE or MODIFY operations to reorder columns within a table.`