{"record":{"id":"9cca01610e6fe548","repo":"apache/cassandra","slug":"cannot-set-the-value-of-counter-column-s-counter","errorCode":null,"errorMessage":"Cannot set the value of counter column %s (counters can only be incremented/decremented, not set)","messagePattern":"Cannot set the value of counter column (.+?) \\(counters can only be incremented/decremented, not set\\)","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/Operation.java","lineNumber":200,"sourceCode":"         */\n        public Operation prepare(String keyspace, ColumnMetadata receiver, TableMetadata metadata) throws InvalidRequestException;\n    }\n\n    public static class SetValue implements RawUpdate\n    {\n        private final Term.Raw value;\n\n        public SetValue(Term.Raw value)\n        {\n            this.value = value;\n        }\n\n        public Operation prepare(TableMetadata metadata, ColumnMetadata receiver, boolean canReadExistingState) throws InvalidRequestException\n        {\n            Term v = value.prepare(metadata.keyspace, receiver);\n\n            if (receiver.type instanceof CounterColumnType)\n                throw new InvalidRequestException(String.format(\"Cannot set the value of counter column %s (counters can only be incremented/decremented, not set)\", receiver.name));\n\n            if (receiver.type.isCollection())\n            {\n                switch (((CollectionType<?>) receiver.type).kind)\n                {\n                    case LIST:\n                        return new Lists.Setter(receiver, v);\n                    case SET:\n                        return new Sets.Setter(receiver, v);\n                    case MAP:\n                        return new Maps.Setter(receiver, v);\n                    default:\n                        throw new AssertionError();\n                }\n            }\n\n            if (receiver.type.isUDT())\n                return new UserTypes.Setter(receiver, v);","sourceCodeStart":182,"sourceCodeEnd":218,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/Operation.java#L182-L218","documentation":"Operations like SetValue.prepare throw this InvalidRequestException when the target column's type is CounterColumnType. Counter columns in Cassandra cannot be assigned a value directly — they only support increment/decrement via CounterMutation — so any SET operation against a counter is rejected at statement preparation time.","triggerScenarios":"Executing UPDATE t SET counter_col = 5 or INSERT with a literal/term assigned to a counter column; also bound terms prepared against a counter receiver.","commonSituations":"Reusing an UPDATE/INSERT builder across schemas where a column changed from regular to counter; migrating SQL habits where assignment of counters is legal; ORMs generating SET clauses without knowing column types.","solutions":["Use UPDATE t SET counter_col = counter_col + 5 to increment (or - n to decrement)","Counters cannot be set to an absolute value; remove direct assignment code paths","If you need settable values, change the column type (requires a new table; ALTER to/from counter is unsupported)","Fix ORM/code generators to detect CounterColumnType and emit increment syntax"],"exampleFix":"// before\nUPDATE stats SET views = 10 WHERE id = 1;\n// after\nUPDATE stats SET views = views + 10 WHERE id = 1;","handlingStrategy":"validation","validationCode":"void assertNotCounterAssignment(com.datastax.driver.core.ColumnMetadata col, String assignment) {\n    if (col.getType().getName() == com.datastax.driver.core.DataType.Name.COUNTER && !assignment.contains(\"+\") && !assignment.contains(\"-\"))\n        throw new IllegalArgumentException(\"Counter column \" + col.getName() + \" cannot be set directly; use counter_col = counter_col + n\");\n}","typeGuard":null,"tryCatchPattern":"try { session.execute(update); } catch (InvalidRequestException e) { if (e.getMessage().startsWith(\"Cannot set the value of counter column\")) { throw new IllegalArgumentException(\"Use increment/decrement syntax for counters\", e); } throw e; }","preventionTips":["Always use col = col + n / col - n for counters","Never generate SET assignments for counter columns in ORMs","Remember counters can't be set, altered into, or reset — model accordingly","Check column types via cluster metadata before building UPDATE statements"],"tags":["cql","counter","update"],"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"}