Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i remove a column from table using rails console

It is easily possible to remove a column using rails migration.

class SomeClass < ActiveRecord::Migration   def self.up     remove_column :table_name, :column_name   end end 

I want to know if there is any way to remove a column from table using console.

like image 857
Aman Garg Avatar asked Apr 18 '13 04:04

Aman Garg


People also ask

How remove column from table in rails?

remove_column :table_name, :column_name, :type Removes column, also adds column back if migration is rollbacked. Note: If you skip the data_type, the migration will remove the column successfully but if you rollback the migration it will throw an error.


1 Answers

You can run the codes in up method directly in rails console:

>> ActiveRecord::Migration.remove_column :table_name, :column_name 

If you already have a migration file such as "db/migrate/20130418125100_remove_foo.rb", you can do this:

>> require "db/migrate/20130418125100_remove_foo.rb" >> RemoveFoo.up 

If you just want to do rake db:migrate, try this:

>> ActiveRecord::Migrator.migrate "db/migrate" 
like image 119
Jun Zhou Avatar answered Sep 17 '22 19:09

Jun Zhou