apache/answer · error

get tag rel failed: %w

Error message

get tag rel failed: %w

What it means

Wraps an xorm Find error while loading all tag relations (entity.TagRel) at the start of updateTagCount, which recomputes tag question counts. If the tag_rel table cannot be read, the migration step aborts with this wrapped error.

Source

Thrown at internal/migrations/v13.go:178

		_, 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)
	err := x.Context(ctx).Find(&tagRelList, &entity.TagRel{})
	if err != nil {
		return fmt.Errorf("get tag rel failed: %w", err)
	}
	questionIDs := make([]string, 0)
	questionsAvailableMap := make(map[string]bool)
	questionsHideMap := make(map[string]bool)
	for _, item := range tagRelList {
		questionIDs = append(questionIDs, item.ObjectID)
		questionsAvailableMap[item.ObjectID] = false
		questionsHideMap[item.ObjectID] = false
	}
	questionList := make([]QuestionV13, 0)
	err = x.Context(ctx).In("id", questionIDs).And(builder.Lt{"question.status": entity.QuestionStatusDeleted}).Find(&questionList, &QuestionV13{})
	if err != nil {
		return fmt.Errorf("get questions failed: %w", err)
	}
	for _, question := range questionList {
		_, ok := questionsAvailableMap[question.ID]
		if ok {
			questionsAvailableMap[question.ID] = true

View on GitHub (pinned to 3b9f137061)

Solutions

  1. Read the wrapped cause to identify the driver error
  2. Verify tag_rel table exists and matches entity.TagRel fields
  3. Re-run the migration after restoring connectivity
  4. Run against the primary DB, not a read replica, for migrations

Example fix

// before
err := x.Context(ctx).Find(&tagRelList, &entity.TagRel{})
// after
if err := x.Context(ctx).Find(&tagRelList, &entity.TagRel{}); err != nil {
    return fmt.Errorf("get tag rel failed: %w", err)
}
Defensive patterns

Strategy: try-catch

Validate before calling

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

Try / catch

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

Prevention

When it happens

Trigger: x.Context(ctx).Find(&tagRelList, &entity.TagRel{}) failing due to missing tag_rel table, connection loss, schema drift between entity.TagRel and the table, or cancelled context.

Common situations: Databases upgraded manually where tag_rel schema lags the code; connection pool exhausted during long migration runs; read replicas lagging or unavailable.

Related errors


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