Why this rake db:migrate error

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