{"record":{"id":"2668a5fd1d957d0e","repo":"apache/cassandra","slug":"constraint-s-was-not-specified-on-a-column-it-ope","errorCode":null,"errorMessage":"Constraint %s was not specified on a column it operates on: %s but on: %s","messagePattern":"Constraint (.+?) was not specified on a column it operates on: (.+?) but on: (.+?)","errorType":"validation","errorClass":"InvalidConstraintDefinitionException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/constraints/ColumnConstraints.java","lineNumber":247,"sourceCode":"        public Raw()\n        {\n            this.constraints = Collections.emptyList();\n        }\n\n        public ColumnConstraints prepare(ColumnIdentifier column)\n        {\n            if (constraints.isEmpty())\n                return NO_OP;\n\n            for (ColumnConstraint<?> constraint : constraints)\n            {\n                // We only check scalar constraints column name, as the rest of the constraints\n                // imply the name from the column they are defined at\n                if (constraint.getConstraintType() == ConstraintType.SCALAR)\n                {\n                    if (!column.equals(constraint.columnName))\n                    {\n                        throw new InvalidConstraintDefinitionException(format(\"Constraint %s was not specified on a column it operates on: %s but on: %s\",\n                                                                              constraint, column.toCQLString(), constraint.columnName));\n                    }\n                }\n            }\n\n            ColumnConstraints columnConstraints = new ColumnConstraints(constraints);\n            columnConstraints.setColumnName(column);\n            return columnConstraints;\n        }\n    }\n\n    public static class Serializer implements MetadataSerializer<ColumnConstraints>\n    {\n        @Override\n        public void serialize(ColumnConstraints columnConstraint, DataOutputPlus out, Version version) throws IOException\n        {\n            out.writeInt(columnConstraint.getSize());\n            for (ColumnConstraint constraint : columnConstraint.getConstraints())","sourceCodeStart":229,"sourceCodeEnd":265,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/constraints/ColumnConstraints.java#L229-L265","documentation":"ColumnConstraints.prepare verifies that SCALAR-type constraints were actually declared on the column they operate on. A scalar constraint carries an explicit columnName; if the constraint's columnName differs from the column it is being attached to, the schema is inconsistent and InvalidConstraintDefinitionException is thrown, showing the expected column and the actual declared one.","triggerScenarios":"Preparing schema (CREATE TABLE / ALTER TABLE with constraints) where a scalar constraint is attached to column A but internally references columnName of column B — typically from programmatically built ColumnConstraints or malformed constraint DDL arguments.","commonSituations":"ORM/schema-builder code that copies constraint objects between columns without updating the constraint's columnName; hand-edited generated DDL; refactoring a table that renamed a column but not the constraint's target.","solutions":["Attach the constraint to the column named in the constraint's target (or change the constraint's target column to match the column it is declared on).","Regenerate/fix the schema DDL so each constraint is declared on its own column.","Update the schema-building code to set constraint.columnName to the declaring column."],"exampleFix":"// before (constraint built for 'age' but attached to 'height')\nColumnConstraints.of(heightColumn, ageConstraint);\n// after\nColumnConstraints.of(ageColumn, ageConstraint);","handlingStrategy":"validation","validationCode":"if (constraint.getConstraintType() == ConstraintType.SCALAR && !column.equals(constraint.columnName))\n    throw new IllegalArgumentException(\"Constraint targets \" + constraint.columnName + \" but is declared on \" + column);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never share constraint objects between columns in schema builders","Update constraint columnName when renaming/moving columns","Unit-test generated schema for constraint-to-column coherence"],"tags":["cql","schema","constraints","column-mismatch"],"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"}