【问题标题】:On heroku, rake db:migrate fails with "database configuration does not specify adapter"在 heroku 上,rake db:migrate 因“数据库配置未指定适配器”而失败
【发布时间】:2012-03-12 02:38:05
【问题描述】:

我已跟随Rails Tutorial 到达链接点。

这是外壳输出:

jrhorn424 at hook in ~/Learning/rails/rails-tutorial/demo_app on master
$ heroku run rake db:migrate          
Running rake db:migrate attached to terminal... up, run.2

### Snip ###

Migrating to CreateUsers (20120310145100)
Migrating to CreateMicroposts (20120311052021)
rake aborted!
database configuration does not specify adapter

Tasks: TOP => db:schema:dump
(See full trace by running task with --trace)

我咨询了Heroku quick start,并进行了一些谷歌搜索。我怀疑问题出在config/database.yml 上,因为我的开发环境中充满了对sqlite3 的引用。但是,在服务器上,同一个文件包括以下几行:

adapter = uri.scheme
adapter = "postgresql" if adapter == "postgres"

通过已部署的应用程序添加数据成功,但运行heroku run rake db:migrate 仍然失败。

这是我的 Gemfile:

source 'https://rubygems.org'

gem 'rails', '3.2.2'

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

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

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

  # See https://github.com/sstephenson/execjs#readme for more supported runtimes
  # gem 'therubyracer'

  gem 'uglifier', '1.2.3'
end

gem 'jquery-rails', '2.0.0'

group :production do
    gem 'pg', '0.12.2'
end

【问题讨论】:

  • Heroku 覆盖 database.yml 文件以使用它自己的 PostgreSQL 实例。您能否将您的 Gemfile 添加到此问题中?
  • 谢谢,@RyanBigg。添加了 Gemfile。如前所述,服务器上的 database.yml 似乎包含正确的适配器。

标签: ruby-on-rails heroku rake


【解决方案1】:

对我来说,当我尝试在生产环境中打开 rails 控制台时遇到了这个错误,

当我给的时候,

bundle exec rails c RAILS_ENV=production

它会抛出错误

 `resolve_hash_connection': database configuration does not specify adapter (ActiveRecord::AdapterNotSpecified)

但是当我给的时候它会起作用,

RAILS_ENV=production bundle exec rails c

【讨论】:

    【解决方案2】:

    我建议删除您的数据库:

    bundle exec rake db:drop:all
    

    如果您要在 heroku 上进行托管,请继续为您的所有环境使用 postgres。删除 sqlite gem 并只包含

    gem 'pg'
    

    靠近 Gemfile 的顶部。

    执行一个:

    bundle
    bundle exec rake db:create
    bundle exec rake db:migrate
    

    尝试再次提交并推送(你已经知道该怎么做了)。

    如果这不起作用,请告诉我。

    PS,这是我的 database.yml 文件的样子:

    # PostgreSQL v0.8.x
    #   gem install pg
     development:
      adapter: postgresql
      encoding: unicode
      host: localhost
      database: health_development
      pool: 5
      username: volpine
      password: password
    
      # Warning: The database defined as "test" will be erased and
      # re-generated from your development database when you run         "rake".
      # Do not set this db to the same as development or production.
     test:
      adapter: postgresql
      encoding: unicode
      host: localhost
      database: health_test
      pool: 5
      username: volpine
      password: password
    
    production:
      adapter: postgresql
      encoding: unicode
      host: localhost
      database: health_production
      pool: 5
      username: volpine
      password: password
    

    【讨论】:

    • 这可能是个好建议,因为Heroku recommends 就是这样做的。但是,据我所知,这对使用heroku run rake db:migrate 进行迁移的远程调用没有帮助。
    • wbat 关于尝试:heroku run rake db:push
    • 谢谢,你的建议(好吧,我做了heroku db:push)引导我找到我的解决方案。
    【解决方案3】:

    我遇到了这个问题,发现手动设置 RAILS_ENV 对我有用:

    heroku run RAILS_ENV=production rake db:migrate
    

    【讨论】:

      【解决方案4】:

      当我运行 heroku db:push 时,我得到了一个 taps error,这导致我切换到使用 ruby​​ 1.9.2。切换版本并启动新应用后,我不再收到来自heroku run rake db:migrate 的错误,一切正常。

      更新:我刚刚被告知版本不匹配对于db:migrate 来说应该无关紧要。这对db:push 来说肯定很重要,所以我不确定出了什么问题。无论如何,当我使用相同的 Gemfile 和 1.9.2 初始化一个新应用程序时,它可以工作。

      【讨论】:

        【解决方案5】:

        我遇到了同样的问题。之所以发生这种情况,是因为我从 heroku 应用程序中删除了 RAILS_ENV 环境变量,只留下了一个 RACK_ENV 。

        添加 RAILS_ENV=production 确实解决了问题:

        heroku config:add RAILS_ENV=production --app XXXX-production
        

        【讨论】:

          【解决方案6】:

          Heroku 不会查看您的 database.yml 文件,而是查看 DATABASE_URL - 如果未设置,或者其中有拼写错误,那么您将收到该错误。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-11-26
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-01-09
            • 2014-06-13
            相关资源
            最近更新 更多