Modifying existing model with migrations

I'm having trouble modifying an existng database.

My initial command to create was: ruby script/generate model Album title:string picture:string description:string price:float itemType:string

Being that I forgot to put artist name I tried to go into the migrations file, set :force => true and added t.string :artist into the the create_table block, and then     add_index :albums, :artist after it. so the new modified version looks like this:

  def self.up     create_table :albums, :force => true do |t|       t.string :artist       t.string :title       t.float :price       t.string :description       t.string :itemType       t.string :picture

      t.timestamps     end

    add_index :albums, :artist   end

I ran db:migrate and the changes still haven't taken place....am I missing something here?

Thanks

Jon

That's not how it is supposed to work. rake db:migrate will only run migrations higher than the current version as reported by rake db:version. If you want to re-run that migration (assuming you haven't entered data yet), you can issue a rake db:rollback afterwhich you can run rake db:migrate

I'm having trouble modifying an existng database.

My initial command to create was: ruby script/generate model Album title:string picture:string description:string price:float itemType:string

Being that I forgot to put artist name I tried to go into the migrations file, set :force => true and added t.string :artist into the the create_table block, and then    add_index :albums, :artist after it. so the new modified version looks like this:

def self.up    create_table :albums, :force => true do |t|      t.string :artist      t.string :title      t.float :price      t.string :description      t.string :itemType      t.string :picture

     t.timestamps    end

   add_index :albums, :artist end

I ran db:migrate and the changes still haven't taken place....am I missing something here?

Yes. Rails keeps track of which migrations have run and which ones
haven't so running db:migrate again does nothing. You need to migrate
down one version and back up again. Rake db:migrate:redo does exactly
that if my memory is correct

Fred

Fred,

Thanks.

So I ran rake db:migrate VERSION=0. Then I tried to view my table structure under the console command line, to which I got an error(as expected since we roll back to version 0)

I ran the rake db:migrate again and now I have my updated table.

Why do you have to rollback a version though? If you set force to true, shouldn’t rails see this and make the change in the db structure?

Let’s say I have data(which I did) in the table, and I want to keep that data. Rolling back to version 0 erases all records in the table, how would I do this without erasing all the exisiting records? Thanks for the insight.

Jon

Like I said, Rails tracks which migrations have been run and which hasn't. Setting :force => true is irrelevant: the migration has been run and so rails does not look at it. As far as preserving data goes, you didn't have to migrate down to 0 - you only have to go far enough to undo the migration you edited. If that is not acceptable (because there is data in that table or because there are other migrations in the way) then you should write a migration that just performs the changes you want.

Fred