Skip to content

Commit

Permalink
Allow to use tag to disable auto create/update time
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Apr 2, 2022
1 parent f7b52bb commit 9144969
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 4 deletions.
4 changes: 2 additions & 2 deletions schema/field.go
Expand Up @@ -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" {
Expand All @@ -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" {
Expand Down
1 change: 0 additions & 1 deletion tests/associations_test.go
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion tests/go.mod
Expand Up @@ -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
Expand Down

0 comments on commit 9144969

Please sign in to comment.