From 9144969c83829d2f14049a6e4882f785a90b6cf9 Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Sat, 2 Apr 2022 17:17:47 +0800 Subject: [PATCH] Allow to use tag to disable auto create/update time --- schema/field.go | 4 ++-- tests/associations_test.go | 1 - tests/go.mod | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/schema/field.go b/schema/field.go index 3b5cc5c5..77521ad3 100644 --- a/schema/field.go +++ b/schema/field.go @@ -275,7 +275,7 @@ func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field { field.DataType = DataType(dataTyper.GormDataType()) } - if v, ok := field.TagSettings["AUTOCREATETIME"]; ok || (field.Name == "CreatedAt" && (field.DataType == Time || field.DataType == Int || field.DataType == Uint)) { + if v, ok := field.TagSettings["AUTOCREATETIME"]; (ok && utils.CheckTruth(v)) || (!ok && field.Name == "CreatedAt" && (field.DataType == Time || field.DataType == Int || field.DataType == Uint)) { if field.DataType == Time { field.AutoCreateTime = UnixTime } else if strings.ToUpper(v) == "NANO" { @@ -287,7 +287,7 @@ func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field { } } - if v, ok := field.TagSettings["AUTOUPDATETIME"]; ok || (field.Name == "UpdatedAt" && (field.DataType == Time || field.DataType == Int || field.DataType == Uint)) { + if v, ok := field.TagSettings["AUTOUPDATETIME"]; (ok && utils.CheckTruth(v)) || (!ok && field.Name == "UpdatedAt" && (field.DataType == Time || field.DataType == Int || field.DataType == Uint)) { if field.DataType == Time { field.AutoUpdateTime = UnixTime } else if strings.ToUpper(v) == "NANO" { diff --git a/tests/associations_test.go b/tests/associations_test.go index bc3dac55..e729e979 100644 --- a/tests/associations_test.go +++ b/tests/associations_test.go @@ -263,7 +263,6 @@ func TestSaveHasManyCircularReference(t *testing.T) { } func TestAssociationError(t *testing.T) { - DB = DB.Debug() user := *GetUser("TestAssociationError", Config{Pets: 2, Company: true, Account: true, Languages: 2}) DB.Create(&user) diff --git a/tests/go.mod b/tests/go.mod index b85ebdad..fc6600b7 100644 --- a/tests/go.mod +++ b/tests/go.mod @@ -9,7 +9,7 @@ require ( github.com/jinzhu/now v1.1.5 github.com/lib/pq v1.10.4 github.com/mattn/go-sqlite3 v1.14.12 // indirect - golang.org/x/crypto v0.0.0-20220321153916-2c7772ba3064 // indirect + golang.org/x/crypto v0.0.0-20220331220935-ae2d96664a29 // indirect gorm.io/driver/mysql v1.3.2 gorm.io/driver/postgres v1.3.1 gorm.io/driver/sqlite v1.3.1