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

前端 未结 5 1326
旧时难觅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:53

    Sure you can.

    • Short answer:

      add_column :users, :gender, :string, :after => :column_name
      
    • Long answer:

    Here is an example, let's say you want to add a column called "gender" after column "username" to "users" table.

    1. Type rails g migration AddGenderToUser gender:string
    2. Add "after => :username" in migration that was created so it looks like this:

      class AddSlugToDictionary < ActiveRecord::Migration
        def change
          add_column :users, :gender, :string, :after => :username
        end
      end
      

提交回复
热议问题