Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can't activate mysql2 (~> 0.3.6), already activated mysql2-0.3.2 in Rails 3.1

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

like image 826
Mark Avatar asked Jul 26 '11 06:07

Mark


3 Answers

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
like image 133
orien Avatar answered Oct 03 '22 00:10

orien


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

like image 41
Hector Correa Avatar answered Oct 03 '22 00:10

Hector Correa


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)

like image 39
Armand Du Plessis Avatar answered Oct 02 '22 22:10

Armand Du Plessis