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

前端 未结 5 1325
旧时难觅i
旧时难觅i 2020-12-08 18:36

If I\'m adding a column via MySQL, I can specify where in the table that column will be using the AFTER modifier. But if I do the add_column via a Rails migration, the colum

5条回答
  •  长情又很酷
    2020-12-08 18:47

    There does not seem to be a position option for the add_column method in migrations. But migrations support executing literal SQL. I'm no Rails developer, but something like the following:

    class AddColumnAfterOtherColumn < ActiveRecord::Migration
      def self.up
        execute "ALTER TABLE table_name ADD COLUMN column_name INTEGER 
          AFTER other_column"
      end
    
      def self.down
        remove_column :table_name, :column_name
      end
    end
    

提交回复
热议问题