apache/cassandra · error · InvalidRequestException

Cannot create secondary index on the only partition key colu

Error message

Cannot create secondary index on the only partition key column %s

What it means

Creating a secondary index on the table's single partition key column is forbidden: such an index is useless because every query on that column already resolves directly to a partition, so Cassandra refuses it. Raised in validateIndexTarget when the target column is a partition key and the partition key has exactly one column.

Source

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

                throw ire(TUPLES_WITH_DURATIONS_NOT_SUPPORTED);

            if (column.type.isUDT())
                throw  ire(UDTS_WITH_DURATIONS_NOT_SUPPORTED);

            throw ire(DURATIONS_NOT_SUPPORTED);
        }

        if (table.isCompactTable())
        {
            TableMetadata.CompactTableMetadata compactTable = (TableMetadata.CompactTableMetadata) table;
            if (column.isPrimaryKeyColumn())
                throw new InvalidRequestException(PRIMARY_KEY_IN_COMPACT_STORAGE);
            if (compactTable.compactValueColumn.equals(column))
                throw new InvalidRequestException(COMPACT_COLUMN_IN_COMPACT_STORAGE);
        }

        if (column.isPartitionKey() && table.partitionKeyColumns().size() == 1)
            throw ire(ONLY_PARTITION_KEY, column);

        if (target.type == Type.FULL && isNonSAIIndex && (!baseType.isCollection() || column.type.isMultiCell()))
            throw ire(FULL_ON_FROZEN_COLLECTIONS);

        if (!baseType.isCollection() && target.type != Type.SIMPLE)
            throw ire(NON_COLLECTION_SIMPLE_INDEX, target.type, column);

        // Frozen collections are only supported with SAI indexes.
        if (isNonSAIIndex && baseType.isCollection() && !column.type.isMultiCell())
        {
            if (target.type == Type.VALUES || target.type == Type.KEYS || target.type == Type.KEYS_AND_VALUES)
            {
                throw ire(CREATE_ON_FROZEN_COLUMN, target.type.toString(), column.name, column.name);
            }
        }

        if (!(baseType instanceof MapType) && (target.type == Type.KEYS || target.type == Type.KEYS_AND_VALUES ))
            throw ire(CREATE_WITH_NON_MAP_TYPE, target.type, column);

View on GitHub (pinned to 88fd0f6a0e)

Solutions

  1. Drop the index creation; queries on the single partition key are already efficient without an index.
  2. If you meant to index a non-key column, target that column instead.
  3. If the query shape needs a different primary key, redesign the table or add a query-driven table (denormalization).

Example fix

// before
CREATE TABLE users (id uuid PRIMARY KEY, email text);
CREATE INDEX ON users (id);
// after
CREATE INDEX ON users (email);
Defensive patterns

Strategy: validation

Validate before calling

if (column.isPartitionKey() && table.partitionKeyColumns().length === 1) throw new Error('indexing the sole partition key is redundant and rejected');

Try / catch

try { session.execute(ddl); } catch (e) { if (/Cannot create secondary index on the only partition key column/.test(e.message)) { /* skip index; query is already partition-pruned */ } else throw e; }

Prevention

When it happens

Trigger: `CREATE INDEX ON t (pk_col)` where pk_col is the sole partition key column, e.g. CREATE TABLE t (id text PRIMARY KEY, val int); CREATE INDEX ON t (id);

Common situations: ORMs or admin tools auto-generating indexes on every column; developers used to relational DBs adding indexes on primary keys out of habit.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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