LoginSignup
3
1

More than 5 years have passed since last update.

MySQL Mac DEFAULT CURRENT_TIMESTAMPなのにcannot be null

Last updated at Posted at 2017-02-14

環境:Mac
MySQL:5.6.25

MacでDB(MySQL)を構築。
DEFAULT CURRENT_TIMESTAMPで定義したtimestampNULLをINSERTしようとしたらエラー。

Column 'updated_at' cannot be null.

カラム定義

`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

おかしい。NULLの場合は現在時刻が登録されるはずなのに。
ググってみるとexplicit_defaults_for_timestampの設定が原因とのこと。これが有効になっていると、エラーになるみたい。

ローカル開発環境なのでmy.cnfを直接編集する。

% sudo vi /usr/local/etc/my.cnf
explicit_defaults_for_timestamp = 0 // 編集 1 -> 0

MySQL再起動

% sudo mysql.server restart

確認

mysql> show global variables like 'explicit_defaults_for_timestamp';
+---------------------------------+-------+
| Variable_name                   | Value |
+---------------------------------+-------+
| explicit_defaults_for_timestamp | OFF   |
+---------------------------------+-------+

参考

timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP can be null on one machine but not another?
MySQL5.6で増えたexplicit_defaults_for_timestamp

3
1
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
1