2013-01-02 117 views
0

这是我的移民文件:耙分贝:迁移错误错误的参数数目(5 4)

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 
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 3.0.9和Postgres数据库,如果这有所作为。

回答

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

:string两次,所以你最终被传递,而不是4

5个参数,您可能还需要考虑写你与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语句中,这通常要快得多。

这两种方式当然会起作用。

+0

/facepalm有时你只需要另一双眼睛来检查它。 – JonathanW

0

你给:string参数两次在这一行:

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