ruby-on-rails

Rails 5: How to remove a column from a database?


What is the command for removing an existing column from a table using migration?

The column I want to remove is: country:string

From the table: sample_apps


Solution

  • To remove a column with migration:

    rails g migration Remove..From.. col1:type col2:type col3:type
    

    In your case:

    rails g migration RemoveCountryFromSampleApps country:string
    

    This will generate the following migration in Rails 5.0:

    class RemoveCountryFromSampleApps < ActiveRecord::Migration[5.0]
      def change
        remove_column :sample_apps, :country, :string
      end
    end