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:inblock (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:in
each' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-4.0.0/lib/rails/engine.rb:608:inblock in <class:Engine>' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-4.0.0/lib/rails/initializable.rb:30:in
instance_exec' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-4.0.0/lib/rails/initializable.rb:30:inrun' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-4.0.0/lib/rails/initializable.rb:55:in
block in run_initializers' from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:228:inblock in tsort_each' from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:350:in
block (2 levels) in each_strongly_connected_component' from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:431:ineach_strongly_connected_component_from' from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:349:in
block in each_strongly_connected_component' from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:347:ineach' from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:347:in
call' from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:347:ineach_strongly_connected_component' from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:226:in
tsort_each' from C:/Ruby23-x64/lib/ruby/2.3.0/tsort.rb:205:intsort_each' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-4.0.0/lib/rails/initializable.rb:54:in
run_initializers' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-4.0.0/lib/rails/application.rb:215:ininitialize!' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-4.0.0/lib/rails/railtie/configurable.rb:30:in
method_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:in
require' from C:/Users/John/Documents/RoRCmS/simple_cms/config.ru:3:inblock in <main>' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rack-1.5.5/lib/rack/builder.rb:55:in
instance_eval' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rack-1.5.5/lib/rack/builder.rb:55:ininitialize' from C:/Users/John/Documents/RoRCmS/simple_cms/config.ru:in
new' 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:in
eval' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rack-1.5.5/lib/rack/builder.rb:49:innew_from_string' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rack-1.5.5/lib/rack/builder.rb:40:in
parse_file' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rack-1.5.5/lib/rack/server.rb:277:inbuild_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:in
app' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-4.0.0/lib/rails/commands/server.rb:48:inapp' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/rack-1.5.5/lib/rack/server.rb:314:in
wrapped_app' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-4.0.0/lib/rails/commands/server.rb:75:instart' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-4.0.0/lib/rails/commands.rb:78:in
block in ' from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/railties-4.0.0/lib/rails/commands.rb:73:intap' 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:inrequire' from bin/rails:4:in
'
I really need to solve this problem now.. thanks guys.
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
SOLVED: i added the gem : gem 'activerecord-native_db_types_override'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With