{"record":{"id":"4b46dba3951f1a01","repo":"pathwaycom/pathway","slug":"the-column-column-doesn-t-belong-to-the-target-t","errorCode":null,"errorMessage":"The column {column} doesn't belong to the target table {table}","messagePattern":"The column (.+?) doesn't belong to the target table (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/pathway/internals/datasink.py","lineNumber":28,"sourceCode":"from pathway.internals import api\nfrom pathway.internals.expression import ColumnReference\n\nif TYPE_CHECKING:\n    from pathway.internals.table import Table\n\n\nclass DataSink(ABC):\n    @property\n    def name(self) -> str:\n        return type(self).__qualname__.lower().removesuffix(\"datasink\")\n\n    def check_sort_by_columns(self, table: Table):\n        sort_by = getattr(self, \"sort_by\", None)\n        if sort_by is None:\n            return\n        for column in sort_by:\n            if column._table != table:\n                raise ValueError(\n                    f\"The column {column} doesn't belong to the target table {table}\"\n                )\n\n\n@dataclass(frozen=True)\nclass GenericDataSink(DataSink):\n    datastorage: api.DataStorage\n    dataformat: api.DataFormat\n    datasink_name: str\n    unique_name: str | None\n    sort_by: Iterable[ColumnReference] | None = None\n    on_pipeline_finished: Callable | None = None\n\n    @property\n    def name(self) -> str:\n        return self.datasink_name\n\n    @property","sourceCodeStart":10,"sourceCodeEnd":46,"githubUrl":"https://github.com/pathwaycom/pathway/blob/fa2f74a4649b7c5908690cf60137263d8d80de5f/python/pathway/internals/datasink.py#L10-L46","documentation":"DataSink.check_sort_by_columns validates a sink's sort_by columns before writing: every ColumnReference in sort_by must belong to the exact Table being written (column._table is the table object). Columns coming from another table (e.g. the pre-join inputs, or an intermediate table) fail this identity check.","triggerScenarios":"Passing sort_by=[t_src.col] where t_src is not the table handed to pw.io.json.write / csv.write etc., typically after a select/rename/join so the saved table object is a new instance while sort_by still references the old table's columns.","commonSituations":"Reusing column references captured before a transformation: cols = [t.x, t.y]; out = t.select(...); pw.io.csv.write(out, ..., sort_by=cols). Join outputs where sort_by references an input table's column.","solutions":["Re-derive the columns from the table being written: out = t.select(...); pw.io.csv.write(out, sort_by=[out.x, out.y]).","Chain immediately: write the table whose attribute columns you use in sort_by in the same expression scope.","Drop sort_by if ordering is not required."],"exampleFix":"# before\nsorted_cols = [t.x, t.y]\nout = t.select(x=pw.this.x, y=pw.this.y)\npw.io.csv.write(out, path, sort_by=sorted_cols)  # cols belong to t, not out\n\n# after\nout = t.select(x=pw.this.x, y=pw.this.y)\npw.io.csv.write(out, path, sort_by=[out.x, out.y])","handlingStrategy":"validation","validationCode":"assert all(c._table is table_being_written for c in (sort_by or [])), 'sort_by columns must come from the exact table written'","typeGuard":"def sort_by_columns_valid(table, sort_by) -> bool:\n    return all(c._table is table for c in (sort_by or []))","tryCatchPattern":null,"preventionTips":["Derive sort_by from the final table object right at the write call.","Never reuse column refs captured before a select/join transformation."],"tags":["pathway","io","output","sort","argument-validation"],"backgroundTag":null,"analyzedSha":"fa2f74a4649b7c5908690cf60137263d8d80de5f","analyzedAt":"2026-08-15T01:48:17.006Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}