在测试环境中运行rake db:create时,String不能强制转换为Integer

时间:2018-01-18 07:37:18

标签: ruby-on-rails rspec rspec-rails

我正在尝试使用rake db:create创建一个Test数据库,它给出了以下错误,我最近从rails 3.2.17升级到3.2.22.5。

RAILS_ENV=test rake db:create --trace

** Invoke db:create (first_time)
** Invoke db:load_config (first_time)
** Execute db:load_config
** Invoke rails_env (first_time)
** Execute rails_env
** Execute db:create
String can't be coerced into Integer
/data/rvm_installtion/.rvm/gems/ruby-2.4.3@waltz/gems/activesupport3.2.22.5/lib/active_support/core_ext/enumerable.rb:60:in `+'
/data/rvm_installtion/.rvm/gems/ruby-2.4.3@waltz/gems/activesupport3.2.22.5/lib/active_support/core_ext/enumerable.rb:60:in `sum'
/data/rvm_installtion/.rvm/gems/ruby-2.4.3@waltz/gems/activesupport3.2.22.5/lib/active_support/core_ext/enumerable.rb:60:in `sum'
/data/rvm_installtion/.rvm/gems/ruby-2.4.3@waltz/gems/activerecord3.2.22.5/lib/active_record/connection_adapters/postgresql_adapter.rb:747:in `create_database'
/data/rvm_installtion/.rvm/gems/ruby-2.4.3@waltz/gems/activerecord3.2.22.5/lib/active_record/railties/databases.rake144:in `rescue in create_database'
/data/rvm_installtion/.rvm/gems/ruby-2.4.3@waltz/gems/activerecord3.2.22.5/lib/active_record/railties/databases.rake:84:in `create_database'
/data/rvm_installtion/.rvm/gems/ruby-2.4.3@waltz/gems/activerecord3.2.22.5/lib/active_record/railties/databases.rake:62:in `block (3 levels) in <top (required)>'
/data/rvm_installtion/.rvm/gems/ruby-2.4.3@waltz/gems/activerecord3.2.22.5/lib/active_record/railties/databases.rake:62:in `each'
/data/rvm_installtion/.rvm/gems/ruby-2.4.3@waltz/gems/activerecord3.2.22.5/lib/active_record/railties/databases.rake:62:in `block (2 levels) in <top (required)>'
/data/rvm_installtion/.rvm/gems/ruby2.4.3@waltz/gems/rake10.5.0/lib/rake/task.rb:240:in `block in execute'
/data/rvm_installtion/.rvm/gems/ruby-2.4.3@waltz/gems/rake10.5.0/lib/rake/task.rb:235:in `each'
/data/rvm_installtion/.rvm/gems/ruby-2.4.3@waltz/gems/rake10.5.0/lib/rake/task.rb:235:in `execute'
/data/rvm_installtion/.rvm/gems/ruby-2.4.3@waltz/gems/rake10.5.0/lib/rake/task.rb:179:in `block in invoke_with_call_chain'
/data/rvm_installtion/.rvm/rubies/ruby-2.4.3/lib/ruby/2.4.0/monitor.rb:214:in `mon_synchronize'
/data/rvm_installtion/.rvm/gems/ruby-2.4.3@waltz/gems/rake-10.5.0/lib/rake/task.rb:172:in `invoke_with_call_chain'
/data/rvm_installtion/.rvm/gems/ruby-2.4.3@waltz/gems/rake-10.5.0/lib/rake/task.rb:165:in `invoke'
/data/rvm_installtion/.rvm/gems/ruby-2.4.3@waltz/gems/rake-10.5.0/lib/rake/application.rb:150:in `invoke_task'
/data/rvm_installtion/.rvm/gems/ruby-2.4.3@waltz/gems/rake-10.5.0/lib/rake/application.rb:106:in `block (2 levels) in top_level'
/data/rvm_installtion/.rvm/gems/ruby-2.4.3@waltz/gems/rake-10.5.0/lib/rake/application.rb:106:in `each'
/data/rvm_installtion/.rvm/gems/ruby-2.4.3@waltz/gems/rake-10.5.0/lib/rake/application.rb:106:in `block in top_level'
/data/rvm_installtion/.rvm/gems/ruby-2.4.3@waltz/gems/rake-10.5.0/lib/rake/application.rb:115:in `run_with_threads'
/data/rvm_installtion/.rvm/gems/ruby-2.4.3@waltz/gems/rake-10.5.0/lib/rake/application.rb:100:in `top_level'
/data/rvm_installtion/.rvm/gems/ruby-2.4.3@waltz/gems/rake-10.5.0/lib/rake/application.rb:78:in `block in run'
/data/rvm_installtion/.rvm/gems/ruby-2.4.3@waltz/gems/rake-10.5.0/lib/rake/application.rb:176:in `standard_exception_handling'
/data/rvm_installtion/.rvm/gems/ruby-2.4.3@waltz/gems/rake-10.5.0/lib/rake/application.rb:75:in `run'
/data/rvm_installtion/.rvm/gems/ruby-2.4.3@waltz/gems/rake-10.5.0/bin/rake:33:in `<top (required)>'
/data/rvm_installtion/.rvm/gems/ruby-2.4.3@waltz/bin/rake:23:in `load'
/data/rvm_installtion/.rvm/gems/ruby-2.4.3@waltz/bin/rake:23:in `<main>'
/data/rvm_installtion/.rvm/gems/ruby-2.4.3@waltz/bin/ruby_executable_hooks:15:in `eval'
/data/rvm_installtion/.rvm/gems/ruby-2.4.3@waltz/bin/ruby_executable_hooks:15:in `<main>'
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "pool"=>5, "username"=>"postgres", "host"=>"localhost", "database"=>"test_database_name"}

请建议。 我使用的是Ruby 2.4.3和Rails 3.2.22.5。

这是我的database.yml文件

DEFAULTS: &DEFAULTS
adapter: postgresql
encoding: unicode
pool: 5
username: postgres
host: localhost

development:
<<: *DEFAULTS
database: development

test: &test
<<: *DEFAULTS
database: test_2

staging:
<<: *test

production:
<<: *test

先谢谢

2 个答案:

答案 0 :(得分:2)

降级ruby版本(例如2.2)

看起来rails 3.2不支持ruby 2.4(https://stackoverflow.com/a/23532476/299774

答案 1 :(得分:0)

这很糟糕,但您也可以将以下代码添加到初始化程序中:

module ActiveRecord
  module ConnectionAdapters
    class PostgreSQLAdapter < AbstractAdapter
      def create_database(name, options = {})
        options = options.reverse_merge(:encoding => "utf8")

        option_string = options.symbolize_keys.map do |key, value|
          case key
          when :owner
            " OWNER = \"#{value}\""
          when :template
            " TEMPLATE = \"#{value}\""
          when :encoding
            " ENCODING = '#{value}'"
          when :tablespace
            " TABLESPACE = \"#{value}\""
          when :connection_limit
            " CONNECTION LIMIT = #{value}"
          else
            ""
          end
        end

        option_string = option_string.join("")

        execute "CREATE DATABASE #{quote_table_name(name)}#{option_string}"
      end
    end
  end
end

这改变了 activerecord3.2.22.5/lib/active_record/connection_adapters/postgresql_adapter.rb:747 中的问题代码;其他一切都应该有效。