LoginSignup
3
4

More than 5 years have passed since last update.

Railsでmysqlに「db:migrate」しようとすると「Mysql2::Error: All parts of a PRIMARY KEY must be NOT NULL」

Posted at

参考ページ
PRIMARY KEY issue with creating tables in Rails using rake db:migrate command with mysql

◆事象
railsでmysqlに「db:migrate」しようとすると「Mysql2::Error: All parts of a PRIMARY KEY must be NOT NULL」

◆原因
MySQL 5.7以降では主キーのデフォルト値に「null」は設定不可。

◆解決
・mysqlの場合
 config/initializers/abstract_mysql_adapter.rbを作成

config/initializers/abstract_mysql_adapter.rb
class ActiveRecord::ConnectionAdapters::MysqlAdapter
  NATIVE_DATABASE_TYPES[:primary_key] = "int(11) auto_increment PRIMARY KEY"
end

・mysql2の場合
 config/initializers/abstract_mysql2_adapter.rbを作成

config/initializers/abstract_mysql2_adapter.rb
class ActiveRecord::ConnectionAdapters::Mysql2Adapter
  NATIVE_DATABASE_TYPES[:primary_key] = "int(11) auto_increment PRIMARY KEY"
end```

3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4