JonathanW
JonathanW

Reputation: 424

rake db:migrate error wrong number of arguments (5 for 4)

This is my migration file:

class AddSeoMetaInfoToArticles < ActiveRecord::Migration
  def self.up
    add_column :articles, :seo_title, :string, { :default => "", :null => false }
    add_column :articles, :seo_description, :string, { :default => "", :null => false }
    add_column :articles, :seo_keywords, :string, :string, { :default => "", :null => false }
  end

  def self.down
    remove_column :articles, :seo_keywords
    remove_column :articles, :seo_description
    remove_column :articles, :seo_title
  end
end

When I try to run 'rake db:migrate' I get the following error

$ rake db:migrate
AddSeoMetaInfoToArticles: migrating =======================================
-- add_column(:articles, :seo_title, :string, {:default=>"", :null=>false})
   -> 0.0341s
-- add_column(:articles, :seo_description, :string, {:default=>"", :null=>false})
   -> 0.0100s
-- add_column(:articles, :seo_keywords, :string, :string, {:default=>"", :null=>false})
rake aborted!
An error has occurred, this and all later migrations canceled:

wrong number of arguments (5 for 4)

Tasks: TOP => db:migrate
(See full trace by running task with --trace)

I'm relatively new to rails and I'm not sure what I'm doing wrong. This is Rails 3.0.9, and a Postgres db if that makes a difference.

Upvotes: 0

Views: 2300

Answers (2)

Frederick Cheung
Frederick Cheung

Reputation: 84124

add_column :articles, :seo_keywords, :string, :string, { :default => "", :null => false }

has :string twice, so you end up with 5 arguments being passed instead of 4.

You might also want to consider writing you migration with change - your migration is equivalent to

class AddSeoMetaInfoToArticles < ActiveRecord::Migration
  def change
    change_table :articles do |t|
      t.string :seo_title, :default => "", :null => false
      t.string :seo_description, :default => "", :null => false
      t.string :seo_keywords, :default => "", :null => false
    end
  end
end

which I find easier on the eye. It also has the advantage that you can pass :bulk => true to change_table which will combine all 3 column additions into 1 alter table statement, which is usually much faster.

Both ways will of course work.

Upvotes: 2

Jeremy Roman
Jeremy Roman

Reputation: 16355

You give the :string argument twice in this line:

    add_column :articles, :seo_keywords, :string, :string, { :default => "", :null => false }

Upvotes: 0

Related Questions