关于数据库适配器与database.yml不一致的问题

时间:2014-04-09 13:20:52

标签: ruby-on-rails rake

database.yml(rails生成的默认文件)中:

default: &default
    adapter: sqlite3
    pool: 5
    timeout: 5000

development:
  <<: *default
  database: db/development.sqlite3

test:
  <<: *default
  database: db/test.sqlite3

production:
  <<: *default
  database: db/production.sqlite3

运行rake about时出现此错误:

Gem::LoadError: Specified 'postgresql' for database adapter, but the gem is not loaded.    
Add `gem 'pg'` to your Gemfile (and ensure its version is at the minimum required by    
ActiveRecord).

如果我添加pg gem,则rake about会提供此输出:

About your application's environment
Ruby version              2.1.0-p0 (x86_64-darwin13.0)
RubyGems version          2.2.2
Rack version              1.5
Rails version             4.1.0
JavaScript Runtime        Node.js (V8)
Active Record version     4.1.0
Action Pack version       4.1.0
Action View version       4.1.0
Action Mailer version     4.1.0
Active Support version    4.1.0
Middleware                Rack::Sendfile, ActionDispatch::Static, Rack::Lock, #   <ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x007f8043154a30>, Rack::Runtime, Rack::MethodOverride, ActionDispatch::RequestId, Rails::Rack::Logger, ActionDispatch::ShowExceptions, ActionDispatch::DebugExceptions, ActionDispatch::RemoteIp, ActionDispatch::Reloader, ActionDispatch::Callbacks, ActiveRecord::Migration::CheckPending, ActiveRecord::ConnectionAdapters::ConnectionManagement, ActiveRecord::QueryCache, ActionDispatch::Cookies, ActionDispatch::Session::CookieStore, ActionDispatch::Flash, ActionDispatch::ParamsParser, Rack::Head, Rack::ConditionalGet, Rack::ETag
Environment               development
Database adapter          postgresql
Database schema version   0

知道为什么会这样吗?我想使用sqlite3适配器。

2 个答案:

答案 0 :(得分:9)

我发现了问题。显然Rake正在寻找一个环境变量DATABASE_URL(我设置为postgres),这比database.yml文件优先。删除环境变量后,一切正常。

答案 1 :(得分:0)

database.yml中将adapter: sqlite3更改为adapter: postgresql

您还需要指定:
encoding: unicode
database: yourapp_development
pool: 5
username: your_username
password: your_password

如果它是您正在使用的新应用,请使用:rails new appname -d postgresql再次运行rails new generator。这将使用postgres适配器创建应用程序。

在你的情况下:

default: &default  
  adapter: postgresql
  encoding: unicode  
  pool: 5  
  timeout: 5000  

development:  
  <<: *default  
  database: yourapp_development
  username: your_username  
  password: your_password

test:  
  <<: *default  
  database: yourapp_test  
  username: your_username  
  password: your_password  

production:  
  <<: *default  
  database: yourapp_production
  username: your_username  
  password: your_password`