Adding a column to an existing table in a Rails migration

后端 未结 11 1800
借酒劲吻你
借酒劲吻你 2020-11-28 00:34

I have a Users model which needs an :email column (I forgot to add that column during the initial scaffold).

I opened the migration file and added

11条回答
  •  离开以前
    2020-11-28 00:38

    Sometimes rails generate migration add_email_to_users email:string produces a migration like this

    class AddEmailToUsers < ActiveRecord::Migration[5.0]
      def change
      end
    end
    

    In that case you have to manually an add_column to change:

    class AddEmailToUsers < ActiveRecord::Migration[5.0]
      def change
        add_column :users, :email, :string
      end
    end
    

    And then run rake db:migrate

提交回复
热议问题