Removing Fields From Table via migration
TAGS:
ruby rails migration database add
Related:
- Adding fields to table via migration.
- Creating migration file using generate migration
- Creation migration file using generate model.
- Creating migration file using scaffold.
- Drop tables using migration.
- Editing migration file manually.</li>
- Seeding data into tables via migration.
- Joining two tables via migration.
- Migration specific VERSION or STEP.
- Setting rails environment via migration command.
- Setup, reset and drop database
</ul>
Removing Fields From Table Via Rails Migration.
First, We suppose that you have a `books` table as: Now, You can remove the column `price` using:rails g scaffold book name brief:text price:float rake db:migrate
This mean, Remove price field from the books table, and will create a migration file as:rails g migration RemovePriceFromBooks price:float
Then runclass RemovePriceFromBooks<ActiveRecord::Migration def change remove_column :books, :price, :float end end
This will remove the `price` column form `books` table. Take care, The data stored in this field will be destoyed and can't be restored, So, What will happened if you run:rake db:migrate
Sure, It will restore `price` field but not restore its `data`.rake db:rollback