apache/cassandra · error · InvalidRequestException
You have not set a keyspace for this session
Error message
You have not set a keyspace for this session
What it means
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.
Source
Thrown at src/java/org/apache/cassandra/service/ClientState.java:736
"always use fully qualified table names (e.g. <keyspace>.<table>). " +
"Keyspace used: %s, statement keyspace: %s, statement id: %s", getRawKeyspace(), preparedKeyspace, statementId));
issuedPreparedStatementsUseWarning = true;
}
}
public void warnAboutUneligiblePreparedStatement(MD5Digest statementId)
{
if (!issuedWarningForUneligiblePreparedStatements)
{
ClientWarn.instance.warn(String.format("Prepared statements for other than modification and selection statements should be avoided, statement id: %s", statementId));
issuedWarningForUneligiblePreparedStatements = true;
}
}
private static void validateKeyspace(String keyspace)
{
if (keyspace == null)
throw new InvalidRequestException("You have not set a keyspace for this session");
}
public AuthenticatedUser getUser()
{
return user;
}
private Set<Permission> authorize(IResource resource)
{
return user.getPermissions(resource);
}
}
View on GitHub (pinned to 88fd0f6a0e)
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.
Example fix
// before
session.execute("CREATE TABLE users (id uuid PRIMARY KEY, name text)");
// after
session.execute("CREATE TABLE myapp.users (id uuid PRIMARY KEY, name text)");
// or
session.execute("USE myapp"); Defensive patterns
Strategy: validation
Validate before calling
// Java driver
if (session.getKeyspace().isEmpty() && !statementIsKeyspaceQualified(query))
throw new IllegalArgumentException("Query has no keyspace and session keyspace is unset"); Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.
Related errors
- Index %s is not in the same keyspace as the queried table.
- Unknown keyspace: '" + keyspaceName + "'
- No keyspace has been specified. USE a keyspace, or explicitl
- Keyspace '${ks}' does not exist
- Invalid null value of timestamp
AI-assisted analysis of apache/cassandra@88fd0f6a0e (2026-09-10).
Data as JSON: /api/errors/8b78e3cb76c97861.
Report an issue: GitHub.