Skip to content

Commit d8f35bf

Browse files
committed
fix(pgsql): do not pass schema to SlaveLink in Tables() and TableFields()
In PostgreSQL, schema is a namespace within the database, not a separate database. Passing schema to SlaveLink() attempts to connect to a different database with that name, which fails. For proper PostgreSQL schema support: - Tables(ctx, schema): schema parameter is used in SQL query to filter tables - TableFields(ctx, "schema.table"): table can be schema-qualified in regclass Removed unused gutil import from pgsql_table_fields.go.
1 parent 6bedb24 commit d8f35bf

2 files changed

Lines changed: 10 additions & 8 deletions

File tree

contrib/drivers/pgsql/pgsql_table_fields.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"fmt"
1212

1313
"github.com/gogf/gf/v2/database/gdb"
14-
"github.com/gogf/gf/v2/util/gutil"
1514
)
1615

1716
var (
@@ -49,13 +48,14 @@ func init() {
4948
// TableFields retrieves and returns the fields' information of specified table of current schema.
5049
func (d *Driver) TableFields(ctx context.Context, table string, schema ...string) (fields map[string]*gdb.TableField, err error) {
5150
var (
52-
result gdb.Result
53-
link gdb.Link
54-
usedSchema = gutil.GetOrDefaultStr(d.GetSchema(), schema...)
55-
// TODO duplicated `id` result?
51+
result gdb.Result
52+
link gdb.Link
53+
// In PostgreSQL, schema is a namespace within the database.
54+
// The table parameter can be schema-qualified (e.g., "myschema.mytable").
55+
// DO NOT pass schema to SlaveLink - it would attempt to connect to a different database.
5656
structureSql = fmt.Sprintf(tableFieldsSqlTmp, table)
5757
)
58-
if link, err = d.SlaveLink(usedSchema); err != nil {
58+
if link, err = d.SlaveLink(); err != nil {
5959
return nil, err
6060
}
6161
result, err = d.DoSelect(ctx, link, structureSql)

contrib/drivers/pgsql/pgsql_tables.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,10 @@ func (d *Driver) Tables(ctx context.Context, schema ...string) (tables []string,
7070
result gdb.Result
7171
usedSchema = gutil.GetOrDefaultStr(d.GetConfig().Namespace, schema...)
7272
)
73-
// DO NOT use `usedSchema` as parameter for function `SlaveLink`.
74-
link, err := d.SlaveLink(schema...)
73+
// DO NOT pass schema to SlaveLink - in PostgreSQL, schema is a namespace within
74+
// the database, not the database itself. Passing schema to SlaveLink would attempt
75+
// to connect to a different database named "schema", which is incorrect.
76+
link, err := d.SlaveLink()
7577
if err != nil {
7678
return nil, err
7779
}

0 commit comments

Comments
 (0)