From a7097106b18f4e88ae2c3d5d0c5b927b52cf21a5 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Sun, 14 Feb 2016 22:42:17 +0800 Subject: [PATCH] Fix #784 set mysql datetime's type to NOT NULL --- dialect_mysql.go | 6 +++++- structs_test.go | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/dialect_mysql.go b/dialect_mysql.go index 51b1926a..10d63db2 100644 --- a/dialect_mysql.go +++ b/dialect_mysql.go @@ -61,7 +61,11 @@ func (mysql) DataTypeOf(field *StructField) string { } case reflect.Struct: if _, ok := dataValue.Interface().(time.Time); ok { - sqlType = "timestamp NULL" + if _, ok := field.TagSettings["NOT NULL"]; ok { + sqlType = "timestamp" + } else { + sqlType = "timestamp NULL" + } } default: if _, ok := dataValue.Interface().([]byte); ok { diff --git a/structs_test.go b/structs_test.go index 42eb6bc3..e7bae25f 100644 --- a/structs_test.go +++ b/structs_test.go @@ -42,7 +42,7 @@ type CreditCard struct { ID int8 Number string UserId sql.NullInt64 - CreatedAt time.Time + CreatedAt time.Time `sql:"not null"` UpdatedAt time.Time DeletedAt *time.Time }