{"record":{"id":"7a9aff63ebec05f5","repo":"jmoiron/sqlx","slug":"length-of-array-is-0-v","errorCode":null,"errorMessage":"length of array is 0: %#v","messagePattern":"length of array is 0: %#v","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"named.go","lineNumber":283,"sourceCode":"\t\tbuffer.WriteString(bound[openingBracketIndex:closingBracketIndex])\n\t}\n\tbuffer.WriteString(bound[closingBracketIndex:])\n\treturn buffer.String()\n}\n\n// bindArray binds a named parameter query with fields from an array or slice of\n// structs argument.\nfunc bindArray(bindType int, query string, arg interface{}, m *reflectx.Mapper) (string, []interface{}, error) {\n\t// do the initial binding with QUESTION;  if bindType is not question,\n\t// we can rebind it at the end.\n\tbound, names, err := compileNamedQuery([]byte(query), QUESTION)\n\tif err != nil {\n\t\treturn \"\", []interface{}{}, err\n\t}\n\tarrayValue := reflect.ValueOf(arg)\n\tarrayLen := arrayValue.Len()\n\tif arrayLen == 0 {\n\t\treturn \"\", []interface{}{}, fmt.Errorf(\"length of array is 0: %#v\", arg)\n\t}\n\tvar arglist = make([]interface{}, 0, len(names)*arrayLen)\n\tfor i := 0; i < arrayLen; i++ {\n\t\telemArglist, err := bindAnyArgs(names, arrayValue.Index(i).Interface(), m)\n\t\tif err != nil {\n\t\t\treturn \"\", []interface{}{}, err\n\t\t}\n\t\targlist = append(arglist, elemArglist...)\n\t}\n\tif arrayLen > 1 {\n\t\tbound = fixBound(bound, arrayLen)\n\t}\n\t// adjust binding type if we weren't on question\n\tif bindType != QUESTION {\n\t\tbound = Rebind(bindType, bound)\n\t}\n\treturn bound, arglist, nil\n}","sourceCodeStart":265,"sourceCodeEnd":301,"githubUrl":"https://github.com/jmoiron/sqlx/blob/41dac167fdad5e3fd81d66cafba0951dc6823a30/named.go#L265-L301","documentation":"When binding an array/slice argument with the array-style bind type (e.g. Postgres' expanded ?* binding), bindArray needs at least one element to expand parameters for. A zero-length array yields no parameter groups, which would produce an invalid/empty IN clause, so sqlx errors out instead of emitting broken SQL.","triggerScenarios":"Binding a slice/array via BindNamed/Named-style APIs where the mapped bind type expands arrays (e.g. BindType returns Question for slices?) and the slice has len==0 — e.g. NamedExec with an empty slice for an IN clause.","commonSituations":"SELECT ... IN (:ids) with ids from an empty filter result; slice nil/empty because an earlier query returned nothing; passing an empty array-typed struct field.","solutions":["Guard for empty slices before binding and return an empty result or use a query variant without the IN clause.","If SQL semantics allow, use a sentinel value (e.g. WHERE (:ids IS NULL) patterns) or `= ANY($1)` with pq.Array.","Ensure the slice is populated before calling the named API.","Handle the error and skip executing the query when there is nothing to bind."],"exampleFix":"// before\nvar ids []int64\nrows, _ := db.NamedQuery(\"SELECT * FROM t WHERE id IN (:ids)\", map[string]interface{}{\"ids\": ids})\n// after\nif len(ids) == 0 {\n    return nil, nil // or use always-false query\n}\nrows, _ := db.NamedQuery(\"SELECT * FROM t WHERE id IN (:ids)\", map[string]interface{}{\"ids\": ids})","handlingStrategy":"validation","validationCode":"if len(ids) == 0 {\n\treturn nil, nil // skip the IN-clause query entirely\n}","typeGuard":null,"tryCatchPattern":"q, args, err := db.BindNamed(query, arg)\nif err != nil && strings.Contains(err.Error(), \"length of array is 0\") {\n\t// empty input: return empty result instead of executing\n\treturn nil, err\n}","preventionTips":["Early-return on empty slices before building IN queries.","Use SQL that tolerates empty sets (e.g. = ANY(array) with drivers that accept it).","Never feed nil/empty slices into array-expanding bind types.","Add a guard helper like inClause(items) that returns a known-empty result."],"tags":["sql","named-parameters","array","bind","empty-slice"],"backgroundTag":"empty-slice-in-clause","analyzedSha":"41dac167fdad5e3fd81d66cafba0951dc6823a30","analyzedAt":"2026-09-03T06:10:20.382Z","contentChangedAt":"2026-09-03T06:10:20.382Z","schemaVersion":2},"datasetVersion":"2026-09-10T12:17:11.382Z"}