选择所有依赖项。

huangapple go评论68阅读模式
英文:

Select all dependencies

问题

请注意,我将为您提供的代码进行翻译,但是我无法运行代码或提供实际的解决方案。以下是代码的翻译:

假设有以下模型:

type (
    Account struct {
        gorm.Model

        CustomID string `gorm:"index;unique"`
        Name     string
        Profiles []*Profile `gorm:"ForeignKey:AccountID"`
    }

    Profile struct {
        gorm.Model

        AccountID uint   `gorm:"index"`
        TheFoo    *Foo
        TheDoo    *Doo
    }

    Foo struct {
        ProfileID uint   `gorm:"index"`
        Boo       string
    }

    Doo struct {
        ProfileID uint   `gorm:"index"`
        Moo       string
    }
)

我尝试获取整个结构的所有尝试都失败了。acc 只填充了账户数据,没有填充任何配置文件。

我甚至尝试了 db.Model(&acc).Related(&Profile{}),但仍然没有成功。文档(假设是相当糟糕的文档)也没有解释清楚这一点。

var acc Account
db.Find(&acc, "custom_id = ?", myCustomID)

您应该如何实际完成这个任务呢?

英文:

Imagine the following model:

type (
    
    Account struct {
        gorm.Model

        CustomID string `gorm:"index;unique"`
        Name string
        Profiles []*Profiles `gorm:"ForeignKey:AccountID"`
    }

    Profile struct {
        gorm.Model

        AccountID uint `gorm:"index"`
        TheFoo *Foo
        TheDoo *Doo
    }

    Foo struct {
        ProfileID uint `gorm:"index"`
        Boo string
    }

    Doo struct {
        ProfileID uint `gorm:"index"`
        Moo string
    }
)

All my attempts of getting the entire structure always fails. acc is always filled with only the account data and no profiles.

I even expereimented with this db.Model(&acc).Related(&Profile{}) stuff but still no success. The (lets say, pretty bad) docs also do not clarify this.

var acc Account
db.Find(&acc, "custom_id = ?", myCustomID) 

how would you actually do this?

答案1

得分: 1

我相信你可以使用Preload方法来支持ne,例如:

account := new(Account)
db.Preload("Profiles.TheDoo").Preload("Profiles.TheFoo").Find(account, "custom_id = ?", myCustomID)

我还没有检查过是否实际上你也需要预加载Profiles,但如果最后确实需要的话,使用它也不会有坏处。

英文:

I believe you can use the Preload method which supports ne, i.e.:

account := new(Account)
db.Preload("Profiles.TheDoo").Preload("Profiles.TheFoo").Find(account, "custom_id = ?", myCustomID)

I haven't checked yet if you actually need to preload Profiles as well, but it wouldn't hurt to use it if it turns out you do.

答案2

得分: 0

你可以在调用相关函数时添加你的代码吗?http://jinzhu.me/gorm/associations.html#has-many 根据我在has-many文档中看到的,代码应该是这样的。

var acc Account
db.Find(&acc, "custom_id = ?", myCustomID)
db.Model(&acc).Related(&profiles)

英文:

Can you add your code for when you call the related function? http://jinzhu.me/gorm/associations.html#has-many What Im seeing on the documentation for has-many it should look like this.

var acc Account
db.Find(&acc, "custom_id = ?", myCustomID)
db.Model(&acc).Related(&profiles)

huangapple
  • 本文由 发表于 2017年7月16日 03:36:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/45122077.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定