bytebase/bytebase · error

unsupported order field %q

Error message

unsupported order field %q

What it means

getOrderByKeys parses an AIP-132 order_by string against a whitelist of allowed column names (columns map) and returns a SQL ORDER BY key list. This error is thrown when a field name in the order_by string is not present in the caller-provided whitelist, e.g. GetSavedQueryOrders for saved queries. It prevents building ORDER BY clauses from arbitrary user input.

Source

Thrown at backend/store/common.go:58

// mapping API field names to SQL columns. Strict where parseOrderBy is not:
// every comma-separated entry must be a whitelisted field with an optional
// "asc"/"desc" suffix, and a repeated field is rejected — malformed input
// errors instead of being silently reinterpreted.
func getOrderByKeys(orderBy string, columns map[string]string) ([]*OrderByKey, error) {
	if orderBy == "" {
		return nil, nil
	}

	var result []*OrderByKey
	seen := make(map[string]bool)
	for entry := range strings.SplitSeq(orderBy, ",") {
		parts := strings.Fields(entry)
		if len(parts) == 0 || len(parts) > 2 {
			return nil, errors.Errorf("invalid order_by entry %q", strings.TrimSpace(entry))
		}
		column, ok := columns[parts[0]]
		if !ok {
			return nil, errors.Errorf("unsupported order field %q", parts[0])
		}
		if seen[parts[0]] {
			return nil, errors.Errorf("duplicate order field %q", parts[0])
		}
		seen[parts[0]] = true
		sortOrder := ASC
		if len(parts) == 2 {
			switch parts[1] {
			case "asc":
			case "desc":
				sortOrder = DESC
			default:
				return nil, errors.Errorf("invalid order direction %q, expect asc or desc", parts[1])
			}
		}
		result = append(result, &OrderByKey{Key: column, SortOrder: sortOrder})
	}
	return result, nil

View on GitHub (pinned to 1870550677)

Solutions

  1. Check the columns whitelist in the calling Get*Orders function and use exactly those field names in order_by
  2. Fix typos and use the AIP-132 snake_case API field name, not the SQL column name
  3. If the field should be sortable, add it to the columns map in the caller

Example fix

// before
order_by = "created_time desc"
// after
order_by = "create_time desc"
Defensive patterns

Strategy: validation

Validate before calling

allowed := []string{"create_time", "update_time", "name"} // check the Get*Orders whitelist
for _, f := range strings.FieldsFunc(orderBy, func(r rune) bool { return r == ',' }) {
    parts := strings.Fields(strings.TrimSpace(f))
    if len(parts) == 0 || !slices.Contains(allowed, parts[0]) {
        return fmt.Errorf("unsupported order field %q", parts)
    }
}

Try / catch

keys, err := store.GetSavedQueryOrders(ctx, orderBy)
if err != nil {
    var cerr *common.Error
    if errors.As(err, &cerr) || strings.Contains(err.Error(), "unsupported order field") {
        orderBy = "" // fall back to default order
        keys, err = store.GetSavedQueryOrders(ctx, orderBy)
    }
    if err != nil { return err }
}

Prevention

When it happens

Trigger: Calling GetSavedQueryOrders (or any getOrderByKeys caller) with an order_by string containing a field not in the columns map, e.g. order_by="created_at desc" when only "create_time" is allowed, a typo like "nmae", or a raw column name instead of the API field name.

Common situations: Client code copying field names from another resource's list API, hand-writing order_by strings instead of using generated AIP-132 helpers, or API drift after a field was renamed/removed from the allowed columns.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


AI-assisted analysis of bytebase/bytebase@1870550677 (2026-09-06). Data as JSON: /api/errors/287a9be159d51bda. Report an issue: GitHub.