diff --git a/schema/serializer.go b/schema/serializer.go index 2c9db986..0a237794 100644 --- a/schema/serializer.go +++ b/schema/serializer.go @@ -122,6 +122,9 @@ func (UnixSecondSerializer) Value(ctx context.Context, field *Field, dst reflect switch v := fieldValue.(type) { case int64, int, uint, uint64, int32, uint32, int16, uint16, *int64, *int, *uint, *uint64, *int32, *uint32, *int16, *uint16: rv := reflect.ValueOf(v) + if rv.Kind() == reflect.Ptr && rv.IsNil() { + return nil, nil + } if rv.IsZero() { return time.Unix(0, 0), nil } diff --git a/tests/serializer_test.go b/tests/serializer_test.go index 230cf195..b345cc46 100644 --- a/tests/serializer_test.go +++ b/tests/serializer_test.go @@ -131,19 +131,7 @@ func TestSerializerZeroValue(t *testing.T) { t.Fatalf("no error should happen when migrate scanner, valuer struct, got error %v", err) } - createdAt := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC) - updatedAt := createdAt.Unix() - - data := SerializerStruct{ - Name: []byte("jinzhu"), - Roles: []string{"r1", "r2"}, - Contracts: map[string]interface{}{"name": "jinzhu", "age": 10}, - EncryptedString: EncryptedString("pass"), - CreatedTime: createdAt.Unix(), - UpdatedTime: &updatedAt, - JobInfo: Job{}, - CustomSerializerString: "world", - } + data := SerializerStruct{} if err := DB.Create(&data).Error; err != nil { t.Fatalf("failed to create data, got error %v", err)