{"record":{"id":"d51e36d1adc84aac","repo":"apache/answer","slug":"get-questions-failed-w-d51e36","errorCode":null,"errorMessage":"get questions failed: %w","messagePattern":"get questions failed: %w","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/migrations/v13.go","lineNumber":157,"sourceCode":"\t// question answer count\n\tanswers := make([]AnswerV13, 0)\n\terr := x.Context(ctx).Find(&answers, &AnswerV13{Status: entity.AnswerStatusAvailable})\n\tif err != nil {\n\t\treturn fmt.Errorf(\"get answers failed: %w\", err)\n\t}\n\tquestionAnswerCount := make(map[string]int)\n\tfor _, answer := range answers {\n\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)","sourceCodeStart":139,"sourceCodeEnd":175,"githubUrl":"https://github.com/apache/answer/blob/3b9f1370612e690a0b7f230f05e688930db4c6d3/internal/migrations/v13.go#L139-L175","documentation":"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.","triggerScenarios":"x.Context(ctx).Find(&questionList, &QuestionV13{}) failing due to a dropped connection, missing question table, schema mismatch with QuestionV13 fields, or cancelled context.","commonSituations":"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.","solutions":["Inspect the wrapped error to identify the driver-level cause","Ensure prior migrations created/evolved the question table to match QuestionV13","Re-run on a stable DB connection with a non-request-scoped context","Verify DB user has SELECT permission on the question table"],"exampleFix":"// before\nerr = x.Context(ctx).Find(&questionList, &QuestionV13{})\n// after\nif err = x.Context(ctx).Find(&questionList, &QuestionV13{}); err != nil {\n    log.Errorf(\"find questions failed: %v\", err)\n    return fmt.Errorf(\"get questions failed: %w\", err)\n}","handlingStrategy":"try-catch","validationCode":"if exists, _ := x.IsTableExist(&QuestionV13{}); !exists {\n    return fmt.Errorf(\"question table missing; run prior migrations first\")\n}","typeGuard":null,"tryCatchPattern":"if err := updateQuestionCount(ctx, engine); err != nil {\n    log.Errorf(\"question count migration failed: %v\", err)\n    return err\n}","preventionTips":["Never skip migration versions; partial schema causes Find failures","Run migrations on the primary DB, not a replica","Use stable connections with keepalive for long migrations","Check wrapped errors with errors.As to route table-missing vs connection errors"],"tags":["database","xorm","migration"],"backgroundTag":"migration-db-query-failed","analyzedSha":"3b9f1370612e690a0b7f230f05e688930db4c6d3","analyzedAt":"2026-09-05T18:18:39.533Z","contentChangedAt":"2026-09-05T18:18:39.533Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}