Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

uninitialized constant ActiveRecord::ConnectionAdapters::Mysql2Adapter::NATIVE_DATABASE_TYPES (NameError)

Im having a problem with running my rails server. ive set the abstract_mysql2_adapters at initializer that consist of:

class ActiveRecord::ConnectionAdapters::Mysql2Adapter
NATIVE_DATABASE_TYPES[:primary_key] = "int(11) auto_increment PRIMARY KEY"
end

and it solved my rake db:migrate problem, but when i try to run my rails server it gives me this error.

C:/Users/XXXX/Documents/RoRCmS/simple_cms/config/initializers/abstract_mysql2_adapter.rb:2:in <class:Mysql2Adapter>': uninitialized constant ActiveRecord::ConnectionAda pters::Mysql2Adapter::NATIVE_DATABASE_TYPES (NameError) from C:/Users/John/Documents/RoRCmS/simple_cms/config/initializers/abstract_mysql2_adapter.rb:1:in' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-4.0.0/lib/rails/engine.rb:609:in block (2 levels) in <class:Engine>' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-4.0.0/lib/rails/engine.rb:608:ineach' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-4.0.0/lib/rails/engine.rb:608:in block in <class:Engine>' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-4.0.0/lib/rails/initializable.rb:30:ininstance_exec' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-4.0.0/lib/rails/initializable.rb:30:in run' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-4.0.0/lib/rails/initializable.rb:55:inblock in run_initializers' from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:228:in block in tsort_each' from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:350:inblock (2 levels) in each_strongly_connected_component' from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:431:in each_strongly_connected_component_from' from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:349:inblock in each_strongly_connected_component' from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:347:in each' from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:347:incall' from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:347:in each_strongly_connected_component' from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:226:intsort_each' from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:205:in tsort_each' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-4.0.0/lib/rails/initializable.rb:54:inrun_initializers' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-4.0.0/lib/rails/application.rb:215:in initialize!' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-4.0.0/lib/rails/railtie/configurable.rb:30:inmethod_missing' from C:/Users/John/Documents/RoRCmS/simple_cms/config/environment.rb:5:in <top (required)>' from C:/Users/John/Documents/RoRCmS/simple_cms/config.ru:3:inrequire' from C:/Users/John/Documents/RoRCmS/simple_cms/config.ru:3:in block in <main>' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rack-1.5.5/lib/rack/builder.rb:55:ininstance_eval' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rack-1.5.5/lib/rack/builder.rb:55:in initialize' from C:/Users/John/Documents/RoRCmS/simple_cms/config.ru:innew' from C:/Users/John/Documents/RoRCmS/simple_cms/config.ru:in <main>' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rack-1.5.5/lib/rack/builder.rb:49:ineval' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rack-1.5.5/lib/rack/builder.rb:49:in new_from_string' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rack-1.5.5/lib/rack/builder.rb:40:inparse_file' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rack-1.5.5/lib/rack/server.rb:277:in build_app_and_options_from_config' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rack-1.5.5/lib/rack/server.rb:199:inapp' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-4.0.0/lib/rails/commands/server.rb:48:in app' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rack-1.5.5/lib/rack/server.rb:314:inwrapped_app' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-4.0.0/lib/rails/commands/server.rb:75:in start' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-4.0.0/lib/rails/commands.rb:78:inblock in ' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-4.0.0/lib/rails/commands.rb:73:in tap' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-4.0.0/lib/rails/commands.rb:73:in' from bin/rails:4:in require' from bin/rails:4:in'

I really need to solve this problem now.. thanks guys.

like image 505
John Geliberte Avatar asked May 19 '16 06:05

John Geliberte


2 Answers

I solved it as follows. After importing a legacy sql dumpfile into MySQL with mysql -u username -p database_name < file.sql, I proceeded to perform a migration with bin/rake db:migrate, but I encountered the error Mysql2::Error: All parts of a PRIMARY KEY must be NOT NULL; if you need NULL in a key, use UNIQUE instead.

So I tried the same monkey patch as you, as suggested by user pjg here https://github.com/rails/rails/pull/13247#issuecomment-32425844, but then I encountered your error

uninitialized constant ActiveRecord::ConnectionAdapters::Mysql2Adapter::NATIVE_DATABASE_TYPES (NameError)

I followed user John Geliberte's advice and updated my Gemfile with gem 'activerecord-native_db_types_override' and checked that I also had gem 'mysql2' (NOT 'activerecord-mysql2-adapter' which causes other errors) and ran bundle install.

I used the the documentation associated with the activerecord-native_db_types_override gem and applied property for NATIVE_DATABASE_TYPES mentioned in Rails ActiveRecord library file for abstract_mysql_adapter, such that I made the following changes:

I created config/initializers/abstract_mysql2_adapter.rb and added:

require 'active_record/connection_adapters/mysql2_adapter'
NativeDbTypesOverride.configure({
  ActiveRecord::ConnectionAdapters::Mysql2Adapter => {
    primary_key: "int(11) auto_increment PRIMARY KEY"
  }
})

I created config/environment.rb and add the following to load the monkey patch:

require File.expand_path('../initializers/abstract_mysql2_adapter.rb', __FILE__)

I checked that config/database.yml contained 'adapter: mysql2' for the db connection I wanted to use.

I then ran bin/rake db:migrate and it successfully performed the migration.

I have made a pull request to the documentation for the ActiveRecord Native Database Types Override Gem.

It was much easier to downgrade from MySQL 5.7 to 5.5 instead though.

My system:

  • mysql -V is mysql Ver 14.14 Distrib 5.7.13
  • rails -v is 4.2.4
  • ruby -v is ruby 2.3.0p0
like image 149
Luke Schoen Avatar answered Oct 20 '22 06:10

Luke Schoen


SOLVED: i added the gem : gem 'activerecord-native_db_types_override'

like image 21
John Geliberte Avatar answered Oct 20 '22 05:10

John Geliberte