Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: I update migration file then run db:migrate, but my schema isn't updating

Tags:

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?

like image 921
Evolve Avatar asked Sep 13 '09 01:09

Evolve


People also ask

How can I check my rails migration status?

To check for status, run rails db:migrate:status .

Can you edit a migration file rails?

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.

How do I reset migration in rails?

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.

How does rails know migrations are pending?

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 .


2 Answers

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 run rake db:migrate to run the corrected version.

like image 124
utapyngo Avatar answered Sep 30 '22 14:09

utapyngo


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

like image 45
jluebbert Avatar answered Sep 30 '22 16:09

jluebbert