apache/answer · error

add config failed: %w

Error message

add config failed: %w

What it means

Returned by updateRolePinAndHideFeatures when xorm Insert() of a missing config row (ID 125/126, rank.question.show/hide) fails. This path runs when the config row does not yet exist. The driver error is wrapped with %w.

Source

Thrown at internal/migrations/v11.go:56

		{ID: 124, Key: "rank.question.unpin", Value: `-1`},
		{ID: 125, Key: "rank.question.show", Value: `-1`},
		{ID: 126, Key: "rank.question.hide", Value: `-1`},
	}
	for _, c := range defaultConfigTable {
		exist, err := x.Context(ctx).Get(&entity.Config{ID: c.ID})
		if err != nil {
			return fmt.Errorf("get config failed: %w", err)
		}
		if exist {
			if _, err = x.Context(ctx).Update(c, &entity.Config{ID: c.ID}); err != nil {
				log.Errorf("update %+v config failed: %s", c, err)
				return fmt.Errorf("update config failed: %w", err)
			}
			continue
		}
		if _, err = x.Context(ctx).Insert(&entity.Config{ID: c.ID, Key: c.Key, Value: c.Value}); err != nil {
			log.Errorf("insert %+v config failed: %s", c, err)
			return fmt.Errorf("add config failed: %w", err)
		}
	}

	return nil
}

View on GitHub (pinned to 3b9f137061)

Solutions

  1. Check the wrapped error for a duplicate-key violation; if so, the row was inserted concurrently and the migration can be re-run
  2. Verify INSERT privilege for the DB user on the config table
  3. Check free disk space and DB health
  4. Re-run the migration; Get/Insert logic is idempotent

Example fix

// before
if _, err = x.Context(ctx).Insert(&entity.Config{ID: c.ID, Key: c.Key, Value: c.Value}); err != nil {
	return fmt.Errorf("add config failed: %w", err)
}
// after (treat duplicate key as success)
if _, err = x.Context(ctx).Insert(&entity.Config{ID: c.ID, Key: c.Key, Value: c.Value}); err != nil {
	if isDuplicateKeyError(err) {
		continue
	}
	return fmt.Errorf("add config failed: %w", err)
}
Defensive patterns

Strategy: retry

Validate before calling

// ensure the row does not already exist to avoid duplicate keys
var count int64
db.QueryRow("SELECT COUNT(*) FROM config WHERE id = 125").Scan(&count)
// if count > 0 the migration already ran; safe to skip

Try / catch

if err := runMigrations(); err != nil {
	if strings.Contains(err.Error(), "add config failed") && isDuplicateKey(errors.Unwrap(err)) {
		log.Println("config row already present; treating as success")
		return nil
	}
	return err
}

Prevention

When it happens

Trigger: x.Context(ctx).Insert(&entity.Config{ID: c.ID, Key: c.Key, Value: c.Value}) returns non-nil err when the config row was absent (exist == false) in the v11 migration.

Common situations: Another node inserted the same config ID concurrently causing a duplicate-key error; ID sequence out of sync; insufficient INSERT privileges; disk full on the DB host.

Related errors


AI-assisted analysis of apache/answer@3b9f137061 (2026-09-05). Data as JSON: /api/errors/59ca8fa625454d80. Report an issue: GitHub.