{"record":{"id":"567be18067d83e8a","repo":"apache/cassandra","slug":"cannot-create-secondary-index-on-the-only-partitio","errorCode":null,"errorMessage":"Cannot create secondary index on the only partition key column %s","messagePattern":"Cannot create secondary index on the only partition key column (.+?)","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/statements/schema/CreateIndexStatement.java","lineNumber":289,"sourceCode":"                throw ire(TUPLES_WITH_DURATIONS_NOT_SUPPORTED);\n\n            if (column.type.isUDT())\n                throw  ire(UDTS_WITH_DURATIONS_NOT_SUPPORTED);\n\n            throw ire(DURATIONS_NOT_SUPPORTED);\n        }\n\n        if (table.isCompactTable())\n        {\n            TableMetadata.CompactTableMetadata compactTable = (TableMetadata.CompactTableMetadata) table;\n            if (column.isPrimaryKeyColumn())\n                throw new InvalidRequestException(PRIMARY_KEY_IN_COMPACT_STORAGE);\n            if (compactTable.compactValueColumn.equals(column))\n                throw new InvalidRequestException(COMPACT_COLUMN_IN_COMPACT_STORAGE);\n        }\n\n        if (column.isPartitionKey() && table.partitionKeyColumns().size() == 1)\n            throw ire(ONLY_PARTITION_KEY, column);\n\n        if (target.type == Type.FULL && isNonSAIIndex && (!baseType.isCollection() || column.type.isMultiCell()))\n            throw ire(FULL_ON_FROZEN_COLLECTIONS);\n\n        if (!baseType.isCollection() && target.type != Type.SIMPLE)\n            throw ire(NON_COLLECTION_SIMPLE_INDEX, target.type, column);\n\n        // Frozen collections are only supported with SAI indexes.\n        if (isNonSAIIndex && baseType.isCollection() && !column.type.isMultiCell())\n        {\n            if (target.type == Type.VALUES || target.type == Type.KEYS || target.type == Type.KEYS_AND_VALUES)\n            {\n                throw ire(CREATE_ON_FROZEN_COLUMN, target.type.toString(), column.name, column.name);\n            }\n        }\n\n        if (!(baseType instanceof MapType) && (target.type == Type.KEYS || target.type == Type.KEYS_AND_VALUES ))\n            throw ire(CREATE_WITH_NON_MAP_TYPE, target.type, column);","sourceCodeStart":271,"sourceCodeEnd":307,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/statements/schema/CreateIndexStatement.java#L271-L307","documentation":"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.","triggerScenarios":"`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);","commonSituations":"ORMs or admin tools auto-generating indexes on every column; developers used to relational DBs adding indexes on primary keys out of habit.","solutions":["Drop the index creation; queries on the single partition key are already efficient without an index.","If you meant to index a non-key column, target that column instead.","If the query shape needs a different primary key, redesign the table or add a query-driven table (denormalization)."],"exampleFix":"// before\nCREATE TABLE users (id uuid PRIMARY KEY, email text);\nCREATE INDEX ON users (id);\n// after\nCREATE INDEX ON users (email);","handlingStrategy":"validation","validationCode":"if (column.isPartitionKey() && table.partitionKeyColumns().length === 1) throw new Error('indexing the sole partition key is redundant and rejected');","typeGuard":null,"tryCatchPattern":"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; }","preventionTips":["Never generate indexes for primary key columns","Remember Cassandra partition key lookups need no index","Drive index creation from query patterns, not column lists"],"tags":["cassandra","cql","secondary-index","partition-key"],"backgroundTag":"unsupported-operation","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}