apache/cassandra · error · InvalidRequestException
No keyspace has been specified. USE a keyspace, or explicitl
Error message
No keyspace has been specified. USE a keyspace, or explicitly specify keyspace.tablename
What it means
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.
Source
Thrown at src/java/org/apache/cassandra/service/ClientState.java:393
public InetSocketAddress getRemoteAddress()
{
return remoteAddress;
}
InetAddress getClientAddress()
{
return isInternal ? null : remoteAddress.getAddress();
}
public String getRawKeyspace()
{
return keyspace;
}
public String getKeyspace() throws InvalidRequestException
{
if (keyspace == null)
throw new InvalidRequestException("No keyspace has been specified. USE a keyspace, or explicitly specify keyspace.tablename");
return keyspace;
}
public void setKeyspace(String ks)
{
// Skip keyspace validation for non-authenticated users. Apparently, some client libraries
// call set_keyspace() before calling login(), and we have to handle that.
if (user != null && Schema.instance.getKeyspaceMetadata(ks) == null)
throw new InvalidRequestException("Keyspace '" + ks + "' does not exist");
keyspace = ks;
}
/**
* Attempts to login the given user.
*/
public void login(AuthenticatedUser user)
{
if (user.isAnonymous() || canLogin(user))View on GitHub (pinned to 88fd0f6a0e)
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.
Example fix
// before
session.execute("SELECT * FROM users");
// after
session.execute("SELECT * FROM mykeyspace.users");
// or: session.execute("USE mykeyspace"); session.execute("SELECT * FROM users"); Defensive patterns
Strategy: try-catch
Validate before calling
String ks = session.getLoggedKeyspace();
if (ks == null && !statementFullyQualified(cql))
throw new IllegalArgumentException("Set a keyspace with USE or fully qualify table names"); Try / catch
try { session.execute(cql); } catch (InvalidRequestException e) {
if (e.getMessage().startsWith("No keyspace has been specified")) { session.execute("USE " + defaultKs); retry(cql); }
} Prevention
- 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)
When it happens
Trigger: 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.
Common situations: 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.
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 + "'
- Keyspace '${ks}' does not exist
- You have not set a keyspace for this session
- Invalid null value of timestamp
AI-assisted analysis of apache/cassandra@88fd0f6a0e (2026-09-10).
Data as JSON: /api/errors/6b2bebee7885e990.
Report an issue: GitHub.