Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Foreign key labels for tables with unique indexes on multiple columns #2413

Open
simonw opened this issue Aug 31, 2024 · 1 comment
Open

Comments

@simonw
Copy link
Owner

simonw commented Aug 31, 2024

While hacking around with https://github.com/datasette/datasette-acl I noticed this table:

CleanShot 2024-08-30 at 17 01 07@2x

Those resource_id integers link to this table:

CleanShot 2024-08-30 at 17 01 17@2x

CREATE TABLE acl_resources (
    id integer primary key autoincrement,
    database text not null,
    resource text,
    unique(database, resource)
);

In this particular case, showing the database and then the resource as the foreign key label would make sense - both of those are strings and they are unique together, which makes them a valid label.

I poked around with Claude and came up with this query:

select
  m.name as table_name,
  json_group_array(ii.name) as unique_column_names
from
  sqlite_master as m,
  pragma_index_list(m.name) AS il,
  pragma_index_info(il.name) AS ii
where
  m.type = 'table'
  and il.origin = 'u'
group by
  il.name;

Which returns this for that schema:

CleanShot 2024-08-30 at 17 03 14@2x

This could be a useful alternative way to determine default foreign key labels.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant