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
- Check the wrapped error for a duplicate-key violation; if so, the row was inserted concurrently and the migration can be re-run
- Verify INSERT privilege for the DB user on the config table
- Check free disk space and DB health
- 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
- Run migrations from a single instance (avoid multi-node concurrent upgrades)
- Verify INSERT privileges on the config table
- Check for duplicate-key errors in the wrapped cause before panicking
- Keep enough free disk space on the DB host
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
- add config failed: %w
- %s failed: %s
- sync version failed: %v
- get config failed: %w
- update config failed: %w
AI-assisted analysis of apache/answer@3b9f137061 (2026-09-05).
Data as JSON: /api/errors/59ca8fa625454d80.
Report an issue: GitHub.