{"record":{"id":"d3124fdde89ea41a","repo":"apache/cassandra","slug":"constraint-s-can-be-used-only-for-columns-of-ty","errorCode":null,"errorMessage":"Constraint '%s' can be used only for columns of type %s but it was %s","messagePattern":"Constraint '(.+?)' can be used only for columns of type (.+?) but it was (.+?)","errorType":"validation","errorClass":"InvalidConstraintDefinitionException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/constraints/ColumnConstraint.java","lineNumber":164,"sourceCode":"     */\n    public abstract ConstraintType getConstraintType();\n\n\n    /**\n     * Tells what types of columns are supported by this constraint.\n     * Returning empty list or null means that all types are supported.\n     *\n     * @return supported types for given constraint\n     */\n    public abstract List<AbstractType<?>> getSupportedTypes();\n\n    protected void validateTypes(ColumnMetadata columnMetadata)\n    {\n        if (getSupportedTypes() == null || getSupportedTypes().isEmpty())\n            return;\n\n        if (!getSupportedTypes().contains(columnMetadata.type.unwrap()))\n            throw new InvalidConstraintDefinitionException(format(\"Constraint '%s' can be used only for columns of type %s but it was %s\",\n                                                                  name(),\n                                                                  getSupportedTypes(),\n                                                                  columnMetadata.type.getClass()));\n    }\n}\n","sourceCodeStart":146,"sourceCodeEnd":170,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/constraints/ColumnConstraint.java#L146-L170","documentation":"This InvalidConstraintDefinitionException is thrown by ColumnConstraint.validateTypes when a constraint is attached to a column whose type is not in the constraint's list of supported types. Cassandra validates constraint/type compatibility at schema definition time (CREATE/ALTER TABLE) so invalid constraints never reach the write path. The message reports the allowed types and the actual column type class.","triggerScenarios":"Declaring a column constraint (e.g. LENGTH, JSON) on a column whose AbstractType is not contained in the constraint's getSupportedTypes() — e.g. applying a LENGTH constraint to a boolean or uuid column in CREATE TABLE or ALTER TABLE.","commonSituations":"Copying constraint DDL from an int/text column example to an incompatible column type; changing a column's type with ALTER TABLE so an existing constraint's supported type no longer matches; tools generating schema DDL that apply constraints indiscriminately.","solutions":["Change the constrained column to one of the supported types listed in the message (or change the column type via ALTER).","Remove the constraint from the incompatible column.","Extend the constraint's getSupportedTypes() to include the column type if you control the constraint implementation.","Wrap the column in the required type (e.g. make it a text column) if the constraint semantics allow it."],"exampleFix":"// before\nCREATE TABLE ks.t (id uuid PRIMARY KEY, flags int CONSTRAINT CHECK LENGTH(3..10));\n// after\nCREATE TABLE ks.t (id uuid PRIMARY KEY, name text CONSTRAINT CHECK LENGTH(3..10));","handlingStrategy":"validation","validationCode":"Set<AbstractType<?>> supported = constraint.getSupportedTypes();\nif (supported != null && !supported.contains(column.type.unwrap()))\n    throw new IllegalArgumentException(\"Constraint \" + constraint.name() + \" not applicable to column type \" + column.type.asCQL3Type());","typeGuard":"static boolean constraintSupports(ColumnConstraint<?> c, ColumnMetadata col) {\n    var s = c.getSupportedTypes();\n    return s == null || s.isEmpty() || s.contains(col.type.unwrap());\n}","tryCatchPattern":null,"preventionTips":["Check getSupportedTypes() against column type before emitting constraint DDL","Test generated schema DDL against a real keyspace before deployment","When ALTERing column types, review existing constraints for compatibility"],"tags":["cql","schema","constraints","type-mismatch"],"backgroundTag":"type-mismatch","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"}