{"record":{"id":"d69debd95f40d8c5","repo":"cocoindex-io/cocoindex","slug":"columns-invalid-cols-not-found-in-row-type-field","errorCode":null,"errorMessage":"Columns {invalid_cols} not found in row_type fields: {field_names}","messagePattern":"Columns (.+?) not found in row_type fields: (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/cocoindex/connectors/postgres/_source.py","lineNumber":223,"sourceCode":"            raise ValueError(\"Cannot specify both row_factory and row_type\")\n\n        # Determine columns based on row_type\n        resolved_columns: Sequence[str] | None = columns\n        if row_type is not None:\n            if not is_record_type(row_type):\n                raise TypeError(\n                    f\"row_type must be a record type (dataclass, NamedTuple, or Pydantic model), \"\n                    f\"got {row_type}\"\n                )\n            record_info = RecordType(row_type)\n            field_names = [f.name for f in record_info.fields]\n            field_set = frozenset(field_names)\n\n            if columns is not None:\n                # Validate that all specified columns exist in the record type\n                invalid_cols = [c for c in columns if c not in field_set]\n                if invalid_cols:\n                    raise ValueError(\n                        f\"Columns {invalid_cols} not found in row_type fields: {field_names}\"\n                    )\n            else:\n                # Use record type fields as columns\n                resolved_columns = field_names\n\n            row_factory = _create_row_factory(row_type, field_set)\n\n        self._pool = pool\n        self._spec = PgSourceSpec(\n            table_name=table_name,\n            columns=resolved_columns,\n            pg_schema_name=pg_schema_name,\n        )\n        self._row_factory = row_factory\n\n    def fetch_rows(self) -> RowFetcher[RowT]:\n        \"\"\"","sourceCodeStart":205,"sourceCodeEnd":241,"githubUrl":"https://github.com/cocoindex-io/cocoindex/blob/e84aa99b3292c5270a4b313b2a7137ad9ce8ab3b/python/cocoindex/connectors/postgres/_source.py#L205-L241","documentation":"When row_type is a record type and explicit columns are supplied, every column name must correspond to a field of the record type; otherwise the connector could not map query results onto the record. Mismatched names raise ValueError.","triggerScenarios":"PostgresSource(..., row_type=MyRecord, columns=['id','emial']) where 'emial' is not a field of MyRecord (typo or renamed field).","commonSituations":"Typos in column lists, renaming a dataclass field without updating columns, or listing DB column names that differ from the record field names.","solutions":["Correct the column names so each matches a field of row_type.","Remove the columns argument to auto-derive columns from all record fields.","Rename the record fields to match the database columns."],"exampleFix":"// before\n@dataclass\nclass User:\n    id: int\n    email: str\nsrc = PostgresSource(table=\"u\", row_type=User, columns=[\"id\", \"emial\"])\n// after\nsrc = PostgresSource(table=\"u\", row_type=User, columns=[\"id\", \"email\"])","handlingStrategy":"validation","validationCode":"import dataclasses\nfield_names = {f.name for f in dataclasses.fields(MyRow)}\nbad = [c for c in columns if c not in field_names]\nassert not bad, f\"Unknown columns: {bad}\"","typeGuard":"def columns_valid(row_type: type, columns: list[str]) -> bool:\n    names = {f.name for f in dataclasses.fields(row_type)}\n    return all(c in names for c in columns)","tryCatchPattern":"try:\n    src = PostgresSource(table=\"t\", row_type=R, columns=cols)\nexcept ValueError as e:\n    if \"not found in row_type fields\" in str(e):\n        src = PostgresSource(table=\"t\", row_type=R)  # derive columns from fields","preventionTips":["Derive column names from the record class fields programmatically instead of hand-writing lists.","Keep column lists next to the dataclass definition and update both together.","Add a unit test comparing columns to dataclasses.fields()."],"tags":["python","validation","configuration"],"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"}