From 463eea2bd0dabe48ec7f004ff96d78f3178183a5 Mon Sep 17 00:00:00 2001 From: Ritesh Kewlani Date: Wed, 24 Jun 2026 02:48:49 +0530 Subject: [PATCH] Fix _col= producing duplicate column in output (#2774) Closes #1975 --- datasette/views/table.py | 4 ++-- tests/test_table_api.py | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/datasette/views/table.py b/datasette/views/table.py index 4b923d20..1fc151e6 100644 --- a/datasette/views/table.py +++ b/datasette/views/table.py @@ -1606,8 +1606,8 @@ async def _columns_to_select(table_columns, pks, request): "_col={} - invalid columns".format(", ".join(bad_columns)), status=400, ) - # De-duplicate maintaining order: - columns.extend(dict.fromkeys(_cols)) + # De-duplicate maintaining order, skipping columns already added (pks): + columns.extend(c for c in dict.fromkeys(_cols) if c not in columns) if "_nocol" in request.args: # Return all columns EXCEPT these bad_columns = [ diff --git a/tests/test_table_api.py b/tests/test_table_api.py index 0cb67164..272e39e3 100644 --- a/tests/test_table_api.py +++ b/tests/test_table_api.py @@ -1420,6 +1420,16 @@ def test_generated_columns_are_visible_in_datasette(): "/fixtures/facetable.json?_col=state&_col=state", ["pk", "state"], ), + ( + # https://github.com/simonw/datasette/issues/1975 + "/fixtures/facetable.json?_col=pk&_col=state", + ["pk", "state"], + ), + ( + # https://github.com/simonw/datasette/issues/1975 + "/fixtures/facetable.json?_col=pk", + ["pk"], + ), ( "/fixtures/facetable.json?_col=state&_col=created&_nocol=created", ["pk", "state"],