Golang Gorm不从关联表中检索数据

发布于 2025-01-24 10:39:15 字数 2888 浏览 0 评论 0原文

我正在使用MySQL使用GORM制作杜松子酒应用程序。为了在Gorm模型中定义A 属于关系,您必须执行以下操作(示例从Gorm Docs中获取):

// `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
    }
}

有什么想法发生了什么事?

I'm working on a Gin app using Gorm with MySQL. In order to define a belongs to relationship in a Gorm Model, you have to do the following (example taken from Gorm docs):

// `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
}

That's what I did with my models:

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
}

yet when getting data back, those two associations are not being populated:

{
    "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
    }
}

any idea what's going on there?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

燃情 2025-01-31 10:39:15

代码看起来像是将数据保存到数据库的样子?

我猜您可能需要使用FullSaveasSociations
类似的内容:


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

要收回数据,您需要使用预加载。


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

您也可以用来

.Preload(clause.Associations)

加载它们。您可以将其与其他预紧力一起使用。

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

What does the code look like that is saving the data to the db?

I'm guessing that you probably need to use FullSaveAssociations
something like this:


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

To get the data back, you need to use Preloads.


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

you can also use

.Preload(clause.Associations)

to load them all. You can use this with other Preloads to go deeper.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文