Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WithAll().Scan(&list) can't fetch embedded struct from HasOne relation's children table #2119

Open
nana35807731-163-com opened this issue Sep 10, 2022 · 1 comment · May be fixed by #3559
Open
Assignees
Labels
planned This issue/proposal is planned into our next steps.

Comments

@nana35807731-163-com
Copy link

1. What version of Go and system type/arch are you using?

go version go1.19 windows/amd64

2. What version of GoFrame are you using?

GoFrame CLI Tool v2.1.4, https://goframe.org
GoFrame Version: v2.1.4 in current go.mod
CLI Installed At: C:\Program Files\Go\1.19\bin\gf.exe
Current is a custom installed version, no installation information.

3. Can this issue be re-produced with the latest release?

Sure!

4. What did you do?

create tables in MySQL 8.0

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for sys_role
-- ----------------------------
DROP TABLE IF EXISTS `sys_role`;
CREATE TABLE `sys_role`  (
  `id` int(0) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '||s',
  `name` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '角色名称||s,r',
  `code` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '角色 code||s,r',
  `description` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '描述信息|text',
  `weight` int(0) UNSIGNED NOT NULL DEFAULT 0 COMMENT '排序||r|min:0#发布状态不能小于 0',
  `status_id` int(0) UNSIGNED NOT NULL DEFAULT 1 COMMENT '发布状态|hasOne|f:status,fk:id',
  `created_at` datetime(0) NULL DEFAULT NULL,
  `updated_at` datetime(0) NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE,
  INDEX `code`(`code`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 1091 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '系统角色表' ROW_FORMAT = Compact;

-- ----------------------------
-- Records of sys_role
-- ----------------------------
INSERT INTO `sys_role` VALUES (1, '开发人员', 'developer', '123123', 900, 2, '2022-09-03 21:25:03', '2022-09-09 23:35:23');
INSERT INTO `sys_role` VALUES (2, '管理员', 'admin', '', 800, 1, '2022-09-03 21:25:03', '2022-09-09 23:00:17');
INSERT INTO `sys_role` VALUES (3, '运营', 'operator', '', 700, 1, '2022-09-03 21:25:03', '2022-09-03 21:25:03');
INSERT INTO `sys_role` VALUES (4, '客服', 'service', '', 600, 1, '2022-09-03 21:25:03', '2022-09-03 21:25:03');
INSERT INTO `sys_role` VALUES (5, '收银', 'account', '', 500, 1, '2022-09-03 21:25:03', '2022-09-03 21:25:03');

-- ----------------------------
-- Table structure for sys_status
-- ----------------------------
DROP TABLE IF EXISTS `sys_status`;
CREATE TABLE `sys_status`  (
  `id` int(0) UNSIGNED NOT NULL AUTO_INCREMENT,
  `en` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '英文名称',
  `cn` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '中文名称',
  `weight` int(0) UNSIGNED NOT NULL DEFAULT 0 COMMENT '排序权重',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 7 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '发布状态' ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of sys_status
-- ----------------------------
INSERT INTO `sys_status` VALUES (1, 'on line', '上线', 900);
INSERT INTO `sys_status` VALUES (2, 'undecided', '未决定', 800);
INSERT INTO `sys_status` VALUES (3, 'off line', '下线', 700);

define custom Entity structs, Base struct for re-use in other struct

type RoleBase struct {
	gmeta.Meta  `orm:"table:sys_role"`
	Name        string      `json:"name"           description:"角色名称"     `
	Code        string      `json:"code"           description:"角色 code"    `
	Description string      `json:"description"    description:"描述信息"     `
	Weight      int         `json:"weight"         description:"排序"         `
	StatusId    int         `json:"statusId"       description:"发布状态"     `
	CreatedAt   *gtime.Time `json:"createdAt"      description:""             `
	UpdatedAt   *gtime.Time `json:"updatedAt"      description:""             `
}

type Role struct {
	gmeta.Meta `orm:"table:sys_role"`
	RoleBase
	Id     uint    `json:"id"          description:""`
	Status *Status `json:"status"       description:"发布状态"     orm:"with:id=status_id"        `
}

type StatusBase struct {
	gmeta.Meta `orm:"table:sys_status"`
	En         string `json:"en"        description:"英文名称"    `
	Cn         string `json:"cn"        description:"中文名称"    `
	Weight     int    `json:"weight"    description:"排序权重"    `
}

type Status struct {
	gmeta.Meta `orm:"table:sys_status"`
	StatusBase
	Id uint `json:"id"          description:""`
}

try to fetch data by dao's WithAll().Scan(&roles)

fetch Statuses is CORRECT, it can get embedded struct

	statuses := make([]*po.Status, 0)
	err = dao.Status.Ctx(context.Background()).Scan(&statuses)
	if err != nil {
		t.Error("err != nil")
	}
	g.Dump(statuses)

fetch Roles is INCORRECT, it can't get embedded Status struct ( En, Cn, Weight is empty )

	roles := make([]*po.Role, 0)
	err = dao.Role.Ctx(context.Background()).WithAll().Scan(&roles)
	if err != nil {
		t.Error("err != nil")
	}
	g.Dump(roles)

5. What did you expect to see?

log file

2022-09-10 13:14:03.380 [DEBU] {64e2bc00d367131752f835214fec97e5} [  3 ms] [default] [rows:8  ] SHOW FULL COLUMNS FROM `sys_role`
2022-09-10 13:14:03.383 [DEBU] {d8f9f900d367131753f83521dc03fecb} [  2 ms] [default] [rows:5  ] SELECT `name`,`code`,`description`,`weight`,`status_id`,`created_at`,`updated_at`,`id` FROM `sys_role`
2022-09-10 13:14:03.386 [DEBU] {eccb2701d367131754f83521cd6a9892} [  2 ms] [default] [rows:2  ] SELECT `en`,`cn`,`weight`,`id` FROM `sys_status` WHERE `id` IN(2,1)

g.Dump result

[
    {
        Name:        "开发人员",
        Code:        "developer",
        Description: "123123",
        Weight:      900,
        StatusId:    2,
        CreatedAt:   "2022-09-03 21:25:03",
        UpdatedAt:   "2022-09-09 23:35:23",
        Id:          1,
        Status:      {
            En:     "undecided",
            Cn:     "未决定",
            Weight: 800,
            Id:     2,
        },
    },
    {
        Name:        "管理员",
        Code:        "admin",
        Description: "",
        Weight:      800,
        StatusId:    1,
        CreatedAt:   "2022-09-03 21:25:03",
        UpdatedAt:   "2022-09-09 23:00:17",
        Id:          2,
        Status:      {
            En:     "on line",
            Cn:     "上线",
            Weight: 900,
            Id:     1,
        },
    },
    {
        Name:        "运营",
        Code:        "operator",
        Description: "",
        Weight:      700,
        StatusId:    1,
        CreatedAt:   "2022-09-03 21:25:03",
        UpdatedAt:   "2022-09-03 21:25:03",
        Id:          3,
        Status:      {
            En:     "on line",
            Cn:     "上线",
            Weight: 900,
            Id:     1,
        },
    },
    {
        Name:        "客服",
        Code:        "service",
        Description: "",
        Weight:      600,
        StatusId:    1,
        CreatedAt:   "2022-09-03 21:25:03",
        UpdatedAt:   "2022-09-03 21:25:03",
        Id:          4,
        Status:      {
            En:     "on line",
            Cn:     "上线",
            Weight: 900,
            Id:     1,
        },
    },
    {
        Name:        "收银",
        Code:        "account",
        Description: "",
        Weight:      500,
        StatusId:    1,
        CreatedAt:   "2022-09-03 21:25:03",
        UpdatedAt:   "2022-09-03 21:25:03",
        Id:          5,
        Status:      {
            En:     "on line",
            Cn:     "上线",
            Weight: 900,
            Id:     1,
        },
    },
]

6. What did you see instead?

log file

2022-09-10 13:12:28.632 [DEBU] {c02f39f1bc67131795be29475114e8a9} [  4 ms] [default] [rows:8  ] SHOW FULL COLUMNS FROM `sys_role`               
2022-09-10 13:12:28.635 [DEBU] {344776f1bc67131796be29478341a168} [  3 ms] [default] [rows:5  ] SELECT `name`,`code`,`description`,`weight`,`status_id`,`created_at`,`updated_at`,`id` FROM `sys_role`
2022-09-10 13:12:28.639 [DEBU] {6ca4c2f1bc67131797be29475cbbe865} [  2 ms] [default] [rows:2  ] SELECT `id` FROM `sys_status` WHERE `id` IN(2,1)     

g.Dump result

[
  {
    Name:        "开发人员",
    Code:        "developer",
    Description: "123123",
    Weight:      900,
    StatusId:    2,
    CreatedAt:   "2022-09-03 21:25:03",
    UpdatedAt:   "2022-09-09 23:35:23",
    Id:          1,
    Status:      {
      En:     "",
      Cn:     "",
      Weight: 0,
      Id:     2,
    },
  },
  {
    Name:        "管理员",
    Code:        "admin",
    Description: "",
    Weight:      800,
    StatusId:    1,
    CreatedAt:   "2022-09-03 21:25:03",
    UpdatedAt:   "2022-09-09 23:00:17",
    Id:          2,
    Status:      {
      En:     "",
      Cn:     "",
      Weight: 0,
      Id:     1,
    },
  },
  {
    Name:        "运营",
    Code:        "operator",
    Description: "",
    Weight:      700,
    StatusId:    1,
    CreatedAt:   "2022-09-03 21:25:03",
    UpdatedAt:   "2022-09-03 21:25:03",
    Id:          3,
    Status:      {
      En:     "",
      Cn:     "",
      Weight: 0,
      Id:     1,
    },
  },
  {
    Name:        "客服",
    Code:        "service",
    Description: "",
    Weight:      600,
    StatusId:    1,
    CreatedAt:   "2022-09-03 21:25:03",
    UpdatedAt:   "2022-09-03 21:25:03",
    Id:          4,
    Status:      {
      En:     "",
      Cn:     "",
      Weight: 0,
      Id:     1,
    },
  },
  {
    Name:        "收银",
    Code:        "account",
    Description: "",
    Weight:      500,
    StatusId:    1,
    CreatedAt:   "2022-09-03 21:25:03",
    UpdatedAt:   "2022-09-03 21:25:03",
    Id:          5,
    Status:      {
      En:     "",
      Cn:     "",
      Weight: 0,
      Id:     1,
    },
  },
]
@gqcn gqcn added the planned This issue/proposal is planned into our next steps. label Jan 18, 2023
@gqcn
Copy link
Member

gqcn commented Jan 18, 2023

@nana35807731-163-com Hello, can this issue be reproduced in latest version?

@wln32 wln32 linked a pull request May 4, 2024 that will close this issue
@wln32 wln32 self-assigned this May 4, 2024
@wln32 wln32 linked a pull request May 4, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
planned This issue/proposal is planned into our next steps.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants