{"record":{"id":"64f3fac61cf84c14","repo":"gastownhall/beads","slug":"w-apply-batch-item-d-reuses-key-q-already-dec","errorCode":null,"errorMessage":"%w: apply batch item %d reuses key %q, already declared by item %d","messagePattern":"%w: apply batch item (.+?) reuses key %q, already declared by item (.+?)","errorType":"validation","errorClass":"issueops.ErrValidation","httpStatus":null,"severity":"error","filePath":"internal/storage/batch_apply.go","lineNumber":157,"sourceCode":"\t\tif !matches {\n\t\t\treturn nil, fmt.Errorf(\"%w: apply batch item %d is kind %q but carries another kind's payload\",\n\t\t\t\tissueops.ErrValidation, i, item.Kind)\n\t\t}\n\t\tif item.Create == nil {\n\t\t\tcontinue\n\t\t}\n\t\tif item.Create.Issue == nil {\n\t\t\treturn nil, fmt.Errorf(\"%w: apply batch item %d requires an issue\", issueops.ErrValidation, i)\n\t\t}\n\t\tif len(item.Create.Issue.Comments) > 0 || len(item.Create.Issue.Dependencies) > 0 {\n\t\t\treturn nil, fmt.Errorf(\"%w: apply batch item %d must not carry comments or dependencies on the issue; edges are their own items\",\n\t\t\t\tissueops.ErrValidation, i)\n\t\t}\n\t\tif item.Create.Key == \"\" {\n\t\t\tcontinue\n\t\t}\n\t\tif prior, dup := keyIndex[item.Create.Key]; dup {\n\t\t\treturn nil, fmt.Errorf(\"%w: apply batch item %d reuses key %q, already declared by item %d\",\n\t\t\t\tissueops.ErrValidation, i, item.Create.Key, prior)\n\t\t}\n\t\tkeyIndex[item.Create.Key] = i\n\t}\n\treturn keyIndex, nil\n}\n\n// planApplyBatchItem validates one item's refs, guards and edge metadata, and\n// records what it touches for the items after it.\nfunc planApplyBatchItem(item *issueops.ApplyItem, index int, keyIndex map[string]int, touched map[string]bool) error {\n\tswitch item.Kind {\n\tcase issueops.ItemCreate:\n\t\treturn planApplyBatchCreate(item.Create, index, keyIndex, touched)\n\tcase issueops.ItemUpdate:\n\t\treturn planApplyBatchUpdate(item.Update, index, keyIndex, touched)\n\tcase issueops.ItemClose:\n\t\treturn planApplyBatchClose(item.Close, index, keyIndex, touched)\n\tcase issueops.ItemDepAdd:","sourceCodeStart":139,"sourceCodeEnd":175,"githubUrl":"https://github.com/gastownhall/beads/blob/71377f276968b452ee607177637970a4ff888584/internal/storage/batch_apply.go#L139-L175","documentation":"Each create item may declare a Key so later items can reference it; keys must be unique within the batch. This error fires when two create items declare the same Key, since the key-to-item map would be ambiguous. Wraps issueops.ErrValidation.","triggerScenarios":"PlanApplyBatch with two create items both setting Create.Key to the same string (e.g. two items with Key \"bd-1\"); the second one triggers the error.","commonSituations":"Reusing a slug/key across loop iterations without templating it; appending a fixture batch twice; generating keys from a field that isn't unique in the source data.","solutions":["Make each Create.Key unique (append a suffix/counter)","Remove the Key from the duplicate item if nothing references it (empty keys are allowed)","Track used keys in the generator and skip or rename duplicates"],"exampleFix":"// before\nfor _, it := range issues { items = append(items, mkCreate(\"issue\", it)) } // key always \"issue\"\n// after\nfor i, it := range issues { items = append(items, mkCreate(fmt.Sprintf(\"issue-%d\", i), it)) }","handlingStrategy":"validation","validationCode":"seen := map[string]bool{}\nfor i, it := range items {\n  if it.Create != nil && it.Create.Key != \"\" {\n    if seen[it.Create.Key] {\n      return fmt.Errorf(\"item %d reuses key %q\", i, it.Create.Key)\n    }\n    seen[it.Create.Key] = true\n  }\n}","typeGuard":null,"tryCatchPattern":"if err := store.PlanApplyBatch(plan); err != nil {\n  if errors.Is(err, issueops.ErrValidation) && strings.Contains(err.Error(), \"reuses key\") {\n    // regenerate unique keys and rebuild the plan\n  }\n  return err\n}","preventionTips":["Template keys with an index or slug to guarantee uniqueness","Leave Key empty for items nothing references","Centralize key generation in one function that de-duplicates"],"tags":["validation","batch-apply","duplicate-key"],"backgroundTag":"duplicate-key-in-batch","analyzedSha":"71377f276968b452ee607177637970a4ff888584","analyzedAt":"2026-08-30T18:55:39.744Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}