{"record":{"id":"dc3081f86f9802d7","repo":"cocoindex-io/cocoindex","slug":"primary-key-column-pk-not-found-in-columns-l-dc3081","errorCode":null,"errorMessage":"Primary key column '{pk}' not found in columns: {list(self.columns.keys())}","messagePattern":"Primary key column '(.+?)' not found in columns: (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/cocoindex/connectors/sqlite/_target.py","lineNumber":333,"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_overrides: dict[str, SqliteType | 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 SQLite types.\n\n        Args:","sourceCodeStart":315,"sourceCodeEnd":351,"githubUrl":"https://github.com/cocoindex-io/cocoindex/blob/e84aa99b3292c5270a4b313b2a7137ad9ce8ab3b/python/cocoindex/connectors/sqlite/_target.py#L315-L351","documentation":"The SQLite table schema validates at construction time that every name in `primary_key` refers to an existing column derived from the record type. This fails fast because a primary key referencing a nonexistent column would produce invalid DELETE/UPDATE SQL later. The error lists the actual available columns to ease diagnosis.","triggerScenarios":"Calling `table_target(..., primary_key=[\"id\"])` where the record type has no field named 'id' — due to a typo, renamed field, or a primary_key entry matching a column override name rather than the field name.","commonSituations":"Renaming a dataclass field without updating primary_key, referencing the DB column name after column renaming/overrides changed it, or copying an example whose record type differs.","solutions":["Correct primary_key entries to exactly match field names in the record type (check the column list in the error message).","If fields were renamed, update both the record type and primary_key together.","If column overrides rename the PK column, use the name that appears in the derived columns dict."],"exampleFix":"// before\n@dataclasses.dataclass\nclass Row:\n    doc_id: str\n    text: str\n\ntarget = sqlite.table_target(record_type=Row, primary_key=[\"id\"])\n// after\ntarget = sqlite.table_target(record_type=Row, primary_key=[\"doc_id\"])","handlingStrategy":"validation","validationCode":"import dataclasses\nfields = {f.name for f in dataclasses.fields(Row)}\nbad = [pk for pk in primary_key if pk not in fields]\nif bad:\n    raise ValueError(f\"primary_key {bad} not fields of {Row.__name__}: {sorted(fields)}\")","typeGuard":null,"tryCatchPattern":"try:\n    target = sqlite.table_target(record_type=Row, primary_key=pk, ...)\nexcept ValueError as e:\n    if \"not found in columns\" in str(e):\n        raise ConfigError(f\"Fix primary_key {pk}; valid columns are listed in: {e}\") from e\n    raise","preventionTips":["Derive primary_key from dataclasses.fields(Row) names instead of hardcoding strings.","Keep record type definitions and primary_key settings in one module so renames stay in sync.","Grep for renamed fields whenever you change the record dataclass."],"tags":["sqlite","schema","primary-key"],"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"}