apache/answer · error

get answers failed: %w

Error message

get answers failed: %w

What it means

Wraps an xorm Find error returned while loading all available answers (AnswerV13 rows) during the v13 migration's updateQuestionCount step. The migration needs to recompute each question's answer count from existing answers; if the answers table cannot be read (connection failure, missing table, bad schema), the migration aborts with this wrapped error. It is a pass-through of the underlying database error.

Source

Thrown at internal/migrations/v13.go:143

		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 {
			questionAnswerCount[answer.QuestionID]++
		}
	}
	questionList := make([]QuestionV13, 0)
	err = x.Context(ctx).Find(&questionList, &QuestionV13{})
	if err != nil {
		return fmt.Errorf("get questions failed: %w", err)
	}
	for _, item := range questionList {
		_, ok := questionAnswerCount[item.ID]
		if ok {

View on GitHub (pinned to 3b9f137061)

Solutions

  1. Check the wrapped cause (%w chain) for the real driver error — fix connectivity, credentials, or missing table first
  2. Verify the answer table exists with the schema expected by AnswerV13 (migrations must run sequentially)
  3. Re-run the migration on a healthy connection; migrations are idempotent up to the failed step
  4. Increase context timeout or run the migration with a background context so it is not cancelled

Example fix

// before
err := x.Context(ctx).Find(&answers, &AnswerV13{Status: entity.AnswerStatusAvailable})
// after
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
err := x.Context(ctx).Find(&answers, &AnswerV13{Status: entity.AnswerStatusAvailable})
Defensive patterns

Strategy: try-catch

Validate before calling

// before running migration
if exists, _ := x.IsTableExist(&AnswerV13{}); !exists {
    return fmt.Errorf("answer table missing; run prior migrations first")
}

Try / catch

if err := updateQuestionCount(ctx, engine); err != nil {
    var dbErr xorm.ErrNotExist
    if errors.As(err, &dbErr) { /* table missing: run schema migrations */ }
    log.Errorf("migration v13 failed: %v", err)
    return err
}

Prevention

When it happens

Trigger: Calling x.Context(ctx).Find(&answers, &AnswerV13{Status: entity.AnswerStatusAvailable}) when the answer table is missing, the DB connection is dropped mid-migration, the table schema predates expected columns, or the context is cancelled during the query.

Common situations: Running the v13 migration against a database where the answers table was renamed or dropped; network blips to a remote MySQL/Postgres during long migrations; context timeouts when migrations run under a request-scoped context; partial upgrade states where schema changes were applied manually out of order.

Related errors


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