I generated a new rails migration:
rails generate migration some_new_column_to_table
edit: —
ran the migration:
rake db:migrate
updated test db:
rake db:test:prepare
realized I hadn’t added anything to the change method, then
updated the migration file:
class AddSomeColumnToTable < ActiveRecord::Migration
def change
add_column :table, :some_column, :string
add_index :table, :some_column
end
end
ran the migration (again):
rake db:migrate
updated test db (again):
rake db:test:prepare
After running these commands “some_column” had not be added to the database. I found a solution to this problem here: rake db:migrate is not working
rake db:drop:all
rake db:create:all
rake db:migrate
Why did this fix the problem?
How can I prevent it in the future?
It’s not clear what your problem is. But your last commands do straighten things out.
In my experience, migrations don’t ‘stop working’ and they do exactly as layed out, as they are no more than individual commands. But, they require their execution to be done in order, so if you start editing the migrations, you have to be aware of whether a migration has run or not. Migration problems are typically the result of our editing them out-of-sequence. The safest best is to don’t edit migrations that have run already. First do any of the following:
Hopefully that’s helpful.