rake db:create为nil提供未定义的方法`[]':NilClass错误

时间:2014-02-27 20:25:24

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

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'

1 个答案:

答案 0 :(得分: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