apache/answer · error

get questions failed: %w

Error message

get questions failed: %w

What it means

Wraps an xorm Find error while loading all questions (QuestionV13) in updateQuestionCount, so the migration can apply the recomputed answer counts. Thrown when the question table cannot be queried. The underlying driver error is preserved via %w.

Source

Thrown at internal/migrations/v13.go:157

	// 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 {
			item.AnswerCount = questionAnswerCount[item.ID]
			if _, err = x.Context(ctx).Cols("answer_count").Update(item, &QuestionV13{ID: item.ID}); err != nil {
				log.Errorf("update %+v config failed: %s", item, err)
				return fmt.Errorf("update question failed: %w", err)
			}
		}
	}

	return nil
}

// updateTagCount update tag count
func updateTagCount(ctx context.Context, x *xorm.Engine) error {
	tagRelList := make([]entity.TagRel, 0)

View on GitHub (pinned to 3b9f137061)

Solutions

  1. Inspect the wrapped error to identify the driver-level cause
  2. Ensure prior migrations created/evolved the question table to match QuestionV13
  3. Re-run on a stable DB connection with a non-request-scoped context
  4. Verify DB user has SELECT permission on the question table

Example fix

// before
err = x.Context(ctx).Find(&questionList, &QuestionV13{})
// after
if err = x.Context(ctx).Find(&questionList, &QuestionV13{}); err != nil {
    log.Errorf("find questions failed: %v", err)
    return fmt.Errorf("get questions failed: %w", err)
}
Defensive patterns

Strategy: try-catch

Validate before calling

if exists, _ := x.IsTableExist(&QuestionV13{}); !exists {
    return fmt.Errorf("question table missing; run prior migrations first")
}

Try / catch

if err := updateQuestionCount(ctx, engine); err != nil {
    log.Errorf("question count migration failed: %v", err)
    return err
}

Prevention

When it happens

Trigger: x.Context(ctx).Find(&questionList, &QuestionV13{}) failing due to a dropped connection, missing question table, schema mismatch with QuestionV13 fields, or cancelled context.

Common situations: Long migrations losing their DB connection before reaching this step; manual schema edits breaking the question table; running migration against a fresh/empty DB with tables not yet created; context cancellation.

Related errors


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