{"record":{"id":"a0c80d90864e30a2","repo":"apache/cassandra","slug":"cannot-mix-counter-and-non-counter-columns-in-the","errorCode":null,"errorMessage":"Cannot mix counter and non counter columns in the same table","messagePattern":"Cannot mix counter and non counter columns in the same table","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/statements/schema/CreateTableStatement.java","lineNumber":381,"sourceCode":"        }\n        else\n        {\n            // Static columns only make sense if we have at least one clustering column. Otherwise everything is static anyway\n            if (clusteringColumns.isEmpty() && !staticColumns.isEmpty())\n                throw ire(\"Static columns are only useful (and thus allowed) if the table has at least one clustering column\");\n        }\n\n        /*\n         * Counter table validation\n         */\n\n        boolean hasCounters = rawColumns.values().stream().anyMatch(c -> c.rawType.isCounter());\n        if (hasCounters)\n        {\n            // We've handled anything that is not a PRIMARY KEY so columns only contains NON-PK columns. So\n            // if it's a counter table, make sure we don't have non-counter types\n            if (columns.values().stream().anyMatch(t -> !t.type.isCounter()))\n                throw ire(\"Cannot mix counter and non counter columns in the same table\");\n\n            if (params.defaultTimeToLive > 0)\n                throw ire(\"Cannot set %s on a table with counters\", TableParams.Option.DEFAULT_TIME_TO_LIVE);\n        }\n\n        /*\n         * Create the builder\n         */\n\n        TableMetadata.Builder builder = TableMetadata.builder(keyspaceName, tableName);\n\n        if (attrs.hasProperty(TableAttributes.ID))\n            builder.id(attrs.getId());\n\n        builder.isCounter(hasCounters)\n               .params(params);\n\n        for (int i = 0; i < partitionKeyColumns.size(); i++)","sourceCodeStart":363,"sourceCodeEnd":399,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/statements/schema/CreateTableStatement.java#L363-L399","documentation":"Counter columns cannot coexist with non-counter columns in the same table (except within the primary key). The builder checks whether any column uses the counter type, and if so verifies every non-primary-key column is also a counter; otherwise it throws this InvalidRequest.","triggerScenarios":"CREATE TABLE t (pk int, c counter, v text, PRIMARY KEY (pk)) — mixing a counter column with a regular text column.","commonSituations":"Adding a counter column to an existing normal table's schema or vice versa; modeling statistics (counters) alongside descriptive fields in one table.","solutions":["Split into two tables: one for counters and one for non-counter data.","Change regular columns to counter type if only counts are needed (with default 0).","Remove the counter column if regular columns are what you need."],"exampleFix":"// before\nCREATE TABLE stats (pk int, hits counter, label text, PRIMARY KEY (pk));\n// after\nCREATE TABLE stats (pk int, hits counter, PRIMARY KEY (pk));\nCREATE TABLE meta (pk int, label text, PRIMARY KEY (pk));","handlingStrategy":"validation","validationCode":"// Ensure a table with a counter column has only counter columns outside the PK:\nboolean hasCounter = true;   // any column of type counter\nList<String> nonPkTypes = List.of(\"counter\"); // types of non-PK columns\nif (hasCounter && nonPkTypes.stream().anyMatch(t -> !t.equals(\"counter\"))) throw new IllegalArgumentException(\"Cannot mix counter and non-counter columns\");","typeGuard":null,"tryCatchPattern":"try { session.execute(ddl); } catch (com.datastax.driver.core.exceptions.InvalidQueryException e) { if (e.getMessage().contains(\"Cannot mix counter\")) { /* split tables */ } else throw e; }","preventionTips":["Dedicate tables to counters; keep descriptive data in a companion table.","Check counter usage before ALTERing a table to add regular columns.","Document counter-table restrictions in schema design guides."],"tags":["cql","schema","create-table","counters"],"backgroundTag":"schema-validation-failed","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"}