Resolve implicit primary key references in table.foreign_keys

For foreign keys declared as "REFERENCES other_table" with no explicit
columns, PRAGMA foreign_key_list returns None for the referenced
column. The foreign_keys property now resolves these to the other
table's primary key columns, so other_column=None unambiguously
indicates a compound foreign key.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Simon Willison 2026-07-05 14:42:30 -07:00
commit 8443d7f3ba
2 changed files with 43 additions and 0 deletions

View file

@ -2075,6 +2075,12 @@ class Table(Queryable):
other_table = rows[0][1]
columns = [row[2] for row in rows]
other_columns = [row[3] for row in rows]
if all(c is None for c in other_columns):
# "REFERENCES other_table" with no columns - the pragma
# returns None, meaning the other table's primary key
other_table_pks = self.db.table(other_table).pks
if len(other_table_pks) == len(columns):
other_columns = other_table_pks
is_compound = len(rows) > 1
fks.append(
ForeignKey(