{"record":{"id":"0c4fc3004117d5e4","repo":"apache/answer","slug":"add-config-failed-w-0c4fc3","errorCode":null,"errorMessage":"add config failed: %w","messagePattern":"add config failed: %w","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/migrations/v13.go","lineNumber":132,"sourceCode":"\t\t}\n\t}\n\n\tdefaultConfigTable := []*entity.Config{\n\t\t{ID: 127, Key: \"rank.answer.invite_someone_to_answer\", Value: `1000`},\n\t}\n\tfor _, c := range defaultConfigTable {\n\t\texist, err := x.Context(ctx).Get(&entity.Config{ID: c.ID})\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"get config failed: %w\", err)\n\t\t}\n\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 {","sourceCodeStart":114,"sourceCodeEnd":150,"githubUrl":"https://github.com/apache/answer/blob/3b9f1370612e690a0b7f230f05e688930db4c6d3/internal/migrations/v13.go#L114-L150","documentation":"Returned by addPrivilegeForInviteSomeoneToAnswer when the xorm Insert() of the new config ID 127 row (rank.answer.invite_someone_to_answer = 1000) fails. This path runs only when the config row was absent. The driver error is wrapped with %w.","triggerScenarios":"x.Context(ctx).Insert(&entity.Config{ID: c.ID, Key: c.Key, Value: c.Value}) returns non-nil err when config row 127 does not exist (exist == false) in the v13 migration.","commonSituations":"Concurrent insert from another instance causing duplicate-key; INSERT privilege missing; DB disk full; malformed value type for the value column.","solutions":["Check the wrapped error for a duplicate-key violation; if present, the row exists and the migration can be re-run safely","Verify INSERT privilege on the config table","Check DB health (disk, connections) and re-run the migration"],"exampleFix":"// before\nif _, err = x.Context(ctx).Insert(&entity.Config{ID: c.ID, Key: c.Key, Value: c.Value}); err != nil {\n\treturn fmt.Errorf(\"add config failed: %w\", err)\n}\n// after (idempotent: ignore duplicate key)\nif _, err = x.Context(ctx).Insert(&entity.Config{ID: c.ID, Key: c.Key, Value: c.Value}); err != nil {\n\tif isDuplicateKeyError(err) {\n\t\tcontinue\n\t}\n\treturn fmt.Errorf(\"add config failed: %w\", err)\n}","handlingStrategy":"retry","validationCode":"// pre-check whether the privilege row already exists\nvar count int64\ndb.QueryRow(\"SELECT COUNT(*) FROM config WHERE id = 127\").Scan(&count)\n// count == 0 means insert path will run","typeGuard":null,"tryCatchPattern":"if err := runMigrations(); err != nil {\n\tif strings.Contains(err.Error(), \"add config failed\") && isDuplicateKey(errors.Unwrap(err)) {\n\t\tlog.Println(\"config 127 already exists; migration effectively applied\")\n\t\treturn nil\n\t}\n\treturn err\n}","preventionTips":["Run migrations from one node only to avoid duplicate-key races","Verify INSERT privileges on the config table","Treat duplicate-key errors as success for idempotent config seeds","Back up the config table before upgrading"],"tags":["database","migration","xorm","config-insert"],"backgroundTag":"db-duplicate-key-violation","analyzedSha":"3b9f1370612e690a0b7f230f05e688930db4c6d3","analyzedAt":"2026-09-05T18:18:39.533Z","contentChangedAt":"2026-09-05T18:18:39.533Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}