mirror of
https://gitee.com/johng/gf
synced 2026-06-06 02:25:47 +08:00
fix issue #1412
This commit is contained in:
@ -7,6 +7,7 @@
|
||||
package gdb
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/errors/gcode"
|
||||
"reflect"
|
||||
@ -156,7 +157,8 @@ func (m *Model) doWithScanStruct(pointer interface{}) error {
|
||||
model = model.Order(parsedTagOutput.Order)
|
||||
}
|
||||
err = model.Fields(fieldKeys).Where(relatedSourceName, relatedTargetValue).Scan(bindToReflectValue)
|
||||
if err != nil {
|
||||
// It ignores sql.ErrNoRows in with feature.
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@ -265,7 +267,8 @@ func (m *Model) doWithScanStructs(pointer interface{}) error {
|
||||
err = model.Fields(fieldKeys).
|
||||
Where(relatedSourceName, relatedTargetValue).
|
||||
ScanList(pointer, fieldName, parsedTagOutput.With)
|
||||
if err != nil {
|
||||
// It ignores sql.ErrNoRows in with feature.
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
@ -1992,6 +1992,7 @@ PRIMARY KEY (id)
|
||||
})
|
||||
}
|
||||
|
||||
// https://github.com/gogf/gf/issues/1401
|
||||
func Test_With_Feature_Issue1401(t *testing.T) {
|
||||
var (
|
||||
table1 = "parcels"
|
||||
@ -2032,3 +2033,65 @@ func Test_With_Feature_Issue1401(t *testing.T) {
|
||||
t.Assert(parcelDetail.Items[0].ParcelId, 3)
|
||||
})
|
||||
}
|
||||
|
||||
// https://github.com/gogf/gf/issues/1412
|
||||
func Test_With_Feature_Issue1412(t *testing.T) {
|
||||
var (
|
||||
table1 = "parcels"
|
||||
table2 = "items"
|
||||
)
|
||||
array := gstr.SplitAndTrim(gtest.TestDataContent(`issue1412.sql`), ";")
|
||||
for _, v := range array {
|
||||
if _, err := db.Exec(v); err != nil {
|
||||
gtest.Error(err)
|
||||
}
|
||||
}
|
||||
defer dropTable(table1)
|
||||
defer dropTable(table2)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type Items struct {
|
||||
gmeta.Meta `orm:"table:items"`
|
||||
Id int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type ParcelRsp struct {
|
||||
gmeta.Meta `orm:"table:parcels"`
|
||||
Id int `json:"id"`
|
||||
ItemId int `json:"item_id"`
|
||||
Items Items `json:"items" orm:"with:Id=ItemId"`
|
||||
}
|
||||
|
||||
entity := &ParcelRsp{}
|
||||
err := db.Model("parcels").With(Items{}).Where("id=3").Scan(&entity)
|
||||
t.AssertNil(err)
|
||||
t.Assert(entity.Id, 3)
|
||||
t.Assert(entity.ItemId, 0)
|
||||
t.Assert(entity.Items.Id, 0)
|
||||
t.Assert(entity.Items.Name, "")
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type Items struct {
|
||||
gmeta.Meta `orm:"table:items"`
|
||||
Id int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type ParcelRsp struct {
|
||||
gmeta.Meta `orm:"table:parcels"`
|
||||
Id int `json:"id"`
|
||||
ItemId int `json:"item_id"`
|
||||
Items Items `json:"items" orm:"with:Id=ItemId"`
|
||||
}
|
||||
|
||||
entity := &ParcelRsp{}
|
||||
err := db.Model("parcels").With(Items{}).Where("id=30000").Scan(&entity)
|
||||
t.AssertNE(err, nil)
|
||||
t.Assert(entity.Id, 0)
|
||||
t.Assert(entity.ItemId, 0)
|
||||
t.Assert(entity.Items.Id, 0)
|
||||
t.Assert(entity.Items.Name, "")
|
||||
})
|
||||
}
|
||||
|
||||
@ -435,7 +435,6 @@ func Test_Model_Clone(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
db.SetDebug(true)
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
md := db.Model(table).Where("id IN(?)", g.Slice{1, 3})
|
||||
count, err := md.Count()
|
||||
|
||||
30
database/gdb/testdata/issue1412.sql
vendored
Normal file
30
database/gdb/testdata/issue1412.sql
vendored
Normal file
@ -0,0 +1,30 @@
|
||||
-- ----------------------------
|
||||
-- Table structure for items
|
||||
-- ----------------------------
|
||||
CREATE TABLE `items` (
|
||||
`id` int(11) NOT NULL,
|
||||
`name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
|
||||
PRIMARY KEY (`id`) USING BTREE
|
||||
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
|
||||
|
||||
-- ----------------------------
|
||||
-- Records of items
|
||||
-- ----------------------------
|
||||
INSERT INTO `items` VALUES (1, '金秋产品1');
|
||||
INSERT INTO `items` VALUES (2, '金秋产品2');
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for parcels
|
||||
-- ----------------------------
|
||||
CREATE TABLE `parcels` (
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`item_id` int(11) NULL DEFAULT NULL,
|
||||
PRIMARY KEY (`id`) USING BTREE
|
||||
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
|
||||
|
||||
-- ----------------------------
|
||||
-- Records of parcels
|
||||
-- ----------------------------
|
||||
INSERT INTO `parcels` VALUES (1, 1);
|
||||
INSERT INTO `parcels` VALUES (2, 2);
|
||||
INSERT INTO `parcels` VALUES (3, 0);
|
||||
Reference in New Issue
Block a user