@@ -17,9 +17,10 @@ import (
17
17
)
18
18
19
19
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"`
23
24
}
24
25
25
26
func GetPrimaryKeyColumns (ctx context.Context ) (cols []PrimaryKeyColumn , err error ) {
@@ -29,7 +30,18 @@ func GetPrimaryKeyColumns(ctx context.Context) (cols []PrimaryKeyColumn, err err
29
30
select
30
31
tc.table_schema||'.'||tc.table_name as "table",
31
32
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"
33
45
from information_schema.table_constraints as tc
34
46
inner join information_schema.key_column_usage as kc
35
47
on kc.table_schema = tc.table_schema
@@ -43,8 +55,7 @@ func GetPrimaryKeyColumns(ctx context.Context) (cols []PrimaryKeyColumn, err err
43
55
and kc.ordinal_position is not null
44
56
order by
45
57
tc.table_schema,
46
- tc.table_name,
47
- kc.position_in_unique_constraint` ,
58
+ tc.table_name` ,
48
59
).ScanStructSlice (& cols )
49
60
if err != nil {
50
61
return nil , err
@@ -59,7 +70,18 @@ func GetPrimaryKeyColumnsOfType(ctx context.Context, pkType string) (cols []Prim
59
70
select
60
71
tc.table_schema||'.'||tc.table_name as "table",
61
72
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"
63
85
from information_schema.table_constraints as tc
64
86
inner join information_schema.key_column_usage as kc
65
87
on kc.table_schema = tc.table_schema
@@ -74,8 +96,7 @@ func GetPrimaryKeyColumnsOfType(ctx context.Context, pkType string) (cols []Prim
74
96
and col.data_type = $1
75
97
order by
76
98
tc.table_schema,
77
- tc.table_name,
78
- kc.position_in_unique_constraint` ,
99
+ tc.table_name` ,
79
100
pkType ,
80
101
).ScanStructSlice (& cols )
81
102
if err != nil {
0 commit comments