forked from mirror/gorm
Test set string field's default value to blank string
This commit is contained in:
parent
c888560a0e
commit
af632199cf
|
@ -64,7 +64,7 @@ func (m Migrator) FullDataTypeOf(field *schema.Field) (expr clause.Expr) {
|
||||||
expr.SQL += " UNIQUE"
|
expr.SQL += " UNIQUE"
|
||||||
}
|
}
|
||||||
|
|
||||||
if field.HasDefaultValue && field.DefaultValue != "" {
|
if field.HasDefaultValue && (field.DefaultValueInterface != nil || field.DefaultValue != "") {
|
||||||
if field.DefaultValueInterface != nil {
|
if field.DefaultValueInterface != nil {
|
||||||
defaultStmt := &gorm.Statement{Vars: []interface{}{field.DefaultValueInterface}}
|
defaultStmt := &gorm.Statement{Vars: []interface{}{field.DefaultValueInterface}}
|
||||||
m.Dialector.BindVarTo(defaultStmt, defaultStmt, field.DefaultValueInterface)
|
m.Dialector.BindVarTo(defaultStmt, defaultStmt, field.DefaultValueInterface)
|
||||||
|
|
|
@ -12,6 +12,7 @@ func TestDefaultValue(t *testing.T) {
|
||||||
Email string `gorm:"not null;index:,unique"`
|
Email string `gorm:"not null;index:,unique"`
|
||||||
Name string `gorm:"not null;default:'foo'"`
|
Name string `gorm:"not null;default:'foo'"`
|
||||||
Name2 string `gorm:"size:233;not null;default:'foo'"`
|
Name2 string `gorm:"size:233;not null;default:'foo'"`
|
||||||
|
Name3 string `gorm:"size:233;not null;default:''"`
|
||||||
Age int `gorm:"default:18"`
|
Age int `gorm:"default:18"`
|
||||||
Enabled bool `gorm:"default:true"`
|
Enabled bool `gorm:"default:true"`
|
||||||
}
|
}
|
||||||
|
@ -25,14 +26,14 @@ func TestDefaultValue(t *testing.T) {
|
||||||
var harumph = Harumph{Email: "hello@gorm.io"}
|
var harumph = Harumph{Email: "hello@gorm.io"}
|
||||||
if err := DB.Create(&harumph).Error; err != nil {
|
if err := DB.Create(&harumph).Error; err != nil {
|
||||||
t.Fatalf("Failed to create data with default value, got error: %v", err)
|
t.Fatalf("Failed to create data with default value, got error: %v", err)
|
||||||
} else if harumph.Name != "foo" || harumph.Name2 != "foo" || harumph.Age != 18 || !harumph.Enabled {
|
} else if harumph.Name != "foo" || harumph.Name2 != "foo" || harumph.Name3 != "" || harumph.Age != 18 || !harumph.Enabled {
|
||||||
t.Fatalf("Failed to create data with default value, got: %+v", harumph)
|
t.Fatalf("Failed to create data with default value, got: %+v", harumph)
|
||||||
}
|
}
|
||||||
|
|
||||||
var result Harumph
|
var result Harumph
|
||||||
if err := DB.First(&result, "email = ?", "hello@gorm.io").Error; err != nil {
|
if err := DB.First(&result, "email = ?", "hello@gorm.io").Error; err != nil {
|
||||||
t.Fatalf("Failed to find created data, got error: %v", err)
|
t.Fatalf("Failed to find created data, got error: %v", err)
|
||||||
} else if result.Name != "foo" || result.Name2 != "foo" || result.Age != 18 || !result.Enabled {
|
} else if result.Name != "foo" || result.Name2 != "foo" || result.Name3 != "" || result.Age != 18 || !result.Enabled {
|
||||||
t.Fatalf("Failed to find created data with default data, got %+v", result)
|
t.Fatalf("Failed to find created data with default data, got %+v", result)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue