Skip to content

Commit

Permalink
Fix: foreign key column not found issue
Browse files Browse the repository at this point in the history
  • Loading branch information
dosco committed Oct 7, 2022
1 parent 3dc8fd3 commit 01d54ca
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 5 deletions.
4 changes: 4 additions & 0 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ func (gj *graphjin) _initSchema() error {
gj.dbinfo,
getDBTableAliases(gj.conf))

if err != nil {
return err
}

ssufx := gj.conf.SingularSuffix
if ssufx == "" {
ssufx = "ByID"
Expand Down
1 change: 1 addition & 0 deletions core/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ func addForeignKey(conf *Config, di *sdata.DBInfo, c Column, t Table) error {
}

fks, fkt, fkc := v[0], v[1], v[2]

c3, err := di.GetColumn(fks, fkt, fkc)
if err != nil {
return fmt.Errorf(
Expand Down
1 change: 0 additions & 1 deletion core/internal/sdata/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ func (s *DBSchema) addColumnRels(t DBTable) error {

for i := range t.Columns {
c := t.Columns[i]

if c.FKeyTable == "" {
continue
}
Expand Down
4 changes: 3 additions & 1 deletion core/internal/sdata/sql/postgres_columns.sql
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,6 @@ WHERE
AND n.nspname NOT IN ('_graphjin', 'information_schema', 'pg_catalog')
AND c.relname != 'schema_version'
AND f.attnum > 0
AND f.attisdropped = false;
AND f.attisdropped = false
ORDER BY
f.attrelid, f.attnum ASC;
13 changes: 10 additions & 3 deletions core/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ func TestMutiSchema(t *testing.T) {
}

for j := 0; j < totalTables; j++ {
createTableSQL := `CREATE TABLE test_schema_%d.test_table_%d_%d (
st := fmt.Sprintf("test_schema_%d.test_table_%d_%d", i, i, j)
refCol := "bigint"
if i != 0 {
refCol = fmt.Sprintf("bigint references test_schema_%d.test_table_%d_%d(id)",
(i - 1), (i - 1), j)
}
createTableSQL := `CREATE TABLE %s (
id BIGSERIAL PRIMARY KEY,
column1 TEXT,
column2 TEXT,
Expand All @@ -37,10 +43,11 @@ func TestMutiSchema(t *testing.T) {
column9 NUMERIC,
column10 JSONB,
column11 TEXT,
column12 TEXT
column12 TEXT,
column13 %s
);`

_, err := db.Exec(fmt.Sprintf(createTableSQL, i, i, j))
_, err := db.Exec(fmt.Sprintf(createTableSQL, st, refCol))
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 01d54ca

Please sign in to comment.