From ae696d051fdd183c27ca75f7aa13bde5649b7264 Mon Sep 17 00:00:00 2001 From: miyauchi Date: Fri, 20 Oct 2017 10:24:09 +0900 Subject: [PATCH 1/2] corresponds timestamp precision for mysql --- dialect_mysql.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dialect_mysql.go b/dialect_mysql.go index b9887a5c..573bfc0f 100644 --- a/dialect_mysql.go +++ b/dialect_mysql.go @@ -96,9 +96,9 @@ func (s *mysql) DataTypeOf(field *StructField) string { case reflect.Struct: if _, ok := dataValue.Interface().(time.Time); ok { if _, ok := field.TagSettings["NOT NULL"]; ok { - sqlType = "timestamp" + sqlType = fmt.Sprintf("timestamp(%d)", size) } else { - sqlType = "timestamp NULL" + sqlType = fmt.Sprintf("timestamp(%d) NULL", size) } } default: From 8d4e3e5a832d78a11ea13bb1166569095238cfd0 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Fri, 9 Feb 2018 23:18:47 +0800 Subject: [PATCH 2/2] Use tag PRECISION to set time's precision for mysql --- dialect_mysql.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/dialect_mysql.go b/dialect_mysql.go index 573bfc0f..fee61819 100644 --- a/dialect_mysql.go +++ b/dialect_mysql.go @@ -95,10 +95,15 @@ func (s *mysql) DataTypeOf(field *StructField) string { } case reflect.Struct: if _, ok := dataValue.Interface().(time.Time); ok { + precision := "" + if p, ok := field.TagSettings["PRECISION"]; ok { + precision = fmt.Sprintf("(%s)", p) + } + if _, ok := field.TagSettings["NOT NULL"]; ok { - sqlType = fmt.Sprintf("timestamp(%d)", size) + sqlType = fmt.Sprintf("timestamp%v", precision) } else { - sqlType = fmt.Sprintf("timestamp(%d) NULL", size) + sqlType = fmt.Sprintf("timestamp%v NULL", precision) } } default: