Should ignore error when parsing default value for time, close #5176

This commit is contained in:
Jinzhu 2022-03-20 08:55:08 +08:00
parent 540b47571a
commit 0097b39a77
3 changed files with 4 additions and 4 deletions

View File

@ -260,8 +260,8 @@ func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field {
field.DataType = Time field.DataType = Time
} }
if field.HasDefaultValue && !skipParseDefaultValue && field.DataType == Time { if field.HasDefaultValue && !skipParseDefaultValue && field.DataType == Time {
if field.DefaultValueInterface, err = now.Parse(field.DefaultValue); err != nil { if t, err := now.Parse(field.DefaultValue); err == nil {
schema.err = fmt.Errorf("failed to parse default value `%v` for field %v", field.DefaultValue, field.Name) field.DefaultValueInterface = t
} }
} }
case reflect.Array, reflect.Slice: case reflect.Array, reflect.Slice:

View File

@ -14,7 +14,7 @@ require (
gorm.io/driver/postgres v1.3.1 gorm.io/driver/postgres v1.3.1
gorm.io/driver/sqlite v1.3.1 gorm.io/driver/sqlite v1.3.1
gorm.io/driver/sqlserver v1.3.1 gorm.io/driver/sqlserver v1.3.1
gorm.io/gorm v1.23.1 gorm.io/gorm v1.23.3
) )
replace gorm.io/gorm => ../ replace gorm.io/gorm => ../

View File

@ -19,7 +19,7 @@ func TestPostgres(t *testing.T) {
Name string `gorm:"check:name_checker,name <> ''"` Name string `gorm:"check:name_checker,name <> ''"`
Test uuid.UUID `gorm:"type:uuid;not null;default:gen_random_uuid()"` Test uuid.UUID `gorm:"type:uuid;not null;default:gen_random_uuid()"`
CreatedAt time.Time `gorm:"type:TIMESTAMP WITHOUT TIME ZONE"` CreatedAt time.Time `gorm:"type:TIMESTAMP WITHOUT TIME ZONE"`
UpdatedAt time.Time `gorm:"type:TIMESTAMP WITHOUT TIME ZONE"` UpdatedAt time.Time `gorm:"type:TIMESTAMP WITHOUT TIME ZONE;default:current_timestamp"`
Things pq.StringArray `gorm:"type:text[]"` Things pq.StringArray `gorm:"type:text[]"`
} }