{"id":"b82f27529b3be982","repo":"go-sql-driver/mysql","slug":"unsupported-type-t-a-slice-of-s","errorCode":null,"errorMessage":"unsupported type %T, a slice of %s","messagePattern":"unsupported type %T, a slice of (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"statement.go","lineNumber":206,"sourceCode":"\t\t} else {\n\t\t\treturn c.ConvertValue(rv.Elem().Interface())\n\t\t}\n\tcase reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:\n\t\treturn rv.Int(), nil\n\tcase reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:\n\t\treturn rv.Uint(), nil\n\tcase reflect.Float32, reflect.Float64:\n\t\treturn rv.Float(), nil\n\tcase reflect.Bool:\n\t\treturn rv.Bool(), nil\n\tcase reflect.Slice:\n\t\tswitch t := rv.Type(); {\n\t\tcase t == jsonType:\n\t\t\treturn v, nil\n\t\tcase t.Elem().Kind() == reflect.Uint8:\n\t\t\treturn rv.Bytes(), nil\n\t\tdefault:\n\t\t\treturn nil, fmt.Errorf(\"unsupported type %T, a slice of %s\", v, t.Elem().Kind())\n\t\t}\n\tcase reflect.String:\n\t\treturn rv.String(), nil\n\t}\n\treturn nil, fmt.Errorf(\"unsupported type %T, a %s\", v, rv.Kind())\n}\n\nvar valuerReflectType = reflect.TypeFor[driver.Valuer]()\n\n// callValuerValue returns vr.Value(), with one exception:\n// If vr.Value is an auto-generated method on a pointer type and the\n// pointer is nil, it would panic at runtime in the panicwrap\n// method. Treat it like nil instead.\n//\n// This is so people can implement driver.Value on value types and\n// still use nil pointers to those types to mean nil/NULL, just like\n// string/*string.\n//","sourceCodeStart":188,"sourceCodeEnd":224,"githubUrl":"https://github.com/go-sql-driver/mysql/blob/c426bd93799de0f0e094c8f0582872c529d0ed0a/statement.go#L188-L224","documentation":"The driver's converter (statement.go:206) encountered a slice whose element type is not byte/uint8 and not json.RawMessage. Only []byte and json.RawMessage slices are accepted; slices of any other element type (e.g. []int, []string) cannot be transmitted as a single parameter.","triggerScenarios":"Passing []int{1,2,3}, []string{\"a\",\"b\"}, or any non-byte slice directly as a query/exec argument, often expecting IN-clause expansion or automatic serialization.","commonSituations":"Expecting the driver to expand a slice into an IN (...) clause (it does not); passing a collection expecting automatic JSON encoding; passing a typed slice from a domain model.","solutions":["For IN clauses, build the placeholders manually and pass each element as a separate argument.","Marshal non-byte slices to JSON and pass the resulting []byte.","Convert the slice to a formatted string if the SQL expects that representation."],"exampleFix":"// before — []int as a single arg\nids := []int{1, 2, 3}\ndb.Query(\"SELECT * FROM t WHERE id IN (?)\", ids)\n\n// after — expand placeholders\nph := strings.TrimSuffix(strings.Repeat(\"?,\", len(ids)), \",\")\nargs := make([]any, len(ids))\nfor i, id := range ids { args[i] = id }\ndb.Query(\"SELECT * FROM t WHERE id IN (\"+ph+\")\", args...)","handlingStrategy":"type-guard","validationCode":"func isBindableSlice(v any) bool {\n    rv := reflect.ValueOf(v)\n    if rv.Kind() != reflect.Slice { return true }\n    return rv.Type().Elem().Kind() == reflect.Uint8 ||\n        rv.Type() == reflect.TypeOf(json.RawMessage(nil))\n}","typeGuard":"func acceptableArg(v any) bool {\n    rv := reflect.ValueOf(v)\n    if rv.Kind() != reflect.Slice { return true }\n    return rv.Type().Elem().Kind() == reflect.Uint8 ||\n        rv.Type() == reflect.TypeOf(json.RawMessage(nil))\n}","tryCatchPattern":"if _, err := db.Query(q, arg); err != nil {\n    if strings.Contains(err.Error(), \"a slice of\") {\n        // expand IN-clause placeholders or marshal the slice to []byte\n    }\n}","preventionTips":["Never pass non-byte slices as parameters.","Build IN-clause placeholders explicitly rather than relying on the driver.","Marshal collections to JSON []byte when a single value is needed."],"tags":["types","slice","conversion","in-clause","api-misuse"],"analyzedSha":"c426bd93799de0f0e094c8f0582872c529d0ed0a","analyzedAt":"2026-08-04T21:52:59.219Z","schemaVersion":2}