3

以下是错误信息:

/Users/davidzabner/.rvm/gems/ruby-1.9.3-p448/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/connection_specification.rb:47:in `resolve_hash_connection'
    /Users/davidzabner/.rvm/gems/ruby-1.9.3-p448/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/connection_specification.rb:41:in `resolve_string_connection'
    ...
    /Users/davidzabner/.rvm/gems/ruby-1.9.3-p448/gems/railties-3.2.13/lib/rails/application.rb:103:in `require_environment!'
    /Users/davidzabner/.rvm/gems/ruby-1.9.3-p448/gems/railties-3.2.13/lib/rails/application.rb:297:in `block (2 levels) in initialize_tasks'
    /Users/davidzabner/.rvm/gems/ruby-1.9.3-p448/bin/ruby_noexec_wrapper:14:in `eval'
    /Users/davidzabner/.rvm/gems/ruby-1.9.3-p448/bin/ruby_noexec_wrapper:14:in `<main>'

我的 database.yml 文件:

development:
  adapter: sqlite3
  database: db/development.sqlite3
  pool: 5
  timeout: 5000

test:
  adapter: sqlite3
  database: db/test.sqlite3
  pool: 5
  timeout: 5000

production:
  adapter: sqlite3
  database: db/production.sqlite3
  pool: 5
  timeout: 5000

我的宝石文件:

source 'https://rubygems.org'

gem "therubyracer"
gem "less-rails" #Sprockets (what Rails 3.1 uses for its asset pipeline) supports LESS
gem "twitter-bootstrap-rails"
gem 'jquery-rails'
gem 'devise'

gem 'rails', '3.2.13'

# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'

group :development, :test do
    gem 'sqlite3'
end

group :production do
    gem 'pg'
end

gem 'mini_magick'
gem "rmagick"
gem "carrierwave"


# Gems used only for assets and not required
# in production environments by default.
group :assets do
  gem 'sass-rails',   '~> 3.2.3'
  gem 'coffee-rails', '~> 3.2.1'

  # See https://github.com/sstephenson/execjs#readme for more supported runtimes
  # gem 'therubyracer', :platforms => :ruby

  gem 'uglifier', '>= 1.0.3'
end

gem 'jquery-rails'

# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'

# To use Jbuilder templates for JSON
# gem 'jbuilder'

# Use unicorn as the app server
# gem 'unicorn'

# Deploy with Capistrano
# gem 'capistrano'

# To use debugger
# gem 'debugger'
4

6 回答 6

10

I solved a similar problem in a somewhat intricate project. Not sure it's directly related, but I'm posting this as the way of debugging the problem may be helpful.

In my case, I had the following scenario:

  • The situation only occurred when RAILS_ENV=production. When I did RAILS_ENV=development it worked. Weirdly enough, when I changed the production entry in database.yml to production2 and ran the command with RAILS_ENV=production2, it worked.
  • In the project I was connecting to multiple database connections through various models and libraries.

Here is what I did to detect the issue:

vim /Users/davidzabner/.rvm/gems/ruby-1.9.3-p448/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/connection_specification.rb

(or wherever the backtrace is telling you the issue is).

Then, I found the place in the code that has these lines:

def resolve_hash_connection(spec) # :nodoc:
  spec = spec.symbolize_keys

  raise(AdapterNotSpecified, "database configuration does not specify adapter") unless spec.key?(:adapter)

And changed it to the following:

def resolve_hash_connection(spec) # :nodoc:
  spec = spec.symbolize_keys

  # Debug printing
  puts "*" * 80, spec.inspect, "*" * 80

  raise(AdapterNotSpecified, "database configuration does not specify adapter") unless spec.key?(:adapter)

Then I reran the command, in my case bundle exec rails c production.

By doing this I realized that Rails wasn't looking for the production entry as I was thinking it was. It was looking for a different entry called abc_production, which was required in my project due to the multiple database connections I was referring to earlier. On that particular server someone forgot to add that abc_production entry to database.yml. Adding the entry solved the issue.

I believe it happened only when RAILS_ENV=production because in environments/production.rb I have config.eager_load = true, which means Rails will eager-load the application and classes to memory, and probably attempt to establish all database connections defined in those classes (one of them being abc_production).

Hope this helps someone in a similar situation... If you're not using multiple connections, try and debug the problem by changing connection_specification.rb and see if it gives you any lead..

于 2013-10-17T20:26:15.540 回答
6

我现在不完全是你试图做的,但是。

目前我遇到了与尝试使用 ruby​​-2.10 运行 <rails c -e production> 相同的错误。

`resolve_hash_connection':数据库配置未指定适配器(ActiveRecord::AdapterNotSpecified)

当我运行 <rails c production> 时,一切正常。

也许这可以帮助某人

于 2014-01-28T21:00:39.063 回答
1

我在登台环境中遇到了类似的问题,我做到了:

  1. 将设计秘密添加到 devise.rb 初始化文件
  2. 在 secrets.yml 中配置暂存“secret_key_base”

之后我在那里工作得很好。

于 2014-05-13T08:43:31.600 回答
0

我也遇到了这个问题。我尝试了所有我能找到的类似问题,但没有一个能解决我的问题然后我尝试打印从 database.yml 中读取的配置系统activerecord-3.2.13/lib/active_record/connection_adapters/abstract/connection_specification.rb

def resolve_string_connection(spec) # :nodoc:
  hash = configurations.fetch(spec) do |k|
    connection_url_to_hash(k)
  end
  p configurations
  p spec
  raise(AdapterNotSpecified, "#{spec} database is not configured") unless hash

  resolve_hash_connection hash
end

输出是

{"production"=>nil, "  adapter"=>"mysql2", "  encoding"=>"utf8mb4", "  username"=>"myUsername", "  password"=>"myPassword", "  pool"=>5, "  database"=>"mydb", "  host"=>"myHost", "  port"=>3306, "mydb_production"=>nil}

所以我的问题是 database.yml 有问题导致 YAML 解析错误。但是,我没有发现任何问题,所以我从另一台服务器复制了一个文件,然后问题解决了。

希望这可以帮助某人:p

于 2014-12-24T06:39:07.547 回答
0

我已经用示例数据库配置创建了一个要点,请确保使用它。要点位于:https ://gist.github.com/fidalgo/5970617

还要确保你运行 rakedb:setup来设置你的数据库。

此外,由于在您的环境中您也使用 Sqlite 进行生产和测试,因此在您的 Gemfile 中更改以下行:

group :development, :test do
    gem 'sqlite3'
end

#group :development, :test do
    gem 'sqlite3'
#end

它不应该有任何显着差异,除非您使用的是开发以外的其他环境。

于 2013-07-10T21:51:35.697 回答
0

意识到这有点老了,但在寻找类似问题的答案时刚刚遇到。只是想指出您的 .yml 中仍然有 sqlite3 作为适配器。从那以后您可能已经发现,Heroku 不允许 sqlite3 作为生产数据库。Heroku 上的 SQLite

于 2017-03-30T12:20:12.347 回答