jmoiron/sqlx · error
missing destination name %s in %T
Error message
missing destination name %s in %T
What it means
During Rows.StructScan, sqlx maps result-set column names to fields of the destination struct via the reflectx mapper. If a returned column has no matching (tagged or name-matching) exported struct field and the mapper is not in unsafe mode, the scan is aborted with this error. It is a contract check that every selected column has somewhere to go in the struct.
Source
Thrown at sqlx.go:621
v := reflect.ValueOf(dest)
if v.Kind() != reflect.Ptr {
return errors.New("must pass a pointer, not a value, to StructScan destination")
}
v = v.Elem()
if !r.started {
columns, err := r.Columns()
if err != nil {
return err
}
m := r.Mapper
r.fields = m.TraversalsByName(v.Type(), columns)
// if we are not unsafe and are missing fields, return an error
if f, err := missingFields(r.fields); err != nil && !r.unsafe {
return fmt.Errorf("missing destination name %s in %T", columns[f], dest)
}
r.values = make([]interface{}, len(columns))
r.started = true
}
err := fieldsByTraversal(v, r.fields, r.values, true)
if err != nil {
return err
}
// scan into the struct field pointers and append to our results
err = r.Scan(r.values...)
if err != nil {
return err
}
return r.Err()
}
// Connect to a database and verify with a ping.View on GitHub (pinned to 41dac167fd)
Solutions
- Add or fix `db:"column_name"` tags on the struct fields for the unmatched columns.
- Rename either the struct field or the SQL column alias (e.g. SELECT user_id AS id) so names match.
- Run the query with db.Unsafe() so unmapped columns are silently ignored.
- Drop the extra column from the SELECT list or select explicit columns instead of *.
Example fix
// before
type User struct {
Name string `db:"name"`
}
db.Select(&users, "SELECT id, name FROM users")
// after
type User struct {
ID int `db:"id"`
Name string `db:"name"`
}
db.Select(&users, "SELECT id, name FROM users") Defensive patterns
Strategy: validation
Validate before calling
cols, _ := rows.Columns()
for _, c := range cols {
if _, ok := structFieldFor(c, reflect.TypeOf(dest)); !ok {
return fmt.Errorf("column %q has no matching struct field", c)
}
} Type guard
func isStructPtr(dest interface{}) bool {
t := reflect.TypeOf(dest)
return t != nil && t.Kind() == reflect.Ptr && t.Elem().Kind() == reflect.Struct
} Prevention
- Always tag struct fields with db:"column_name" explicitly.
- Run a startup test that scans every query against its destination struct.
- Select explicit columns instead of SELECT *.
- Keep struct definitions in sync with schema migrations.
When it happens
Trigger: Calling rows.StructScan(dest) (via the internal Fields scanner) where m.TraversalsByName finds no struct field for at least one column in the current result set, while r.unsafe is false (default).
Common situations: Adding a column to a SELECT without updating the struct; using DB column names that don't match struct fields and forgetting `db:"column_name"` tags; SELECT * on a table whose schema changed; struct fields being unexported so the mapper can't see them.
Related errors
- expected a struct, but struct %s has no exported fields
- must pass a pointer, not a value, to StructScan destination
- expected %s but got %s
- structscan expects a struct dest but the provided struct typ
- empty slice passed to 'in' query
AI-assisted analysis of jmoiron/sqlx@41dac167fd (2026-09-03).
Data as JSON: /api/errors/588837d02c8f0139.
Report an issue: GitHub.