{"record":{"id":"5a1fb5e0e0d2aa95","repo":"Mintplex-Labs/anything-llm","slug":"system-prompt-variable-with-this-key-already-exist","errorCode":null,"errorMessage":"System prompt variable with this key already exists","messagePattern":"System prompt variable with this key already exists","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"server/models/systemPromptVariables.js","lineNumber":369,"sourceCode":"   * Internal function to check if a variable key is valid\n   * @param {string} key\n   * @param {boolean} checkExisting\n   * @returns {Promise<boolean>}\n   */\n  _checkVariableKey: async function (key = null, checkExisting = true) {\n    if (!key) throw new Error(\"Key is required\");\n    if (typeof key !== \"string\") throw new Error(\"Key must be a string\");\n    if (!/^[a-zA-Z0-9_]+$/.test(key))\n      throw new Error(\"Key must contain only letters, numbers and underscores\");\n    if (key.length > 255)\n      throw new Error(\"Key must be less than 255 characters\");\n    if (key.length < 3) throw new Error(\"Key must be at least 3 characters\");\n    if (key.startsWith(\"user.\"))\n      throw new Error(\"Key cannot start with 'user.'\");\n    if (key.startsWith(\"system.\"))\n      throw new Error(\"Key cannot start with 'system.'\");\n    if (checkExisting && (await this.get(key)) !== null)\n      throw new Error(\"System prompt variable with this key already exists\");\n\n    return true;\n  },\n};\n\nmodule.exports = { SystemPromptVariables };\n","sourceCodeStart":351,"sourceCodeEnd":376,"githubUrl":"https://github.com/Mintplex-Labs/anything-llm/blob/526360e320da9d1b36074be5ed64fe76e5bbfbbd/server/models/systemPromptVariables.js#L351-L376","documentation":"Thrown by SystemPromptVariables._checkVariableKey when checkExisting is true and this.get(key) returns a non-null record. Only the create path passes checkExisting=true (line 180), so this is a uniqueness guard preventing two static variables with the same key. update passes checkExisting=false (line 204), so updates do not trip it.","triggerScenarios":"POST /system/prompt-variables with a key that already exists in the system_prompt_variables table.","commonSituations":"Admin clicks 'create' twice or retries after a network blip. Two admins defining the same variable concurrently. Migration script re-running without dedup.","solutions":["If you want a new value for an existing key, use PUT /system/prompt-variables/:id instead of POST.","Look up the variable by key first and update it if it already exists.","Make the create flow idempotent by checking existence before submitting."],"exampleFix":"// before\nawait SystemPromptVariables.create({ key: existingKey, value });\n// after\nconst existing = await SystemPromptVariables.get(existingKey);\nif (existing) {\n  await SystemPromptVariables.update(existing.id, { key: existingKey, value });\n} else {\n  await SystemPromptVariables.create({ key: existingKey, value });\n}","handlingStrategy":"try-catch","validationCode":"const existing = await SystemPromptVariables.get(key);\nif (existing) {\n  // update instead of create\n}","typeGuard":null,"tryCatchPattern":"try {\n  await SystemPromptVariables.create({ key, value });\n} catch (e) {\n  if (/already exists/.test(e.message)) {\n    const existing = await SystemPromptVariables.get(key);\n    await SystemPromptVariables.update(existing.id, { key, value });\n  } else throw e;\n}","preventionTips":["Make create flows idempotent: check existence first.","Guard against double-submits in the UI (disable button after click)."],"tags":["validation","system-prompt-variables","duplicate","uniqueness"],"backgroundTag":null,"analyzedSha":"526360e320da9d1b36074be5ed64fe76e5bbfbbd","analyzedAt":"2026-08-13T01:45:47.170Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}