{"record":{"id":"ba788a20ce196993","repo":"jmoiron/sqlx","slug":"missing-field","errorCode":null,"errorMessage":"missing field","messagePattern":"missing field","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"sqlx.go","lineNumber":1050,"sourceCode":"\tfor i, traversal := range traversals {\n\t\tif len(traversal) == 0 {\n\t\t\tvalues[i] = new(interface{})\n\t\t\tcontinue\n\t\t}\n\t\tf := reflectx.FieldByIndexes(v, traversal)\n\t\tif ptrs {\n\t\t\tvalues[i] = f.Addr().Interface()\n\t\t} else {\n\t\t\tvalues[i] = f.Interface()\n\t\t}\n\t}\n\treturn nil\n}\n\nfunc missingFields(transversals [][]int) (field int, err error) {\n\tfor i, t := range transversals {\n\t\tif len(t) == 0 {\n\t\t\treturn i, errors.New(\"missing field\")\n\t\t}\n\t}\n\treturn 0, nil\n}\n","sourceCodeStart":1032,"sourceCodeEnd":1055,"githubUrl":"https://github.com/jmoiron/sqlx/blob/41dac167fdad5e3fd81d66cafba0951dc6823a30/sqlx.go#L1032-L1055","documentation":"missingFields iterates over reflectx traversals (column-to-field index paths) and fires when any traversal is empty, meaning a returned column could not be matched to a field in the destination struct. sqlx refuses to scan rows where columns lack a corresponding exported struct field, so partial/ambiguous mapping fails loudly instead of silently dropping data.","triggerScenarios":"StructScan/Get/Select with a query returning a column whose name (after db tag mapping) matches no field of the destination struct — e.g. column `user_id` with no `user_id`/`userID` field or `db:\"user_id\"` tag.","commonSituations":"Schema changed to add a column the struct does not have; SELECT * returning extra columns; missing or misspelled `db` struct tags; unexported fields (cannot be mapped); column-name case mismatches with default MapperFunc (lowercase).","solutions":["Add the missing column to the struct (exported field, correct `db:\"column_name\"` tag).","Restrict the SELECT to only the columns the struct defines.","Ensure the field is exported and its `db` tag matches the column name exactly.","Customize the mapper (sqlx.NameMapper / db.Mapper) if naming conventions differ."],"exampleFix":"// before\ntype User struct {\n    Name string `db:\"name\"`\n}\n// SELECT id, name FROM users -> \"missing field\"\n// after\ntype User struct {\n    ID   int    `db:\"id\"`\n    Name string `db:\"name\"`\n}","handlingStrategy":"validation","validationCode":"cols, _ := rows.Columns()\nfor _, c := range cols {\n\tif !structHasField(MyStruct{}, c) {\n\t\tlog.Printf(\"column %q has no struct field\", c)\n\t}\n}","typeGuard":null,"tryCatchPattern":"if err := rows.StructScan(&dest); err != nil {\n\tif strings.HasPrefix(err.Error(), \"missing field\") {\n\t\t// log rows.Columns() vs struct db tags and align them\n\t}\n\treturn err\n}","preventionTips":["Keep SELECT column lists explicit and matched to struct fields.","Add `db:\"column_name\"` tags to every scanned field.","Run a startup smoke test that StructScans one row per query.","Re-sync struct definitions when the schema migrates.","Use SELECT * only with structs that mirror the whole table."],"tags":["sql","reflection","scan","schema-mismatch"],"backgroundTag":"column-has-no-struct-field","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"}