apache/answer · error

add config failed: %w

Error message

add config failed: %w

What it means

Returned by addPrivilegeForInviteSomeoneToAnswer when the xorm Insert() of the new config ID 127 row (rank.answer.invite_someone_to_answer = 1000) fails. This path runs only when the config row was absent. The driver error is wrapped with %w.

Source

Thrown at internal/migrations/v13.go:132

		}
	}

	defaultConfigTable := []*entity.Config{
		{ID: 127, Key: "rank.answer.invite_someone_to_answer", Value: `1000`},
	}
	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 {
				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 {
			return fmt.Errorf("add config failed: %w", err)
		}
	}
	return nil
}

func updateQuestionCount(ctx context.Context, x *xorm.Engine) error {
	// question answer count
	answers := make([]AnswerV13, 0)
	err := x.Context(ctx).Find(&answers, &AnswerV13{Status: entity.AnswerStatusAvailable})
	if err != nil {
		return fmt.Errorf("get answers failed: %w", err)
	}
	questionAnswerCount := make(map[string]int)
	for _, answer := range answers {
		_, ok := questionAnswerCount[answer.QuestionID]
		if !ok {
			questionAnswerCount[answer.QuestionID] = 1
		} else {

View on GitHub (pinned to 3b9f137061)

Solutions

  1. Check the wrapped error for a duplicate-key violation; if present, the row exists and the migration can be re-run safely
  2. Verify INSERT privilege on the config table
  3. Check DB health (disk, connections) and re-run the migration

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 (idempotent: ignore duplicate key)
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

// pre-check whether the privilege row already exists
var count int64
db.QueryRow("SELECT COUNT(*) FROM config WHERE id = 127").Scan(&count)
// count == 0 means insert path will run

Try / catch

if err := runMigrations(); err != nil {
	if strings.Contains(err.Error(), "add config failed") && isDuplicateKey(errors.Unwrap(err)) {
		log.Println("config 127 already exists; migration effectively applied")
		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 config row 127 does not exist (exist == false) in the v13 migration.

Common situations: Concurrent insert from another instance causing duplicate-key; INSERT privilege missing; DB disk full; malformed value type for the value column.

Related errors


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