【问题标题】:Rails 6 on Heroku: ActiveRecord::AdapterNotSpecified: database configuration does not specify adapterHeroku 上的 Rails 6:ActiveRecord::AdapterNotSpecified:数据库配置未指定适配器
【发布时间】:2019-10-26 12:46:53
【问题描述】:

我们的 Rails 应用刚刚升级到 Rails 6.0,但部署到 Heroku 失败:

Preparing app for Rails asset pipeline
Running: rake assets:precompile
DEPRECATION WARNING: Including LoggerSilence is deprecated and will be removed in Rails 6.1. Please use `ActiveSupport::LoggerSilence` instead (called from <top (required)> at /tmp/build_b9759d496c72c1085bb8441e3c2159fb/config/application.rb:7)
rake aborted!
ActiveRecord::AdapterNotSpecified: database configuration does not specify adapter   
/tmp/build_b9759d496c72c1085bb8441e3c2159fb/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/connection_adapters/connection_specification.rb:163:in `spec'

config/database.yml:

default: &default
  adapter: postgresql
  encoding: utf8
  pool: 5
  username: <%= ENV['POSTGRESQL_USER'] %>
  password:
  socket: /tmp/mysql.sock

development:
  <<: *default
  database: OurApplication_development

test:
  <<: *default
  database: OurApplication_test

production:
  url: <%= ENV['DATABASE_URL'] %>

使用版本:

  • 导轨 (6.0.0)
  • 红宝石 (2.6.5)

有人知道如何解决这个问题吗?

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-6


    【解决方案1】:

    已通过在 database.yml 中将“适配器”添加到“生产”来解决问题。当我们的项目在 Rails 4 和 5 上时,这不是必需的。

    production:
     adapter: postgresql
    

    【讨论】:

    • 看起来您可能在文件的生产部分中缺少此行 &lt;&lt;: *default,这会将适配器自动添加到您的生产配置中。这可能存在于您的 Rails 4 和 5 项目中,我不记得适配器是可选的。
    • 这解决了我的应用程序中的问题。我在 *default 中使用 sqlite3 适配器并继承 &lt;&lt;: *default 进行开发和测试,但在 production 中没有指定适配器,我使用 postgres 代替。这在 Rails 4 和 5(我认为是 3)中很好,但在 6 中不行。
    【解决方案2】:

    更好的解决方案是使用ENV["DATABASE_URL"] 指定连接详细信息并将您的默认段保持为实际的最小默认值:

    default: &default
      adapter: postgresql
      encoding: utf8
      pool: 5
    
    development:
      <<: *default
      database: OurApplication_development
    
    test:
      <<: *default
      database: OurApplication_test
    
    production:
      # doing url: is just stupid as thats what rails does anyways
      <<: *default
    

    这避免了潜在的开发者战争。您可以使用 DotEnv 为每个环境加载不同的 ENV 变量。或者,您可以将另一个哈希添加到您的 database.yml 并合并它:

    default: &default
      adapter: postgresql
      encoding: utf8
      pool: 5
    
    local_settings: &local_settings
      # will raise an exception on nil instead of failing silently
      username: <%= ENV.fetch('POSTGRESQL_USER') %> 
      password:
      socket: /tmp/mysql.sock
    
    development:
      <<: *default
      <<: *local_settings
      database: OurApplication_development
    
    test:
      <<: *default
      <<: *local_settings
      database: OurApplication_test
    
    production:
      # doing url: is just stupid as thats what rails does anyways
      <<: *default
    

    【讨论】:

      猜你喜欢
      • 2014-06-13
      • 1970-01-01
      • 2020-11-25
      • 2015-11-26
      • 1970-01-01
      • 1970-01-01
      • 2022-08-19
      • 2021-02-28
      • 1970-01-01
      相关资源
      最近更新 更多