{"record":{"id":"598cbb0bfbb0b78a","repo":"apache/cassandra","slug":"already-exists-598cbb","errorCode":null,"errorMessage":"Already exists","messagePattern":"Already exists","errorType":"exception","errorClass":"AlreadyExistsException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/schema/SchemaTransformations.java","lineNumber":59,"sourceCode":"     *                       {@code keyspace} already exists in the schema the transformation is applied on. Otherwise,\n     *                       the transformation throws an {@link AlreadyExistsException} in that case.\n     * @return the created transformation.\n     */\n    public static SchemaTransformation addKeyspace(KeyspaceMetadata keyspace, boolean ignoreIfExists)\n    {\n        return new SchemaTransformation()\n        {\n            @Override\n            public Keyspaces apply(ClusterMetadata metadata)\n            {\n                Keyspaces schema = metadata.schema.getKeyspaces();\n                KeyspaceMetadata existing = schema.getNullable(keyspace.name);\n                if (existing != null)\n                {\n                    if (ignoreIfExists)\n                        return schema;\n\n                    throw new AlreadyExistsException(keyspace.name);\n                }\n\n                return schema.withAddedOrUpdated(keyspace);\n            }\n\n            @Override\n            public boolean compatibleWith(ClusterMetadata metadata)\n            {\n                return metadata.directory.commonSerializationVersion.isAtLeast(Version.V0);\n            }\n        };\n    }\n\n    /**\n     * Creates a schema transformation that adds the provided table.\n     *\n     * @param table          the table to add.\n     * @param ignoreIfExists if {@code true}, the transformation is a no-op if a table of the same name than","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/schema/SchemaTransformations.java#L41-L77","documentation":"Thrown as an AlreadyExistsException by a CreateKeyspace schema transformation when the target keyspace already exists in the current schema and ignoreIfExists is false. It prevents silently overwriting existing keyspace metadata during schema changes.","triggerScenarios":"Calling SchemaTransformations.createKeyspace(keyspace, ignoreIfExists=false).apply() (e.g. via a CREATE KEYSPACE statement without IF NOT EXISTS) when schema.getNullable(keyspace.name) returns existing metadata.","commonSituations":"Running CREATE KEYSPACE without IF NOT EXISTS on an existing keyspace; re-running an initialization script; racing concurrent setup processes where two nodes apply the same keyspace creation.","solutions":["Add IF NOT EXISTS to the CREATE KEYSPACE statement (sets ignoreIfExists=true)","Check Schema.instance.getNullableKeyspaceMetadata(name) before issuing the creation","Catch AlreadyExistsException and treat it as a no-op if creation is idempotent in your tooling","Use a different keyspace name if a genuinely new keyspace was intended"],"exampleFix":"// before\nCREATE KEYSPACE my_ks WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 3};\n// after\nCREATE KEYSPACE IF NOT EXISTS my_ks WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 3};","handlingStrategy":"try-catch","validationCode":"if (Schema.instance.getNullableKeyspaceMetadata(ksName) != null) { /* already exists: skip */ }","typeGuard":"boolean keyspaceExists(String ks) { return Schema.instance.getNullableKeyspaceMetadata(ks) != null; }","tryCatchPattern":"try { schemaChange(createKeyspaceCql); } catch (AlreadyExistsException e) { log.info(\"keyspace already present, continuing\"); }","preventionTips":["Always use IF NOT EXISTS in bootstrap DDL","Pre-check schema metadata before creation","Make setup scripts idempotent","Serialize schema changes across nodes/app instances"],"tags":["cassandra","schema","duplicate"],"backgroundTag":"file-already-exists","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}