fix(create): fix insert column order (#6855)
* fix(create): fix insert column order * chore: add ConvertToCreateValues ut for Slice case * fix: remvoe testify dependency --------- Co-authored-by: lujinghao <lujinghao@bytedance.com>
This commit is contained in:
		
							parent
							
								
									ab89d54d87
								
							
						
					
					
						commit
						f7ebf049da
					
				| @ -293,7 +293,8 @@ func ConvertToCreateValues(stmt *gorm.Statement) (values clause.Values) { | |||||||
| 				} | 				} | ||||||
| 			} | 			} | ||||||
| 
 | 
 | ||||||
| 			for field, vs := range defaultValueFieldsHavingValue { | 			for _, field := range stmt.Schema.FieldsWithDefaultDBValue { | ||||||
|  | 				if vs, ok := defaultValueFieldsHavingValue[field]; ok { | ||||||
| 					values.Columns = append(values.Columns, clause.Column{Name: field.DBName}) | 					values.Columns = append(values.Columns, clause.Column{Name: field.DBName}) | ||||||
| 					for idx := range values.Values { | 					for idx := range values.Values { | ||||||
| 						if vs[idx] == nil { | 						if vs[idx] == nil { | ||||||
| @ -303,6 +304,7 @@ func ConvertToCreateValues(stmt *gorm.Statement) (values clause.Values) { | |||||||
| 						} | 						} | ||||||
| 					} | 					} | ||||||
| 				} | 				} | ||||||
|  | 			} | ||||||
| 		case reflect.Struct: | 		case reflect.Struct: | ||||||
| 			values.Values = [][]interface{}{make([]interface{}, len(values.Columns))} | 			values.Values = [][]interface{}{make([]interface{}, len(values.Columns))} | ||||||
| 			for idx, column := range values.Columns { | 			for idx, column := range values.Columns { | ||||||
|  | |||||||
							
								
								
									
										71
									
								
								callbacks/create_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										71
									
								
								callbacks/create_test.go
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,71 @@ | |||||||
|  | package callbacks | ||||||
|  | 
 | ||||||
|  | import ( | ||||||
|  | 	"reflect" | ||||||
|  | 	"sync" | ||||||
|  | 	"testing" | ||||||
|  | 	"time" | ||||||
|  | 
 | ||||||
|  | 	"gorm.io/gorm" | ||||||
|  | 	"gorm.io/gorm/clause" | ||||||
|  | 	"gorm.io/gorm/schema" | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | var schemaCache = &sync.Map{} | ||||||
|  | 
 | ||||||
|  | func TestConvertToCreateValues_DestType_Slice(t *testing.T) { | ||||||
|  | 	type user struct { | ||||||
|  | 		ID    int `gorm:"primaryKey"` | ||||||
|  | 		Name  string | ||||||
|  | 		Email string `gorm:"default:(-)"` | ||||||
|  | 		Age   int    `gorm:"default:(-)"` | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	s, err := schema.Parse(&user{}, schemaCache, schema.NamingStrategy{}) | ||||||
|  | 	if err != nil { | ||||||
|  | 		t.Errorf("parse schema error: %v, is not expected", err) | ||||||
|  | 		return | ||||||
|  | 	} | ||||||
|  | 	dest := []*user{ | ||||||
|  | 		{ | ||||||
|  | 			ID:    1, | ||||||
|  | 			Name:  "alice", | ||||||
|  | 			Email: "email", | ||||||
|  | 			Age:   18, | ||||||
|  | 		}, | ||||||
|  | 		{ | ||||||
|  | 			ID:    2, | ||||||
|  | 			Name:  "bob", | ||||||
|  | 			Email: "email", | ||||||
|  | 			Age:   19, | ||||||
|  | 		}, | ||||||
|  | 	} | ||||||
|  | 	stmt := &gorm.Statement{ | ||||||
|  | 		DB: &gorm.DB{ | ||||||
|  | 			Config: &gorm.Config{ | ||||||
|  | 				NowFunc: func() time.Time { return time.Time{} }, | ||||||
|  | 			}, | ||||||
|  | 			Statement: &gorm.Statement{ | ||||||
|  | 				Settings: sync.Map{}, | ||||||
|  | 				Schema:   s, | ||||||
|  | 			}, | ||||||
|  | 		}, | ||||||
|  | 		ReflectValue: reflect.ValueOf(dest), | ||||||
|  | 		Dest:         dest, | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	stmt.Schema = s | ||||||
|  | 
 | ||||||
|  | 	values := ConvertToCreateValues(stmt) | ||||||
|  | 	expected := clause.Values{ | ||||||
|  | 		// column has value + defaultValue column has value (which should have a stable order)
 | ||||||
|  | 		Columns: []clause.Column{{Name: "name"}, {Name: "email"}, {Name: "age"}, {Name: "id"}}, | ||||||
|  | 		Values: [][]interface{}{ | ||||||
|  | 			{"alice", "email", 18, 1}, | ||||||
|  | 			{"bob", "email", 19, 2}, | ||||||
|  | 		}, | ||||||
|  | 	} | ||||||
|  | 	if !reflect.DeepEqual(expected, values) { | ||||||
|  | 		t.Errorf("expected: %v got %v", expected, values) | ||||||
|  | 	} | ||||||
|  | } | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Jinghao Lu
						Jinghao Lu