{"record":{"id":"3ece562d4cd1b50d","repo":"apache/answer","slug":"update-tag-failed-w","errorCode":null,"errorMessage":"update tag failed: %w","messagePattern":"update tag failed: %w","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/migrations/v13.go","lineNumber":245,"sourceCode":"\t\t_, ok := tagCountMap[v.TagID]\n\t\tif !ok {\n\t\t\ttagCountMap[v.TagID] = 1\n\t\t} else {\n\t\t\ttagCountMap[v.TagID]++\n\t\t}\n\t}\n\tTagList := make([]entity.Tag, 0)\n\terr = x.Context(ctx).Find(&TagList, &entity.Tag{})\n\tif err != nil {\n\t\treturn fmt.Errorf(\"get tag  failed: %w\", err)\n\t}\n\tfor _, tag := range TagList {\n\t\t_, ok := tagCountMap[tag.ID]\n\t\tif ok {\n\t\t\ttag.QuestionCount = tagCountMap[tag.ID]\n\t\t\tif _, err = x.Context(ctx).Update(tag, &entity.Tag{ID: tag.ID}); err != nil {\n\t\t\t\tlog.Errorf(\"update %+v tag failed: %s\", tag.ID, err)\n\t\t\t\treturn fmt.Errorf(\"update tag failed: %w\", err)\n\t\t\t}\n\t\t} else {\n\t\t\ttag.QuestionCount = 0\n\t\t\tif _, err = x.Context(ctx).Cols(\"question_count\").Update(tag, &entity.Tag{ID: tag.ID}); err != nil {\n\t\t\t\tlog.Errorf(\"update %+v tag failed: %s\", tag.ID, err)\n\t\t\t\treturn fmt.Errorf(\"update tag failed: %w\", err)\n\t\t\t}\n\t\t}\n\t}\n\treturn nil\n}\n\n// updateUserQuestionCount update user question count\nfunc updateUserQuestionCount(ctx context.Context, x *xorm.Engine) error {\n\tquestionList := make([]QuestionV13, 0)\n\terr := x.Context(ctx).Where(builder.Lt{\"status\": entity.QuestionStatusDeleted}).Find(&questionList, &QuestionV13{})\n\tif err != nil {\n\t\treturn fmt.Errorf(\"get question  failed: %w\", err)","sourceCodeStart":227,"sourceCodeEnd":263,"githubUrl":"https://github.com/apache/answer/blob/3b9f1370612e690a0b7f230f05e688930db4c6d3/internal/migrations/v13.go#L227-L263","documentation":"Wraps the xorm Update error when persisting a tag's recomputed question_count in the main branch of updateTagCount (tags that have available tag relations). The row UPDATE failed, the tag ID is logged, and the migration aborts.","triggerScenarios":"x.Context(ctx).Update(tag, &entity.Tag{ID: tag.ID}) failing mid-loop due to connection drop, missing question_count column, row lock contention, or cancelled context.","commonSituations":"Large tag tables updated one-by-one over unreliable connections; concurrent tag updates causing lock waits; running before the schema change added question_count.","solutions":["Read the wrapped driver error to distinguish lock timeout from schema issues","Verify the question_count column exists","Re-run the migration; updates are idempotent","Batch the updates or run during low-traffic windows to reduce lock contention"],"exampleFix":"// before\nif _, err = x.Context(ctx).Update(tag, &entity.Tag{ID: tag.ID}); err != nil {\n    return fmt.Errorf(\"update tag failed: %w\", err)\n}\n// after\nif _, err = x.Context(ctx).Cols(\"question_count\").Update(tag, &entity.Tag{ID: tag.ID}); err != nil {\n    return fmt.Errorf(\"update tag failed: %w\", err)\n}","handlingStrategy":"retry","validationCode":"if has, _ := x.Context(ctx).IsColumnExist(\"tag\", \"question_count\"); !has {\n    return fmt.Errorf(\"tag.question_count column missing; apply DDL first\")\n}","typeGuard":null,"tryCatchPattern":"if _, err = x.Context(ctx).Update(tag, &entity.Tag{ID: tag.ID}); err != nil {\n    if isRetryable(err) { // e.g. lock wait timeout / connection reset\n        time.Sleep(time.Second)\n        _, err = x.Context(ctx).Update(tag, &entity.Tag{ID: tag.ID})\n    }\n    if err != nil { return fmt.Errorf(\"update tag failed: %w\", err) }\n}","preventionTips":["Apply schema changes before count backfills","Avoid concurrent writers during migrations","Retry transient driver errors with backoff","Keep updates idempotent so retries are safe"],"tags":["database","xorm","migration"],"backgroundTag":"migration-db-update-failed","analyzedSha":"3b9f1370612e690a0b7f230f05e688930db4c6d3","analyzedAt":"2026-09-05T18:18:39.533Z","contentChangedAt":"2026-09-05T18:18:39.533Z","schemaVersion":2},"datasetVersion":"2026-09-12T22:17:10.623Z"}