From 7bc42e6eaa29f8f39bf096659b8ff3d3189febb5 Mon Sep 17 00:00:00 2001 From: jflyfox Date: Mon, 4 Jan 2021 19:46:46 +0800 Subject: [PATCH] improve Fields filtering for package gdb --- database/gdb/gdb_core_structure.go | 7 +------ database/gdb/gdb_z_mysql_model_test.go | 12 +++++++++++- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/database/gdb/gdb_core_structure.go b/database/gdb/gdb_core_structure.go index 33dda6e36..9458de0ab 100644 --- a/database/gdb/gdb_core_structure.go +++ b/database/gdb/gdb_core_structure.go @@ -7,7 +7,6 @@ package gdb import ( - "github.com/gogf/gf/errors/gerror" "github.com/gogf/gf/util/gutil" "strings" "time" @@ -163,15 +162,11 @@ func (c *Core) mappingAndFilterData(schema, table string, data map[string]interf if foundKey != "" { data[foundKey] = dataValue delete(data, dataKey) - } else if !filter { - if schema != "" { - return nil, gerror.Newf(`no column of name "%s" found for table "%s" in schema "%s"`, dataKey, table, schema) - } - return nil, gerror.Newf(`no column of name "%s" found for table "%s"`, dataKey, table) } } } // Data filtering. + // It deletes all key-value pairs that has incorrect field name. if filter { for dataKey, _ := range data { if _, ok := fieldsMap[dataKey]; !ok { diff --git a/database/gdb/gdb_z_mysql_model_test.go b/database/gdb/gdb_z_mysql_model_test.go index c52a13919..697f78382 100644 --- a/database/gdb/gdb_z_mysql_model_test.go +++ b/database/gdb/gdb_z_mysql_model_test.go @@ -1,4 +1,4 @@ -// Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved. +// 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, @@ -496,6 +496,16 @@ func Test_Model_Update(t *testing.T) { n, _ := result.RowsAffected() t.Assert(n, 1) }) + // Update + Fields(string) + gtest.C(t, func(t *gtest.T) { + result, err := db.Table(table).Fields("passport").Data(g.Map{ + "passport": "user_44", + "none": "none", + }).Where("passport='user_4'").Update() + t.Assert(err, nil) + n, _ := result.RowsAffected() + t.Assert(n, 1) + }) } func Test_Model_Clone(t *testing.T) {