Allow use NULL as default value for string, close #3363

This commit is contained in:
Jinzhu 2020-09-02 20:15:12 +08:00
parent 680dda2c15
commit dbe0f4d8d7
2 changed files with 2 additions and 1 deletions

View File

@ -201,7 +201,7 @@ func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field {
case reflect.String: case reflect.String:
field.DataType = String field.DataType = String
isFunc := strings.Contains(field.DefaultValue, "(") && isFunc := strings.Contains(field.DefaultValue, "(") &&
strings.Contains(field.DefaultValue, ")") strings.Contains(field.DefaultValue, ")") || strings.ToLower(field.DefaultValue) == "null"
if field.HasDefaultValue && !isFunc { if field.HasDefaultValue && !isFunc {
field.DefaultValue = strings.Trim(field.DefaultValue, "'") field.DefaultValue = strings.Trim(field.DefaultValue, "'")

View File

@ -13,6 +13,7 @@ func TestDefaultValue(t *testing.T) {
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:''"` Name3 string `gorm:"size:233;not null;default:''"`
Name4 string `gorm:"size:233;default:null"`
Age int `gorm:"default:18"` Age int `gorm:"default:18"`
Enabled bool `gorm:"default:true"` Enabled bool `gorm:"default:true"`
} }