apache/answer · error

get tag failed: %w

Error message

get tag  failed: %w

What it means

Wraps an xorm Find error while loading all tags (entity.Tag) in updateTagCount, prior to writing the recomputed question_count. A failure reading the tag table aborts the migration step; the driver error is preserved via %w.

Source

Thrown at internal/migrations/v13.go:237

	// select tag count
	newTagRelList := make([]entity.TagRel, 0)
	err = x.Context(ctx).Find(&newTagRelList, &entity.TagRel{Status: entity.TagRelStatusAvailable})
	if err != nil {
		return fmt.Errorf("get tag rel failed: %w", err)
	}
	tagCountMap := make(map[string]int)
	for _, v := range newTagRelList {
		_, ok := tagCountMap[v.TagID]
		if !ok {
			tagCountMap[v.TagID] = 1
		} else {
			tagCountMap[v.TagID]++
		}
	}
	TagList := make([]entity.Tag, 0)
	err = x.Context(ctx).Find(&TagList, &entity.Tag{})
	if err != nil {
		return fmt.Errorf("get tag  failed: %w", err)
	}
	for _, tag := range TagList {
		_, ok := tagCountMap[tag.ID]
		if ok {
			tag.QuestionCount = tagCountMap[tag.ID]
			if _, err = x.Context(ctx).Update(tag, &entity.Tag{ID: tag.ID}); err != nil {
				log.Errorf("update %+v tag failed: %s", tag.ID, err)
				return fmt.Errorf("update tag failed: %w", err)
			}
		} else {
			tag.QuestionCount = 0
			if _, err = x.Context(ctx).Cols("question_count").Update(tag, &entity.Tag{ID: tag.ID}); err != nil {
				log.Errorf("update %+v tag failed: %s", tag.ID, err)
				return fmt.Errorf("update tag failed: %w", err)
			}
		}
	}
	return nil

View on GitHub (pinned to 3b9f137061)

Solutions

  1. Check the wrapped cause for table-missing vs connection vs permission errors
  2. Run all prior migrations so the tag table exists with the expected schema
  3. Re-run the migration on a healthy connection
  4. Grant the migration DB user SELECT on the tag table

Example fix

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

Strategy: try-catch

Validate before calling

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

Try / catch

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

Prevention

When it happens

Trigger: x.Context(ctx).Find(&TagList, &entity.Tag{}) failing because the tag table is missing, the connection dropped, entity.Tag fields mismatch the table, or the context was cancelled.

Common situations: Tag table missing on databases where earlier migrations did not run; connectivity loss during a long migration; permission issues preventing SELECT on the tag table.

Related errors


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