【问题标题】:Rails loading incorrect database config for test env?Rails 为测试环境加载不正确的数据库配置?
【发布时间】:2016-09-24 23:19:29
【问题描述】:

有一个奇怪的问题...我的测试一直在针对我的开发数据库运行。我在这里尝试了binding.pry 中的test_helper.rb:

# this is the top of the file
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
binding.pry

并注意到 3 件事:

  1. ENV['RAILS_ENV']/Rails.env 已正确设置为 "test"。
  2. Rails 配置似乎加载了正确的值:

    Rails.application.config.database_configuration[Rails.env]
    
    # => {"adapter"=>"postgis",
    #     "encoding"=>"unicode",
    #     "pool"=>5,
    #     "username"=>"user",
    #     "host"=>"localhost",
    #     "database"=>"db_test"} <= CORRECT
    
  3. ...但 ActiveRecord 加载的值不正确:

    ActiveRecord::Base.configurations[Rails.env]
    
    # => {"adapter"=>"postgis",
    #     "encoding"=>"unicode",
    #     "pool"=>5,
    #     "username"=>"user",
    #     "host"=>"localhost",
    #     "port"=>5432,
    #     "database"=>"db_development"} <= INCORRECT
    

此外,ActiveRecord::Base.configurations[Rails.env] 返回一个 port 键,而 Rails.application.config.database_configuration[Rails.env] 不返回。


为什么这些不同?这是我的配置:

default: &default
  adapter: postgis
  encoding: unicode
  pool: 5
  username: user
  host: localhost

development:
  <<: *default
  database: db_development

test:
  <<: *default
  database: db_test

我greped 我的整个项目文件夹和config/database.yml 是我命名我的数据库的唯一地方,所以据我所知,没有另一个配置覆盖这个。

我正在运行rails 4.2.5。

救命!

【问题讨论】:

  • 您的环境中是否设置了DATABASE_URL?
  • @AhmadSherif 呃,是的。删除它可以解决我的问题。想把它作为答案发布,我会奖励你赏金吗?谢谢。

标签: activerecord ruby-on-rails-4.2


【解决方案1】:

尝试从您的环境变量中删除 DATABASE_URL。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-29
    • 2013-09-07
    • 2021-08-19
    • 1970-01-01
    • 2019-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多