LoginSignup
12
12

More than 5 years have passed since last update.

mysql 5.7でdb:createするとエラー

Posted at

MySQL 5.7.4-m14をインストールしてrailsアプリを作成しようとしたらこんなエラーが

> rake db:migrate

Mysql2::Error: All parts of a PRIMARY KEY must be NOT NULL; if you need NULL in a key, use UNIQUE instead: CREATE TABLE `tags` (`id` int(11) DEFAULT NULL auto_increment PRIMARY KEY, `name` varchar(255), `created_at` datetime, `updated_at` datetime) ENGINE=InnoDB

5.7.3からかわってたみたい。
http://dev.mysql.com/doc/relnotes/mysql/5.7/en/news-5-7-3.html
主キーは基本的にNOT NULLであるべきなのですが、以前のバージョンだと明示的に指定してやれば主キーの値としてNULL値が使用可能でした。5.7.3からはそれがエラーになるようです。
NULL値を指定したい場合はUNIQUEを指定すれば良いようです。
上記のエラーは、NOT NULLはもう冗長なので…ということのようです。

ActiveRecordのバグになるのかな、と思ってググってみるとcloseされてた。
https://github.com/rails/rails/issues/13203

コードみる限りNOT NULLの記述が消えてます。
https://github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb

現象が起きたrailsのバージョンは4.0.4
4.1.1にアップデートすれば再現しませんでした。

12
12
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
12
12