{"record":{"id":"4fafd091ae3c1667","repo":"apache/cassandra","slug":"cannot-insert-null-value-for-primary-key-column","errorCode":null,"errorMessage":"Cannot insert null value for primary key column '%s'.","messagePattern":"Cannot insert null value for primary key column '(.+?)'\\.","errorType":"validation","errorClass":"ParseError","httpStatus":null,"severity":"error","filePath":"pylib/cqlshlib/copyutil.py","lineNumber":2167,"sourceCode":"        or \"NULL\" otherwise. Note that for counters we never use prepared statements, so we\n        only check is_counter when use_prepared_statements is false.\n        \"\"\"\n        return None if self.use_prepared_statements else (\"0\" if self.is_counter else \"NULL\")\n\n    def convert_row(self, row):\n        \"\"\"\n        Convert the row into a list of parsed values if using prepared statements, else simply apply the\n        protection functions to escape values with quotes when required. Also check on the row length and\n        make sure primary partition key values aren't missing.\n        \"\"\"\n        converters = self.converters if self.use_prepared_statements else self.protectors\n\n        if len(row) != len(converters):\n            raise ParseError('Invalid row length %d should be %d' % (len(row), len(converters)))\n\n        for i in self.primary_key_indexes:\n            if row[i] == self.nullval:\n                raise ParseError(self.get_null_primary_key_message(i))\n\n        def convert(c, v):\n            try:\n                return c(v) if v != self.nullval else self.get_null_val()\n            except Exception as e:\n                # if we could not convert an empty string, then self.nullval has been set to a marker\n                # because the user needs to import empty strings, except that the converters for some types\n                # will fail to convert an empty string, in this case the null value should be inserted\n                # see CASSANDRA-12794\n                if v == '':\n                    return self.get_null_val()\n\n                if self.debug:\n                    traceback.print_exc()\n                raise ParseError(\"Failed to parse %s : %s\" % (v, e.message if hasattr(e, 'message') else str(e)))\n\n        return [convert(conv, val) for conv, val in zip(converters, row)]\n","sourceCodeStart":2149,"sourceCodeEnd":2185,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/pylib/cqlshlib/copyutil.py#L2149-L2185","documentation":"COPY FROM rejects rows where a primary key column (partition key or clustering column) contains the nullval marker, since null primary keys cannot be inserted. The raised message names the offending column via get_null_primary_key_message.","triggerScenarios":"COPY FROM a CSV row whose partition key or clustering key cell equals the null marker (default empty string), e.g. an empty first field; the nullval may also be a custom marker set by WITH NULLS.","commonSituations":"CSVs produced by exports that padded missing keys with empty cells, joins/spreadsheets that blanked key columns, NULLS option colliding with real key values, rows appended with empty keys.","solutions":["Fill in the missing primary key values in the CSV","If empty-string keys are meaningful, choose a different NULLS marker that does not appear in key columns","Skip/filter rows with empty primary keys before importing"],"exampleFix":"// before (CSV)\n,2026-01-01,value\n// after\nkey1,2026-01-01,value","handlingStrategy":"validation","validationCode":"pk_indexes = [0, 1]\nfor i, row in enumerate(rows, 1):\n    for j in pk_indexes:\n        if row[j] in ('', null_marker):\n            raise ValueError(f\"line {i}: null primary key col {j}\")","typeGuard":null,"tryCatchPattern":"try:\n    run_copy_from(...)\nexcept ParseError as e:\n    print(f\"Null primary key: {e}\")  # supply key or skip row","preventionTips":["Never leave primary key cells empty in CSVs","Ensure the NULLS marker doesn't equal any real key value","Reject empty keys at CSV-generation time"],"tags":["cqlsh","copy","primary-key","validation"],"backgroundTag":"empty-required-field","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"}