添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
文章

Go开发

Go资源

  • Go学习文档: https://www.topgoer.com/

  • Go实战项目: https://github.com/flipped-aurora/gin-vue-admin

  • Go学习路径:https://mp.weixin.qq.com/s/DIIpLUrGj2_-7Qd6vu0kuA

  • Go编码规范:https://github.com/xxjwxc/uber_go_guide_cn#%E4%BB%8B%E7%BB%8D

GORM配置外键,满足关联查询

一句话总结外键查询配置: gorm:"foreignKey:关联表的结构体字段;references:当前表的结构体字段; 不需要配置数据表的外键,只需在Model中定义外键和引用就可以了。

举例:

1
type ProblemBasic struct {
	ID                uint               `gorm:"primarykey;" json:"id"`
	CreatedAt         MyTime             `json:"created_at"`
	UpdatedAt         MyTime             `json:"updated_at"`
	DeletedAt         gorm.DeletedAt     `gorm:"index;" json:"deleted_at"`
	Identity          string             `gorm:"column:identity;type:varchar(36);" json:"identity"`                  // 问题表的唯一标识
	ProblemCategories []*ProblemCategory `gorm:"foreignKey:problem_id;references:id" json:"problem_categories"`      // 关联问题分类表 ProblemCategory
	Title             string             `gorm:"column:title;type:varchar(255);" json:"title"`                       // 文章标题
	Content           string             `gorm:"column:content;type:text;" json:"content"`                           // 文章正文
	MaxRuntime        int                `gorm:"column:max_runtime;type:int(11);" json:"max_runtime"`                // 最大运行时长
	MaxMem            int                `gorm:"column:max_mem;type:int(11);" json:"max_mem"`                        // 最大运行内存
	PassNum           int64              `gorm:"column:pass_num;type:int(11);" json:"pass_num"`                      // 通过次数
	SubmitNum         int64              `gorm:"column:submit_num;type:int(11);" json:"submit_num"`                  // 提交次数
type ProblemCategory struct {
	ID            uint           `gorm:"primarykey;" json:"id"`
	CreatedAt     MyTime         `json:"created_at"`
	UpdatedAt     MyTime         `json:"updated_at"`
	DeletedAt     gorm.DeletedAt `gorm:"index;" json:"deleted_at"`
	ProblemId     uint           `gorm:"column:problem_id;type:int(11);" json:"problem_id"`           // 问题的ID
	CategoryId    uint           `gorm:"column:category_id;type:int(11);" json:"category_id"`         // 分类的ID
	CategoryBasic *CategoryBasic `gorm:"foreignKey:id;references:category_id;" json:"category_basic"` // 关联分类的基础信息表 CategoryBasic
type CategoryBasic struct {
	ID        uint           `gorm:"primarykey;" json:"id"`
	CreatedAt MyTime         `json:"created_at"`
	UpdatedAt MyTime         `json:"updated_at"`
	DeletedAt gorm.DeletedAt `gorm:"index;" json:"deleted_at"`
	Identity  string         `gorm:"column:identity;type:varchar(36);" json:"identity"` // 分类的唯一标识
	Name      string         `gorm:"column:name;type:varchar(100);" json:"name"`        // 分类名称
	ParentId  int            `gorm:"column:parent_id;type:int(11);" json:"parent_id"`   // 父级ID

查询ProblemBasic表记录时,得到的json数据:

1
  "code": 200,
  "data": {
    "id": 1,
    "created_at": "2023-03-22 18:20:22",
    "updated_at": "2023-03-22 18:42:47",
    "deleted_at": null,
    "identity": "1",
    "problem_categories": [
        "id": 1,
        "created_at": "2023-03-22 18:26:33",
        "updated_at": "2023-03-22 18:53:24",
        "deleted_at": null,
        "problem_id": 1,
        "category_id": 1,
        "category_basic": {
          "id": 1,
          "created_at": "2023-03-22 18:30:26",
          "updated_at": "2023-03-22 18:53:34",
          "deleted_at": null,
          "identity": "1",
          "name": "分类1",
          "parent_id": 0
        "id": 2,
        "created_at": "2023-03-22 18:26:33",
        "updated_at": "2023-03-22 18:53:24",
        "deleted_at": null,
        "problem_id": 1,
        "category_id": 2,
        "category_basic": {
          "id": 2,
          "created_at": "2023-03-22 18:30:26",
          "updated_at": "2023-03-22 18:53:34",
          "deleted_at": null,
          "identity": "2",
          "name": "分类2",
          "parent_id": 0
    "title": "题目1",
    "content": "这是题目1的详细内容",
    "max_runtime": 0,