{"record":{"id":"1164fc3ec56aeb14","repo":"tobi/qmd","slug":"collection-newname-already-exists","errorCode":null,"errorMessage":"Collection '${newName}' already exists","messagePattern":"Collection '(.+?)' already exists","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/collections.ts","lineNumber":352,"sourceCode":"  }\n\n  delete config.collections[name];\n  saveConfig(config);\n  return true;\n}\n\n/**\n * Rename a collection\n */\nexport function renameCollection(oldName: string, newName: string): boolean {\n  const config = loadConfig();\n\n  if (!config.collections[oldName]) {\n    return false;\n  }\n\n  if (config.collections[newName]) {\n    throw new Error(`Collection '${newName}' already exists`);\n  }\n\n  config.collections[newName] = config.collections[oldName];\n  delete config.collections[oldName];\n  saveConfig(config);\n  return true;\n}\n\n// ============================================================================\n// Context management\n// ============================================================================\n\n/**\n * Get global context\n */\nexport function getGlobalContext(): string | undefined {\n  const config = loadConfig();\n  return config.global_context;","sourceCodeStart":334,"sourceCodeEnd":370,"githubUrl":"https://github.com/tobi/qmd/blob/dbfd0b4736aeaf761d1a16ca8e424f071df8feb9/src/collections.ts#L334-L370","documentation":"renameCollection refuses to rename a collection because the target name already exists in the config, which would silently overwrite its settings. The check runs after confirming the old name exists, so this indicates a genuine collision, not a missing source.","triggerScenarios":"Calling qmd collection rename mynotes notes when a collection named notes already exists; calling renameCollection('a','b') programmatically where both keys are present.","commonSituations":"Re-running a rename script that partially completed; renaming to a name you forgot you created earlier; shell completion picking an existing name.","solutions":["Choose a different new name, or first remove the existing collection: qmd collection remove <newName> then retry the rename","List collections with `qmd collection list` to confirm which names are taken","If the existing target is stale, rename or remove it first"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Before renaming, check target availability\nimport { loadConfig } from './src/collections.js';\nconst config = loadConfig(configPath);\nconst targetFree = !config.collections?.[newName];","typeGuard":null,"tryCatchPattern":"try {\n  ok = renameCollection(oldName, newName);\n} catch (e) {\n  if (e instanceof Error && /already exists/.test(e.message)) {\n    // pick another name or remove existing first\n  } else throw e;\n}","preventionTips":["List collections with `qmd collection list` before renaming","Make rename scripts idempotent: check target first","Avoid rename targets similar to existing names"],"tags":["collections","rename","duplicate-key"],"backgroundTag":"duplicate-resource-name","analyzedSha":"dbfd0b4736aeaf761d1a16ca8e424f071df8feb9","analyzedAt":"2026-08-28T18:07:46.628Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}