apache/cassandra · error · InvalidRequestException

Keyspace '%s' doesn't exist

Error message

Keyspace '%s' doesn't exist

What it means

CREATE INDEX validates the target keyspace exists before resolving the table. If schema.getNullable(keyspaceName) returns null, it throws this InvalidRequestException because an index cannot be created in a nonexistent keyspace.

Source

Thrown at src/java/org/apache/cassandra/cql3/statements/schema/CreateIndexStatement.java:157

    public boolean compatibleWith(ClusterMetadata metadata)
    {
        return metadata.directory.commonSerializationVersion.isAtLeast(Version.V0);
    }

    @Override
    public Keyspaces apply(ClusterMetadata metadata)
    {
        attrs.validate();

        Guardrails.createSecondaryIndexesEnabled.ensureEnabled("Creating secondary indexes", state);

        if (attrs.isCustom && attrs.customClass.equals(SASIIndex.class.getName()) && !DatabaseDescriptor.getSASIIndexesEnabled())
            throw new InvalidRequestException(SASI_INDEX_DISABLED);

        Keyspaces schema = metadata.schema.getKeyspaces();
        KeyspaceMetadata keyspace = schema.getNullable(keyspaceName);
        if (null == keyspace)
            throw ire(KEYSPACE_DOES_NOT_EXIST, keyspaceName);

        TableMetadata table = keyspace.getTableOrViewNullable(tableName);
        if (null == table)
            throw ire(TABLE_DOES_NOT_EXIST, tableName);

        if (null != indexName && keyspace.hasIndex(indexName))
        {
            if (ifNotExists)
                return schema;

            throw ire(INDEX_ALREADY_EXISTS, indexName);
        }

        if (table.isCounter())
            throw ire(COUNTER_TABLES_NOT_SUPPORTED);

        if (table.isView())
            throw ire(MATERIALIZED_VIEWS_NOT_SUPPORTED);

View on GitHub (pinned to 88fd0f6a0e)

Solutions

  1. Create the keyspace first or correct its spelling (DESCRIBE KEYSPACES to verify)
  2. Ensure USE <keyspace> was issued or the table name is keyspace-qualified correctly
  3. Point the client/cqlsh at the intended cluster where the keyspace exists

Example fix

// before
CREATE INDEX ON analytics.users(email);   -- analytics does not exist
// after
CREATE KEYSPACE IF NOT EXISTS analytics WITH replication = {'class':'SimpleStrategy','replication_factor':1};
CREATE INDEX ON analytics.users(email);
Defensive patterns

Strategy: validation

Validate before calling

ResultSet rs = session.execute("SELECT keyspace_name FROM system_schema.keyspaces WHERE keyspace_name = ?", ks);
if (rs.all().isEmpty()) throw new IllegalStateException("Keyspace does not exist: " + ks);

Type guard

boolean keyspaceExists(Session s, String ks) { return !s.execute("SELECT keyspace_name FROM system_schema.keyspaces WHERE keyspace_name = ?", ks).all().isEmpty(); }

Try / catch

try { session.execute(createIndexStmt); } catch (InvalidRequestException e) { if (e.getMessage().startsWith("Keyspace") && e.getMessage().contains("doesn't exist")) { /* create keyspace or fix name */ } else throw e; }

Prevention

When it happens

Trigger: CREATE INDEX / CREATE CUSTOM INDEX on table 'ks.tbl' where keyspace ks does not exist; typo in the keyspace name.

Common situations: Misspelled keyspace in DDL scripts; running index migrations on a cluster missing the keyspace; connecting to the wrong environment/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


AI-assisted analysis of apache/cassandra@88fd0f6a0e (2026-09-10). Data as JSON: /api/errors/284539838fed4dcf. Report an issue: GitHub.