这是我的迁移文件:
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数据库。
答案 0 :(得分: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 =&gt; true为change_table
,它将所有3列添加组合成1个alter table语句,这通常要快得多。
这两种方式当然都有效。
答案 1 :(得分:0)
您在此行中提供两次:string
参数:
add_column :articles, :seo_keywords, :string, :string, { :default => "", :null => false }