From f3f42957128c1e7ece584d45d9167f2ac003a3b8 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Sun, 22 Apr 2018 13:46:18 -0700 Subject: [PATCH] label_column now defined on the table-being-linked-to, fixes #234 --- datasette/app.py | 4 ++-- tests/fixtures.py | 11 +++++++++-- tests/test_api.py | 19 +++++++++++++++---- tests/test_html.py | 2 +- 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/datasette/app.py b/datasette/app.py index 0b425c04..a69ac537 100644 --- a/datasette/app.py +++ b/datasette/app.py @@ -525,8 +525,8 @@ class RowTableShared(BaseView): foreign_keys = table_info['foreign_keys']['outgoing'] for fk in foreign_keys: label_column = ( - # First look for metadata.json definition: - table_metadata.get('label_column') + # First look in metadata.json definition for this foreign key table: + self.table_metadata(database, fk['other_table']).get('label_column') # Fall back to label_column from .inspect() detection: or tables.get(fk['other_table'], {}).get('label_column') ) diff --git a/tests/fixtures.py b/tests/fixtures.py index 37e1f1b0..95f7a3da 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -97,7 +97,7 @@ METADATA = { 'frequency': 'Hz' } }, - 'custom_foreign_key_label': { + 'primary_key_multiple_columns_explicit_label': { 'label_column': 'content2', }, } @@ -145,6 +145,12 @@ CREATE TABLE primary_key_multiple_columns ( content2 text ); +CREATE TABLE primary_key_multiple_columns_explicit_label ( + id varchar(30) primary key, + content text, + content2 text +); + CREATE TABLE compound_primary_key ( pk1 varchar(30), pk2 varchar(30), @@ -221,7 +227,7 @@ CREATE TABLE "complex_foreign_keys" ( CREATE TABLE "custom_foreign_key_label" ( pk varchar(30) primary key, foreign_key_with_custom_label text, - FOREIGN KEY ("foreign_key_with_custom_label") REFERENCES [primary_key_multiple_columns](id) + FOREIGN KEY ("foreign_key_with_custom_label") REFERENCES [primary_key_multiple_columns_explicit_label](id) ); CREATE TABLE units ( @@ -246,6 +252,7 @@ INSERT INTO simple_primary_key VALUES (2, 'world'); INSERT INTO simple_primary_key VALUES (3, ''); INSERT INTO primary_key_multiple_columns VALUES (1, 'hey', 'world'); +INSERT INTO primary_key_multiple_columns_explicit_label VALUES (1, 'hey', 'world2'); INSERT INTO foreign_key_references VALUES (1, 1, 1); diff --git a/tests/test_api.py b/tests/test_api.py index 839dc1cb..130f30a3 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -17,7 +17,7 @@ def test_homepage(app_client): assert response.json.keys() == {'test_tables': 0}.keys() d = response.json['test_tables'] assert d['name'] == 'test_tables' - assert d['tables_count'] == 14 + assert d['tables_count'] == 15 def test_database_page(app_client): @@ -89,12 +89,12 @@ def test_database_page(app_client): 'outgoing': [{ 'column': 'foreign_key_with_custom_label', 'other_column': 'id', - 'other_table': 'primary_key_multiple_columns' + 'other_table': 'primary_key_multiple_columns_explicit_label' }], }, 'label_column': None, 'primary_keys': ['pk'], - }, { + }, { 'columns': ['pk', 'foreign_key_with_label', 'foreign_key_with_no_label'], 'name': 'foreign_key_references', 'count': 1, @@ -130,7 +130,18 @@ def test_database_page(app_client): 'column': 'id', 'other_column': 'foreign_key_with_no_label', 'other_table': 'foreign_key_references' - }, { + }], + 'outgoing': [] + }, + 'hidden': False, + 'label_column': None, + 'primary_keys': ['id'] + }, { + 'columns': ['id', 'content', 'content2'], + 'name': 'primary_key_multiple_columns_explicit_label', + 'count': 1, + 'foreign_keys': { + 'incoming': [{ 'column': 'id', 'other_column': 'foreign_key_with_custom_label', 'other_table': 'custom_foreign_key_label' diff --git a/tests/test_html.py b/tests/test_html.py index ee12ce35..13433af6 100644 --- a/tests/test_html.py +++ b/tests/test_html.py @@ -301,7 +301,7 @@ def test_table_html_foreign_key_custom_label_column(app_client): expected = [ [ '1', - 'world\xa01', + 'world2\xa01', ] ] assert expected == [[str(td) for td in tr.select('td')] for tr in table.select('tbody tr')]