{"record":{"id":"a3c4e2e4117382e8","repo":"apache/cassandra","slug":"reason","errorCode":null,"errorMessage":"${reason}","messagePattern":"\\$\\{reason\\}","errorType":"exception","errorClass":"AlreadyExistsException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/schema/Schema.java","lineNumber":313,"sourceCode":"    {\n        return getTableMetadata(descriptor.ksname, descriptor.cfname);\n    }\n\n    @Override\n    public ClusterMetadata submit(SchemaTransformation transformation)\n    {\n        logger.debug(\"Submitting schema transformation {}\", transformation);\n\n        // result of this execution can be either a complete failure/timeout, or a success, but together with a log of\n        // operations that have to be applied before we can do anything\n        return ClusterMetadataService.instance()\n                                     .commit(new AlterSchema(transformation),\n                                             (metadata) -> metadata,\n                                             (code, reason) -> {\n                                                 switch (code)\n                                                 {\n                                                     case ALREADY_EXISTS:\n                                                         throw new AlreadyExistsException(reason);\n                                                     case CONFIG_ERROR:\n                                                         throw new ConfigurationException(reason);\n                                                     case SYNTAX_ERROR:\n                                                         throw new SyntaxException(reason);\n                                                     case UNAUTHORIZED:\n                                                         throw new UnauthorizedException(reason);\n                                                     default:\n                                                         throw new InvalidRequestException(reason);\n                                                 }\n                                             });\n    }\n\n    // We need to lazy-initialize schema for test purposes: since column families are initialized\n    // eagerly, if local schema initialization is attempted before commit log instance is started,\n    // cf initialization will fail to grab a current commit log position.\n    //\n    // This could be further improved by providing a special Schema instance for tests, or\n    // using supplier with precomputed value for regular code path, and lazy variable for tests.","sourceCodeStart":295,"sourceCodeEnd":331,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/schema/Schema.java#L295-L331","documentation":"Schema.submit distributes a schema transformation through the cluster and maps the resulting commit failure code to a client-facing exception. When the coordinator reports ALREADY_EXISTS, the reason string is rethrown as an AlreadyExistsException indicating the keyspace/table being created already exists.","triggerScenarios":"Executing CREATE KEYSPACE / CREATE TABLE / CREATE TYPE via Schema.submit whose name already exists in the current schema, or a raced concurrent create from another coordinator that committed first.","commonSituations":"Idempotent migration scripts re-run without IF NOT EXISTS; two clients concurrently creating the same schema object; application startup code that unconditionally creates keyspaces.","solutions":["Use IF NOT EXISTS in the CREATE statement (e.g. CREATE KEYSPACE IF NOT EXISTS ...).","Check existing schema with DESCRIBE or system_schema queries before issuing the create.","Catch AlreadyExistsException and treat it as success if the existing definition matches what you need."],"exampleFix":"// before\nsession.execute(\"CREATE KEYSPACE ks WITH replication = {'class':'SimpleStrategy','replication_factor':1}\");\n// after\nsession.execute(\"CREATE KEYSPACE IF NOT EXISTS ks WITH replication = {'class':'SimpleStrategy','replication_factor':1}\");","handlingStrategy":"try-catch","validationCode":"Row r = session.execute(\"SELECT keyspace_name FROM system_schema.keyspaces WHERE keyspace_name = ?\", ks).one();\nboolean exists = r != null;","typeGuard":null,"tryCatchPattern":"try { session.execute(createStmt); }\ncatch (AlreadyExistsException e) { logger.info(\"Keyspace/table already exists, continuing\"); }","preventionTips":["Always use IF NOT EXISTS for idempotent schema migrations.","Serialize DDL through a single migration runner, not concurrent nodes.","Check system_schema before create operations in bootstrap code."],"tags":["schema","ddl","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"}