Skip to content

Commit b1c2c58

Browse files
bchapuisclaude
andcommitted
Fix database explorer not showing foreign key associations
When REFERENCES omits the column name, SQLite returns an empty string for the referenced column in PRAGMA foreign_key_list. The edge target handle ID was built with an empty column, so it never matched. Now falls back to the primary key column of the referenced table. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent bca10f4 commit b1c2c58

1 file changed

Lines changed: 23 additions & 8 deletions

File tree

apps/app/src/pages/database-explorer-page.tsx

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,29 @@ function SchemaFlowCanvas({ tables }: SchemaFlowCanvasProps) {
136136
}));
137137

138138
const edgeList: Edge[] = tables.flatMap((table) =>
139-
table.foreignKeys.map((fk) => ({
140-
id: `${table.name}-${fk.column}-${fk.referencedTable}-${fk.referencedColumn}`,
141-
source: table.name,
142-
sourceHandle: `${table.name}-${fk.column}-source`,
143-
target: fk.referencedTable,
144-
targetHandle: `${fk.referencedTable}-${fk.referencedColumn}-target`,
145-
type: "smoothstep",
146-
}))
139+
table.foreignKeys
140+
.map((fk) => {
141+
// When REFERENCES omits the column, SQLite returns empty string;
142+
// fall back to the primary key column of the referenced table.
143+
let targetCol = fk.referencedColumn;
144+
if (!targetCol) {
145+
const refTable = tables.find(
146+
(t) => t.name === fk.referencedTable
147+
);
148+
const pk = refTable?.columns.find((c) => c.primaryKey);
149+
if (!pk) return null;
150+
targetCol = pk.name;
151+
}
152+
return {
153+
id: `${table.name}-${fk.column}-${fk.referencedTable}-${targetCol}`,
154+
source: table.name,
155+
sourceHandle: `${table.name}-${fk.column}-source`,
156+
target: fk.referencedTable,
157+
targetHandle: `${fk.referencedTable}-${targetCol}-target`,
158+
type: "smoothstep",
159+
};
160+
})
161+
.filter((e) => e !== null)
147162
);
148163

149164
return { initialNodes: nodes, edges: edgeList };

0 commit comments

Comments
 (0)