Skip to content

Commit 44ef258

Browse files
authored
Merge pull request #465 from dolthub/liuliu/foreign-key-column-index-fix
Graphql: foreign key column index fix
2 parents 9de98a2 + b708672 commit 44ef258

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

graphql-server/src/queryFactory/dolt/queries.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,18 @@ import { RawRows } from "../types";
55

66
export const columnsQuery = `DESCRIBE ??`;
77

8-
export const foreignKeysQuery = `SELECT * FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
9-
WHERE table_name=? AND table_schema=?
10-
AND referenced_table_schema IS NOT NULL`;
8+
// Use ORDINAL_POSITION from INFORMATION_SCHEMA.COLUMNS to get the column's position within the table
9+
export const foreignKeysQuery = `SELECT
10+
kcu.*,
11+
cols.ORDINAL_POSITION
12+
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS kcu
13+
JOIN INFORMATION_SCHEMA.COLUMNS AS cols
14+
ON kcu.TABLE_SCHEMA = cols.TABLE_SCHEMA
15+
AND kcu.TABLE_NAME = cols.TABLE_NAME
16+
AND kcu.COLUMN_NAME = cols.COLUMN_NAME
17+
WHERE kcu.table_name = ?
18+
AND kcu.table_schema = ?
19+
AND kcu.referenced_table_schema IS NOT NULL`;
1120

1221
export const indexQuery = `SELECT
1322
table_name,

0 commit comments

Comments
 (0)