为什么rails控制台忽略了我的config / database.yml开发:设置?

时间:2015-08-21 03:36:31

标签: ruby-on-rails ruby ruby-on-rails-4 rails-console

我的database.yml文件指定了一个非默认数据库:并没有明确的用户名:如果我使用rails c并尝试访问它失败的任何内容,例如:

2.2.2 :001 > User.connection
ActiveRecord::NoDatabaseError: FATAL:  role "scope" does not exist

如果我尝试使用my:development设置明确连接,我会看到:

2.2.2 :005 >   ActiveRecord::Base.establish_connection :development
 =>
#<ActiveRecord::ConnectionAdapters::ConnectionPool:...
 @config={:adapter=>"postgresql", :encoding=>"unicode", :database=>"scope", :pool=>5, :timeout=>5000, :port=>5432, :username=>"scope", :password=>"...", :host=>"127.0.0.1"},
...

所以它明显忽略了我的自定义数据库和我自己的用户名。奇怪的是,如果我将yaml块从development:重命名为其他任何内容,例如staging:,我可以与ActiveRecord::Base.establish_connection :staging很好地联系并访问我的模型。

任何人都知道会发生什么事吗?我没有DATABASE_URL环境变量集,因此无法覆盖事物。还有什么可能导致这种行为?

这是实际的yml:

development:
  adapter: postgresql
  encoding: unicode
  database: mydb_development
  pool: 5
  timeout: 5000
  port: <%= ENV["BOXEN_POSTGRESQL_PORT"] || 5432 %>

和堆栈跟踪:

2.2.2 :002 >   User.count
ActiveRecord::NoDatabaseError: FATAL:  role "scope" does not exist

    from /Users/me/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:665:in `rescue in connect'
    from /Users/me/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:655:in `connect'
    from /Users/me/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:242:in `initialize'
    from /Users/me/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:44:in `new'
    from /Users/me/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:44:in `postgresql_connection'
    from /Users/me/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:438:in `new_connection'
    from /Users/me/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:448:in `checkout_new_connection'
    from /Users/me/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:422:in `acquire_connection'
    from /Users/me/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:349:in `block in checkout'
    from /Users/me/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
    from /Users/me/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:348:in `checkout'
    from /Users/me/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:263:in `block in connection'
    from /Users/me/.rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
    from /Users/me/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:262:in `connection'
    from /Users/me/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:571:in `retrieve_connection'
    from /Users/me/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/connection_handling.rb:113:in `retrieve_connection'
... 11 levels...
    from /Users/me/.rvm/gems/ruby-2.2.2/gems/railties-4.2.3/lib/rails/commands/console.rb:9:in `start'
    from /Users/me/.rvm/gems/ruby-2.2.2/gems/railties-4.2.3/lib/rails/commands/commands_tasks.rb:68:in `console'
    from /Users/me/.rvm/gems/ruby-2.2.2/gems/railties-4.2.3/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /Users/me/.rvm/gems/ruby-2.2.2/gems/railties-4.2.3/lib/rails/commands.rb:17:in `<top (required)>'
    from /Users/me/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:274:in `require'
    from /Users/me/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:274:in `block in require'
    from /Users/me/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /Users/me/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:274:in `require'
    from /Users/me/Documents/projects/deliveries/bin/rails:8:in `<top (required)>'
    from /Users/me/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:268:in `load'
    from /Users/me/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:268:in `block in load'
    from /Users/me/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /Users/me/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.3/lib/active_support/dependencies.rb:268:in `load'
    from /Users/me/.rvm/rubies/ruby-2.2.2/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /Users/me/.rvm/rubies/ruby-2.2.2/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from -e:1:in `<main>'2.2.2 :003 >

更新 使用新的shell并连接到控制台似乎神奇地修复了一些事情。我仍然不知道最初腐败的东西......

1 个答案:

答案 0 :(得分:0)

试试这个:

development:
    adapter: postgresql
    encoding: unicode
    database: XXX_development
    host: localhost
    pool: 5
    username: yourusername
    password: yourpassword