【问题标题】:rake db:create gives undefined method `[]' for nil:NilClass errorrake db:create 为 nil:NilClass 错误提供未定义的方法“[]”
【发布时间】:2014-03-31 12:50:00
【问题描述】:

Postgres 9.3、Ruby 2.1.0

rake db:create 没有创建测试数据库。我已经有一个生产数据库。我尝试使用 RAILS_ENV=test rake db:create 强制它,但它返回“未配置测试数据库。

我的database.yml ->

development:
  adapter: postgresql
  database: app_prod
  host: localhost

test: &test
  adapter: postgresql
  database: app_test
  host: localhost

cucumber:
  <<: *test

production:
  adapter: postgresql
  database: app_prod
  host: localhost

这样就配置好了。我也尝试使用控制台 createdb app_test 创建我的测试数据库,但是当我尝试运行 rake db:test:prepare 时收到相同的错误。

有人有什么想法吗?

这是 --trace on db:create:all

** Invoke db:create:all (first_time)

** Invoke db:load_config (first_time)

** Execute db:load_config

** Execute db:create:all

rake aborted!
undefined method `[]' for nil:NilClass
/Users/username/.rvm/rubies/ruby-2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.3/lib/active_record/tasks/database_tasks.rb:189:in `block in each_local_configuration'

这是对 db:test:prepare 的跟踪

** Invoke db:test:prepare (first_time)

** Invoke db:load_config (first_time)

** Execute db:load_config

** Execute db:test:prepare

** Invoke db:test:load (first_time)

** Invoke db:test:purge (first_time)

** Invoke environment (first_time)

** Execute environment

** Invoke db:load_config

** Execute db:test:purge
rake aborted!
undefined method `[]' for nil:NilClass
/Users/username/.rvm/rubies/ruby-2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.3/lib/active_record/tasks/database_tasks.rb:137:in `purge'

【问题讨论】:

  • 请注意,使用{}按钮格式化代码。
  • 使用--trace 找出哪里出了问题
  • 谢谢 我试过了,没有看到任何我可以使用的东西:(
  • 也许你需要明确设置端口,我记得我前一段时间需要这样做
  • 99% 肯定不是,不过谢谢!

标签: ruby-on-rails ruby database rake psql


【解决方案1】:

尝试在控制台中运行它

ActiveRecord::Base.configurations

你应该得到你的数据库配置。

此方法中的第 3 行(github 中的第 189 行)在您的情况下失败,因为配置为零

def each_local_configuration
    ActiveRecord::Base.configurations.each_value do |configuration|
      next unless configuration['database']

      if local_database?(configuration)
        yield configuration
      else
        $stderr.puts "This task only modifies local databases. #{configuration['database']} is on a remote host."
      end
    end
  end

【讨论】:

  • 就是这样,因为 我认为 我的 .yml 文件中有一个不可见的字符,所以它是 nil。谢谢 Vimsha :D
猜你喜欢
  • 1970-01-01
  • 2013-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-17
  • 2012-12-27
  • 2015-10-18
相关资源
最近更新 更多