{"record":{"id":"b035fde255356d2b","repo":"apache/cassandra","slug":"s-constraint-can-not-be-specified-on-a-s-key-col","errorCode":null,"errorMessage":"%s constraint can not be specified on a %s key column '%s'","messagePattern":"(.+?) constraint can not be specified on a (.+?) key column '(.+?)'","errorType":"validation","errorClass":"InvalidConstraintDefinitionException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/constraints/NotNullConstraint.java","lineNumber":59,"sourceCode":"    }\n\n    public NotNullConstraint(List<String> args)\n    {\n        super(FUNCTION_NAME, args);\n    }\n\n    @Override\n    public void internalEvaluate(AbstractType<?> valueType, Operator relationType, String term, ByteBuffer columnValue)\n    {\n        // on purpose empty as evaluate method already covered nullity\n    }\n\n    @Override\n    public void validate(ColumnMetadata columnMetadata, String term) throws InvalidConstraintDefinitionException\n    {\n        super.validate(columnMetadata, term);\n        if (columnMetadata.isPrimaryKeyColumn())\n            throw new InvalidConstraintDefinitionException(format(\"%s constraint can not be specified on a %s key column '%s'\",\n                                                                  name,\n                                                                  columnMetadata.isPartitionKey() ? \"partition\" : \"clustering\",\n                                                                  columnMetadata.name));\n    }\n\n    @Override\n    public List<AbstractType<?>> getSupportedTypes()\n    {\n        return null;\n    }\n\n    @Override\n    public String toString()\n    {\n        return CQL_FUNCTION_NAME;\n    }\n\n    @Override","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/constraints/NotNullConstraint.java#L41-L77","documentation":"NotNullConstraint.validate rejects applying a NOT NULL constraint to any primary key column (partition key or clustering column). Primary key columns are already implicitly required by Cassandra's data model, so the explicit constraint is redundant and disallowed, thrown as InvalidConstraintDefinitionException at schema definition time. The message names partition vs clustering and the column.","triggerScenarios":"CREATE TABLE / ALTER TABLE declaring `NOT NULL` on a column that isPrimaryKeyColumn() — e.g. `PRIMARY KEY (id)` plus `id uuid NOT NULL`, or a clustering column with NOT NULL.","commonSituations":"Porting schemas from relational databases where NOT NULL on keys is customary; schema-generator tools that annotate every non-nullable entity field; misunderstanding that keys already cannot be null.","solutions":["Remove the NOT NULL constraint from the primary key column — key columns are inherently non-null.","If the intent was to enforce presence on a data column, apply the constraint to that column instead.","Fix schema-generation code to skip primary key columns when emitting NOT NULL constraints."],"exampleFix":"// before\nCREATE TABLE ks.t (id uuid NOT NULL PRIMARY KEY, name text);\n// after\nCREATE TABLE ks.t (id uuid PRIMARY KEY, name text NOT NULL);","handlingStrategy":"validation","validationCode":"if (columnMetadata.isPrimaryKeyColumn() && constraints.stream().anyMatch(c -> c instanceof NotNullConstraint))\n    throw new IllegalArgumentException(\"NOT NULL is redundant on primary key column \" + columnMetadata.name);","typeGuard":"static boolean notNullAllowed(ColumnMetadata col) { return !col.isPrimaryKeyColumn(); }","tryCatchPattern":null,"preventionTips":["Skip NOT NULL emission for primary key columns in schema generators","Remember partition and clustering keys are inherently non-null in Cassandra","Compare relational schemas on migration and drop redundant NOT NULLs on keys"],"tags":["cql","schema","constraints","primary-key","not-null"],"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"}