{"record":{"id":"6b2bebee7885e990","repo":"apache/cassandra","slug":"no-keyspace-has-been-specified-use-a-keyspace-or","errorCode":null,"errorMessage":"No keyspace has been specified. USE a keyspace, or explicitly specify keyspace.tablename","messagePattern":"No keyspace has been specified\\. USE a keyspace, or explicitly specify keyspace\\.tablename","errorType":"exception","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/service/ClientState.java","lineNumber":393,"sourceCode":"    public InetSocketAddress getRemoteAddress()\n    {\n        return remoteAddress;\n    }\n\n    InetAddress getClientAddress()\n    {\n        return isInternal ? null : remoteAddress.getAddress();\n    }\n\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))","sourceCodeStart":375,"sourceCodeEnd":411,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/service/ClientState.java#L375-L411","documentation":"ClientState.getKeyspace() returns the keyspace bound to the current client session; if no USE statement was issued and no keyspace was set programmatically, it throws InvalidRequestException telling the client to USE a keyspace or fully qualify table names. Cassandra requires an explicit keyspace context to resolve unqualified table references.","triggerScenarios":"Executing a statement like `SELECT * FROM users` or `INSERT INTO users ...` without a prior `USE mykeyspace` on the session and without qualifying the name as mykeyspace.users; calling ClientState.getKeyspace() on a session with keyspace == null.","commonSituations":"cqlsh sessions opened without USE; application drivers preparing statements without an explicit keyspace; internal code paths (auth/permissions checks) calling getKeyspace on a fresh session.","solutions":["Issue `USE <keyspace>` on the session before running unqualified statements.","Fully qualify table names: `SELECT * FROM mykeyspace.users`.","In drivers, set the session/cluster default keyspace (e.g. connection string keyspace or keyspace on statement execution).","For internal code, use ClientState.forInternalCalls(keyspaceName) to preset the keyspace."],"exampleFix":"// before\nsession.execute(\"SELECT * FROM users\");\n// after\nsession.execute(\"SELECT * FROM mykeyspace.users\");\n// or: session.execute(\"USE mykeyspace\"); session.execute(\"SELECT * FROM users\");","handlingStrategy":"try-catch","validationCode":"String ks = session.getLoggedKeyspace();\nif (ks == null && !statementFullyQualified(cql))\n    throw new IllegalArgumentException(\"Set a keyspace with USE or fully qualify table names\");","typeGuard":null,"tryCatchPattern":"try { session.execute(cql); } catch (InvalidRequestException e) {\n    if (e.getMessage().startsWith(\"No keyspace has been specified\")) { session.execute(\"USE \" + defaultKs); retry(cql); }\n}","preventionTips":["Always fully qualify table names in application queries","Set the driver session keyspace explicitly at connection time","In cqlsh, USE the keyspace at session start (or via -k flag)"],"tags":["cql","keyspace","invalid-request"],"backgroundTag":"missing-required-argument","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"}