Merge 441092d82f35f183376a5afc2e3d1aefac0f2adf into 4e34a6d21b63e9a9b701a70be9759e5539bf26e9
This commit is contained in:
		
						commit
						c4c49dcadf
					
				| @ -3,7 +3,6 @@ package callbacks | |||||||
| import ( | import ( | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"reflect" | 	"reflect" | ||||||
| 	"strings" |  | ||||||
| 
 | 
 | ||||||
| 	"gorm.io/gorm" | 	"gorm.io/gorm" | ||||||
| 	"gorm.io/gorm/clause" | 	"gorm.io/gorm/clause" | ||||||
| @ -373,8 +372,9 @@ func ConvertToCreateValues(stmt *gorm.Statement) (values clause.Values) { | |||||||
| 				for _, column := range values.Columns { | 				for _, column := range values.Columns { | ||||||
| 					if field := stmt.Schema.LookUpField(column.Name); field != nil { | 					if field := stmt.Schema.LookUpField(column.Name); field != nil { | ||||||
| 						if v, ok := selectColumns[field.DBName]; (ok && v) || (!ok && !restricted) { | 						if v, ok := selectColumns[field.DBName]; (ok && v) || (!ok && !restricted) { | ||||||
| 							if !field.PrimaryKey && (!field.HasDefaultValue || field.DefaultValueInterface != nil || | 							// We can confirm the column either has a value or default value,
 | ||||||
| 								strings.EqualFold(field.DefaultValue, "NULL")) && field.AutoCreateTime == 0 { | 							// so checking HasDefaultValue again will cause some columns to be missed.
 | ||||||
|  | 							if !field.PrimaryKey && field.AutoCreateTime == 0 { | ||||||
| 								if field.AutoUpdateTime > 0 { | 								if field.AutoUpdateTime > 0 { | ||||||
| 									assignment := clause.Assignment{Column: clause.Column{Name: field.DBName}, Value: curTime} | 									assignment := clause.Assignment{Column: clause.Column{Name: field.DBName}, Value: curTime} | ||||||
| 									switch field.AutoUpdateTime { | 									switch field.AutoUpdateTime { | ||||||
|  | |||||||
| @ -3,6 +3,7 @@ package tests_test | |||||||
| import ( | import ( | ||||||
| 	"testing" | 	"testing" | ||||||
| 
 | 
 | ||||||
|  | 	"gorm.io/datatypes" | ||||||
| 	"gorm.io/gorm" | 	"gorm.io/gorm" | ||||||
| 	"gorm.io/gorm/clause" | 	"gorm.io/gorm/clause" | ||||||
| 	"gorm.io/gorm/schema" | 	"gorm.io/gorm/schema" | ||||||
| @ -183,6 +184,56 @@ func TestForeignKeyConstraintsBelongsTo(t *testing.T) { | |||||||
| 	} | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | func TestFullSaveAssociationsWithJSONDefault(t *testing.T) { | ||||||
|  | 	if DB.Dialector.Name() == "mysql" { | ||||||
|  | 		t.Skip() // mysql json can't have a default value
 | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	type ValueDep struct { | ||||||
|  | 		ID      int | ||||||
|  | 		ValueID int | ||||||
|  | 		Name    string | ||||||
|  | 		Params  datatypes.JSONMap `gorm:"default:'{}'"` | ||||||
|  | 	} | ||||||
|  | 	type Value struct { | ||||||
|  | 		ID   int | ||||||
|  | 		Name string | ||||||
|  | 		Dep  ValueDep | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	if err := DB.Migrator().DropTable(&ValueDep{}, &Value{}); err != nil { | ||||||
|  | 		t.Fatalf("failed to drop value table, got err: %v", err) | ||||||
|  | 	} | ||||||
|  | 	if err := DB.AutoMigrate(&ValueDep{}, &Value{}); err != nil { | ||||||
|  | 		t.Fatalf("failed to migrate value table, got err: %v", err) | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	if err := DB.Create(&Value{ | ||||||
|  | 		Name: "foo", | ||||||
|  | 		Dep:  ValueDep{Name: "bar", Params: map[string]interface{}{"foo": "bar"}}, | ||||||
|  | 	}).Error; err != nil { | ||||||
|  | 		t.Errorf("failed to create value, got err: %v", err) | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	var value Value | ||||||
|  | 	if err := DB.Preload("Dep").First(&value).Error; err != nil { | ||||||
|  | 		t.Errorf("failed to find value, got err: %v", err) | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	value.Dep.Params["foo"] = "new bar" | ||||||
|  | 	if err := DB.Session(&gorm.Session{FullSaveAssociations: true}).Save(&value).Error; err != nil { | ||||||
|  | 		t.Errorf("failed to svae value, got err: %v", err) | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	var result Value | ||||||
|  | 	if err := DB.Preload("Dep").First(&result).Error; err != nil { | ||||||
|  | 		t.Errorf("failed to find value, got err: %v", err) | ||||||
|  | 	} | ||||||
|  | 	if result.Dep.Params["foo"] != "new bar" { | ||||||
|  | 		t.Errorf("failed to save value dep params, got: %v", result.Dep.Params) | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | 
 | ||||||
| func TestFullSaveAssociations(t *testing.T) { | func TestFullSaveAssociations(t *testing.T) { | ||||||
| 	coupon := &Coupon{ | 	coupon := &Coupon{ | ||||||
| 		AppliesToProduct: []*CouponProduct{ | 		AppliesToProduct: []*CouponProduct{ | ||||||
|  | |||||||
| @ -8,6 +8,7 @@ import ( | |||||||
| 	"time" | 	"time" | ||||||
| 
 | 
 | ||||||
| 	"github.com/jinzhu/now" | 	"github.com/jinzhu/now" | ||||||
|  | 	"gorm.io/datatypes" | ||||||
| 	"gorm.io/gorm" | 	"gorm.io/gorm" | ||||||
| 	"gorm.io/gorm/clause" | 	"gorm.io/gorm/clause" | ||||||
| 	. "gorm.io/gorm/utils/tests" | 	. "gorm.io/gorm/utils/tests" | ||||||
| @ -634,6 +635,38 @@ func TestCreateOnConflictWithDefaultNull(t *testing.T) { | |||||||
| 	AssertEqual(t, u2.Mobile, "133xxxx") | 	AssertEqual(t, u2.Mobile, "133xxxx") | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | func TestCreateOnConflictWithDefaultJSON(t *testing.T) { | ||||||
|  | 	if DB.Dialector.Name() == "mysql" { | ||||||
|  | 		t.Skip() // mysql json can't have a default value
 | ||||||
|  | 	} | ||||||
|  | 	type OnConflictValue struct { | ||||||
|  | 		ID     int | ||||||
|  | 		Params datatypes.JSONMap `gorm:"default:'{}'"` | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	err := DB.Migrator().DropTable(&OnConflictValue{}) | ||||||
|  | 	AssertEqual(t, err, nil) | ||||||
|  | 	err = DB.AutoMigrate(&OnConflictValue{}) | ||||||
|  | 	AssertEqual(t, err, nil) | ||||||
|  | 
 | ||||||
|  | 	v := OnConflictValue{ | ||||||
|  | 		Params: datatypes.JSONMap{"foo": "bar"}, | ||||||
|  | 	} | ||||||
|  | 	err = DB.Create(&v).Error | ||||||
|  | 	AssertEqual(t, err, nil) | ||||||
|  | 
 | ||||||
|  | 	err = DB.Clauses(clause.OnConflict{UpdateAll: true}).Create(&OnConflictValue{ | ||||||
|  | 		ID:     v.ID, | ||||||
|  | 		Params: datatypes.JSONMap{"foo": "new-bar"}, | ||||||
|  | 	}).Error | ||||||
|  | 	AssertEqual(t, err, nil) | ||||||
|  | 
 | ||||||
|  | 	var v2 OnConflictValue | ||||||
|  | 	err = DB.Where("id = ?", v.ID).First(&v2).Error | ||||||
|  | 	AssertEqual(t, err, nil) | ||||||
|  | 	AssertEqual(t, v2.Params, datatypes.JSONMap{"foo": "new-bar"}) | ||||||
|  | } | ||||||
|  | 
 | ||||||
| func TestCreateFromMapWithoutPK(t *testing.T) { | func TestCreateFromMapWithoutPK(t *testing.T) { | ||||||
| 	if !isMysql() { | 	if !isMysql() { | ||||||
| 		t.Skipf("This test case skipped, because of only supporting for mysql") | 		t.Skipf("This test case skipped, because of only supporting for mysql") | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 black-06
						black-06