From 9bd39672dead0da8570a9e68b70f9ca7a2b4e63e Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Sat, 25 May 2019 09:14:57 -0700 Subject: [PATCH] Better label detection, refs #485 This needs unit tests. --- datasette/app.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/datasette/app.py b/datasette/app.py index 77a33cf3..4f57db2d 100644 --- a/datasette/app.py +++ b/datasette/app.py @@ -647,8 +647,16 @@ class Datasette: return explicit_label_column # If a table has two columns, one of which is ID, then label_column is the other one column_names = await self.table_columns(db_name, table) - if column_names and len(column_names) == 2 and "id" in column_names: - return [c for c in column_names if c != "id"][0] + # Is there a name or title column? + name_or_title = [c for c in column_names if c in ("name", "title")] + if name_or_title: + return name_or_title[0] + if ( + column_names + and len(column_names) == 2 + and ("id" in column_names or "pk" in column_names) + ): + return [c for c in column_names if c not in ("id", "pk")][0] # Couldn't find a label: return None