{"record":{"id":"7b0ed7199a681615","repo":"apache/cassandra","slug":"keyspace-s-doesn-t-exist-7b0ed7","errorCode":null,"errorMessage":"Keyspace '%s' doesn't exist","messagePattern":"Keyspace '(.+?)' doesn't exist","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/statements/schema/AlterKeyspaceStatement.java","lineNumber":88,"sourceCode":"        this.ifExists = ifExists;\n    }\n\n    @Override\n    public boolean compatibleWith(ClusterMetadata metadata)\n    {\n        return metadata.directory.commonSerializationVersion.isAtLeast(Version.V0);\n    }\n\n    public Keyspaces apply(ClusterMetadata metadata)\n    {\n        attrs.validate();\n\n        Keyspaces schema = metadata.schema.getKeyspaces();\n        KeyspaceMetadata keyspace = schema.getNullable(keyspaceName);\n        if (null == keyspace)\n        {\n            if (!ifExists)\n                throw ire(\"Keyspace '%s' doesn't exist\", keyspaceName);\n            return schema;\n        }\n\n        KeyspaceMetadata newKeyspace = keyspace.withSwapped(attrs.asAlteredKeyspaceParams(keyspace.params));\n\n        if (attrs.getReplicationStrategyClass() != null && attrs.getReplicationStrategyClass().equals(SimpleStrategy.class.getSimpleName()))\n            Guardrails.simpleStrategyEnabled.ensureEnabled(state);\n\n        if (keyspace.params.replication.isMeta() && !keyspace.name.equals(SchemaConstants.METADATA_KEYSPACE_NAME))\n            throw ire(\"Can not alter a keyspace to use MetaReplicationStrategy\");\n\n        if (newKeyspace.params.replication.klass.equals(LocalStrategy.class))\n            throw ire(\"Unable to use given strategy class: LocalStrategy is reserved for internal use.\");\n\n        newKeyspace.params.validate(keyspaceName, state, metadata);\n        newKeyspace.replicationStrategy.validate(metadata);\n\n        validateNoRangeMovements();","sourceCodeStart":70,"sourceCodeEnd":106,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/statements/schema/AlterKeyspaceStatement.java#L70-L106","documentation":"ALTER KEYSPACE was issued for a keyspace that does not exist in the schema and IF EXISTS was not specified. AlterKeyspaceStatement.apply looks up the keyspace metadata and throws an InvalidRequestException when the lookup returns null and ifExists is false.","triggerScenarios":"`ALTER KEYSPACE ks WITH ...` where ks was never created or was already dropped, without the IF EXISTS clause.","commonSituations":"Typo in the keyspace name; running migrations against an environment where the keyspace was never created; keyspace dropped by another process between steps.","solutions":["Create the keyspace first with CREATE KEYSPACE, or fix the name","Add IF EXISTS: ALTER KEYSPACE IF EXISTS ks WITH ... to skip when missing","Verify existing keyspaces with DESCRIBE KEYSPACES or system_schema"],"exampleFix":"// before\nALTER KEYSPACE myks WITH replication = {'class':'NetworkTopologyStrategy','dc1':3};\n// after\nCREATE KEYSPACE IF NOT EXISTS myks WITH replication = {'class':'NetworkTopologyStrategy','dc1':3};\nALTER KEYSPACE myks WITH replication = {'class':'NetworkTopologyStrategy','dc1':3};","handlingStrategy":"validation","validationCode":"boolean exists = cluster.getMetadata().getKeyspace(keyspace) != null; if (!exists) throw new IllegalStateException(\"keyspace does not exist: \" + keyspace);","typeGuard":null,"tryCatchPattern":"try { session.execute(alterCql); } catch (InvalidRequestException e) { if (e.getMessage().contains(\"doesn't exist\")) { session.execute(\"CREATE KEYSPACE IF NOT EXISTS \" + keyspace + \" WITH ...\", /* then retry alter */); } else throw e; }","preventionTips":["Run CREATE KEYSPACE IF NOT EXISTS before ALTER in migration scripts","Verify keyspace names against cluster metadata at startup","Use IF EXISTS on ALTER KEYSPACE when absence is acceptable"],"tags":["cql","keyspace","schema","not-found"],"backgroundTag":"resource-not-found","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}