Ruby rails 怎么解决{ production => production_sec }' is not a valid configuration. Expected 'production_sec' to be a URL string or a Hash. (ActiveRecord::DatabaseConfigurations::InvalidConfigurationError)

linlin · 2023年06月27日 · 最后由 spike76 回复于 2023年06月27日 · 299 次阅读

原来是 rails4.1,ruby2.2,现在进行了升级,使用了 rails 7.0.4 ruby 3.2。 启动后,为什么会这样:

/home/ubuntu/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/activerecord-7.0.5/lib/active_record/database_configurations.rb:217:in `build_db_config_from_string': '{ production => production_sec }' is not a valid configuration. Expected 'production_sec' to be a URL string or a Hash. (ActiveRecord::DatabaseConfigurations::InvalidConfigurationError)
    from /home/ubuntu/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/activerecord-7.0.5/lib/active_record/database_configurations.rb:203:in `build_db_config_from_raw_config'
    from /home/ubuntu/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/activerecord-7.0.5/lib/active_record/database_configurations.rb:129:in `resolve'
    from /home/ubuntu/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/activerecord-7.0.5/lib/active_record/connection_handling.rb:353:in `resolve_config_for_connection'
    from /home/ubuntu/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/activerecord-7.0.5/lib/active_record/connection_handling.rb:51:in `establish_connection'
    from /opt/app/art/current/app/models/artwork.rb:2:in `<class:Artwork>'
    from /opt/app/art/current/app/models/artwork.rb:1:in `<top (required)>'
    from <internal:/home/ubuntu/.asdf/installs/ruby/3.2.2/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:37:in `require'

config/database.yml是这样的:

default: &default

  adapter: mysql2
  encoding: utf8
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: root
  password: xx
  host: 172.17.0.1#(使用了docker mysql 5.7)
  port: 3306

development: &development
  <<: *default
  database: web

test: &test
  <<: *default
  database: web_test

production: &production
  <<: *default
  database: web

development_sec: &development
  <<: *default
  database: art

test_sec: &test
  <<: *default
  database: art_test

production_sec: &production
  <<: *default
  database: art

在 model 进行了数据库的选择

class Artwork < ActiveRecord::Base
  ActiveRecord::Base.establish_connection("#{Rails.env}_sec")
  #establish_connection :test_sec
  #ActiveRecord::Base.establish_connection(
  #  "mysql2://root:[email protected]/art_test"
  #)
  #self.table_name = "artworks"

我尝试修改了连接数据库 的方式,但是无法连接,应该怎么解决?

升级跨度这么大...恐怕这个问题解决之后还有更多的坑。是自己的项目还能折腾一下,公司的项目那就容易进坑了。之前一个关键字参数的特性变动就搞得很多老 gem 没法用....

linlin 关闭了讨论。 06月27日 23:10
需要 登录 后方可回复, 如果你还没有账号请 注册新账号