Zakoff
Zakoff

Reputation: 12995

Mixed up new column name and type in migration, now table in schema is corrupted

I have added multiple columns to a model using a migration

class AddManyfieldsToBooks < ActiveRecord::Migration
  def self.up
    add_column :books, :item_one, :float
    add_column :books, :item_two, :float

  end

  def self.down
    remove_column :books, :item_one
    remove_column :books, :item_two    
  end
end

I have also updated attr_accessible in the Book model

attr_accessible :item_one, :item_two

I then ran the bundle exec rake db:migrate command

However the new fields have not been added to the schema.rb

It has been a while since I last used rails, and was wondering where I am going wrong. I have previously run migrations for this project one field at a time and they have been fine. Is there something I am missing?

Thanks

EDIT 1: I have realised when I did my migration I wrote

rails migration add_manyfields_to_books float:item_one

instead of

rails migration add_manyfields_to_books item_one:float

This has led to the following error in the schema.rb

# Could not dump table "books" because of following StandardError
#   Unknown type 'item_one' for column 'float'

I had created a new branch 'adding-items' to make the database change. However even when I return to the master branch, the same error exists in the schema. I have tried rolling back the migration

bundle exec rake db:rollback STEP=1

but this does not fix the problem. I have also deleted the adding-items branch where I was making these changes in the hope they wouldn't be in the master file

What is the best way to resolve this? Should I drop the Books table and add it again from scratch?

Upvotes: 1

Views: 311

Answers (1)

Zakoff
Zakoff

Reputation: 12995

I resolved this by dropping the whole Books table and re-adding it with a new migration that included the extra fields I wanted.

Upvotes: 1

Related Questions