why this rake db:migrate error

D

darius

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

Rick DeNatale

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

darius

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

darius

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

thank you. I'll check it out.
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top