【问题标题】:rake about database adapter inconsistent with database.ymlrake 关于与 database.yml 不一致的数据库适配器
【发布时间】:2014-10-11 01:25:33
【问题描述】:

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 适配器。

【问题讨论】:

  • 可以分享完整的database.yml内容吗?
  • 在 database.yml 中,你的适配器仍然指定 sqlite3,将其更改为 postgresql,如下所示
  • 我正在尝试使用 sqlite3 作为适配器,而不是 postgres。出于某种原因,Rake 说我正在使用 postgres 适配器。
  • @sysofwan 您是在本地机器上还是在 Heroku、Nitrous 等托管服务提供商上?如果是本地机器并且您不想要 postgresql,请从 Gemfile 中删除 gem pg 并运行 bundle install。让我知道结果。
  • 我发现了问题。显然,Rake wan 正在寻找环境变量 DATABASE_URL(我已将其设置为 postgres),这优先于 database.yml 文件。删除环境变量后,一切正常。谢谢大家!

标签: ruby-on-rails rake


【解决方案1】:

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

【讨论】:

  • 嘿,我也有同样的问题。您能否详细介绍一下您是如何解决这个问题的(如果您还记得的话)?
【解决方案2】:

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

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

如果您正在玩的是一个新应用程序,请再次运行 rails new 生成器:rails new appname -d postgresql。这将使用 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`  

【讨论】:

  • 您的建议有效,但我正在尝试使用 sqlite3 作为适配器,而不是 postgres。谢谢!
  • 请发布您的 Gemfile
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-30
  • 1970-01-01
  • 2012-04-13
  • 1970-01-01
  • 1970-01-01
  • 2011-09-06
相关资源
最近更新 更多