{"record":{"id":"e5d12ff624382ae8","repo":"appwrite/appwrite","slug":"key-already-exists-e5d12f","errorCode":"KEY_ALREADY_EXISTS","errorMessage":"KEY_ALREADY_EXISTS","messagePattern":"KEY_ALREADY_EXISTS","errorType":"exception","errorClass":"Appwrite\\Exception","httpStatus":409,"severity":"error","filePath":"src/Appwrite/Platform/Modules/Organization/Http/Projects/Keys/Update.php","lineNumber":111,"sourceCode":"        // The request may run through the console project; events and audits belong to the resolved project\n        $queueForEvents->setProject($project);\n        $auditContext->project = $project;\n        $key = $authorization->skip(fn () => $dbForPlatform->getDocument('keys', $keyId));\n\n        if ($key->isEmpty() || $key->getAttribute('resourceType', '') !== 'projects' || $key->getAttribute('resourceInternalId', '') !== $project->getSequence()) {\n            throw new Exception(Exception::KEY_NOT_FOUND);\n        }\n\n        $updates = new Document([\n            'name' => $name,\n            'scopes' => $scopes,\n            'expire' => $expire,\n        ]);\n\n        try {\n            $key = $authorization->skip(fn () => $dbForPlatform->updateDocument('keys', $key->getId(), $updates));\n        } catch (Duplicate) {\n            throw new Exception(Exception::KEY_ALREADY_EXISTS);\n        }\n\n        $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId()));\n\n        $queueForEvents->setParam('keyId', $key->getId());\n\n        $response->dynamic($key, Response::MODEL_KEY);\n    }\n}\n","sourceCodeStart":93,"sourceCodeEnd":121,"githubUrl":"https://github.com/appwrite/appwrite/blob/ca3c55abfe2c8014937fdf778716af4524f2fcfc/src/Appwrite/Platform/Modules/Organization/Http/Projects/Keys/Update.php#L93-L121","documentation":"Thrown when updating a project key triggers a Duplicate exception from the database adapter during updateDocument, mapped to key_already_exists. On update this fires when a changed unique attribute (e.g. the key's $id/ID field) collides with another key document, not when merely editing name or scopes.","triggerScenarios":"PATCH /v1/organization/projects/:projectId/keys/:keyId where the update payload changes a unique field to a value already held by another key in the 'keys' collection (e.g. renaming an ID to one that exists).","commonSituations":"Automation that renames/re-ids keys to a deterministic value already used; merging two environments where both contain a key with the same identifier; retry logic replaying an update that partially succeeded under a new ID.","solutions":["Ensure the update does not change the key's ID to an existing one; only update name/scopes/expire.","Choose a different unique value or delete the conflicting key first.","Use ID.unique() when generating replacement identifiers.","List keys first to detect the conflicting ID before issuing the update."],"exampleFix":"// before\nawait organization.updateProjectKey(projectId, keyId, name, scopes); // keyId changed to existing id\n// after\nawait organization.updateProjectKey(projectId, existingKeyId, name, scopes); // update in place, keep original ID","handlingStrategy":"try-catch","validationCode":"const keys = await organization.listProjectKeys(projectId);\nif (keys.keys.some(k => k.$id === newId && k.$id !== currentKeyId)) throw new Error(`ID ${newId} conflicts with another key`);","typeGuard":null,"tryCatchPattern":"try {\n  await organization.updateProjectKey(projectId, keyId, name, scopes);\n} catch (e) {\n  if (e.code === 409) { /* unique-field collision: choose another ID or update in place */ }\n  else throw e;\n}","preventionTips":["Update name/scopes/expire in place; do not change unique identifiers on update.","Check for conflicting IDs before issuing updates that alter identifiers.","Clean up duplicate deterministic keys when merging environments."],"tags":["appwrite","api-key","duplicate-id","conflict"],"backgroundTag":"file-already-exists","analyzedSha":"ca3c55abfe2c8014937fdf778716af4524f2fcfc","analyzedAt":"2026-09-16T21:43:23.363Z","contentChangedAt":"2026-09-16T21:43:23.363Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}