未定义的方法'信息'的零:NilClass运行活动记录迁移时,(undefined method 'info' for nil:NilClass when running active record migration)

528 IT屋

I am trying to run an active record migration but am receiving the following error:

undefined method 'info' for nil:NilClass

Here is the 2 lines of code in my rake task that runs the migration

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 )

And here is my only migration class in the folder

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

And here is the trace statement:

** Invoke migrate (first_time)
** Execute migrate
rake aborted!
undefined method info' for nil:NilClass<br /> C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/migration.rb :473:inmigrate'
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/migration.rb :472:in each'<br /> C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/migration.rb :472:inmigrate'
C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/migration.rb :400:in up'<br /> C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/migration.rb :383:inmigrate'
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 execute'
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 execute'
C:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:597:in invoke_with_call_c hain'<br /> C:/Ruby/lib/ruby/1.8/monitor.rb:242:in synchronize'
C:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:590:in invoke_with_call_c hain'<br /> C:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:583:in invoke'
C:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2051:in invoke_task'<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: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/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:1998:in run'<br /> C:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/bin/rake:31<br /> C:/Ruby/bin/rake:19:inload'
C:/Ruby/bin/rake:19

解决方案

I suggest following Rails' conventions and making your table + field names lowercase_and_underscored rather than CamelCasing. Some macros such as belongs_to look for the lowercase variants and by using them you make your life a lot easier. I'm sorry this does not quite answer your question.

Also, the primary key is "id" by default, you don't need to set it.

Thirdly, migrations are usually ran with rake db:migrate.

Lastly, the reason why you're getting that error is because you're setting up ActiveRecord::Base and not defining a logger object on it, like Rails does for you when you run any task descending from :environment. For more information, see this line in the Rails source.

我想运行一个活动记录迁移,但我收到以下错误:

  

未定义的方法'信息'的零:NilClass

下面是两行code在我的耙任务运行迁移

<$p$p><$c$c>ActiveRecord::Base.establish_connection(YAML::load(File.open('src/SupporterSync.Core/Database/Database.yml'))) 的ActiveRecord :: Migrator.migrate("SRC / SupporterSync.Core /数据库/迁移",ENV ["版本"] ENV ["版本"] to_i?:无)

这里是我唯一的移民类文件夹

 类InitialMigration&LT; ActiveRecord的::迁移
  高清self.up
    CREATE_TABLE:应收,:primary_key =&GT; :身份证办| T |
      t.string:ListId,:上限=&GT; 36:空=&GT;假
      t.string:姓名,:上限=&GT; 31日:空=&GT;假
      t.string:真实姓名,:上限=&GT; 31日:空=&GT;假
      t.string:ParentListId,:上限=&GT; 36
    结束
  结束
  高清self.down
    drop_table:帐户
  结束
结束
 

这里是跟踪语句

  

**调用迁移(FIRST_TIME)
  !**执行迁移
耙中止
未定义的方法信息"的零:NilClass&LT; BR /&GT;   C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/migration.rb   :473:在迁移"
  C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/migration.rb   :472:在每个'&LT; BR /&GT;   C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/migration.rb   :472:在迁移"
  C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/migration.rb   :400:在了'&LT; BR /&GT;   C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/migration.rb   :383:在迁移"
  E:/Working/$c$c/WMF/SupporterSync/rakefile.rb:19
  C:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:在称之为'&LT; BR   /&GT; C:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:在   执行'
  C:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:在每个'&LT; BR   /&GT; C:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:在   执行'
  C:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:597:在    invoke_with_call_c海恩'&LT; BR /&GT; C:/Ruby/lib/ruby/1.8/monitor.rb:242:在   同步"
  C:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:590:在    invoke_with_call_c海恩'&LT; BR /&GT;   C:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:583:在   援引"
  C:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2051:在    invoke_task'&LT; BR /&GT;   C:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:在    top_level"
  C:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:在   每个'&LT; BR /&GT;   C:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:在    top_level"
  C:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:在    standard_exceptio n_handling'&LT; BR /&GT;   C:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2023:在    top_level"
  C:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2001年:在运行"&LT; BR   /&GT; C:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:在    standard_exceptio n_handling"
  C:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:1998年:在运行"&LT; BR   /&GT; C:/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.7/bin/rake:31&LT; BR /&GT;   C:/红宝石/斌/耙:19:在负荷"
C:/红宝石/斌/耙:19

解决方案

我建议您按照Rails的惯例,使lowercase_and_underscored而不是驼峰规则你的表+字段名。某些宏如belongs_to的外观为小写变体,并通过他们,你让你的生活轻松了许多。我很抱歉,这并不完全回答你的问题。

此外,主键是"ID"在默认情况下,你不需要设置它。

第三,迁移通常是跑了耙分贝:迁移

最后,为什么你得到这个错误的原因是因为你设置了的ActiveRecord :: Base的,而不是在它定义一个记录器对象,像Rails会你当你运行任何任务,从降:环境。欲了解更多信息,<一个href="http://github.com/rails/rails/blob/2-3-stable/activerecord/lib/active%5Frecord/migration.rb#L473"相对="nofollow">看到这行在Rails源。

本文地址:IT屋 » 未定义的方法'信息'的零:NilClass运行活动记录迁移时,