当前位置:   article > 正文

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

【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(即:没有可关联主表的)

在这里插入图片描述

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='用户积分变更历史';
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

2、主表:cp_point_reward

在这里插入图片描述

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='积分奖品';
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

3、golang 使用 ent 源码

在这里插入图片描述

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

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
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79

4、解决方案

问题:
报错的位置在:&v.Edges.CpPointRewardMPH.Name
因为 v.Edges.CpPointRewardMPHnil

解决

先判断 v.Edges.CpPointRewardMPH 是否为空
如果不为空,则赋值

if v.Edges.CpPointRewardMPH != nil {
	fmt.Println("------------ v.Edges.CpPointRewardMPH.Name:", v.Edges.CpPointRewardMPH.Name)
	Template.PointName = &v.Edges.CpPointRewardMPH.Name
}
  • 1
  • 2
  • 3
  • 4
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/煮酒与君饮/article/detail/861826
推荐阅读
相关标签
  

闽ICP备14008679号