Skip to content

Commit a03629b

Browse files
simonwclaude
andcommitted
Fix 500 for blob columns in views without rowid, closes #2601
Table pages linked each binary value to a row URL built from the primary key or rowid. Views usually have neither, so the HTML page raised IndexError and returned a 500, and CSV export of the view stopped after the header row. For views with no rowid column, link to the view's .blob renderer by content hash instead, the same approach query pages already use. Also strip _nofacet=1 and _nocount=1 from blob URLs in CSV exports when they come first in the query string. Adds a binary_data_view view to the fixtures database to exercise this. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent f396950 commit a03629b

7 files changed

Lines changed: 69 additions & 12 deletions

File tree

‎datasette/fixtures.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,10 @@ def generate_sortable_rows(num):
335335
CREATE VIEW searchable_view_configured_by_metadata AS
336336
SELECT * from searchable;
337337
338+
-- A view with a BLOB column and no rowid, see #2601
339+
CREATE VIEW binary_data_view AS
340+
SELECT data FROM binary_data;
341+
338342
"""
339343
+ "\n".join(
340344
[

‎datasette/views/base.py‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,11 @@ async def stream_fn(r):
290290
new_row = []
291291
for column, cell in zip(headings, row):
292292
if isinstance(cell, bytes):
293-
# If this is a table page, use .urls.row_blob()
294-
if data.get("table"):
295-
pks = data.get("primary_keys") or []
293+
pks = data.get("primary_keys") or []
294+
# If this is a table page with a way to identify
295+
# the row, use .urls.row_blob(). Views usually
296+
# have no rowid, so they use the content hash.
297+
if data.get("table") and (pks or "rowid" in headings):
296298
cell = datasette.absolute_url(
297299
request,
298300
datasette.urls.row_blob(
@@ -318,9 +320,10 @@ async def stream_fn(r):
318320
replace_format=format_,
319321
),
320322
)
321-
cell = url.replace("&_nocount=1", "").replace(
322-
"&_nofacet=1", ""
323-
)
323+
for internal_arg in ("_nocount=1", "_nofacet=1"):
324+
url = url.replace("&" + internal_arg, "")
325+
url = url.replace("?" + internal_arg + "&", "?")
326+
cell = url
324327
new_row.append(cell)
325328
row = new_row
326329
if not expanded_columns:

‎datasette/views/table.py‎

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import asyncio
2+
import hashlib
23
import itertools
34
import json
45
import time
@@ -772,6 +773,7 @@ async def display_columns_and_rows(
772773
col.name: col for col in await db.table_column_details(table_name)
773774
}
774775
pks = await db.primary_keys(table_name)
776+
has_rowid_column = any(d[0] == "rowid" for d in description)
775777
pks_for_display = pks
776778
if not pks_for_display and not await db.view_exists(table_name):
777779
pks_for_display = ["rowid"]
@@ -929,14 +931,27 @@ async def display_columns_and_rows(
929931
display_value = plugin_display_value
930932
elif isinstance(value, bytes):
931933
formatted = format_bytes(len(value))
934+
if pks or has_rowid_column or request is None:
935+
blob_url = datasette.urls.row_blob(
936+
database_name,
937+
table_name,
938+
path_from_row_pks(row, pks, not pks),
939+
column,
940+
)
941+
else:
942+
# Views usually have no rowid to link to a row by,
943+
# so find the blob by its content hash instead
944+
blob_url = path_with_format(
945+
request=request,
946+
format="blob",
947+
extra_qs={
948+
"_blob_column": column,
949+
"_blob_hash": hashlib.sha256(value).hexdigest(),
950+
},
951+
)
932952
display_value = markupsafe.Markup(
933953
'<a class="blob-download" href="{}"{}>&lt;Binary:&nbsp;{:,}&nbsp;byte{}&gt;</a>'.format(
934-
datasette.urls.row_blob(
935-
database_name,
936-
table_name,
937-
path_from_row_pks(row, pks, not pks),
938-
column,
939-
),
954+
blob_url,
940955
(f' title="{formatted}"' if "bytes" not in formatted else ""),
941956
len(value),
942957
"" if len(value) == 1 else "s",

‎tests/test_csv.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,19 @@ async def test_table_csv_blob_columns(ds_client):
141141
)
142142

143143

144+
@pytest.mark.asyncio
145+
async def test_view_csv_blob_columns(ds_client):
146+
# https://github.com/simonw/datasette/issues/2601
147+
response = await ds_client.get("/fixtures/binary_data_view.csv")
148+
assert response.status_code == 200
149+
assert response.text == (
150+
"data\r\n"
151+
"http://localhost/fixtures/binary_data_view.blob?_blob_column=data&_blob_hash=f3088978da8f9aea479ffc7f631370b968d2e855eeb172bea7f6c7a04262bb6d\r\n"
152+
"http://localhost/fixtures/binary_data_view.blob?_blob_column=data&_blob_hash=b835b0483cedb86130b9a2c280880bf5fadc5318ddf8c18d0df5204d40df1724\r\n"
153+
'""\r\n'
154+
)
155+
156+
144157
@pytest.mark.asyncio
145158
async def test_custom_sql_csv_blob_columns(ds_client):
146159
response = await ds_client.get(

‎tests/test_html.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ async def test_database_page(ds_client):
214214
views_ul = soup.find("h2", string="Views").find_next_sibling("ul")
215215
assert views_ul is not None
216216
assert [
217+
("/fixtures/binary_data_view", "binary_data_view"),
217218
("/fixtures/paginated_view", "paginated_view"),
218219
("/fixtures/searchable_view", "searchable_view"),
219220
(

‎tests/test_internals_database.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,7 @@ async def test_view_names(db):
507507
"simple_view",
508508
"searchable_view",
509509
"searchable_view_configured_by_metadata",
510+
"binary_data_view",
510511
]
511512

512513

‎tests/test_table_html.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3016,6 +3016,26 @@ async def test_format_of_binary_links(size, title, length_bytes):
30163016
assert expected in sql_response.text
30173017

30183018

3019+
@pytest.mark.asyncio
3020+
async def test_binary_links_for_views_without_rowid(ds_client):
3021+
# https://github.com/simonw/datasette/issues/2601
3022+
response = await ds_client.get("/fixtures/binary_data_view")
3023+
assert response.status_code == 200
3024+
links = Soup(response.text, "html.parser").select("a.blob-download")
3025+
assert [link.text for link in links] == ["<Binary:\xa07\xa0bytes>"] * 2
3026+
# Views have no rowid, so each link finds its blob by content hash
3027+
for link, expected in zip(
3028+
links, (b"\x15\x1c\x02\xc7\xad\x05\xfe", b"\x15\x1c\x03\xc7\xad\x05\xfe")
3029+
):
3030+
href = link["href"]
3031+
assert href.startswith(
3032+
"/fixtures/binary_data_view.blob?_blob_column=data&_blob_hash="
3033+
)
3034+
blob_response = await ds_client.get(href)
3035+
assert blob_response.status_code == 200
3036+
assert blob_response.content == expected
3037+
3038+
30193039
@pytest.mark.asyncio
30203040
@pytest.mark.parametrize(
30213041
"config",

0 commit comments

Comments
 (0)