How to drop columns using Rails migration

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

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

20条回答
  •  庸人自扰
    2020-12-12 09:20

    There are two good ways to do this:

    remove_column

    You can simply use remove_column, like so:

    remove_column :users, :first_name
    

    This is fine if you only need to make a single change to your schema.

    change_table block

    You can also do this using a change_table block, like so:

    change_table :users do |t|
      t.remove :first_name
    end
    

    I prefer this as I find it more legible, and you can make several changes at once.

    Here's the full list of supported change_table methods:

    http://apidock.com/rails/ActiveRecord/ConnectionAdapters/SchemaStatements/change_table

提交回复
热议问题