[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

why this rake db:migrate error

darius

11/10/2007 5:30:00 PM

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

Rick DeNatale

11/10/2007 5:45:00 PM

0

On Nov 10, 2007 12:30 PM, darius <noone@here.invalid> wrote:
> 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|
...

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

darius

11/10/2007 6:11:00 PM

0

Rick DeNatale <rick.denatale@gmail.com> wrote in
news:deb2337a0711100945q23319eadu2824ecdfd6a6d466@mail.gmail.com...

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

Phlip

11/11/2007 4:30:00 AM

0

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.

--
Phlip

darius

11/14/2007 2:59:00 PM

0

Phlip <phlip2005@gmail.com> wrote in
news:Qpmdnb7bS_bDGKvanZ2dnUVZ_qGknZ2d@adelphia.com...


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

thank you. I'll check it out.

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

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