{"record":{"id":"7422203ec9285b64","repo":"Textualize/textual","slug":"the-column-key-key-r-already-exists","errorCode":null,"errorMessage":"The column key {key!r} already exists.","messagePattern":"The column key (.+?) already exists\\.","errorType":"exception","errorClass":"DuplicateKey","httpStatus":null,"severity":"error","filePath":"src/textual/widgets/_data_table.py","lineNumber":1635,"sourceCode":"        default: CellType | None = None,\n    ) -> ColumnKey:\n        \"\"\"Add a column to the table.\n\n        Args:\n            label: A str or Text object containing the label (shown top of column).\n            width: Width of the column in cells or None to fit content.\n            key: A key which uniquely identifies this column.\n                If None, it will be generated for you.\n            default: The  value to insert into pre-existing rows.\n\n        Returns:\n            Uniquely identifies this column. Can be used to retrieve this column\n                regardless of its current location in the DataTable (it could have moved\n                after being added due to sorting/insertion/deletion of other columns).\n        \"\"\"\n        column_key = ColumnKey(key)\n        if column_key in self._column_locations:\n            raise DuplicateKey(f\"The column key {key!r} already exists.\")\n        column_index = len(self.columns)\n        label = Text.from_markup(label) if isinstance(label, str) else label\n        content_width = measure(self.app.console, label, 1)\n        if width is None:\n            column = Column(\n                column_key,\n                label,\n                content_width,\n                content_width=content_width,\n                auto_width=True,\n            )\n        else:\n            column = Column(\n                column_key,\n                label,\n                width,\n                content_width=content_width,\n            )","sourceCodeStart":1617,"sourceCodeEnd":1653,"githubUrl":"https://github.com/Textualize/textual/blob/06dbeef4bb70fb718236aa418ed658ef4667a126/src/textual/widgets/_data_table.py#L1617-L1653","documentation":"Raised by DataTable.add_column when the supplied key (or auto-generated ColumnKey wrapping it) already exists in _column_locations. Column keys must be unique for the lifetime of the table; Textual raises DuplicateKey rather than overwriting.","triggerScenarios":"Calling add_column(key='name') twice; re-adding columns with explicit keys after data refresh without calling clear() first; generating keys that collide with previously added columns.","commonSituations":"Refresh routines that call add_column in a loop on every update; using fixed schema keys while old columns still exist; partial clears that remove rows but not columns.","solutions":["Check if key in table.column_keys (or compare ColumnKey(key)) before adding.","Call table.clear(columns=True) before rebuilding the full column set.","Use unique keys per refresh, e.g. f'{base_key}_{n}'.","Catch DuplicateKey and skip or update the existing column instead."],"exampleFix":"# before\ntable.add_column('Name', key='name')\n\n# after\nif 'name' not in table.column_keys:\n    table.add_column('Name', key='name')","handlingStrategy":"validation","validationCode":"if key not in table.column_keys:\n    table.add_column(label, key=key)","typeGuard":null,"tryCatchPattern":"from textual.widgets._data_table import DuplicateKey\ntry:\n    table.add_column(label, key=key)\nexcept DuplicateKey:\n    pass  # column already present","preventionTips":["Call clear(columns=True) before full rebuilds","Use auto-generated keys when uniqueness is unneeded","Deduplicate schema definitions before adding columns"],"tags":["data-table","duplicate-key","textual","column"],"backgroundTag":"duplicate-key-insert","analyzedSha":"06dbeef4bb70fb718236aa418ed658ef4667a126","analyzedAt":"2026-08-27T02:36:57.214Z","schemaVersion":2},"datasetVersion":"2026-08-27T03:17:27.898Z"}