【问题标题】:can't activate mysql2 (~> 0.3.6), already activated mysql2-0.3.2 in Rails 3.1无法激活 mysql2 (~> 0.3.6),已经在 Rails 3.1 中激活了 mysql2-0.3.2
【发布时间】:2011-10-13 03:39:51
【问题描述】:

我只是想获得在 3.1 下运行的 rails 应用程序最基本的 shell,当我运行 bundle exec rake db:migrate- 时出现这个奇怪的错误-

Please install the mysql2 adapter: `gem install activerecord-mysql2-adapter` (can't activate mysql2 (~> 0.3.6), already activated mysql2-0.3.2. Make sure all dependencies are added to Gemfile.)

我在这里和其他地方读过的所有帖子都说我应该为 rails 3.1 使用更新的 mysql2 适配器,所以我有-

gem 'mysql2', '0.3.2'

在我的 gemfile 中。一些帖子建议使用-

gem 'mysql2', '~> 0.3'

但这给了我同样的错误。 gem 安装在-

/Users/mark/.rvm/gems/ruby-1.9.2-p180@rails310pre/gems/mysql2-0.3.2

有人建议我再次切换我的 gemfile 中的那一行,这次是-

gem 'mysql2', '< 0.3'

但是当我这样做时,运行捆绑安装,然后再次尝试运行迁移,我得到-

An error has occurred, all later migrations canceled:
undefined method `rows' for nil:NilClass

我的完整迁移文件如下所示-

class CreatePlaces < ActiveRecord::Migration
  def change
    create_table :places do |t|
      t.string :title
      t.string :meta_description
      t.string :permalink, :limit => 60
      t.string :name, :limit => 60
      t.string :address
      t.string :state, :limit => 2
      t.string :region, :limit => 3
      t.float :latitude
      t.float :longitude
      t.text :description
      t.boolean :active, :default => true

      t.timestamps
    end
    add_index :places, [:permalink, :state, :region, :latitude, :longitude, :active], :name => 'places_index'
  end
end

运行该迁移的完整输出是-

==  CreatePlaces: migrating ===================================================
-- create_table(:places)
   -> 0.0925s
-- add_index(:places, [:permalink, :state, :region, :latitude, :longitude, :active], {:name=>"places_index"})
   -> 0.1097s
==  CreatePlaces: migrated (0.2023s) ==========================================

rake aborted!
An error has occurred, all later migrations canceled:

undefined method `rows' for nil:NilClass

没有以后的迁移,这是唯一的迁移,因为这是我刚刚开始尝试让 Rails 3.1 正常运行的应用程序。删除数据库并重新创建它会将我带到同一个地方。

我可以从控制台访问地点-

ruby-1.9.2-p180 :001 > Place
   (0.3ms)  SHOW TABLES
   (0.1ms)  SHOW TABLES
   (1.1ms)  describe `places`
 => Place(id: integer, title: string, meta_description: string, permalink: string, name: string, address: string, state: string, region: string, latitude: float, longitude: float, description: text, active: boolean, created_at: datetime, updated_at: datetime) 

但是当我真正尝试在 Places 上进行查找或任何操作时,我收到以下错误-

Place.find(:all)
ArgumentError: wrong number of arguments (3 for 2)
    from /Users/mark/.rvm/gems/ruby-1.9.2-p180@rails310pre/gems/mysql2-0.2.7/lib/active_record/connection_adapters/mysql2_adapter.rb:634:in `select'
    from /Users/mark/.rvm/gems/ruby-1.9.2-p180@rails310pre/gems/activerecord-3.1.0.rc5/lib/active_record/connection_adapters/abstract/database_statements.rb:9:in `select_all'
    from /Users/mark/.rvm/gems/ruby-1.9.2-p180@rails310pre/gems/activerecord-3.1.0.rc5/lib/active_record/connection_adapters/abstract/query_cache.rb:62:in `select_all'
    from /Users/mark/.rvm/gems/ruby-1.9.2-p180@rails310pre/gems/activerecord-3.1.0.rc5/lib/active_record/base.rb:470:in `find_by_sql'
    from /Users/mark/.rvm/gems/ruby-1.9.2-p180@rails310pre/gems/activerecord-3.1.0.rc5/lib/active_record/relation.rb:111:in `to_a'
    from /Users/mark/.rvm/gems/ruby-1.9.2-p180@rails310pre/gems/activerecord-3.1.0.rc5/lib/active_record/relation/finder_methods.rb:155:in `all'
    from /Users/mark/.rvm/gems/ruby-1.9.2-p180@rails310pre/gems/activerecord-3.1.0.rc5/lib/active_record/relation/finder_methods.rb:105:in `find'
    from /Users/mark/.rvm/gems/ruby-1.9.2-p180@rails310pre/gems/activerecord-3.1.0.rc5/lib/active_record/base.rb:437:in `find'
    from (irb):2
    from /Users/mark/.rvm/gems/ruby-1.9.2-p180@rails310pre/gems/railties-3.1.0.rc5/lib/rails/commands/console.rb:45:in `start'
    from /Users/mark/.rvm/gems/ruby-1.9.2-p180@rails310pre/gems/railties-3.1.0.rc5/lib/rails/commands/console.rb:8:in `start'
    from /Users/mark/.rvm/gems/ruby-1.9.2-p180@rails310pre/gems/railties-3.1.0.rc5/lib/rails/commands.rb:40:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

有人有什么想法吗?我已经挖了差不多 18 个小时了,只是绕着圈子跑。

谢谢, --马克

【问题讨论】:

标签: mysql ruby-on-rails rake bundler ruby-on-rails-3.1


【解决方案1】:

我在 Redmine 实现中多次遇到同样的问题。

我改变了一个宝石线的位置

RUBYGEM@VERSION/gems/activerecord-3.2.22.2/lib/active_record/connection_adapters/mysql2_adapter.rb

在第 3 行改变这个

gem 'mysql2', '~> 0.3.10'

有了这个

gem 'mysql2', '~> 0.4.10' (or whatever version you need)

一切正常

【讨论】:

    【解决方案2】:

    我知道这是一个非常古老的线程,但是因为它仍然出现在 Google 结果中,所以我虽然会更新它,但我必须在新版本的 Rails 中解决这个问题。

    错误显示为:

    Gem::LoadError: Specified 'mysql2' for database adapter, 
    but the gem is not loaded. Add `gem 'mysql2'` to your Gemfile 
    

    接着是:

    Gem::LoadError: can't activate mysql2 (< 0.5, >= 0.3.13), 
    already activated mysql2-0.5.2. Make sure all dependencies are 
    added to Gemfile.
    

    我已经在我的 Gemfile 中添加了 gem,如下所示:

    group :production do
      gem 'mysql2'
    end
    

    但我必须更新它以防止获取错误消息中指示的范围之外的版本。注意下面的“

    group :production do
      gem 'mysql2', '< 0.5'
    end
    

    另外,不同版本的 Rails 为 MySQL gem 设置了不同的最小/最大版本限制:

    https://github.com/brianmario/mysql2/issues/950#issuecomment-376375844

    “正确,Rails 4.x 不能使用 mysql2 0.5.x。实际代码会 工作正常,但 Rails 4 有一个版本限制来防止 使用mysql2 0.5.x。这是故意的,因为它允许未来的变化 到 API 而不会冒着在旧代码中使用它们的风险 准备使用新的 API。也没有什么神奇的 版本号——Rails 4 使用这完全是巧合 mysql2 0.4 和 Rails 5 将能够使用 mysql2 0.4 或 0.5。”

    【讨论】:

    • 完美,添加gem 'mysql2', '&lt; 0.5' 在尝试了几乎所有方法后都有效!最重要的部分,版本规范'&lt; 0.5'
    【解决方案3】:

    Active Record 对哪些版本的mysql2 兼容有自己的要求。这是 Rails 3.1 的line of code。您必须使用满足这些要求的mysql2 版本。

    请安装mysql2适配器:gem install activerecord-mysql2-adapter(无法激活mysql2(~> 0.3.6),已经激活了mysql2-0.3.2。确保所有依赖都添加到Gemfile中。)

    这表示 Rails 需要一个大于 0.3.6 且小于 0.4.0 的 mysql2 版本,但发现版本为 0.3.2。如果您更改 Gemfile 以请求此范围内的版本,那么 Active Record 应该会很高兴。也许

    gem 'mysql2', '0.3.6'
    

    不要忘记在更改您的 Gemfile 后更新您的包。

    bundle update mysql2
    

    【讨论】:

      【解决方案4】:

      相当老的问题,所以我假设原始海报已经解决了这个问题。但是,如果有人来这篇文章试图解决第一个问题:

      请安装mysql2适配器:gem install activerecord-mysql2-adapter(无法激活mysql2(~> 0.3.6),已经激活mysql2-0.3.2。确保所有依赖都添加到Gemfile中。)

      这很可能是因为您没有通过 bundle exec 运行迁移。尝试运行bundle exec rake db:migrate

      【讨论】:

        【解决方案5】:

        这也让我拔掉了头发。我能得到的唯一合理解决方案是切换到 mysql2 gem 的 master 分支。

        gem 'mysql2', :git => 'git://github.com/brianmario/mysql2.git'

        此次更新后,我的 Rails 3.1.0.rc5 应用程序可以从 MySQL 启动。 (在本文发布时,gem 的最新版本是 0.3.6)

        【讨论】:

        • Armand,感谢您对此进行调查!当我将该行添加到我的 gemfile 并运行 bundle install 时,我在尝试访问应用程序时收到以下错误:“git://github.com/brianmario/mysql2.git (at master) is not check out.”任何其他想法将不胜感激!
        • 我遇到了与操作完全相同的问题,这解决了它。谢谢阿尔芒。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-06-28
        • 2014-08-30
        • 2013-01-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多