{"record":{"id":"1653947e73f37875","repo":"cocoindex-io/cocoindex","slug":"primary-key-schema-primary-key-r-value-cannot-be","errorCode":null,"errorMessage":"Primary key {schema.primary_key!r} value cannot be None.","messagePattern":"Primary key (.+?) value cannot be None\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/cocoindex/connectors/zvec/_target.py","lineNumber":902,"sourceCode":"    _schema: CollectionSchema[RowT]\n\n    def __init__(\n        self,\n        provider: coco.TargetStateProvider[_DocValue, None, coco.MaybePendingS],\n        schema: CollectionSchema[RowT],\n    ) -> None:\n        self._provider = provider\n        self._schema = schema\n\n    def declare_row(self: \"CollectionTarget[RowT]\", *, row: RowT) -> None:\n        \"\"\"Declare a document (row) to be upserted to this collection.\n\n        The primary-key value becomes the document id (converted to ``str``).\n        \"\"\"\n        schema = self._schema\n        pk_value = _row_get(row, schema.primary_key)\n        if pk_value is None:\n            raise ValueError(\n                f\"Primary key {schema.primary_key!r} value cannot be None.\"\n            )\n        doc_id = str(pk_value)\n\n        vectors: dict[str, Any] = {}\n        fields: dict[str, Any] = {}\n        for name, col in schema.columns.items():\n            if name == schema.primary_key:\n                continue\n            value = _row_get(row, name)\n            if col.kind == \"dense\":\n                vectors[name] = None if value is None else _to_float_list(value)\n            elif col.kind == \"sparse\":\n                vectors[name] = (\n                    None\n                    if value is None\n                    else {int(k): float(v) for k, v in dict(value).items()}\n                )","sourceCodeStart":884,"sourceCodeEnd":920,"githubUrl":"https://github.com/cocoindex-io/cocoindex/blob/e84aa99b3292c5270a4b313b2a7137ad9ce8ab3b/python/cocoindex/connectors/zvec/_target.py#L884-L920","documentation":"Guard in CollectionTarget.declare_row. The primary key value doubles as the zvec document id, which must be a concrete value; a None PK (e.g. an optional-typed field left unset in the declared row) cannot be converted to a document id, so the row would be silently unidentifiable. Raised when the row's PK field is None at declaration time. Ensure every declared row has a non-None primary key value.","triggerScenarios":"Calling collection_target handler's declare_row(row=...) where the row's primary-key field is None (unset, or an Optional field defaulting to None).","commonSituations":"Optional id fields in dataclasses; rows built from upstream data with missing ids; autoincrement ids not yet assigned at declare time.","solutions":["Ensure every row has a non-None primary-key value before declaring.","Generate the id in your @coco.fn before declare_row (e.g. uuid or hash of content).","If the id can be absent, skip the row or use a sentinel/default value."],"exampleFix":"// before\ntarget.declare_row(row=Doc(id=None, vec=embedding))\n// after\nimport uuid\ntarget.declare_row(row=Doc(id=str(uuid.uuid4()), vec=embedding))","handlingStrategy":"validation","validationCode":"pk = getattr(row, schema.primary_key, None)\nif pk is None:\n    raise ValueError(f\"row missing primary key {schema.primary_key!r}\")","typeGuard":"def has_pk(row, pk_name: str) -> bool:\n    return getattr(row, pk_name, None) is not None","tryCatchPattern":"try:\n    target.declare_row(row=row)\nexcept ValueError as e:\n    if \"cannot be None\" in str(e):\n        logging.warning(\"skipping row with missing id: %r\", row)","preventionTips":["Assign ids (uuid/hash) before declaring rows.","Avoid Optional primary-key fields in record types.","Filter out rows with None ids upstream."],"tags":["python","primary-key","null-value","vector-database"],"backgroundTag":"null-argument","analyzedSha":"e84aa99b3292c5270a4b313b2a7137ad9ce8ab3b","analyzedAt":"2026-09-08T15:59:19.997Z","contentChangedAt":"2026-09-08T15:59:19.997Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}