apache/cassandra · error · InvalidRequestException
Keyspace '${ks}' does not exist
Error message
Keyspace '${ks}' does not exist What it means
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.
Source
Thrown at src/java/org/apache/cassandra/service/ClientState.java:402
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))
{
this.user = user;
this.superuserStatus = null;
}
else
throw new AuthenticationException(String.format("%s is not permitted to log in", user.getName()));
}
private boolean canLogin(AuthenticatedUser user)View on GitHub (pinned to 88fd0f6a0e)
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.
Example fix
// before
session.execute("USE salesdb"); // keyspace doesn't exist
// after
session.execute("CREATE KEYSPACE IF NOT EXISTS salesdb WITH replication = {'class':'SimpleStrategy','replication_factor':3}");
session.execute("USE salesdb"); Defensive patterns
Strategy: validation
Validate before calling
boolean exists = session.execute("SELECT keyspace_name FROM system_schema.keyspaces WHERE keyspace_name = ?", ks).iterator().hasNext();
if (!exists) throw new IllegalStateException("Keyspace " + ks + " does not exist"); Try / catch
try { session.execute("USE " + ks); } catch (InvalidRequestException e) {
if (e.getMessage().contains("does not exist")) { createKeyspaceIfMissing(ks); session.execute("USE " + ks); }
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — 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
- 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/48dc0522d7ea99ef.
Report an issue: GitHub.