{"record":{"id":"cd96cd0c86e7b281","repo":"gastownhall/beads","slug":"db-commentsqlrepository-insertrecord-issue-s-no","errorCode":null,"errorMessage":"db: CommentSQLRepository.InsertRecord: issue %s not found","messagePattern":"db: CommentSQLRepository\\.InsertRecord: issue (.+?) not found","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/storage/domain/db/comment.go","lineNumber":145,"sourceCode":"\nfunc (r *commentSQLRepositoryImpl) InsertRecord(ctx context.Context, comment *types.Comment, opts domain.CommentOpts) (*types.Comment, error) {\n\tif comment == nil {\n\t\treturn nil, fmt.Errorf(\"db: CommentSQLRepository.InsertRecord: comment must not be nil\")\n\t}\n\tcopy := *comment\n\tif copy.IssueID == \"\" {\n\t\treturn nil, fmt.Errorf(\"db: CommentSQLRepository.InsertRecord: issueID must not be empty\")\n\t}\n\n\tissueTable := pickIssueTable(opts.UseWispsTable)\n\tvar exists bool\n\t//nolint:gosec // G201: issueTable is one of two hardcoded constants\n\tif err := r.runner.QueryRowContext(ctx,\n\t\tfmt.Sprintf(\"SELECT EXISTS(SELECT 1 FROM %s WHERE id = ?)\", issueTable), copy.IssueID).Scan(&exists); err != nil {\n\t\treturn nil, fmt.Errorf(\"db: CommentSQLRepository.InsertRecord: check issue existence: %w\", err)\n\t}\n\tif !exists {\n\t\treturn nil, fmt.Errorf(\"db: CommentSQLRepository.InsertRecord: issue %s not found\", copy.IssueID)\n\t}\n\n\tif copy.CreatedAt.IsZero() {\n\t\tcopy.CreatedAt = time.Now().UTC()\n\t} else {\n\t\tcopy.CreatedAt = copy.CreatedAt.UTC()\n\t}\n\tcreatedAtText := issueops.FormatAuxTime(copy.CreatedAt)\n\tcommentTable := pickCommentTable(opts.UseWispsTable)\n\tif copy.ID == \"\" {\n\t\tid, _, err := issueops.InsertDerivedComment(ctx, r.runner, commentTable, copy.IssueID, copy.Author, copy.Text, createdAtText)\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"db: CommentSQLRepository.InsertRecord: %w\", err)\n\t\t}\n\t\tcopy.ID = id\n\t} else {\n\t\t//nolint:gosec // G201: commentTable is one of two hardcoded constants\n\t\tif _, err := r.runner.ExecContext(ctx, fmt.Sprintf(`","sourceCodeStart":127,"sourceCodeEnd":163,"githubUrl":"https://github.com/gastownhall/beads/blob/71377f276968b452ee607177637970a4ff888584/internal/storage/domain/db/comment.go#L127-L163","documentation":"The existence probe returned exists=false: the parent issue ID is valid non-empty text but no row with that id exists in the selected issue table (issues or issues_wisps). The repository refuses to create a comment for a nonexistent issue, keeping referential integrity.","triggerScenarios":"InsertRecord(ctx, &types.Comment{IssueID: \"bd-123\", ...}, opts) where bd-123 is not in the issue table — wrong prefix, typo'd ID, ID from a different database, or UseWispsTable set inconsistently with where the issue actually lives (issue created in wisps table but comment inserted with the wrong table selection, or vice versa).","commonSituations":"Copy-pasting IDs between repos/databases, importing comments before importing the issues, stale IDs after a DB reset, or a mismatch between the table chosen via opts.UseWispsTable and where the issue was created.","solutions":["Verify the issue ID exists (bd show <id> or SELECT) in the same database and table selection.","Correct the IssueID (typo/prefix) or create the issue first.","Make opts.UseWispsTable consistent with how the issue was created (wisp vs regular issue).","Skip/requeue comments whose parent issue was deleted instead of failing the whole import."],"exampleFix":"// before\nrepo.InsertRecord(ctx, &types.Comment{IssueID: \"bd-999\", Text: \"x\"}, opts) // bd-999 absent\n// after\nif issue, err := issueRepo.Get(ctx, \"bd-999\"); err == nil {\n    repo.InsertRecord(ctx, &types.Comment{IssueID: issue.ID, Text: \"x\"}, opts)\n}","handlingStrategy":"validation","validationCode":"// ensure the parent issue exists before inserting the comment\nvar exists bool\n_ = runner.QueryRowContext(ctx,\n    \"SELECT EXISTS(SELECT 1 FROM issues WHERE id = ?)\", issueID).Scan(&exists)\nif !exists {\n    return fmt.Errorf(\"refusing to comment on unknown issue %s\", issueID)\n}","typeGuard":"func issueExists(ctx context.Context, r domain.IssueSQLRepository, id string) bool {\n    _, err := r.Get(ctx, id)\n    return err == nil\n}","tryCatchPattern":"if _, err := repo.InsertRecord(ctx, c, opts); err != nil {\n    if strings.Contains(err.Error(), \"not found\") && strings.Contains(err.Error(), c.IssueID) {\n        // create or look up the correct issue first; skip orphan comments in imports\n    }\n}","preventionTips":["Fetch the issue object and use its ID rather than re-typing IDs by hand.","During imports, import issues before comments and validate referential integrity.","Never mix IDs across databases; verify with bd show before writing.","Keep UseWispsTable aligned with where the issue was created."],"tags":["go","foreign-key","referential-integrity","not-found"],"backgroundTag":"referenced-record-not-found","analyzedSha":"71377f276968b452ee607177637970a4ff888584","analyzedAt":"2026-08-30T18:55:39.744Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}