apache/cassandra · error · InvalidRequestException

Keyspace name ' ' doesn't match table name

Error message

Keyspace name '%s' doesn't match table name '%s'

What it means

CREATE INDEX validates that the keyspace explicitly given in the table name matches the resolved keyspace (from table name, index name, or session keyspace). A fully-qualified table name whose keyspace disagrees with the resolved one is rejected as ambiguous/inconsistent.

Solutions

  1. Remove the keyspace qualifier from the table name, or make it match the session keyspace.
  2. Fully qualify BOTH the table and the index with the same keyspace: CREATE INDEX ks.idx ON ks.t(col).
  3. Fix the cqlsh/script session: USE <correct_keyspace>; before running the statement.

Example fix

// before (session is on ks2)
CREATE INDEX ON ks1.t (col);

// after
USE ks1;
CREATE INDEX ON t (col);
Defensive patterns

Strategy: validation

Validate before calling

if (tableName.includesKeyspace() && session.getLoggedKeyspace() != null && !tableName.getKeyspace().equals(session.getLoggedKeyspace())) throw new IllegalArgumentException("Table keyspace disagrees with session keyspace");

Try / catch

try { session.execute(ddl); } catch (InvalidQueryException e) { if (e.getMessage().startsWith("Keyspace name")) { /* strip keyspace qualifiers or align with USE */ } else throw e; }

Prevention

When it happens

Trigger: Run e.g. `CREATE INDEX ON otherks.t (col)` while the statement resolves a different keyspaceName — specifically when tableName.hasKeyspace() and that keyspace differs from the computed keyspaceName (which can come from the index name or the session's USE keyspace).

Common situations: Copy-pasting a statement from one environment/namespace to another; scripts with a hardcoded keyspace in the table name executed against a session connected to a different keyspace; confusion between keyspace qualifiers on the table vs index name.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

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

                   List<IndexTarget.Raw> rawIndexTargets,
                   IndexAttributes attrs,
                   boolean ifNotExists)
        {
            this.tableName = tableName;
            this.indexName = indexName;
            this.rawIndexTargets = rawIndexTargets;
            this.attrs = attrs;
            this.ifNotExists = ifNotExists;
        }

        public CreateIndexStatement prepare(ClientState state)
        {
            String keyspaceName = tableName.hasKeyspace()
                                ? tableName.getKeyspace()
                                : indexName.hasKeyspace() ? indexName.getKeyspace() : state.getKeyspace();

            if (tableName.hasKeyspace() && !keyspaceName.equals(tableName.getKeyspace()))
                throw ire(KEYSPACE_DOES_NOT_MATCH_TABLE, keyspaceName, tableName);

            if (indexName.hasKeyspace() && !keyspaceName.equals(indexName.getKeyspace()))
                throw ire(KEYSPACE_DOES_NOT_MATCH_INDEX, keyspaceName, tableName);
            
            // Set the configured default 2i implementation if one isn't specified with USING:
            if (attrs.customClass == null)
            {
                if (DatabaseDescriptor.getDefaultSecondaryIndexEnabled())
                    attrs.customClass = DatabaseDescriptor.getDefaultSecondaryIndex();
                else
                    // However, operators may require an implementation be specified
                    throw ire(MUST_SPECIFY_INDEX_IMPLEMENTATION);
            }
            
            // If we explicitly specify the index type "legacy_local_table", we can just clear the custom class, and the
            // non-custom 2i creation process will begin. Otherwise, if an index type has been specified with 
            // USING, make sure the appropriate custom index is created.
            if (attrs.customClass != null)

View on GitHub (pinned to 88fd0f6a0e)