21
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

mysqlでAlter Tableがうまくいかなかった話

Last updated at Posted at 2017-05-30

症状

nullを許可していないカラムにnullを許可したい。

で、以下のエラー

流したSQL

ALTER TABLE users MODIFY email varchar(255);

エラーメッセージ

Error Code: 1067. Invalid default value for 'created_at'

なんでwwwできないwwwwwwmywwwwsqlさんwwwww俺たちwwww
友達wwwwだろwwww?wwwwwww

前提

| Field | Type | Null | Key | Default | Extra |
|:----------------------+------------------+------+-----+---------------------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | | NULL | |
| email | varchar(255) | NO | UNI | NULL | |
| password | varchar(60) | NO | | NULL | |
| remember_token | varchar(100) | YES | | NULL | |
| created_at | timestamp | NO | | 0000-00-00 00:00:00 | |
| updated_at | timestamp | NO | | 0000-00-00 00:00:00 | |
| confirmation_token | varchar(255) | YES | | NULL | |
| confirmed_at | timestamp | YES | | NULL | |
| confirmation_sent_at | timestamp | YES | | NULL | |

で、こうやったら治った

SET sql_mode = '';

MySQLにはSTRICT_ALL_TABLESなるsqlモードがあり、それが原因らしい

参考

【MySQL】MySQL5.6ではsql_modeがSTRICT_TRANS_TABLESになる

21
20
1

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
21
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?