How to drop columns using Rails migration

前端 未结 20 809
执笔经年
执笔经年 2020-12-12 08:50

What\'s the syntax for dropping a database table column through a Rails migration?

20条回答
  •  孤街浪徒
    2020-12-12 09:27

    Remove Columns For RAILS 5 App

    rails g migration RemoveFrom [columnName:type]
    

    Command above generate a migration file inside db/migrate directory. Snippet blow is one of remove column from table example generated by Rails generator,

    class RemoveAgeFromUsers < ActiveRecord::Migration
      def up
        remove_column :users, :age
      end
      def down
        add_column :users, :age, :integer
      end
    end
    

    I also made a quick reference guide for Rails which can be found at here.

提交回复
热议问题