{"record":{"id":"b780f4e94c979646","repo":"go-gorm/gorm","slug":"failed-to-unmarshal-gob-value-v","errorCode":null,"errorMessage":"failed to unmarshal gob value: %#v","messagePattern":"failed to unmarshal gob value: %#v","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"schema/serializer.go","lineNumber":173,"sourceCode":"\t\terr = fmt.Errorf(\"invalid field type %#v for UnixSecondSerializer, only int, uint supported\", fieldValue)\n\t}\n\treturn\n}\n\n// GobSerializer gob serializer\ntype GobSerializer struct{}\n\n// Scan implements serializer interface\nfunc (GobSerializer) Scan(ctx context.Context, field *Field, dst reflect.Value, dbValue interface{}) (err error) {\n\tfieldValue := reflect.New(field.FieldType)\n\n\tif dbValue != nil {\n\t\tvar bytesValue []byte\n\t\tswitch v := dbValue.(type) {\n\t\tcase []byte:\n\t\t\tbytesValue = v\n\t\tdefault:\n\t\t\treturn fmt.Errorf(\"failed to unmarshal gob value: %#v\", dbValue)\n\t\t}\n\t\tif len(bytesValue) > 0 {\n\t\t\tdecoder := gob.NewDecoder(bytes.NewBuffer(bytesValue))\n\t\t\terr = decoder.Decode(fieldValue.Interface())\n\t\t}\n\t}\n\tfield.ReflectValueOf(ctx, dst).Set(fieldValue.Elem())\n\treturn\n}\n\n// Value implements serializer interface\nfunc (GobSerializer) Value(ctx context.Context, field *Field, dst reflect.Value, fieldValue interface{}) (interface{}, error) {\n\tbuf := new(bytes.Buffer)\n\terr := gob.NewEncoder(buf).Encode(fieldValue)\n\treturn buf.Bytes(), err\n}\n","sourceCodeStart":155,"sourceCodeEnd":190,"githubUrl":"https://github.com/go-gorm/gorm/blob/1d6ce99528060be18a42be09aca8d39efcb47f28/schema/serializer.go#L155-L190","documentation":"GobSerializer.Scan decodes database bytes back into the struct field using encoding/gob. It only accepts dbValue of type []byte; any other driver value (string, nil-adjacent types, int) fails fast with 'failed to unmarshal gob value' showing the received value. Note: SQL NULL (nil) is allowed and skips decoding; the failure is for non-nil, non-[]byte values.","triggerScenarios":"A `gorm:\"serializer:gob\"` column read back as a string (some drivers/TEXT columns return string), or a query casting the column (e.g. SELECT CAST(col AS CHAR)) so the driver no longer returns bytes.","commonSituations":"Switching drivers (MySQL TEXT vs BLOB, SQLite text affinity) where the same column arrives as string; raw SQL projections changing column types; proxy/middleware rewriting results.","solutions":["Ensure the column type returns bytes — use BLOB/VARBINARY rather than TEXT/CHAR.","Avoid casting the column in raw SQL selects.","If the driver insists on strings, switch that field to a custom serializer that converts string → []byte before gob-decoding."],"exampleFix":"// before\nMeta map[string]any `gorm:\"serializer:gob;type:text\"` // driver returns string\n\n// after\nMeta map[string]any `gorm:\"serializer:gob;type:blob\"`","handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":"if err := db.Find(&items).Error; err != nil {\n    if strings.Contains(err.Error(), \"failed to unmarshal gob value\") {\n        return fmt.Errorf(\"gob column came back as %T; ensure column type is binary (BLOB) and not cast in SQL\", err)\n    }\n    return err\n}","preventionTips":["Declare gob-serialized columns as binary types (BLOB/VARBINARY), not TEXT.","Never CAST gob columns in raw SQL selects.","When switching drivers, round-trip test gob columns — driver return types differ.","If a driver returns string, write a small wrapper serializer converting string→[]byte before gob decoding."],"tags":["gorm","serializer","gob","type-mismatch","driver"],"backgroundTag":null,"analyzedSha":"1d6ce99528060be18a42be09aca8d39efcb47f28","analyzedAt":"2026-08-15T13:01:23.433Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}