From 01d54ca2edb1f0051fd7dd8e324239e773e75054 Mon Sep 17 00:00:00 2001 From: dosco <832235+dosco@users.noreply.github.com> Date: Fri, 7 Oct 2022 00:56:49 -0700 Subject: [PATCH] Fix: foreign key column not found issue --- core/core.go | 4 ++++ core/init.go | 1 + core/internal/sdata/schema.go | 1 - core/internal/sdata/sql/postgres_columns.sql | 4 +++- core/postgres_test.go | 13 ++++++++++--- 5 files changed, 18 insertions(+), 5 deletions(-) diff --git a/core/core.go b/core/core.go index fa6d542b..bc6e2e8b 100644 --- a/core/core.go +++ b/core/core.go @@ -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" diff --git a/core/init.go b/core/init.go index c4d96cfb..779cc100 100644 --- a/core/init.go +++ b/core/init.go @@ -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( diff --git a/core/internal/sdata/schema.go b/core/internal/sdata/schema.go index 437e3e52..9e102898 100644 --- a/core/internal/sdata/schema.go +++ b/core/internal/sdata/schema.go @@ -191,7 +191,6 @@ func (s *DBSchema) addColumnRels(t DBTable) error { for i := range t.Columns { c := t.Columns[i] - if c.FKeyTable == "" { continue } diff --git a/core/internal/sdata/sql/postgres_columns.sql b/core/internal/sdata/sql/postgres_columns.sql index 804842d6..3908229d 100644 --- a/core/internal/sdata/sql/postgres_columns.sql +++ b/core/internal/sdata/sql/postgres_columns.sql @@ -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; \ No newline at end of file + AND f.attisdropped = false +ORDER BY + f.attrelid, f.attnum ASC; \ No newline at end of file diff --git a/core/postgres_test.go b/core/postgres_test.go index 5e5850d0..d77bf262 100644 --- a/core/postgres_test.go +++ b/core/postgres_test.go @@ -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, @@ -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) }