我正在使用Gorm和MySQL开发一个Gin应用程序.为了在Gorm模型中定义belongs to个关系,您必须执行以下操作(示例取自Gorm文档):

// `User` belongs to `Company`, `CompanyID` is the foreign key
type User struct {
  gorm.Model
  Name      string
  CompanyID int
  Company   Company
}

type Company struct {
  ID   int
  Name string
}

这就是我对我的模特所做的:

type Record struct {
    Barcode string `json:"barcode" gorm:"size:48;unique;not null" sql:"index"`
    Name    string `json:"name" gorm:"size:160;unique;not null"`
    ArtistID   uint     `json:"artist_id"`
    Artist     Artist   `gorm:"foreignKey:ArtistID;references:ID"`
    CategoryID uint     `json:"category_id"`
    Category   Category `gorm:"foreignKey:CategoryID;references:ID"`

    NumOfRecords        int         `json:"num_of_records" gorm:"not null"`
    OriginalReleaseDate *utils.Date `json:"original_release_date" gorm:"default:null;type:date"`
    ReissueReleaseDate  *utils.Date `json:"reissue_release_date" gorm:"default:null;type:date"`
    SideColor           *string     `json:"side_color" gorm:"default:null"`
    BarcodeInRecord     *bool       `json:"barcode_in_record" gorm:"default=true"`
    gorm.Model
}
type Category struct {
    Name        string `json:"name" gorm:"size:60;unique;not null"`
    Description string `json:"description" gorm:"size:120"`
    Parent      uint   `json:"parent" gorm:"default:null"`
    Active      bool   `json:"active" gorm:"default:true"`
    gorm.Model
}
type Artist struct {
    Name            string `json:"name" gorm:"size:120;unique;not null"`
    Type            string `json:"type" gorm:"default:null"`
    CountryOfOrigin string `json:"countryOfOrigin" gorm:"default:null"`
    gorm.Model
}

然而,在获取数据时,这两个关联并没有被填充:

{
    "data": {
        "barcode": "1231231231231292",
        "name": "ABCD 12342",
        "artist_id": 2,
        "Artist": {
            "name": "",
            "type": "",
            "countryOfOrigin": "",
            "ID": 0,
            "CreatedAt": "0001-01-01T00:00:00Z",
            "UpdatedAt": "0001-01-01T00:00:00Z",
            "DeletedAt": null
        },
        "category_id": 9,
        "Category": {
            "name": "",
            "description": "",
            "parent": 0,
            "active": false,
            "ID": 0,
            "CreatedAt": "0001-01-01T00:00:00Z",
            "UpdatedAt": "0001-01-01T00:00:00Z",
            "DeletedAt": null
        },
        "num_of_records": 2,
        "original_release_date": "1965-02-24",
        "reissue_release_date": null,
        "side_color": null,
        "barcode_in_record": null,
        "ID": 1,
        "CreatedAt": "2022-04-25T12:53:32.275578-04:00",
        "UpdatedAt": "2022-04-25T12:53:32.275578-04:00",
        "DeletedAt": null
    }
}

知道那里发生了什么吗?

推荐答案

将数据保存到数据库的代码是什么样子的?

我猜你可能需要用FullSaveAssociations


DB().Session(&gorm.Session{FullSaveAssociations: true}).Save(&d)

要恢复数据,需要使用预加载.


DB().Preload("Artist").Preload("Category").First(&d)

你也可以使用

.Preload(clause.Associations)

把它们都装进go .您可以将其与其他预加载一起使用,以进行更深入的操作.

https://gorm.io/docs/preload.html#content-inner

Mysql相关问答推荐

正在获取MySQL死锁,因为这个查询需要很长时间.

如何在产品查询中插入GROUP_CONCAT?

MySQL 8.0.34-从后端系统管理AWS RDS上的持久连接内存使用

对多个数据库运行UPDATE mySQL查询

使用NOT EXISTS()时出现MySQL查询错误

使用Check查看过go 两个月是否完成了IRM Meetup

无法确定查询逻辑

在 .NET 6 中使用 Dapper Framework 未能成功连接到 MySql

SQL:如何 Select 每个客户最后购买的产品?

如何对 varchar() 数据值使用聚合窗口函数?

在一个 SQL 查询中对同一列中相同类型的不同值求和

MySQL - 考虑到所有前几天,每天计算唯一用户

Golang Gorm没有从关联表中检索数据

添加一个日期字段大于另一个日期字段的要求

终止空闲的mysql连接

是否可以在内部连接期间重命名连接列?

MySQL ORDER BY rand(),名称为 ASC

在 PHP/MySQL 中将日期时间存储为 UTC

MySQL与空值比较

如何允许 django 管理员将字段设置为 NULL?