fix: WherePri function wrong in pgsql #3330 (#3339)

This commit is contained in:
oldme
2024-03-06 19:19:07 +08:00
committed by GitHub
parent 290f4a3e65
commit 240dadff92
2 changed files with 76 additions and 1 deletions

View File

@ -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

View File

@ -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"])
}
})
}