{"record":{"id":"051961b926222722","repo":"apache/answer","slug":"update-question-failed-w-051961","errorCode":null,"errorMessage":"update question failed: %w","messagePattern":"update question failed: %w","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/migrations/v13.go","lineNumber":165,"sourceCode":"\t\t_, ok := questionAnswerCount[answer.QuestionID]\n\t\tif !ok {\n\t\t\tquestionAnswerCount[answer.QuestionID] = 1\n\t\t} else {\n\t\t\tquestionAnswerCount[answer.QuestionID]++\n\t\t}\n\t}\n\tquestionList := make([]QuestionV13, 0)\n\terr = x.Context(ctx).Find(&questionList, &QuestionV13{})\n\tif err != nil {\n\t\treturn fmt.Errorf(\"get questions failed: %w\", err)\n\t}\n\tfor _, item := range questionList {\n\t\t_, ok := questionAnswerCount[item.ID]\n\t\tif ok {\n\t\t\titem.AnswerCount = questionAnswerCount[item.ID]\n\t\t\tif _, err = x.Context(ctx).Cols(\"answer_count\").Update(item, &QuestionV13{ID: item.ID}); err != nil {\n\t\t\t\tlog.Errorf(\"update %+v config failed: %s\", item, err)\n\t\t\t\treturn fmt.Errorf(\"update question failed: %w\", err)\n\t\t\t}\n\t\t}\n\t}\n\n\treturn nil\n}\n\n// updateTagCount update tag count\nfunc updateTagCount(ctx context.Context, x *xorm.Engine) error {\n\ttagRelList := make([]entity.TagRel, 0)\n\terr := x.Context(ctx).Find(&tagRelList, &entity.TagRel{})\n\tif err != nil {\n\t\treturn fmt.Errorf(\"get tag rel failed: %w\", err)\n\t}\n\tquestionIDs := make([]string, 0)\n\tquestionsAvailableMap := make(map[string]bool)\n\tquestionsHideMap := make(map[string]bool)\n\tfor _, item := range tagRelList {","sourceCodeStart":147,"sourceCodeEnd":183,"githubUrl":"https://github.com/apache/answer/blob/3b9f1370612e690a0b7f230f05e688930db4c6d3/internal/migrations/v13.go#L147-L183","documentation":"Wraps the xorm Update error when persisting a question's recomputed answer_count column during the v13 migration. The row-level UPDATE on the question table failed; the migration logs the offending item and aborts so the failure is visible.","triggerScenarios":"x.Context(ctx).Cols(\"answer_count\").Update(item, &QuestionV13{ID: item.ID}) failing because the connection dropped mid-loop, answer_count column does not exist (schema not yet migrated), a lock/timeout on the row, or context cancellation.","commonSituations":"Migrating a large question table over a flaky connection; running the code before the v13 schema change added answer_count; deadlocks with concurrent writers to the question table.","solutions":["Check the wrapped driver error for lock timeout vs missing column","Confirm answer_count column exists (run the v13 DDL first)","Retry the migration after connection is restored; already-updated rows are safe to re-update","Run migrations during a maintenance window to avoid concurrent write contention"],"exampleFix":"// before\nif _, err = x.Context(ctx).Cols(\"answer_count\").Update(item, &QuestionV13{ID: item.ID}); err != nil {\n    return fmt.Errorf(\"update question failed: %w\", err)\n}\n// after\n// ensure column exists before loop\nif has, _ := x.Context(ctx).IsColumnExist(\"question\", \"answer_count\"); !has {\n    return fmt.Errorf(\"column answer_count missing; run schema migration first\")\n}","handlingStrategy":"retry","validationCode":"if has, _ := x.Context(ctx).IsColumnExist(\"question\", \"answer_count\"); !has {\n    return fmt.Errorf(\"answer_count column missing; apply schema change first\")\n}","typeGuard":null,"tryCatchPattern":"for attempt := 0; attempt < 3; attempt++ {\n    if _, err = x.Context(ctx).Cols(\"answer_count\").Update(item, &QuestionV13{ID: item.ID}); err == nil {\n        break\n    }\n    time.Sleep(time.Duration(attempt+1) * time.Second)\n}","preventionTips":["Run schema DDL before data backfill in the same migration","Schedule migrations in maintenance windows to avoid lock contention","Set reasonable innodb_lock_wait_timeout / statement timeouts","Make update loops 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"}