I'm trying to add an extra field to one of my tables.
I've added the field in the migration file (under db\migrate), then ran 'rake db:migrate' which ran without troubles. My text editor even told me my schema.db file has been updated and needs to refresh.
The schema file does not contain my new field and any attempts to reference the field from my views fail miserably.
How do I do this? It is possible to update a table with an extra field via rails without having to totally drop and recreate the database again?
To check for status, run rails db:migrate:status .
If you have already run the migration then you cannot just edit the migration and run the migration again: Rails thinks it has already run the migration and so will do nothing when you run rake db:migrate.
just use rake db:reset , that will drop your database (same as undoing all migrations) and reset to the last schema. UPDATE: a more correct approach will be using rake db:migrate:reset . That will drop the database, create it again and run all the migrations, instead of resetting to the latest schema.
It uses values from the current time for YYYYMMDDHHMMSS and the name of the migration. Once migrations are run, the value YYYYMMDDHHMMSS in migration file, is inserted in a table named schema_migrations .
http://guides.rubyonrails.org/migrations.html#changing-existing-migrations
Occasionally you will make a mistake when writing a migration. If you have already run the migration then you cannot just edit the migration and run the migration again: Rails thinks it has already run the migration and so will do nothing when you run rake db:migrate. You must rollback the migration (for example with
rake db:rollback
), edit your migration and then runrake db:migrate
to run the corrected version.
You should always create a new migration file when adding/changing something in the database. This is the purpose of migrations. A migration file should have the ability to make the new change and undo the change. This way if something goes wrong or you changed your mind you can easily roll back to a previous migration.
The following link's sections labeled 'Anatomy of a Migration' and 'Writing a Migration' might be of help to you.
http://guides.rubyonrails.org/migrations.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With