{"record":{"id":"30930b8b7b26cab9","repo":"apache/cassandra","slug":"cannot-set-s-on-a-table-with-counters","errorCode":null,"errorMessage":"Cannot set %s on a table with counters","messagePattern":"Cannot set (.+?) on a table with counters","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/statements/schema/CreateTableStatement.java","lineNumber":384,"sourceCode":"            // 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++)\n        {\n            ColumnProperties properties = partitionKeyColumnProperties.get(i);\n            ColumnIdentifier columnIdentifier = partitionKeyColumns.get(i);","sourceCodeStart":366,"sourceCodeEnd":402,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/statements/schema/CreateTableStatement.java#L366-L402","documentation":"Counter tables cannot have a default_time_to_live greater than zero, because TTL semantics on counters are restricted. The builder throws this error when a table containing counter columns specifies any table parameter that includes DEFAULT_TIME_TO_LIVE with a positive value.","triggerScenarios":"CREATE TABLE t (pk int PRIMARY KEY, c counter) WITH default_time_to_live = 86400;","commonSituations":"Reusing a table-options template (with TTL) for a counter table; automated schema generation applying uniform TTL settings.","solutions":["Remove or set default_time_to_live = 0 for the counter table.","Handle expiry at the application level or move counters to a non-counter table (counters can't be mixed with other columns)."],"exampleFix":"// before\nCREATE TABLE t (pk int PRIMARY KEY, c counter) WITH default_time_to_live = 86400;\n// after\nCREATE TABLE t (pk int PRIMARY KEY, c counter);","handlingStrategy":"validation","validationCode":"// Counter tables must not set a positive default_time_to_live:\nboolean isCounterTable = true;\nint defaultTtl = 86400;\nif (isCounterTable && defaultTtl > 0) throw new IllegalArgumentException(\"default_time_to_live not allowed on counter tables\");","typeGuard":null,"tryCatchPattern":"try { session.execute(ddl); } catch (com.datastax.driver.core.exceptions.InvalidQueryException e) { if (e.getMessage().contains(\"Cannot set\") && e.getMessage().contains(\"counters\")) { /* remove TTL */ } else throw e; }","preventionTips":["Exclude default_time_to_live from counter-table templates.","Audit DDL generators that uniformly apply TTL options.","Handle counter expiry in application logic instead."],"tags":["cql","schema","create-table","counters","ttl"],"backgroundTag":"invalid-config-value","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"}