# Why this rake db:migrate error

**URL:** <https://www.ruby-forum.com/t/why-this-rake-db-migrate-error/119805>\
**Category:** Ruby\
**Created:** [November 10, 2007, 6:31pm UTC](https://www.ruby-forum.com/t/why-this-rake-db-migrate-error/119805 "2007-11-10T18:31:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![darius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58956e/32.png) [@darius](https://www.ruby-forum.com/u/darius)\
**Post date:** [November 10, 2007, 6:31pm UTC](https://www.ruby-forum.com/t/why-this-rake-db-migrate-error/119805/1 "2007-11-10T18:31:21Z")

</div>

hello

I’m getting a rake db:migrate error which I can’t understand (newbie).

– add\_column(:stories, :votes\_count, :integer, {:default=\>0})  
rake aborted!  
Mysql::Error: #42S21Duplicate column name ‘votes\_count’: ALTER TABLE  
stories ADD  
`votes_count` int(11) DEFAULT 0

(See full trace by running task with --trace)

the error is in

005\_add\_counter\_cache\_to\_stories.rb

class AddCounterCacheToStories \< ActiveRecord::Migration  
add\_column :stories, :votes\_count, :integer, :default =\> 0  
def self.up  
Story.find(:all).each do |s|  
s.update\_attribute :votes\_count, s.votes.length  
end  
end

def self.down  
remove\_column :stories, :votes\_count  
end  
end

the latest migration file is

006\_add\_story\_creation\_date\_and\_description.rb

class AddStoryCreationDateAndDescription \< ActiveRecord::Migration  
def self.up  
add\_column :stories, :created\_at, :datetime  
add\_column :stories, :description, :text  
end

def self.down  
remove\_column :stories, :created\_at  
remove\_column :stories, :description  
end  
end

here’s schema.db, which I have not touched

ActiveRecord::Schema.define(:version =\> 5) do

create\_table “stories”, :force =\> true do |t|  
t.column “name”, :string  
t.column “link”, :string  
t.column “permalink”, :string  
t.column “user\_id”, :integer  
t.column “votes\_count”, :integer, :default =\> 0  
end

create\_table “users”, :force =\> true do |t|  
t.column “login”, :string  
t.column “password”, :string  
t.column “name”, :string  
t.column “email”, :string  
end

create\_table “votes”, :force =\> true do |t|  
t.column “story\_id”, :integer  
t.column “created\_at”, :datetime  
t.column “user\_id”, :integer  
end

end

(if this looks familiar, it’s b/c I’m working through the examples in a  
RoR book)

In an earlier migration file (003) I also have an add\_column but rake  
doesn’t give me an error. That add\_column however does not have a  
default value option.

any help is appreciated. thanks in advance.

---

<div class="post-metadata">

**Author:** ![darius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58956e/32.png) [@darius](https://www.ruby-forum.com/u/darius)\
**Post date:** [November 10, 2007, 7:10pm UTC](https://www.ruby-forum.com/t/why-this-rake-db-migrate-error/119805/2 "2007-11-10T19:10:48Z")

</div>

Rick DeNatale [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in  
news:removed\_email\_address@domain.invalid…

> Why is the add\_column outside of the up method?
> 
> This means that it will be executed at the time the  
> AddCounterCacheToStores class is created, which I think will happen  
> whatever the current schema version is, at least that would explain  
> what you are seeing.

Right you are. Thank you.

(this was my error, not the book’s)

---

<div class="post-metadata">

**Author:** ![darius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58956e/32.png) [@darius](https://www.ruby-forum.com/u/darius)\
**Post date:** [November 10, 2007, 6:47pm UTC](https://www.ruby-forum.com/t/why-this-rake-db-migrate-error/119805/3 "2007-11-10T18:47:18Z")

</div>

On Nov 10, 2007 12:30 PM, darius [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> (See full trace by running task with --trace)
> 
> the error is in
> 
> 005\_add\_counter\_cache\_to\_stories.rb
> 
> class AddCounterCacheToStories \< ActiveRecord::Migration  
> add\_column :stories, :votes\_count, :integer, :default =\> 0  
> def self.up  
> Story.find(:all).each do |s|  
> …

Why is the add\_column outside of the up method?

This means that it will be executed at the time the  
AddCounterCacheToStores class is created, which I think will happen  
whatever the current schema version is, at least that would explain  
what you are seeing.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![darius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58956e/32.png) [@darius](https://www.ruby-forum.com/u/darius)\
**Post date:** [November 11, 2007, 5:35am UTC](https://www.ruby-forum.com/t/why-this-rake-db-migrate-error/119805/4 "2007-11-11T05:35:35Z")

</div>

darius wrote:

> I’m getting a rake db:migrate error which I can’t understand (newbie).

The best forum for Rails questions is Ruby-on-Rails-talk, at Google  
Groups.

> – add\_column(:stories, :votes\_count, :integer, {:default=\>0})  
> rake aborted!  
> Mysql::Error: #42S21Duplicate column name ‘votes\_count’: ALTER TABLE  
> stories ADD  
> `votes_count` int(11) DEFAULT 0

echo drop database my\_database; | mysql -u root

Then use echo create… to create it again. Then run all the migrations  
again. Your latest migration is 6, but your Schema showed it’s stuck at  
5. This may happen because you somehow added that column without  
simultaneously bumping the database version.

If a deployed database caught this problem, you must change the version  
number itself, carefully, to keep going. But because it’s a learning  
project, and because it’s only the test and development versions of the  
database, you can just rebuild it.

A style tips:

> add\_column :stories, :votes\_count, :integer, :default =\> 0  
> def self.up  
> Story.find(:all).each do |s|  
> s.update\_attribute :votes\_count, s.votes.length  
> end  
> end

In a database, never store a value that you could also get thru a query.  
That’s a redundancy, and the best software always has the fewest  
redundancies of any type. I would erase :votes\_count from all  
migrations, and just use votes.count.

---

<div class="post-metadata">

**Author:** ![darius](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58956e/32.png) [@darius](https://www.ruby-forum.com/u/darius)\
**Post date:** [November 14, 2007, 4:01pm UTC](https://www.ruby-forum.com/t/why-this-rake-db-migrate-error/119805/5 "2007-11-14T16:01:04Z")

</div>

Phlip [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in  
news:removed\_email\_address@domain.invalid…

> The best forum for Rails questions is Ruby-on-Rails-talk, at Google  
> Groups.

thank you. I’ll check it out.

> 

I know. This was done to avoid a costly query.
