0

这是我的迁移文件:

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

当我尝试运行“rake db:migrate”时,出现以下错误

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

我对rails比较陌生,我不确定我做错了什么。这是 Rails 3.0.9 和 Postgres 数据库(如果有区别的话)。

4

2 回答 2

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

:string两次,所以你最终会传递 5 个参数而不是 4 个。

您可能还想考虑编写您的迁移change- 您的迁移相当于

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

我觉得这更容易。它还有一个优点,您可以传递 :bulk => true change_table,它将所有 3 列添加合并到 1 个 alter table 语句中,这通常要快得多。

当然,这两种方式都行得通。

于 2013-01-02T19:52:03.497 回答
0

:string在这一行中给出了两次参数:

    add_column :articles, :seo_keywords, :string, :string, { :default => "", :null => false }
于 2013-01-02T19:51:47.670 回答