38

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

  • 3
    Hi and welcome to Stack overflow! I see your question already has answers, but I figured I should let you know, that one of the best places to get a good basic understanding of rails are the official Rails Guides: http://edgeguides.rubyonrails.org - they are surprisingly readable and will help you level up your basic Rails understanding super quick. The one specifically on migrations is this one: http://edgeguides.rubyonrails.org/active_record_migrations.html but I also recommend just reading them all from the beginning. :) – Taryn East Aug 08 '16 at 01:53

5 Answers5

83

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
davideghz
  • 3,596
  • 5
  • 26
  • 50
  • 4
    do I need to run `rails db:migrate` after this? –  Aug 08 '16 at 01:48
  • 1
    Just 1 more clarification. Since the model name SampleApp is singular and the table name sample_apps is plural, which one do I use? The `RemoveCountryFromSampleApp` or the `RemoveCountryFromSampleApps` –  Aug 08 '16 at 01:52
  • 1
    plural, just edited my answer ^^' for a full overview over migrations, you can check the brand new SO [Documentation section](http://stackoverflow.com/documentation/ruby-on-rails/1087/activerecord-migrations) – davideghz Aug 08 '16 at 01:52
  • Is this reversible? What happens if I run `rails db:rollback`? – dcangulo Sep 17 '19 at 06:03
4

Create migration file:

$ rails generate migration RemoveCountryFromSampleApps country:string

In generated migration file:

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

Then run:

 rake db:migrate
Hasmukh Rathod
  • 1,108
  • 1
  • 14
  • 20
  • 2
    In Rails 5 no more `rake` vs `rails` calls. Just use `rails`, always. –  Aug 08 '16 at 06:14
3

To remove a column(country here) from table(sample_case)

rails generate migration RemoveCountryfromSampleCase country:string

Above command should generate a file YYYYMMDDHHMMSS_remove_countryfrom_sample_case.rb. under db/migrate folder

class RemoveCountryFromSampleCase < ActiveRecord::Migration[5.0]
  def change
    remove_column :sample_case, :country, :string
  end
end

In my case (I was doing it for more than two columns) only this appears

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

remove_column line was not there so I added it manually and then fired the command

rails db:migrate

and it worked for me.

References https://stackoverflow.com/a/2963582 & Ruby guide on Active Record migration https://edgeguides.rubyonrails.org/active_record_migrations.html

Ayush Chaurasia
  • 345
  • 4
  • 9
1

If want to remove the index too, you do with migration too:

rails g migration remove_post_id_from_comments post_id:integer:index

migration file:

class RemovePostIdFromComments < ActiveRecord::Migration
  def change
    remove_index :comments, :post_id
    remove_column :comments, :post_id, :integer
  end
end

then run: rake db:migrate

rld
  • 2,603
  • 2
  • 25
  • 39
  • According the rails API documentation for `remove_column`: "Indexes on the column are automatically removed." https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#method-i-remove_column – James H Sep 08 '20 at 20:14
1

Do you need to specify the type?

Why not just remove_column :sample_apps, :country or remove_column :comments, :post_id for the samples here? Seems to work and removes a chance for error (text or string).

Greg
  • 2,359
  • 5
  • 22
  • 35