无法激活mysql2 (~> 0.3.6),已经在Rails 3.1中激活了mysql2-0.3.2

发布于 2024-11-25 23:55:54 字数 4399 浏览 0 评论 0原文

我只是想获得在 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) 

但是当我实际尝试在“地点”上进行查找或执行任何操作时,我收到以下错误

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 个小时了,只是在原地打转。

谢谢, - 标记

I'm just trying to get the most basic of basic shell of a rails app running under 3.1, and I'm getting this weird error when I run 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.)

All the posts that I've read here and elsewhere say I should be using the newer mysql2 adaptor for rails 3.1, so I have-

gem 'mysql2', '0.3.2'

in my gemfile. Some post have suggested using-

gem 'mysql2', '~> 0.3'

but that gets me the same error. The gem is installed at-

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

It was suggested that I switch up that line in my gemfile again, this time to be-

gem 'mysql2', '< 0.3'

but when I do that, run bundle install, and then try to run migrations again, I get-

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

My complete migration file looks like this-

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

And the full output of running that migration is-

==  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

There are no later migrations, that's the only one, as this is an app that I'm just starting to try to get Rails 3.1 running properly. Dropping the database and recreating it gets me to the same place.

I am able to access Places from the console-

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) 

But when I actually try to do a find or anything on Places, I get the following error-

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>'

Anyone have any ideas? I've been digging for like 18 hours now, and just running in circles.

Thanks,
--Mark

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

夜光 2024-12-02 23:55:54

Active Record 对于兼容哪些 mysql2 版本有自己的要求。这是代码行轨道 3.1。您必须使用满足这些要求的 mysql2 版本。

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

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

gem 'mysql2', '0.3.6'

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

bundle update mysql2

Active Record has it's own requirements on which versions of mysql2 are compatible. Here's the line of code for Rails 3.1. You must use a version of mysql2 that satisfies these requirements.

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.)

This is saying Rails expects a mysql2 version greater than 0.3.6 and less than 0.4.0, but found version 0.3.2. If you change your Gemfile to request a version in this range then Active Record should be happy. Perhaps

gem 'mysql2', '0.3.6'

Don't forget to update your bundle after changing your Gemfile.

bundle update mysql2
阳光下的泡沫是彩色的 2024-12-02 23:55:54

来解决这个问题

如果有人在 2022 年用新的 M2 书“无法激活 mysql2 (~> 0.3.10),已经激活 mysql2-0.5.2”

,你必须更新这个文件:

/usr/local/bundle /gems/activerecord-3.2.22.5/lib/active_record/connection_adapters/mysql2_adapter.rb:3

在此处输入图像描述

并将其更改为您正在使用的 mysql gem 的版本。

if anyone here is stuggling with this in 2022 with the new M2 book

"can't activate mysql2 (~> 0.3.10), already activated mysql2-0.5.2"

you have to update this file:

/usr/local/bundle/gems/activerecord-3.2.22.5/lib/active_record/connection_adapters/mysql2_adapter.rb:3

enter image description here

and change it to the version of the mysql gem you are using.

温柔一刀 2024-12-02 23:55:54

我知道这是一个非常古老的线程,但因为它仍然出现在 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

但我必须更新它以防止选择错误消息中指示的范围之外的版本。注意 '< 0.5' 下面:

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。”

I know this is a very old thread but because it still comes up in Google results a one of the first results I though I would updated it with I had to do work around this issue in newer versions of Rails.

The errors shows as:

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

followed by:

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.

I already had the gem added in my Gemfile as shown below:

group :production do
  gem 'mysql2'
end

but I had to update it to prevent from picking up a version outside of the range indicated in the error message. Notice the '< 0.5' below:

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

Additionally, different versions of Rails set different min/max version restrictions for the MySQL gem:

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

"Correct, Rails 4.x cannot use mysql2 0.5.x. The actual code would
work fine, but Rails 4 has a version restriction in place to prevent
use of mysql2 0.5.x. This is intentional, as it allows future changes
to the API without silently risking their use in older code not
prepared to use the new APIs. There's also nothing magic about the
version numbers -- it's totally by coincidence that Rails 4 uses
mysql2 0.4 and Rails 5 will be able to use mysql2 0.4 or 0.5."

勿忘初心 2024-12-02 23:55:54

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

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

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

This also had me pulling out my hair. To only reasonable solution I could get was to switch to the master branch of the mysql2 gem.

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

After this update my Rails 3.1.0.rc5 application could start with MySQL. (At the time of this post the latest version of the gem was 0.3.6)

油焖大侠 2024-12-02 23:55:54

我在使用 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)

一切正常

I have the same issue several times with an Redmine implementation.

I changed a gem line place on

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

In the line 3 change this

gem 'mysql2', '~> 0.3.10'

with this

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

And all works fine

最后的乘客 2024-12-02 23:55:54

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

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

这很可能发生,因为您没有通过捆绑执行运行迁移。尝试运行 bundle exec rake db:migrate

Pretty old question, so I'm assuming the original poster has already solved the issue. However, in case anyone is coming to this post trying to solve the first issue:

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.)

This is most likely occurring because you are not running your migrations via bundle exec. Try running bundle exec rake db:migrate.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文