nil的未定义方法'info':运行活动记录迁移时的NilClass

Pau*_*aul 0 activerecord ruby-on-rails

我正在尝试运行活动记录迁移,但收到以下错误:

nil的未定义方法'info':NilClass

以下是我的rake任务中运行迁移的2行代码

ActiveRecord::Base.establish_connection(YAML::load(File.open('src/SupporterSync.Core/Database/Database.yml')))
ActiveRecord::Migrator.migrate('src/SupporterSync.Core/Database/Migrations', ENV["VERSION"] ? ENV["VERSION"].to_i : nil )
Run Code Online (Sandbox Code Playgroud)

这是我文件夹中唯一的迁移类

class InitialMigration < ActiveRecord::Migration
  def self.up
    create_table :Accounts, :primary_key => :Id do |t|
      t.string :ListId, :limit => 36, :null => false
      t.string :Name, :limit => 31, :null => false
      t.string :FullName, :limit => 31, :null => false
      t.string :ParentListId, :limit => 36
    end
  end
  def self.down
    drop_table :Accounts
  end
end
Run Code Online (Sandbox Code Playgroud)

以下是跟踪声明:

**调用migrate(first_time)
**执行迁移
rake中止!
未定义的方法info' for nil:NilClass<br /> C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/migration.rb :473:in迁移'
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/migration.rb:472:在each'<br /> C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/migration.rb :472:in迁移'
C:/Ruby/lib/ruby/gems/1.8 /gems/activerecord-2.3.4/lib/active_record/migration.rb:400:在up'<br /> C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/migration.rb :383:in迁移'
E:/Working/Code/WMF/SupporterSync/rakefile.rb:19
C:/Ruby/lib/ruby/gems/1.8 /gems/rake-0.8.7/lib/rake.rb:636:in call'<br /> C:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in 执行'
C:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:in each'<br /> C:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:in 执行'
C:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:597: invoke_with_call_c hain'<br /> C:/Ruby/lib/ruby/1.8/monitor.rb:242:in 同步'
C:/Ruby/lib/ruby/gems/1.8/gems/rake -0.8.7/lib/rake.rb:590:在 invoke_with_call_c hain'<br /> C:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:583:in 调用'
C:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2051:在 invoke_task'<br /> C:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in top_level'中
: /Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in each'<br /> C:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in top_level'C
:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7 /lib/rake.rb:2068:in standard_exceptio n_handling'<br /> C:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2023:in top_level'C
:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2001:in run'<br /> C:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in standard_exceptio n_handling'C
:/ Ruby/LIB /红宝石/宝石/ 1.8 /宝石/耙0.8.7/LIB/rake.rb:1998:在run'<br /> C:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/bin/rake:31<br /> C:/Ruby/bin/rake:19:in负载"
C:/红宝石/斌/耙:19

Rya*_*igg 5

我建议遵循Rails的约定并使你的表+字段名称为lowercase_and_underscored而不是CamelCasing.像belongs_to这样的一些宏寻找小写变体,使用它们可以让你的生活更轻松.对不起,这不能完全回答你的问题.

此外,默认情况下,主键为"id",您无需进行设置.

第三,迁移通常是与rake db:migrate.

最后,您收到该错误的原因是因为您正在设置ActiveRecord::Base并且没有在其上定义记录器对象,就像Rails在您运行任何下降任务时为您做的那样:environment.有关更多信息,请参阅 Rails源代码中的此行.