# Modifying existing model with migrations

**URL:** <https://www.ruby-forum.com/t/modifying-existing-model-with-migrations/149474>\
**Category:** Rails\
**Created:** [September 25, 2008, 6:38am UTC](https://www.ruby-forum.com/t/modifying-existing-model-with-migrations/149474 "2008-09-25T06:38:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jon](https://www.ruby-forum.com/u/jon)\
**Post date:** [September 25, 2008, 6:38am UTC](https://www.ruby-forum.com/t/modifying-existing-model-with-migrations/149474/1 "2008-09-25T06:38:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jon](https://www.ruby-forum.com/u/jon)\
**Post date:** [September 25, 2008, 7:27am UTC](https://www.ruby-forum.com/t/modifying-existing-model-with-migrations/149474/2 "2008-09-25T07:27:13Z")

</div>

On Sep 25, 12:38 pm, Jon [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> looks like this:  
> t.timestamps  
> 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

---

<div class="post-metadata">

**Author:** ![jon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jon](https://www.ruby-forum.com/u/jon)\
**Post date:** [September 25, 2008, 7:49am UTC](https://www.ruby-forum.com/t/modifying-existing-model-with-migrations/149474/3 "2008-09-25T07:49:26Z")

</div>

On 25 Sep 2008, at 05:38, Jon [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> add\_index :albums, :artist after it. so the new modified version
> 
> ```
> 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

---

<div class="post-metadata">

**Author:** ![jon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jon](https://www.ruby-forum.com/u/jon)\
**Post date:** [September 25, 2008, 4:01pm UTC](https://www.ruby-forum.com/t/modifying-existing-model-with-migrations/149474/4 "2008-09-25T16:01:50Z")

</div>

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

On Wed, Sep 24, 2008 at 10:48 PM, Frederick C. \<

---

<div class="post-metadata">

**Author:** ![jon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jon](https://www.ruby-forum.com/u/jon)\
**Post date:** [September 25, 2008, 4:23pm UTC](https://www.ruby-forum.com/t/modifying-existing-model-with-migrations/149474/5 "2008-09-25T16:23:39Z")

</div>

On Sep 25, 3:01 pm, “Jon Liu” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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.

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
