diff --git a/contrib/drivers/pgsql/pgsql_table_fields.go b/contrib/drivers/pgsql/pgsql_table_fields.go index 83b344aba93..22155b41b9e 100644 --- a/contrib/drivers/pgsql/pgsql_table_fields.go +++ b/contrib/drivers/pgsql/pgsql_table_fields.go @@ -17,7 +17,7 @@ import ( var ( tableFieldsSqlTmp = ` SELECT a.attname AS field, t.typname AS type,a.attnotnull as null, - (case when d.contype is not null then 'pri' else '' end) as key + (case when d.contype = 'p' then 'pri' when d.contype = 'u' then 'uni' else '' end) as key ,ic.column_default as default_value,b.description as comment ,coalesce(character_maximum_length, numeric_precision, -1) as length ,numeric_scale as scale diff --git a/contrib/drivers/pgsql/pgsql_z_unit_issue_test.go b/contrib/drivers/pgsql/pgsql_z_unit_issue_test.go new file mode 100644 index 00000000000..0a0f3869ff8 --- /dev/null +++ b/contrib/drivers/pgsql/pgsql_z_unit_issue_test.go @@ -0,0 +1,75 @@ +// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +package pgsql_test + +import ( + "fmt" + "github.com/gogf/gf/v2/database/gdb" + "testing" + + "github.com/gogf/gf/v2/frame/g" + "github.com/gogf/gf/v2/os/gtime" + "github.com/gogf/gf/v2/test/gtest" +) + +// https://github.com/gogf/gf/issues/3330 +func Test_Issue3330(t *testing.T) { + var ( + table = fmt.Sprintf(`%s_%d`, TablePrefix+"test", gtime.TimestampNano()) + uniqueName = fmt.Sprintf(`%s_%d`, TablePrefix+"test_unique", gtime.TimestampNano()) + ) + if _, err := db.Exec(ctx, fmt.Sprintf(` + CREATE TABLE %s ( + id bigserial NOT NULL, + passport varchar(45) NOT NULL, + password varchar(32) NOT NULL, + nickname varchar(45) NOT NULL, + create_time timestamp NOT NULL, + PRIMARY KEY (id), + CONSTRAINT %s unique ("password") + ) ;`, table, uniqueName, + )); err != nil { + gtest.Fatal(err) + } + defer dropTable(table) + + gtest.C(t, func(t *gtest.T) { + var ( + list []map[string]interface{} + one gdb.Record + err error + ) + + fields, err := db.TableFields(ctx, table) + t.AssertNil(err) + + t.Assert(fields["id"].Key, "pri") + t.Assert(fields["password"].Key, "uni") + + for i := 1; i <= 10; i++ { + list = append(list, g.Map{ + "id": i, + "passport": fmt.Sprintf("p%d", i), + "password": fmt.Sprintf("pw%d", i), + "nickname": fmt.Sprintf("n%d", i), + "create_time": "2016-06-01 00:00:00", + }) + } + + _, err = db.Model(table).Data(list).Insert() + t.AssertNil(err) + + for i := 1; i <= 10; i++ { + one, err = db.Model(table).WherePri(i).One() + t.AssertNil(err) + t.Assert(one["id"], list[i-1]["id"]) + t.Assert(one["passport"], list[i-1]["passport"]) + t.Assert(one["password"], list[i-1]["password"]) + t.Assert(one["nickname"], list[i-1]["nickname"]) + } + }) +}