From 173af3e871b206bfc97dd45d86df690f604e03d8 Mon Sep 17 00:00:00 2001 From: Mo Huishou <1@lailin.xyz> Date: Tue, 22 Jul 2025 07:07:09 +0000 Subject: [PATCH 1/2] fix: fix: on confilct with struct default value --- callbacks/create.go | 4 ++-- tests/create_test.go | 17 ++++++++++++----- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/callbacks/create.go b/callbacks/create.go index cb8429b3..e1031303 100644 --- a/callbacks/create.go +++ b/callbacks/create.go @@ -370,8 +370,8 @@ func ConvertToCreateValues(stmt *gorm.Statement) (values clause.Values) { for _, column := range values.Columns { if field := stmt.Schema.LookUpField(column.Name); field != nil { if v, ok := selectColumns[field.DBName]; (ok && v) || (!ok && !restricted) { - if !field.PrimaryKey && (!field.HasDefaultValue || field.DefaultValueInterface != nil || - strings.EqualFold(field.DefaultValue, "NULL")) && field.AutoCreateTime == 0 { + if !field.PrimaryKey && field.AutoCreateTime == 0 && + (!field.HasDefaultValue || field.DefaultValueInterface != nil || field.DefaultValue != "") { if field.AutoUpdateTime > 0 { assignment := clause.Assignment{Column: clause.Column{Name: field.DBName}, Value: curTime} switch field.AutoUpdateTime { diff --git a/tests/create_test.go b/tests/create_test.go index 5200427e..9a07caec 100644 --- a/tests/create_test.go +++ b/tests/create_test.go @@ -598,12 +598,17 @@ func TestCreateWithAutoIncrementCompositeKey(t *testing.T) { } } -func TestCreateOnConflictWithDefaultNull(t *testing.T) { +func TestCreateOnConflictWithDefault(t *testing.T) { type OnConflictUser struct { - ID string - Name string `gorm:"default:null"` - Email string - Mobile string `gorm:"default:'133xxxx'"` + ID string + Name string `gorm:"default:null"` + Email string + Mobile string `gorm:"default:'133xxxx'"` + Company Company `gorm:"default:'{}';serializer:json"` + } + + type Company struct { + Name string } err := DB.Migrator().DropTable(&OnConflictUser{}) @@ -623,6 +628,7 @@ func TestCreateOnConflictWithDefaultNull(t *testing.T) { u.Name = "on-conflict-user-name-2" u.Email = "on-conflict-user-email-2" u.Mobile = "" + u.Company.Name = "on-conflict-user-company-2" err = DB.Clauses(clause.OnConflict{UpdateAll: true}).Create(&u).Error AssertEqual(t, err, nil) @@ -632,6 +638,7 @@ func TestCreateOnConflictWithDefaultNull(t *testing.T) { AssertEqual(t, u2.Name, "on-conflict-user-name-2") AssertEqual(t, u2.Email, "on-conflict-user-email-2") AssertEqual(t, u2.Mobile, "133xxxx") + AssertEqual(t, u2.Company.Name, "on-conflict-user-company-2") } func TestCreateFromMapWithoutPK(t *testing.T) { From 362fda45ca2f74bc860944ab68c2b9d38200bb3b Mon Sep 17 00:00:00 2001 From: Mo Huishou <1@lailin.xyz> Date: Tue, 22 Jul 2025 07:09:23 +0000 Subject: [PATCH 2/2] lint: remove strings --- callbacks/create.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/callbacks/create.go b/callbacks/create.go index e1031303..d793b1d8 100644 --- a/callbacks/create.go +++ b/callbacks/create.go @@ -3,7 +3,6 @@ package callbacks import ( "fmt" "reflect" - "strings" "gorm.io/gorm" "gorm.io/gorm/clause" @@ -370,7 +369,7 @@ func ConvertToCreateValues(stmt *gorm.Statement) (values clause.Values) { for _, column := range values.Columns { if field := stmt.Schema.LookUpField(column.Name); field != nil { if v, ok := selectColumns[field.DBName]; (ok && v) || (!ok && !restricted) { - if !field.PrimaryKey && field.AutoCreateTime == 0 && + if !field.PrimaryKey && field.AutoCreateTime == 0 && (!field.HasDefaultValue || field.DefaultValueInterface != nil || field.DefaultValue != "") { if field.AutoUpdateTime > 0 { assignment := clause.Assignment{Column: clause.Column{Name: field.DBName}, Value: curTime}