{"record":{"id":"45c709769401b7e4","repo":"apache/cassandra","slug":"duplicate-definition-of-not-null-constraint","errorCode":null,"errorMessage":"Duplicate definition of NOT NULL constraint","messagePattern":"Duplicate definition of NOT NULL constraint","errorType":"validation","errorClass":"InvalidRequestException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/statements/schema/CreateTableStatement.java","lineNumber":636,"sourceCode":"        public String table()\n        {\n            return name.getName();\n        }\n\n        public void addColumn(ColumnIdentifier column, CQL3Type.Raw type, boolean isStatic, boolean isNotNull, ColumnMask.Raw mask, ColumnConstraints.Raw constraints)\n        {\n            if (null != rawColumns.put(column, new ColumnProperties.Raw(type, mask)))\n                throw ire(\"Duplicate column '%s' declaration for table '%s'\", column, name);\n\n            if (isStatic)\n                staticColumns.add(column);\n\n            ColumnConstraints preparedConstraints = constraints == null ? ColumnConstraints.NO_OP : constraints.prepare(column);\n\n            if (isNotNull)\n            {\n                 if (preparedConstraints.containsNotNullConstraint())\n                     throw ire(\"Duplicate definition of NOT NULL constraint\");\n\n                List<ColumnConstraint<?>> checkConstraints = new ArrayList<>(preparedConstraints.getConstraints());\n                checkConstraints.add(new UnaryFunctionColumnConstraint(new NotNullConstraint()));\n                preparedConstraints = new ColumnConstraints(checkConstraints);\n                preparedConstraints.setColumnName(column);\n            }\n\n            columnConstraints.put(column, preparedConstraints);\n        }\n\n        public void addColumn(ColumnIdentifier column, CQL3Type.Raw type, boolean isStatic, ColumnMask.Raw mask, ColumnConstraints.Raw constraints)\n        {\n            addColumn(column, type, isStatic, false, mask, constraints);\n        }\n\n        public void setCompactStorage()\n        {\n            useCompactStorage = true;","sourceCodeStart":618,"sourceCodeEnd":654,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/statements/schema/CreateTableStatement.java#L618-L654","documentation":"A NOT NULL constraint was specified for a column that already carries a NOT NULL constraint in its prepared ColumnConstraints — either the column already has an explicit NOT NULL or the isNotNull flag is applied on top of constraints containing one. The builder rejects the redundant definition.","triggerScenarios":"Declaring a column with a NOT NULL constraint (e.g. via constraints syntax) and also passing isNotNull=true; repeating NOT NULL twice for the same column in DDL that supports constraint syntax.","commonSituations":"Generated DDL that adds NOT NULL both in a constraint list and as a column modifier; merging schema fragments that each contribute a NOT NULL for the same column.","solutions":["Remove the redundant NOT NULL specification, keeping only one","Inspect the prepared ColumnConstraints before adding a NOT NULL (use containsNotNullConstraint())","If constraints are built programmatically, guard with if (!constraints.containsNotNullConstraint())"],"exampleFix":"// before\ncol int NOT NULL, -- plus a NOT NULL check constraint from merged DDL\n// after\ncol int NOT NULL,","handlingStrategy":"validation","validationCode":"if (isNotNull && constraints.containsNotNullConstraint()) throw new Error('NOT NULL already present for column ' + column);","typeGuard":null,"tryCatchPattern":"try { session.execute(ddl); } catch (e) { if (/Duplicate definition of NOT NULL/.test(e.message)) { /* strip one NOT NULL source and retry */ } else throw e; }","preventionTips":["Specify NOT NULL in exactly one place per column","When merging schema fragments, de-duplicate constraints","Check ColumnConstraints.containsNotNullConstraint() before adding another"],"tags":["cql","schema","not-null","duplicate-constraint"],"backgroundTag":"conflicting-config-options","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"}