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-
我只是试图获得在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.)
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-
我在这里和其他地方读到的所有帖子都说我应该使用较新的mysql2适配器用于rails 3.1,所以我有 -
gem 'mysql2', '0.3.2'
in my gemfile. Some post have suggested using-
在我的gemfile中。有些帖子建议使用 -
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-
但是当我这样做时,运行bundle install,然后尝试再次运行迁移,我得到 -
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.
没有以后的迁移,这是唯一的迁移,因为这是一个我刚开始尝试让Rails 3.1正常运行的应用程序。删除数据库并重新创建它会让我到同一个地方。
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.
有人有主意吗?我现在已经挖了18个小时,只是在圈子里跑。
Thanks, --Mark
4 个解决方案
#1
8
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.
Active Record对mysql2的哪个版本兼容有自己的要求。这是Rails 3.1的代码行。您必须使用满足这些要求的mysql2版本。
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.)请安装mysql2适配器:gem install activerecord-mysql2-adapter(无法激活mysql2(〜> 0.3.6),已经激活了mysql2-0.3.2。确保所有依赖项都添加到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
这就是说Rails期望mysql2版本大于0.3.6且小于0.4.0,但发现版本为0.3.2。如果您更改Gemfile以请求此范围内的版本,则Active Record应该感到满意。也许
gem 'mysql2', '0.3.6'
Don't forget to update your bundle after changing your Gemfile.
更改Gemfile后不要忘记更新您的软件包。
bundle update mysql2
#2
2
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.
这也让我拉出我的头发。只有合理的解决方案才能切换到mysql2 gem的master分支。
gem 'mysql2', :git => 'git://github.com/brianmario/mysql2.git'
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)
在此更新后,我的Rails 3.1.0.rc5应用程序可以从MySQL开始。 (在这篇文章发布时,最新版本的宝石是0.3.6)
#3
0
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.
我知道这是一个非常古老的主题,但因为它仍然出现在谷歌的结果中,我首先得到了一个结果,但是我必须在新版本的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:
我已经在我的Gemfile中添加了gem,如下所示:
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:
但我必须更新它以防止在错误消息中指示的范围之外选择一个版本。注意下面的“<0.5”:
group :production do
gem 'mysql2', '< 0.5'
end
Additionally, different versions of Rails set different min/max version restrictions for the MySQL gem:
此外,不同版本的Rails为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."
“正确,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。 “
#4
-1
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.)请安装mysql2适配器:gem install activerecord-mysql2-adapter(无法激活mysql2(〜> 0.3.6),已经激活了mysql2-0.3.2。确保所有依赖项都添加到Gemfile中。)
This is most likely occurring because you are not running your migrations via bundle exec. Try running bundle exec rake db:migrate
.
这很可能是因为您没有通过bundle exec运行迁移。尝试运行bundle exec rake db:migrate。
#1
8
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.
Active Record对mysql2的哪个版本兼容有自己的要求。这是Rails 3.1的代码行。您必须使用满足这些要求的mysql2版本。
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.)请安装mysql2适配器:gem install activerecord-mysql2-adapter(无法激活mysql2(〜> 0.3.6),已经激活了mysql2-0.3.2。确保所有依赖项都添加到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
这就是说Rails期望mysql2版本大于0.3.6且小于0.4.0,但发现版本为0.3.2。如果您更改Gemfile以请求此范围内的版本,则Active Record应该感到满意。也许
gem 'mysql2', '0.3.6'
Don't forget to update your bundle after changing your Gemfile.
更改Gemfile后不要忘记更新您的软件包。
bundle update mysql2
#2
2
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.
这也让我拉出我的头发。只有合理的解决方案才能切换到mysql2 gem的master分支。
gem 'mysql2', :git => 'git://github.com/brianmario/mysql2.git'
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)
在此更新后,我的Rails 3.1.0.rc5应用程序可以从MySQL开始。 (在这篇文章发布时,最新版本的宝石是0.3.6)
#3
0
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.
我知道这是一个非常古老的主题,但因为它仍然出现在谷歌的结果中,我首先得到了一个结果,但是我必须在新版本的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:
我已经在我的Gemfile中添加了gem,如下所示:
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:
但我必须更新它以防止在错误消息中指示的范围之外选择一个版本。注意下面的“<0.5”:
group :production do
gem 'mysql2', '< 0.5'
end
Additionally, different versions of Rails set different min/max version restrictions for the MySQL gem:
此外,不同版本的Rails为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."
“正确,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。 “
#4
-1
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.)请安装mysql2适配器:gem install activerecord-mysql2-adapter(无法激活mysql2(〜> 0.3.6),已经激活了mysql2-0.3.2。确保所有依赖项都添加到Gemfile中。)
This is most likely occurring because you are not running your migrations via bundle exec. Try running bundle exec rake db:migrate
.
这很可能是因为您没有通过bundle exec运行迁移。尝试运行bundle exec rake db:migrate。