{"record":{"id":"e927f93671b95b18","repo":"cocoindex-io/cocoindex","slug":"primary-key-column-pk-not-found-in-columns-l-e927f9","errorCode":null,"errorMessage":"Primary key column '{pk}' not found in columns: {list(self.columns.keys())}","messagePattern":"Primary key column '(.+?)' not found in columns: (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/cocoindex/connectors/lancedb/_target.py","lineNumber":252,"sourceCode":"        \"\"\"\n        Create a TableSchema from pre-resolved column definitions.\n\n        For constructing from a record type, use the async classmethod\n        ``from_class`` instead.\n\n        Args:\n            columns: A dict mapping column names to ColumnDef.\n            primary_key: List of column names that form the primary key.\n            row_type: Optional original record type.\n        \"\"\"\n        self.columns = columns\n        self.primary_key = primary_key\n        self.row_type = row_type\n\n        # Validate primary key columns exist\n        for pk in self.primary_key:\n            if pk not in self.columns:\n                raise ValueError(\n                    f\"Primary key column '{pk}' not found in columns: {list(self.columns.keys())}\"\n                )\n\n    @classmethod\n    async def from_class(\n        cls,\n        record_type: type[RowT],\n        primary_key: list[str],\n        *,\n        column_specs: dict[str, LanceType | res_schema.VectorSchemaProvider]\n        | None = None,\n    ) -> \"TableSchema[RowT]\":\n        \"\"\"\n        Create a TableSchema from a record type (dataclass, NamedTuple, or Pydantic model).\n\n        Python types are automatically mapped to PyArrow types.\n\n        Args:","sourceCodeStart":234,"sourceCodeEnd":270,"githubUrl":"https://github.com/cocoindex-io/cocoindex/blob/e84aa99b3292c5270a4b313b2a7137ad9ce8ab3b/python/cocoindex/connectors/lancedb/_target.py#L234-L270","documentation":"The LanceDB target validates that every name in primary_key refers to a column derived from the record type (plus column_specs). A primary key naming a nonexistent column makes row identity unrepresentable in the table, so __init__ raises a ValueError listing available columns.","triggerScenarios":"Constructing the target (directly or via from_class) with primary_key=[\"...\"] containing a name that is not a dataclass/NamedTuple/Pydantic field of the record type and not added via column_specs, or with a typo/case mismatch.","commonSituations":"Renaming a record field without updating primary_key; passing the Python attribute vs serialized column name; including a column that only exists in a different record type.","solutions":["Set primary_key to names that exactly match fields of the record type, e.g. [\"id\"].","Fix typos/case in the primary key names.","If the key column should exist but doesn't, add it to the record type (or via column_specs)."],"exampleFix":"// before\nawait LanceDbTarget.from_class(MyRecord, primary_key=[\"key\"])  # field is named 'id'\n// after\nawait LanceDbTarget.from_class(MyRecord, primary_key=[\"id\"])","handlingStrategy":"validation","validationCode":"from dataclasses import fields\nnames = {f.name for f in fields(MyRecord)}\nassert set(primary_key) <= names, f\"Unknown pk columns: {set(primary_key) - names}; have {sorted(names)}\"","typeGuard":null,"tryCatchPattern":"try:\n    target = await LanceDbTarget.from_class(MyRecord, primary_key=primary_key)\nexcept ValueError as e:\n    if \"Primary key column\" in str(e):\n        ...  # fix primary_key names to match record fields\n    raise","preventionTips":["Keep primary_key in the same module as the record type so renames surface in review.","Validate pk names against record fields before constructing targets.","Watch for case sensitivity in column names."],"tags":["python","lancedb","primary-key","schema-validation"],"backgroundTag":"schema-validation-failed","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"}