{"record":{"id":"b4a07411ff79daaa","repo":"Textualize/textual","slug":"column-key-column-key-r-is-not-valid","errorCode":null,"errorMessage":"Column key {column_key!r} is not valid.","messagePattern":"Column key (.+?) is not valid\\.","errorType":"exception","errorClass":"ColumnDoesNotExist","httpStatus":null,"severity":"error","filePath":"src/textual/widgets/_data_table.py","lineNumber":1059,"sourceCode":"        \"\"\"\n        if row_key not in self._row_locations:\n            raise RowDoesNotExist(f\"No row exists for row_key={row_key!r}\")\n        return self._row_locations.get(row_key)\n\n    def get_column(self, column_key: ColumnKey | str) -> Iterable[CellType]:\n        \"\"\"Get the values from the column identified by the given column key.\n\n        Args:\n            column_key: The key of the column.\n\n        Returns:\n            A generator which yields the cells in the column.\n\n        Raises:\n            ColumnDoesNotExist: If there is no column corresponding to the key.\n        \"\"\"\n        if column_key not in self._column_locations:\n            raise ColumnDoesNotExist(f\"Column key {column_key!r} is not valid.\")\n\n        data = self._data\n        for row_metadata in self.ordered_rows:\n            row_key = row_metadata.key\n            yield data[row_key][column_key]\n\n    def get_column_at(self, column_index: int) -> Iterable[CellType]:\n        \"\"\"Get the values from the column at a given index.\n\n        Args:\n            column_index: The index of the column.\n\n        Returns:\n            A generator which yields the cells in the column.\n\n        Raises:\n            ColumnDoesNotExist: If there is no column with the given index.\n        \"\"\"","sourceCodeStart":1041,"sourceCodeEnd":1077,"githubUrl":"https://github.com/Textualize/textual/blob/06dbeef4bb70fb718236aa418ed658ef4667a126/src/textual/widgets/_data_table.py#L1041-L1077","documentation":"Raised by DataTable.get_column when column_key is not in _column_locations. Columns are identified by ColumnKey (or its string id) assigned at add_column time; this error means no such column exists. get_column_at and internal width updates also funnel through here.","triggerScenarios":"Calling get_column with a key that was never returned by add_column; a key whose column was removed via remove_column; a key from before clear() was called.","commonSituations":"Caching column keys across table rebuilds; passing a row key where a column key is expected; referencing dynamically generated columns by guessed string ids.","solutions":["Check membership first: if column_key in table.column_keys.","Capture and reuse the ColumnKey instances returned from add_column.","Invalidate cached keys whenever remove_column or clear is called.","Catch ColumnDoesNotExist as a safety net."],"exampleFix":"# before\nvalues = list(table.get_column(col_key))\n\n# after\nfrom textual.widgets._data_table import ColumnDoesNotExist\ntry:\n    values = list(table.get_column(col_key))\nexcept ColumnDoesNotExist:\n    values = []","handlingStrategy":"validation","validationCode":"values = list(table.get_column(col_key)) if col_key in table.column_keys else []","typeGuard":null,"tryCatchPattern":"from textual.widgets._data_table import ColumnDoesNotExist\ntry:\n    values = list(table.get_column(col_key))\nexcept ColumnDoesNotExist:\n    values = []","preventionTips":["Store ColumnKey objects returned by add_column","Rebuild key caches after schema changes","Check column_keys membership before lookup"],"tags":["data-table","column-key","textual","lookup-failed"],"backgroundTag":"dict-key-not-found","analyzedSha":"06dbeef4bb70fb718236aa418ed658ef4667a126","analyzedAt":"2026-08-27T02:36:57.214Z","schemaVersion":2},"datasetVersion":"2026-08-27T03:17:27.898Z"}