{"record":{"id":"e66b49f3634d7a60","repo":"kataras/iris","slug":"sqlx-bind-table-q-unexpected-number-of-result","errorCode":null,"errorMessage":"sqlx: bind: table: %q: unexpected number of result columns: %d: expected: %d","messagePattern":"sqlx: bind: table: %q: unexpected number of result columns: (.+?): expected: (.+?)","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"x/sqlx/sqlx.go","lineNumber":139,"sourceCode":"\ttyp = typ.Elem()\n\n\toriginalKind := typ.Kind()\n\tif typ.Kind() == reflect.Slice {\n\t\ttyp = typ.Elem()\n\t}\n\n\tr, ok := s.Rows[typ]\n\tif !ok {\n\t\treturn fmt.Errorf(\"sqlx: bind: unregistered type: %q\", typ.String())\n\t}\n\n\tcolumnTypes, err := src.ColumnTypes()\n\tif err != nil {\n\t\treturn fmt.Errorf(\"sqlx: bind: table: %q: %w\", r.Name, err)\n\t}\n\n\tif expected, got := len(r.Columns), len(columnTypes); expected != got {\n\t\treturn fmt.Errorf(\"sqlx: bind: table: %q: unexpected number of result columns: %d: expected: %d\", r.Name, got, expected)\n\t}\n\n\tval := reflex.IndirectValue(reflect.ValueOf(dst))\n\tif s.AutoCloseRows {\n\t\tdefer src.Close()\n\t}\n\n\tswitch originalKind {\n\tcase reflect.Struct:\n\t\tif src.Next() {\n\t\t\tif err = r.bindSingle(typ, val, columnTypes, src); err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t} else {\n\t\t\treturn sql.ErrNoRows\n\t\t}\n\n\t\treturn src.Err()","sourceCodeStart":121,"sourceCodeEnd":157,"githubUrl":"https://github.com/kataras/iris/blob/7bedaf55a0b64bbb2248a5845a2c60d81a30996a/x/sqlx/sqlx.go#L121-L157","documentation":"Bind validates that the SQL result set matches the Row definition before mapping values into the destination. When the live query returns a different number of columns than the columns declared/registered for the table, it fails with this error. It guards against silent misalignment between struct mapping and actual query output.","triggerScenarios":"Calling Query (which calls Bind) with a destination bound to a table whose registered column count differs from the SELECT list — e.g. selecting a subset of columns, adding an expression column, or the table schema changed after registration.","commonSituations":"Writing `SELECT id, name` but the Row was registered against a struct with more fields; a migration added/dropped a column; using `SELECT *` after a schema change; joining tables and forgetting to alias/trim columns.","solutions":["Make the query's SELECT list match the registered columns exactly (select all columns of the struct, in any order but same count)","Re-register the Row so its Columns reflect the current schema/query","Avoid SELECT * if the struct intentionally maps a subset; enumerate columns explicitly","If using a DB migration, run migrations so the runtime schema matches registration"],"exampleFix":"// before\nrows, err := db.Query(row, &users, \"SELECT id, name FROM users\")\n// after\nrows, err := db.Query(row, &users, \"SELECT id, name, email, created_at FROM users\")","handlingStrategy":"validation","validationCode":"cols := row.Columns\nif len(cols) != expectedCountFromQuery { return fmt.Errorf(\"query columns %d != registered %d\", expectedCountFromQuery, len(cols)) }","typeGuard":null,"tryCatchPattern":"if err := db.Query(row, &dst, q); err != nil { if strings.Contains(err.Error(), \"unexpected number of result columns\") { /* log query + registered columns */ } return err }","preventionTips":["Enumerate columns explicitly in SELECT instead of *","Keep Row registration in sync with migrations","Add an integration test asserting query columns match struct fields"],"tags":["sql","reflection","schema-mismatch"],"backgroundTag":"schema-column-count-mismatch","analyzedSha":"7bedaf55a0b64bbb2248a5845a2c60d81a30996a","analyzedAt":"2026-08-30T20:38:16.250Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}