{"record":{"id":"1776f186bb12e755","repo":"apache/answer","slug":"get-answers-failed-w","errorCode":null,"errorMessage":"get answers failed: %w","messagePattern":"get answers failed: %w","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/migrations/v13.go","lineNumber":143,"sourceCode":"\t\tif exist {\n\t\t\tif _, err = x.Context(ctx).Update(c, &entity.Config{ID: c.ID}); err != nil {\n\t\t\t\treturn fmt.Errorf(\"update config failed: %w\", err)\n\t\t\t}\n\t\t\tcontinue\n\t\t}\n\t\tif _, err = x.Context(ctx).Insert(&entity.Config{ID: c.ID, Key: c.Key, Value: c.Value}); err != nil {\n\t\t\treturn fmt.Errorf(\"add config failed: %w\", err)\n\t\t}\n\t}\n\treturn nil\n}\n\nfunc updateQuestionCount(ctx context.Context, x *xorm.Engine) error {\n\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 {","sourceCodeStart":125,"sourceCodeEnd":161,"githubUrl":"https://github.com/apache/answer/blob/3b9f1370612e690a0b7f230f05e688930db4c6d3/internal/migrations/v13.go#L125-L161","documentation":"Wraps an xorm Find error returned while loading all available answers (AnswerV13 rows) during the v13 migration's updateQuestionCount step. The migration needs to recompute each question's answer count from existing answers; if the answers table cannot be read (connection failure, missing table, bad schema), the migration aborts with this wrapped error. It is a pass-through of the underlying database error.","triggerScenarios":"Calling x.Context(ctx).Find(&answers, &AnswerV13{Status: entity.AnswerStatusAvailable}) when the answer table is missing, the DB connection is dropped mid-migration, the table schema predates expected columns, or the context is cancelled during the query.","commonSituations":"Running the v13 migration against a database where the answers table was renamed or dropped; network blips to a remote MySQL/Postgres during long migrations; context timeouts when migrations run under a request-scoped context; partial upgrade states where schema changes were applied manually out of order.","solutions":["Check the wrapped cause (%w chain) for the real driver error — fix connectivity, credentials, or missing table first","Verify the answer table exists with the schema expected by AnswerV13 (migrations must run sequentially)","Re-run the migration on a healthy connection; migrations are idempotent up to the failed step","Increase context timeout or run the migration with a background context so it is not cancelled"],"exampleFix":"// before\nerr := x.Context(ctx).Find(&answers, &AnswerV13{Status: entity.AnswerStatusAvailable})\n// after\nctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)\ndefer cancel()\nerr := x.Context(ctx).Find(&answers, &AnswerV13{Status: entity.AnswerStatusAvailable})","handlingStrategy":"try-catch","validationCode":"// before running migration\nif exists, _ := x.IsTableExist(&AnswerV13{}); !exists {\n    return fmt.Errorf(\"answer table missing; run prior migrations first\")\n}","typeGuard":null,"tryCatchPattern":"if err := updateQuestionCount(ctx, engine); err != nil {\n    var dbErr xorm.ErrNotExist\n    if errors.As(err, &dbErr) { /* table missing: run schema migrations */ }\n    log.Errorf(\"migration v13 failed: %v\", err)\n    return err\n}","preventionTips":["Run migrations with a background context and generous timeout","Ensure migrations execute sequentially so tables exist","Verify DB connectivity and credentials before starting migrations","Keep a backup so a failed migration can be retried safely"],"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-12T22:17:10.623Z"}