What's the syntax for dropping a database table column through a Rails migration?
相关问题
- Question marks after images and js/css files in ra
- Using :remote => true with hover event
- NOT DISTINCT query in mySQL
- Eager-loading association count with Arel (Rails 3
- Flush single app django 1.9
相关文章
- Ruby using wrong version of openssl
- Right way to deploy Rails + Puma + Postgres app to
- AWS S3 in rails - how to set the s3_signature_vers
- Difference between Thread#run and Thread#wakeup?
- how to call a active record named scope with a str
- How to add a JSON column in MySQL with Rails 5 Mig
- “No explicit conversion of Symbol into String” for
- form_for wrong number of arguments in rails 4
X = column name
Y = table name
EDIT
Changed
RemoveXColumnToY
toRemoveXColumnFromY
as per comments - provides more clarity for what the migration is actually doing.Rails 4 has been updated, so the change method can be used in the migration to drop a column and the migration will successfully rollback. Please read the following warning for Rails 3 applications:
Rails 3 Warning
Please note that when you use this command:
The generated migration will look something like this:
Make sure to not use the change method when removing columns from a database table (example of what you don't want in the migration file in Rails 3 apps):
The change method in Rails 3 is not smart when it comes to remove_column, so you will not be able to rollback this migration.
Simply, You can remove column
For Example,
Remove Columns For RAILS 5 App
Command above generate a migration file inside
db/migrate
directory. Snippet blow is one of remove column from table example generated by Rails generator,I also made a quick reference guide for Rails which can be found at here.
Give below command it will add in migration file on its own
After running above command you can check migration file remove_column code must be added there on its own
Then migrate the db
For removing column from table in just easy 3 steps as follows:
rails g migration remove_column_from_table_name
after running this command in terminal one file created by this name and time stamp (remove_column from_table_name).
Then go to this file.
inside file you have to write
remove_column :table_name, :column_name
Finally go to the console and then do
rake db:migrate