Allow to use tag to disable auto create/update time

This commit is contained in:
Jinzhu 2022-04-02 17:17:47 +08:00
parent f7b52bb649
commit 9144969c83
3 changed files with 3 additions and 4 deletions

View File

@ -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" {

View File

@ -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)

View File

@ -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