0

我有一个使用 mysql 的 rails 项目(过去非常成功)。但是,在升级我的 postgres 安装后,这个 rails 项目抛出未安装适配器的错误:

/Users/stuartnelson/.rvm/gems/ruby-1.9.3-p448@global/gems/bundler-1.3.5/lib/bundler/rubygems_integration.rb:214:in `block in replace_gem': Please install the postgresql adapter: `gem install activerecord-postgresql-adapter` (pg is not part of the bundle. Add it to Gemfile.) (LoadError)

我的database.yml文件正确地将适配器声明为mysql2.

我尝试按照建议安装 activerecord-postgresql-adapter gem(但没有将其添加到我的 gemfile 中,因为我没有使用 postgres),但这并没有解决问题。

我仍然能够登录到我机器上的 mysql 服务器并验证数据库及其表是否存在。

stacktrace 确实显示 rails 正在尝试使用 postgres 适配器,但我在项目中的任何地方都没有指示使用 postgres。

from /Users/stuartnelson/.rvm/gems/ruby-1.9.3-p448@mfacs/gems/activerecord-3.2.12/lib/active_record/connection_adapters/postgresql_adapter.rb:7:in `<top (required)>'
    from /Users/stuartnelson/.rvm/gems/ruby-1.9.3-p448@mfacs/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:251:in `require'
    from /Users/stuartnelson/.rvm/gems/ruby-1.9.3-p448@mfacs/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:251:in `block in require'
    from /Users/stuartnelson/.rvm/gems/ruby-1.9.3-p448@mfacs/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:236:in `load_dependency'
    from /Users/stuartnelson/.rvm/gems/ruby-1.9.3-p448@mfacs/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:251:in `require'

不知何故,我的 postgres 安装引起了这个(我认为)。有没有人有这方面的经验以及如何解决它?

4

2 回答 2

0

弄清楚了。对于另一个项目,我正在导出一个环境变量 DATABASE_URL 以用于我已部署到 heroku 的 golang 博客。Heroku 使用 DATABASE_URL,所以我想我可以在本地机器上设置它,这样我就不必在 dev 和 prod db url 之间切换。

事实证明,如果您导出了 DATABASE_URL,rails 会忽略任何 database.yml 信息以支持此操作。因此,rails 试图使用我声明的 postgres 地址。这就是 heroku 如何轻松地将您的 rails 应用程序连接到他们的 postgres 数据库。

在我的本地机器上删除这个环境变量解决了这个问题。

于 2013-11-14T19:29:20.627 回答
0

当您postgresql在应用程序中作为数据库配置的适配器config/database.yml并且没有pg安装 gem 时,就会发生这种情况。

于 2013-11-14T17:01:09.503 回答