{"record":{"id":"8b78e3cb76c97861","repo":"apache/cassandra","slug":"you-have-not-set-a-keyspace-for-this-session","errorCode":null,"errorMessage":"You have not set a keyspace for this session","messagePattern":"You have not set a keyspace for this session","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/service/ClientState.java","lineNumber":736,"sourceCode":"                                                   \"always use fully qualified table names (e.g. <keyspace>.<table>). \" +\n                                                   \"Keyspace used: %s, statement keyspace: %s, statement id: %s\", getRawKeyspace(), preparedKeyspace, statementId));\n            issuedPreparedStatementsUseWarning = true;\n        }\n    }\n\n    public void warnAboutUneligiblePreparedStatement(MD5Digest statementId)\n    {\n        if (!issuedWarningForUneligiblePreparedStatements)\n        {\n            ClientWarn.instance.warn(String.format(\"Prepared statements for other than modification and selection statements should be avoided, statement id: %s\", statementId));\n            issuedWarningForUneligiblePreparedStatements = true;\n        }\n    }\n\n    private static void validateKeyspace(String keyspace)\n    {\n        if (keyspace == null)\n            throw new InvalidRequestException(\"You have not set a keyspace for this session\");\n    }\n\n    public AuthenticatedUser getUser()\n    {\n        return user;\n    }\n\n    private Set<Permission> authorize(IResource resource)\n    {\n        return user.getPermissions(resource);\n    }\n\n}\n","sourceCodeStart":718,"sourceCodeEnd":750,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/service/ClientState.java#L718-L750","documentation":"InvalidRequestException thrown by ClientState.validateKeyspace() when a statement that must be executed in the context of a keyspace is issued while the session has no USE keyspace set and the statement itself does not name one. ClientState's keyspace is set via USE or a keyspace-qualified statement.","triggerScenarios":"Executing unqualified statements like 'CREATE TABLE t (...)', 'ALTER TABLE t ...', or a SELECT from an unqualified table on a session where setKeyspace/USE was never called; driver sessions created without a keyspace while sending schema statements that omit the keyspace name.","commonSituations":"cqlsh sessions where the user forgot 'USE myks;' after connecting; drivers configured without a keyspace and migrations using unqualified DDL; scripts copied from environments where USE was implicit.","solutions":["Run USE <keyspace>; in the session before issuing unqualified statements.","Fully qualify all table names: CREATE TABLE myks.t (...).","Set a default keyspace on the driver session (withKeyspace(\"myks\") / setKeyspace).","In migration tools, prefix every object reference with the keyspace rather than relying on session state."],"exampleFix":"// before\nsession.execute(\"CREATE TABLE users (id uuid PRIMARY KEY, name text)\");\n// after\nsession.execute(\"CREATE TABLE myapp.users (id uuid PRIMARY KEY, name text)\");\n// or\nsession.execute(\"USE myapp\");","handlingStrategy":"validation","validationCode":"// Java driver\nif (session.getKeyspace().isEmpty() && !statementIsKeyspaceQualified(query))\n    throw new IllegalArgumentException(\"Query has no keyspace and session keyspace is unset\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always set a session keyspace or fully qualify object names in every statement.","Lint migration scripts for unqualified table names.","Configure the driver withKeyspace() where the application uses a single keyspace."],"tags":["cql","session","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"}