{"record":{"id":"0fbf6e0d30ae2015","repo":"apache/cassandra","slug":"keyspace-s-doesn-t-exist-0fbf6e","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/CreateTableStatement.java","lineNumber":152,"sourceCode":"    public String cql()\n    {\n        if (expandedCql != null)\n            return expandedCql;\n        return super.cql();\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        Keyspaces schema = metadata.schema.getKeyspaces();\n        KeyspaceMetadata keyspace = schema.getNullable(keyspaceName);\n        if (null == keyspace)\n            throw ire(\"Keyspace '%s' doesn't exist\", keyspaceName);\n\n        if (keyspace.hasTable(tableName))\n        {\n            if (ifNotExists)\n                return schema;\n\n            throw new AlreadyExistsException(keyspaceName, tableName);\n        }\n\n        // add all user functions to be able to give a good error message to the user if the alter references\n        // a function from another keyspace\n        UserFunctions.Builder ufBuilder = UserFunctions.builder().add();\n        for (KeyspaceMetadata ksm : schema)\n            ufBuilder.add(ksm.userFunctions);\n\n        TableMetadata.Builder builder = builder(keyspace.types, ufBuilder.build()).epoch(metadata.nextEpoch());\n\n        // We do not want to set table ID here just yet, since we are using CQL for serialising a fully expanded CREATE TABLE statement.","sourceCodeStart":134,"sourceCodeEnd":170,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/statements/schema/CreateTableStatement.java#L134-L170","documentation":"Thrown while applying CREATE TABLE when metadata.schema.getKeyspaces().getNullable(keyspaceName) returns null. The statement names a keyspace that is not present in the cluster's schema, so the table cannot be created until the keyspace exists (or the name is corrected).","triggerScenarios":"`CREATE TABLE missing_ks.t (...);` or `CREATE TABLE t (...);` while the session's USE keyspace does not exist — schema.getNullable(keyspaceName) returns null in apply().","commonSituations":"Running table DDL before the keyspace DDL in a migration script; typo in the keyspace name; connecting to a fresh cluster and assuming a default keyspace; migrations applied against the wrong environment.","solutions":["Create the keyspace first: CREATE KEYSPACE ks WITH replication = {...};","Fix the keyspace name typo or qualify the table with an existing keyspace: CREATE TABLE existing_ks.t (...).","Run `DESCRIBE KEYSPACES` (or query system_schema.keyspaces) to confirm the keyspace exists."],"exampleFix":"// before\nCREATE TABLE myapp.users (id uuid PRIMARY KEY, name text);\n\n// after\nCREATE KEYSPACE myapp WITH replication = {'class': 'NetworkTopologyStrategy', 'dc1': 3};\nCREATE TABLE myapp.users (id uuid PRIMARY KEY, name text);","handlingStrategy":"validation","validationCode":"Row r = session.execute(\"SELECT keyspace_name FROM system_schema.keyspaces WHERE keyspace_name = ?\", ks).one(); if (r == null) throw new IllegalArgumentException(\"Keyspace does not exist: \" + ks);","typeGuard":"boolean keyspaceExists(Session s, String ks) { return s.execute(\"SELECT keyspace_name FROM system_schema.keyspaces WHERE keyspace_name = ?\", ks).one() != null; }","tryCatchPattern":"try { session.execute(createTableDdl); } catch (InvalidQueryException e) { if (e.getMessage().endsWith(\"doesn't exist\")) { /* run CREATE KEYSPACE first or fix the name */ } else throw e; }","preventionTips":["Order migrations: keyspaces before tables","Verify keyspace existence with system_schema before DDL","Beware session USE keyspace mismatch across environments"],"tags":["cassandra","cql","keyspace","table"],"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"}