{"record":{"id":"48dc0522d7ea99ef","repo":"apache/cassandra","slug":"keyspace-ks-does-not-exist","errorCode":null,"errorMessage":"Keyspace '${ks}' does not exist","messagePattern":"Keyspace '(.+?)' does not exist","errorType":"exception","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/service/ClientState.java","lineNumber":402,"sourceCode":"\n    public String getRawKeyspace()\n    {\n        return keyspace;\n    }\n\n    public String getKeyspace() throws InvalidRequestException\n    {\n        if (keyspace == null)\n            throw new InvalidRequestException(\"No keyspace has been specified. USE a keyspace, or explicitly specify keyspace.tablename\");\n        return keyspace;\n    }\n\n    public void setKeyspace(String ks)\n    {\n        // Skip keyspace validation for non-authenticated users. Apparently, some client libraries\n        // call set_keyspace() before calling login(), and we have to handle that.\n        if (user != null && Schema.instance.getKeyspaceMetadata(ks) == null)\n            throw new InvalidRequestException(\"Keyspace '\" + ks + \"' does not exist\");\n        keyspace = ks;\n    }\n\n    /**\n     * Attempts to login the given user.\n     */\n    public void login(AuthenticatedUser user)\n    {\n        if (user.isAnonymous() || canLogin(user))\n        {\n            this.user = user;\n            this.superuserStatus = null;\n        }\n        else\n            throw new AuthenticationException(String.format(\"%s is not permitted to log in\", user.getName()));\n    }\n\n    private boolean canLogin(AuthenticatedUser user)","sourceCodeStart":384,"sourceCodeEnd":420,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/service/ClientState.java#L384-L420","documentation":"ClientState.setKeyspace(ks) validates the requested keyspace against the schema (for authenticated users) and throws InvalidRequestException if Schema has no metadata for that name. This prevents sessions from binding to nonexistent keyspaces so subsequent statements fail fast with a clear message.","triggerScenarios":"Executing `USE nonexistent_ks` or calling ClientState.setKeyspace(\"ks\") where Schema.instance.getKeyspaceMetadata(ks) returns null — typo in keyspace name, keyspace dropped, or driver auto-configured with a wrong keyspace.","commonSituations":"Typo'd keyspace in a connection string (e.g. driver session keyspace); race where another client DROP KEYSPACE'd it mid-session; environment-specific config pointing at a keyspace that only exists elsewhere; running migrations against a fresh cluster.","solutions":["Correct the keyspace name (verify with `DESCRIBE keyspaces` / system_schema.keyspaces).","Create the keyspace first: `CREATE KEYSPACE ks WITH replication = {...}`.","Update the driver's session keyspace configuration to a keyspace that exists on the target cluster.","Handle the drop race: re-check existence and recreate before issuing USE."],"exampleFix":"// before\nsession.execute(\"USE salesdb\"); // keyspace doesn't exist\n// after\nsession.execute(\"CREATE KEYSPACE IF NOT EXISTS salesdb WITH replication = {'class':'SimpleStrategy','replication_factor':3}\");\nsession.execute(\"USE salesdb\");","handlingStrategy":"validation","validationCode":"boolean exists = session.execute(\"SELECT keyspace_name FROM system_schema.keyspaces WHERE keyspace_name = ?\", ks).iterator().hasNext();\nif (!exists) throw new IllegalStateException(\"Keyspace \" + ks + \" does not exist\");","typeGuard":null,"tryCatchPattern":"try { session.execute(\"USE \" + ks); } catch (InvalidRequestException e) {\n    if (e.getMessage().contains(\"does not exist\")) { createKeyspaceIfMissing(ks); session.execute(\"USE \" + ks); }\n}","preventionTips":["Verify keyspace names against system_schema.keyspaces before USE","Use CREATE KEYSPACE IF NOT EXISTS in bootstrap/migration scripts","Keep environment configs (dev/staging/prod) with correct keyspace names","Handle concurrent DROP KEYSPACE races in long-lived sessions"],"tags":["cql","keyspace","invalid-request"],"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"}