diff --git a/datasette/app.py b/datasette/app.py index a3ef0752..20b58295 100644 --- a/datasette/app.py +++ b/datasette/app.py @@ -2317,10 +2317,18 @@ ORDER BY allowed.parent, allowed.child from datasette.resources import TableResource other_table = fk["other_table"] - if not await db.table_exists(other_table): + # Foreign key declarations can spell the target with different casing. + target_table = ( + await db.execute( + "select name from sqlite_master where type='table' and name=? collate nocase", + [other_table], + ) + ).first() + if target_table is None: # SQLite accepts a foreign key to a table that does not exist, and # linking to it would only lead to a 404 return {} + other_table = target_table[0] other_column = fk["other_column"] if other_column is None: other_pks = await db.primary_keys(other_table) diff --git a/tests/test_table_html.py b/tests/test_table_html.py index ba484059..99d413fc 100644 --- a/tests/test_table_html.py +++ b/tests/test_table_html.py @@ -835,17 +835,18 @@ async def test_table_html_foreign_key_links(ds_client): @pytest.mark.asyncio -async def test_table_html_foreign_key_to_missing_table_is_not_linked(): +@pytest.mark.parametrize("referenced_table", ("authors", "AuThOrS")) +async def test_table_html_foreign_key_to_missing_table_is_not_linked(referenced_table): # https://github.com/simonw/datasette/issues/1515 ds = Datasette([]) db = ds.add_database( Database(ds, memory_name="test_foreign_key_to_missing_table"), name="data" ) - await db.execute_write_script(""" + await db.execute_write_script(f""" create table authors (id integer primary key, name text); create table books ( id integer primary key, - author_id integer references authors(id), + author_id integer references {referenced_table}(id), missing_id integer references missing_table(id) ); insert into authors (id, name) values (1, 'Ada'); @@ -857,7 +858,7 @@ async def test_table_html_foreign_key_to_missing_table_is_not_linked(): cells = {td["class"][0]: str(td) for td in table.select("tbody tr")[0].select("td")} assert cells["col-author_id"] == ( '' - 'Ada 1' + f'Ada 1' ) assert cells["col-missing_id"] == '7' # The JSON labels are left alone as well