{"record":{"id":"f5303dd16f5d0873","repo":"cocoindex-io/cocoindex","slug":"sqlite-row-is-missing-primary-key-column-pk-r","errorCode":null,"errorMessage":"SQLite row is missing primary key column {pk!r}","messagePattern":"SQLite row is missing primary key column (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/cocoindex/connectors/sqlite/_target.py","lineNumber":1091,"sourceCode":"    ) -> None:\n        self._provider = provider\n        self._table_schema = table_schema\n\n    def declare_row(self: \"TableTarget[RowT]\", *, row: RowT) -> None:\n        \"\"\"\n        Declare a row to be upserted to this table.\n\n        Args:\n            row: A row object (dict, dataclass, NamedTuple, or Pydantic model).\n                 Must include all primary key columns with non-None values.\n                 Dict rows may omit nullable non-primary-key columns; omitted values\n                 are written as NULL.\n        \"\"\"\n        row_dict = self._row_to_dict(row)\n        pk_values: list[Any] = []\n        for pk in self._table_schema.primary_key:\n            if isinstance(row, dict) and pk not in row:\n                raise ValueError(f\"SQLite row is missing primary key column {pk!r}\")\n            pk_value = row_dict[pk]\n            if pk_value is None:\n                raise ValueError(f\"SQLite primary key column {pk!r} cannot be None\")\n            pk_values.append(pk_value)\n        coco.declare_target_state(\n            self._provider.target_state(tuple(pk_values), row_dict)\n        )\n\n    def _row_to_dict(self, row: RowT) -> dict[str, Any]:\n        \"\"\"\n        Convert a row (dict or object) into dict[str, Any] using the schema columns,\n        and apply column encoders for both dict and object inputs.\n        \"\"\"\n        out: dict[str, Any] = {}\n        for col_name, col in self._table_schema.columns.items():\n            if isinstance(row, dict):\n                value = row.get(col_name)\n            else:","sourceCodeStart":1073,"sourceCodeEnd":1109,"githubUrl":"https://github.com/cocoindex-io/cocoindex/blob/e84aa99b3292c5270a4b313b2a7137ad9ce8ab3b/python/cocoindex/connectors/sqlite/_target.py#L1073-L1109","documentation":"`RowTarget.declare_row` converts the row to a dict and reads every primary-key column to build the target state key. When the row is a plain `dict` that lacks one of the schema's primary key column names, a `ValueError` is raised naming the missing key, because the row cannot be identified in the target state.","triggerScenarios":"Calling `target.declare_row({...})` with a dict whose keys do not include every column listed in the table's `primary_key` (e.g. typo'd key name, key omitted, primary_key config changed after the rows were built).","commonSituations":"Renaming a dataclass field while `primary_key=[...]` still references the old name; building row dicts dynamically and dropping the id field; changing `primary_key` in `table_target(...)` without updating the produced rows.","solutions":["Ensure the dict contains every primary key column with the exact name from the schema","Fix typos in the row dict keys","Update the `primary_key` argument to match the actual keys your rows provide"],"exampleFix":"// before\ntarget.declare_row({\"doc_id\": 1, \"text\": \"hi\"})  # schema pk is \"id\"\n// after\ntarget.declare_row({\"id\": 1, \"text\": \"hi\"})","handlingStrategy":"validation","validationCode":"required_pks = {\"id\"}  # schema.primary_key\nmissing = required_pks - row.keys()\nassert not missing, f\"row missing pk fields: {missing}\"","typeGuard":"def has_all_pks(row: dict, pks: list[str]) -> bool:\n    return all(pk in row for pk in pks)","tryCatchPattern":"try:\n    target.declare_row(row_dict)\nexcept ValueError as e:\n    if e.args[0].startswith(\"SQLite row is missing primary key\"):\n        fix_row_keys(row_dict)\n    else:\n        raise","preventionTips":["Keep primary_key names and row dict keys in one shared constant list","Add a schema test asserting every emitted row dict contains the pk fields","Update row producers whenever primary_key config changes"],"tags":["sqlite","validation","primary-key","dict"],"backgroundTag":"missing-required-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"}