【golang-ent】go-zero框架 整合 ent orm框架 | 解决left join未关联报错的问题

一、场景

1、子表:cp_member_point_history

cp_member_point_history表中字段:cp_point_reward_id 是cp_point_reward的主键id

当本表中的cp_point_reward_id字段为0(即:没有可关联主表的)

sql 复制代码
CREATE TABLE `cp_member_point_history` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `point` int DEFAULT NULL COMMENT '获取积分',
  `cp_point_reward_id` bigint DEFAULT NULL COMMENT '外键 cp_point_reward 主键id',
  `show_status` tinyint(1) DEFAULT NULL COMMENT '是否删除:0、可用,1:删除',
  `updated_at` datetime DEFAULT NULL COMMENT '更新时间',
  `created_at` datetime DEFAULT NULL COMMENT '创建时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='用户积分变更历史';

2、主表:cp_point_reward

sql 复制代码
CREATE TABLE `cp_point_reward` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `point` int DEFAULT NULL COMMENT '消耗积分',
  `name` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '奖品名称',
  `reward` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '奖品说明',
  `reward_status` int DEFAULT NULL COMMENT '1:上架、2:下架(默认是1)',
  `show_status` tinyint(1) DEFAULT NULL COMMENT '是否删除:0、可用,1:删除',
  `updated_at` datetime DEFAULT NULL COMMENT '更新时间',
  `created_at` datetime DEFAULT NULL COMMENT '创建时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='积分奖品';

3、golang 使用 ent 源码

当 左连接 WithCpPointRewardMPH()时,无法进行关联时,则下面循环时,直接报错panic

go 复制代码
package cpMemberPointHistory

import (
	"context"
	"fmt"
	"github.com/suyuan32/simple-admin-common/utils/pointy"
	"github.com/zeromicro/go-zero/core/logx"
)

type GetCpMemberPointHistoryListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetCpMemberPointHistoryListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetCpMemberPointHistoryListLogic {
	return &GetCpMemberPointHistoryListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GetCpMemberPointHistoryListLogic) GetCpMemberPointHistoryList(in *cpgobusiness.CpMemberPointHistoryListReq) (*cpgobusiness.CpMemberPointHistoryListResp, error) {
	var predicates []predicate.CpMemberPointHistory
	if in.CpMemberId != nil {
		predicates = append(predicates, cpmemberpointhistory.CpMemberID(*in.CpMemberId))
	}
	if in.CpPointRewardId != nil {
		predicates = append(predicates, cpmemberpointhistory.CpPointRewardID(*in.CpPointRewardId))
	}
	if in.CpResourceId != nil {
		predicates = append(predicates, cpmemberpointhistory.CpResourceID(*in.CpResourceId))
	}

	result, err := l.svcCtx.DB.CpMemberPointHistory.Query().Where(predicates...).
		WithCpMemberMPH().
		WithCpPointRewardMPH().
		WithCpResourceMPH().Page(l.ctx, in.Page, in.PageSize, func(pager *ent.CpMemberPointHistoryPager) {
		pager.Order = ent.Desc(cpmemberpointhistory.FieldID)

	})

	if err != nil {
		return nil, dberrorhandler.DefaultEntError(l.Logger, err, in)
	}

	resp := &cpgobusiness.CpMemberPointHistoryListResp{}
	resp.Total = result.PageDetails.Total

	for _, v := range result.List {
		Template := &cpgobusiness.CpMemberPointHistoryInfo{
			Id:              &v.ID,
			CreatedAt:       pointy.GetPointer(v.CreatedAt.UnixMilli()),
			UpdatedAt:       pointy.GetPointer(v.UpdatedAt.UnixMilli()),
			ShowStatus:      pointy.GetPointer(int64(v.ShowStatus)),
			CpMemberId:      &v.CpMemberID,
			Point:           &v.Point,
			ChangeType:      &v.ChangeType,
			CpResourceId:    &v.CpResourceID,
			CpPointRewardId: &v.CpPointRewardID,
			GainType:        &v.GainType,
			Name:            &v.Edges.CpMemberMPH.Name,
			Phone:           &v.Edges.CpMemberMPH.Phone,
			NickName:        &v.Edges.CpMemberMPH.NickName,
			Gender:          &v.Edges.CpMemberMPH.Gender,
			PointName:       pointy.GetPointer(""),
			ResourceTitle:   &v.Edges.CpResourceMPH.Title,
		}
		if v.Edges.CpPointRewardMPH != nil {
			fmt.Println("------------ v.Edges.CpPointRewardMPH.Name:", v.Edges.CpPointRewardMPH.Name)
			Template.PointName = &v.Edges.CpPointRewardMPH.Name
		}
		resp.Data = append(resp.Data, Template)
	}

	return resp, nil
}

4、解决方案

问题:

报错的位置在:&v.Edges.CpPointRewardMPH.Name

因为 v.Edges.CpPointRewardMPHnil

解决

先判断 v.Edges.CpPointRewardMPH 是否为空

如果不为空,则赋值

go 复制代码
if v.Edges.CpPointRewardMPH != nil {
	fmt.Println("------------ v.Edges.CpPointRewardMPH.Name:", v.Edges.CpPointRewardMPH.Name)
	Template.PointName = &v.Edges.CpPointRewardMPH.Name
}
相关推荐
喵叔哟30 分钟前
重构代码之移动字段
java·数据库·重构
念白44334 分钟前
智能病历xml提取
数据库·sql·oracle
qingy_204638 分钟前
【JavaWeb】JavaWeb入门之XML详解
数据库·oracle
大数据面试宝典42 分钟前
用AI来写SQL:让ChatGPT成为你的数据库助手
数据库·人工智能·chatgpt
努力的小雨1 小时前
快速上手 KSQL:轻松与数据库交互的利器
数据库·经验分享
Gentle5861 小时前
labview中连接sql server数据库查询语句
数据库·labview
Gentle5861 小时前
labview用sql server数据库存取数据到一个单元格
数据库·labview
2401_857636391 小时前
共享汽车管理新纪元:SpringBoot框架应用
数据库·spring boot·汽车
菲兹园长1 小时前
表的设计(MYSQL)
数据库·mysql
Java Fans1 小时前
MySQL数据库常用命令大全(完整版——表格形式)
数据库·mysql