apache/cassandra · error · InvalidRequestException
Duplicate column '%s' in PRIMARY KEY clause for table '%s'
Error message
Duplicate column '%s' in PRIMARY KEY clause for table '%s'
What it means
CreateTableStatement.builder PRIMARY KEY handling: the same column appears more than once across partition key and clustering column lists, detected via a HashSet add; duplicates in PRIMARY KEY are rejected.
Source
Thrown at src/java/org/apache/cassandra/cql3/statements/schema/CreateTableStatement.java:299
if (field.isMultiCell())
throw ire("Non-frozen UDTs with nested non-frozen collections are not supported");
});
}
});
/*
* Deal with PRIMARY KEY columns
*/
HashSet<ColumnIdentifier> primaryKeyColumns = new HashSet<>();
concat(partitionKeyColumns, clusteringColumns).forEach(column ->
{
ColumnProperties properties = columns.get(column);
if (null == properties)
throw ire("Unknown column '%s' referenced in PRIMARY KEY for table '%s'", column, tableName);
if (!primaryKeyColumns.add(column))
throw ire("Duplicate column '%s' in PRIMARY KEY clause for table '%s'", column, tableName);
AbstractType<?> type = properties.type;
if (type.isMultiCell())
{
CQL3Type cqlType = properties.cqlType;
if (type.isCollection())
throw ire("Invalid non-frozen collection type %s for PRIMARY KEY column '%s'", cqlType, column);
else
throw ire("Invalid non-frozen user-defined type %s for PRIMARY KEY column '%s'", cqlType, column);
}
if (type.isCounter())
throw ire("counter type is not supported for PRIMARY KEY column '%s'", column);
if (type.referencesDuration())
throw ire("duration type is not supported for PRIMARY KEY column '%s'", column);
if (staticColumns.contains(column))View on GitHub (pinned to 88fd0f6a0e)
Solutions
- Remove the duplicate occurrence so each column appears once in the PRIMARY KEY
- If a column should be both partition and clustering in effect, reconsider the key design — a column cannot repeat
Example fix
// before CREATE TABLE t (k int, v int, PRIMARY KEY ((k, k), v)); // after CREATE TABLE t (k int, v int, PRIMARY KEY ((k, v)));
Defensive patterns
Strategy: validation
Validate before calling
List<String> seen = new ArrayList<>(); for (String c : allKeyColumns) { if (!seen.add(c)) throw new IllegalArgumentException("Duplicate column in PRIMARY KEY: " + c); } Try / catch
try { session.execute(ddl); } catch (InvalidRequestException e) { if (e.getMessage().contains("Duplicate column") && e.getMessage().contains("PRIMARY KEY")) { /* deduplicate the key clause */ } else throw e; } Prevention
- Build primary key lists programmatically with de-duplication
- Review hand-edited composite keys for repeated components
- Validate DDL with a parser before execution
When it happens
Trigger: `CREATE TABLE t (k int, v int, PRIMARY KEY ((k, k)))` or a column listed both as partition key and clustering column.
Common situations: Hand-editing composite primary keys and duplicating a component; DDL generators concatenating partition and clustering lists incorrectly.
Understand the failure class
Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.
Related errors
- %s constraint can not be specified on a %s key column '%s'
- Cannot drop PRIMARY KEY column %s
- Unknown column '%s' referenced in PRIMARY KEY for table '%s'
- Static column '%s' cannot be part of the PRIMARY KEY
- COMPACT STORAGE with composite PRIMARY KEY allows no more th
AI-assisted analysis of apache/cassandra@88fd0f6a0e (2026-09-10).
Data as JSON: /api/errors/9b5e4b555e29eeb3.
Report an issue: GitHub.