Skip to content

Commit 1f73fbf

Browse files
committed
add information.PrimaryKeyColumn.ForeignKey
1 parent b614f7e commit 1f73fbf

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed

information/primarykeys.go

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ import (
1717
)
1818

1919
type PrimaryKeyColumn struct {
20-
Table string `db:"table"`
21-
Column string `db:"column"`
22-
Type string `db:"type"`
20+
Table string `db:"table"`
21+
Column string `db:"column"`
22+
Type string `db:"type"`
23+
ForeignKey bool `db:"foreign_key"`
2324
}
2425

2526
func GetPrimaryKeyColumns(ctx context.Context) (cols []PrimaryKeyColumn, err error) {
@@ -29,7 +30,18 @@ func GetPrimaryKeyColumns(ctx context.Context) (cols []PrimaryKeyColumn, err err
2930
select
3031
tc.table_schema||'.'||tc.table_name as "table",
3132
kc.column_name as "column",
32-
col.data_type as "type"
33+
col.data_type as "type",
34+
(select exists(
35+
select from information_schema.table_constraints as fk_tc
36+
inner join information_schema.key_column_usage as fk_kc
37+
on fk_kc.table_schema = fk_tc.table_schema
38+
and fk_kc.table_name = fk_tc.table_name
39+
and fk_kc.constraint_name = fk_tc.constraint_name
40+
where fk_tc.constraint_type = 'FOREIGN KEY'
41+
and fk_tc.table_schema = tc.table_schema
42+
and fk_tc.table_name = tc.table_name
43+
and fk_kc.column_name = kc.column_name
44+
)) as "foreign_key"
3345
from information_schema.table_constraints as tc
3446
inner join information_schema.key_column_usage as kc
3547
on kc.table_schema = tc.table_schema
@@ -43,8 +55,7 @@ func GetPrimaryKeyColumns(ctx context.Context) (cols []PrimaryKeyColumn, err err
4355
and kc.ordinal_position is not null
4456
order by
4557
tc.table_schema,
46-
tc.table_name,
47-
kc.position_in_unique_constraint`,
58+
tc.table_name`,
4859
).ScanStructSlice(&cols)
4960
if err != nil {
5061
return nil, err
@@ -59,7 +70,18 @@ func GetPrimaryKeyColumnsOfType(ctx context.Context, pkType string) (cols []Prim
5970
select
6071
tc.table_schema||'.'||tc.table_name as "table",
6172
kc.column_name as "column",
62-
col.data_type as "type"
73+
col.data_type as "type",
74+
(select exists(
75+
select from information_schema.table_constraints as fk_tc
76+
inner join information_schema.key_column_usage as fk_kc
77+
on fk_kc.table_schema = fk_tc.table_schema
78+
and fk_kc.table_name = fk_tc.table_name
79+
and fk_kc.constraint_name = fk_tc.constraint_name
80+
where fk_tc.constraint_type = 'FOREIGN KEY'
81+
and fk_tc.table_schema = tc.table_schema
82+
and fk_tc.table_name = tc.table_name
83+
and fk_kc.column_name = kc.column_name
84+
)) as "foreign_key"
6385
from information_schema.table_constraints as tc
6486
inner join information_schema.key_column_usage as kc
6587
on kc.table_schema = tc.table_schema
@@ -74,8 +96,7 @@ func GetPrimaryKeyColumnsOfType(ctx context.Context, pkType string) (cols []Prim
7496
and col.data_type = $1
7597
order by
7698
tc.table_schema,
77-
tc.table_name,
78-
kc.position_in_unique_constraint`,
99+
tc.table_name`,
79100
pkType,
80101
).ScanStructSlice(&cols)
81102
if err != nil {

0 commit comments

Comments
 (0)