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

  1. Remove the duplicate occurrence so each column appears once in the PRIMARY KEY
  2. 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

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


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