Fix create with default value
This commit is contained in:
		
							parent
							
								
									4cbd99aa94
								
							
						
					
					
						commit
						dcdcc6fedc
					
				| @ -65,10 +65,10 @@ func (m Migrator) FullDataTypeOf(field *schema.Field) (expr clause.Expr) { | ||||
| 	} | ||||
| 
 | ||||
| 	if field.HasDefaultValue && field.DefaultValue != "" { | ||||
| 		if field.DataType == schema.String && field.DefaultValueInterface != nil { | ||||
| 			defaultStmt := &gorm.Statement{Vars: []interface{}{field.DefaultValue}} | ||||
| 			m.Dialector.BindVarTo(defaultStmt, defaultStmt, field.DefaultValue) | ||||
| 			expr.SQL += " DEFAULT " + m.Dialector.Explain(defaultStmt.SQL.String(), field.DefaultValue) | ||||
| 		if field.DefaultValueInterface != nil { | ||||
| 			defaultStmt := &gorm.Statement{Vars: []interface{}{field.DefaultValueInterface}} | ||||
| 			m.Dialector.BindVarTo(defaultStmt, defaultStmt, field.DefaultValueInterface) | ||||
| 			expr.SQL += " DEFAULT " + m.Dialector.Explain(defaultStmt.SQL.String(), field.DefaultValueInterface) | ||||
| 		} else { | ||||
| 			expr.SQL += " DEFAULT " + field.DefaultValue | ||||
| 		} | ||||
|  | ||||
| @ -9,10 +9,11 @@ import ( | ||||
| func TestDefaultValue(t *testing.T) { | ||||
| 	type Harumph struct { | ||||
| 		gorm.Model | ||||
| 		Email string `gorm:"not null;"` | ||||
| 		Name  string `gorm:"not null;default:foo"` | ||||
| 		Name2 string `gorm:"not null;default:'foo'"` | ||||
| 		Age   int    `gorm:"default:18"` | ||||
| 		Email   string `gorm:"not null;index:,unique"` | ||||
| 		Name    string `gorm:"not null;default:'foo'"` | ||||
| 		Name2   string `gorm:"not null;default:'foo'"` | ||||
| 		Age     int    `gorm:"default:18"` | ||||
| 		Enabled bool   `gorm:"default:true"` | ||||
| 	} | ||||
| 
 | ||||
| 	DB.Migrator().DropTable(&Harumph{}) | ||||
| @ -24,14 +25,14 @@ func TestDefaultValue(t *testing.T) { | ||||
| 	var harumph = Harumph{Email: "hello@gorm.io"} | ||||
| 	if err := DB.Create(&harumph).Error; err != nil { | ||||
| 		t.Fatalf("Failed to create data with default value, got error: %v", err) | ||||
| 	} else if harumph.Name != "foo" || harumph.Name2 != "foo" || harumph.Age != 18 { | ||||
| 	} else if harumph.Name != "foo" || harumph.Name2 != "foo" || harumph.Age != 18 || !harumph.Enabled { | ||||
| 		t.Fatalf("Failed to create data with default value, got: %+v", harumph) | ||||
| 	} | ||||
| 
 | ||||
| 	var result Harumph | ||||
| 	if err := DB.First(&result, "email = ?", "hello@gorm.io").Error; err != nil { | ||||
| 		t.Fatalf("Failed to find created data, got error: %v", err) | ||||
| 	} else if result.Name != "foo" || result.Name2 != "foo" || result.Age != 18 { | ||||
| 	} else if result.Name != "foo" || result.Name2 != "foo" || result.Age != 18 || !result.Enabled { | ||||
| 		t.Fatalf("Failed to find created data with default data, got %+v", result) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| @ -14,3 +14,5 @@ require ( | ||||
| ) | ||||
| 
 | ||||
| replace gorm.io/gorm => ../ | ||||
| 
 | ||||
| replace gorm.io/driver/sqlserver => /Users/jinzhu/Projects/sqlserver | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Jinzhu
						Jinzhu