Skip to content

Commit 17385ee

Browse files
authored
database/gdb: fix confusing error message in Insert/Update operations when table not exist or the table contains no fields (#3553)
1 parent 23df83c commit 17385ee

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

contrib/drivers/pgsql/pgsql_z_unit_db_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package pgsql_test
88

99
import (
1010
"fmt"
11+
"strings"
1112
"testing"
1213

1314
"github.com/gogf/gf/v2/frame/g"
@@ -327,3 +328,58 @@ func Test_DB_TableFields(t *testing.T) {
327328
}
328329
})
329330
}
331+
332+
func Test_NoFields_Error(t *testing.T) {
333+
createSql := `CREATE TABLE IF NOT EXISTS %s (
334+
id bigint PRIMARY KEY,
335+
int_col INT);`
336+
337+
type Data struct {
338+
Id int64
339+
IntCol int64
340+
}
341+
// pgsql converts table names to lowercase
342+
tableName := "Error_table"
343+
errStr := fmt.Sprintf(`The table "%s" may not exist, or the table contains no fields`, tableName)
344+
_, err := db.Exec(ctx, fmt.Sprintf(createSql, tableName))
345+
gtest.AssertNil(err)
346+
defer dropTable(tableName)
347+
348+
gtest.C(t, func(t *gtest.T) {
349+
var data = Data{
350+
Id: 2,
351+
IntCol: 2,
352+
}
353+
_, err = db.Model(tableName).Data(data).Insert()
354+
t.Assert(err, errStr)
355+
356+
// Insert a piece of test data using lowercase
357+
_, err = db.Model(strings.ToLower(tableName)).Data(data).Insert()
358+
t.AssertNil(err)
359+
360+
_, err = db.Model(tableName).Where("id", 1).Data(g.Map{
361+
"int_col": 9999,
362+
}).Update()
363+
t.Assert(err, errStr)
364+
365+
})
366+
// The inserted field does not exist in the table
367+
gtest.C(t, func(t *gtest.T) {
368+
data := map[string]any{
369+
"id1": 22,
370+
"int_col_22": 11111,
371+
}
372+
_, err = db.Model(tableName).Data(data).Insert()
373+
t.Assert(err, errStr)
374+
375+
lowerTableName := strings.ToLower(tableName)
376+
_, err = db.Model(lowerTableName).Data(data).Insert()
377+
t.Assert(err, fmt.Errorf(`input data match no fields in table "%s"`, lowerTableName))
378+
379+
_, err = db.Model(lowerTableName).Where("id", 1).Data(g.Map{
380+
"int_col-2": 9999,
381+
}).Update()
382+
t.Assert(err, fmt.Errorf(`input data match no fields in table "%s"`, lowerTableName))
383+
})
384+
385+
}

database/gdb/gdb_core_structure.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,9 @@ func (c *Core) mappingAndFilterData(ctx context.Context, schema, table string, d
381381
if err != nil {
382382
return nil, err
383383
}
384+
if len(fieldsMap) == 0 {
385+
return nil, gerror.Newf(`The table %s may not exist, or the table contains no fields`, table)
386+
}
384387
fieldsKeyMap := make(map[string]interface{}, len(fieldsMap))
385388
for k := range fieldsMap {
386389
fieldsKeyMap[k] = nil
@@ -406,6 +409,9 @@ func (c *Core) mappingAndFilterData(ctx context.Context, schema, table string, d
406409
delete(data, dataKey)
407410
}
408411
}
412+
if len(data) == 0 {
413+
return nil, gerror.Newf(`input data match no fields in table %s`, table)
414+
}
409415
}
410416
return data, nil
411417
}

0 commit comments

Comments
 (0)