{"record":{"id":"d28e86fc6286f25d","repo":"apache/cassandra","slug":"counter-type-is-not-supported-for-primary-key-colu","errorCode":null,"errorMessage":"counter type is not supported for PRIMARY KEY column '%s'","messagePattern":"counter type is not supported for PRIMARY KEY column '(.+?)'","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/statements/schema/CreateTableStatement.java","lineNumber":312,"sourceCode":"            ColumnProperties properties = columns.get(column);\n            if (null == properties)\n                throw ire(\"Unknown column '%s' referenced in PRIMARY KEY for table '%s'\", column, tableName);\n\n            if (!primaryKeyColumns.add(column))\n                throw ire(\"Duplicate column '%s' in PRIMARY KEY clause for table '%s'\", column, tableName);\n\n            AbstractType<?> type = properties.type;\n            if (type.isMultiCell())\n            {\n                CQL3Type cqlType = properties.cqlType;\n                if (type.isCollection())\n                    throw ire(\"Invalid non-frozen collection type %s for PRIMARY KEY column '%s'\", cqlType, column);\n                else\n                    throw ire(\"Invalid non-frozen user-defined type %s for PRIMARY KEY column '%s'\", cqlType, column);\n            }\n\n            if (type.isCounter())\n                throw ire(\"counter type is not supported for PRIMARY KEY column '%s'\", column);\n\n            if (type.referencesDuration())\n                throw ire(\"duration type is not supported for PRIMARY KEY column '%s'\", column);\n\n            if (staticColumns.contains(column))\n                throw ire(\"Static column '%s' cannot be part of the PRIMARY KEY\", column);\n        });\n\n        List<ColumnProperties> partitionKeyColumnProperties = new ArrayList<>();\n        List<ColumnProperties> clusteringColumnProperties = new ArrayList<>();\n\n        partitionKeyColumns.forEach(column ->\n        {\n            ColumnProperties columnProperties = columns.remove(column);\n            partitionKeyColumnProperties.add(columnProperties);\n        });\n\n        clusteringColumns.forEach(column ->","sourceCodeStart":294,"sourceCodeEnd":330,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/statements/schema/CreateTableStatement.java#L294-L330","documentation":"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.","triggerScenarios":"`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.","commonSituations":"Trying to make a counter the row key or clustering identifier; confusing counter usage rules after using regular columns as keys.","solutions":["Move the counter column out of the PRIMARY KEY into regular value columns","Use a non-counter type (int/bigint maintained manually) if the key must be numeric","Create the key from non-counter columns and keep counters as values"],"exampleFix":"// before\nCREATE TABLE t (k counter, PRIMARY KEY (k));\n// after\nCREATE TABLE t (k int PRIMARY KEY, hits counter);","handlingStrategy":"validation","validationCode":"if (keyColumnType.isCounter()) throw new IllegalArgumentException(\"counter columns cannot be PRIMARY KEY components\");","typeGuard":null,"tryCatchPattern":"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; }","preventionTips":["Design counter tables with explicit non-counter keys","Never use counter as partition or clustering type","Review counter table designs against Cassandra counter limitations"],"tags":["cql","counter","primary-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-17T15:17:12.973Z"}