apache/cassandra · error · InvalidRequestException

counter type is not supported for PRIMARY KEY column

Error message

counter type is not supported for PRIMARY KEY column '%s'

What it means

Thrown from CreateTableStatement's builder while validating PRIMARY KEY columns when a key column's type is a counter (type.isCounter()). Counter columns cannot participate in a primary key because counter updates are not idempotent and cannot be used for row identity or clustering comparisons.

Solutions

  1. Move the counter column out of the PRIMARY KEY into regular value columns
  2. Use a non-counter type (int/bigint maintained manually) if the key must be numeric
  3. Create the key from non-counter columns and keep counters as values

Example fix

// before
CREATE TABLE t (k counter, PRIMARY KEY (k));
// after
CREATE TABLE t (k int PRIMARY KEY, hits counter);
Defensive patterns

Strategy: validation

Validate before calling

if (keyColumnType.isCounter()) throw new IllegalArgumentException("counter columns cannot be PRIMARY KEY components");

Try / catch

try { session.execute(ddl); } catch (InvalidRequestException e) { if (e.getMessage().contains("counter type is not supported for PRIMARY KEY")) { /* move counter out of the key */ } else throw e; }

Prevention

When it happens

Trigger: `CREATE TABLE t (k counter, v int, PRIMARY KEY (k))` or `CREATE TABLE t (k int PRIMARY KEY, c counter)` where the counter ends up in the key clause.

Common situations: Trying to make a counter the row key or clustering identifier; confusing counter usage rules after using regular columns as keys.

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/d28e86fc6286f25d. Report an issue: GitHub.

Appendix: source

Thrown at src/java/org/apache/cassandra/cql3/statements/schema/CreateTableStatement.java:312

            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))
                throw ire("Static column '%s' cannot be part of the PRIMARY KEY", column);
        });

        List<ColumnProperties> partitionKeyColumnProperties = new ArrayList<>();
        List<ColumnProperties> clusteringColumnProperties = new ArrayList<>();

        partitionKeyColumns.forEach(column ->
        {
            ColumnProperties columnProperties = columns.remove(column);
            partitionKeyColumnProperties.add(columnProperties);
        });

        clusteringColumns.forEach(column ->

View on GitHub (pinned to 88fd0f6a0e)