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)

私はレールに比較的慣れていないので、何が間違っているのかわかりません。これはRails3.0.9であり、違いが生じる場合はPostgresデータベースです。

4

2 に答える 2

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

には:string2 回あるため、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 ステートメントに結合されます。これは通常、はるかに高速です。

もちろん、どちらの方法も機能します。

于 2013-01-02T19:52:03.497 に答える
0

:stringこの行で引数を 2 回指定します。

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