gorm 连接查询(两表联查,三表联查)预加载的坑

@ GO gorm 连接查询gorm 连接查询(两表联查,三表联查)新人一枚,代码不够漂亮,望多多担待1,gorm模型如下:// 文章type Topics struct { Id int `gorm:"primary_key"` Title
摘要由CSDN通过智能技术生成

@ GO gorm 连接查询

gorm 关接查询(两表联查,三表联查)

新人一枚,代码不够漂亮,望多多担待

多少雄心壮志跃跃欲试,宝剑尚未佩妥,出门已是江湖

1,gorm模型如下:

// 文章
type Topics struct {
   
	Id         int        `gorm:"primary_key"`
	Title      string     `gorm:"not null"`
	UserId     int        `gorm:"not null"`
	CategoryId int        `gorm:"not null"`
	Category   Categories `gorm:"foreignkey:CategoryId"`//文章所属分类外键
	User       Users      `gorm:"foreignkey:UserId"`//文章所属用户外键
}

// 用户
type Users struct {
   
	Id   int    `gorm:"primary_key"`
	Name string `gorm:"not null"`
}

// 分类
type Categories struct {
   
	Id   int    `gorm:"primary_key"`
	Name string `gorm:"not null"`
}
gorm官方文档对foreign key 有这么一句话:To define a belongs to relationship, the foreign key must exists, default foreign key uses owner’s type name plus its primary key(要定义属于关系,外键必须存在,默认外键使用所有者的类型名及其主键)也就是说默认外键满足其所有者的类型名和主键

2, 查询代码

func 
  • 5
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值