How to implement preloading in gorm

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

How to implement preloading in gorm

问题

我正在使用gorm

我遇到了以下问题,希望有人能帮助我解决或更详细地解释一下:

type User struct {
    ID        int
    Name      string
    Addresses []Address
}

type Address struct {
    UserID   int `gorm:"index;not null"`
    Address  string
    City     string
    Zipcode  int
    Country  string
}

if err := db.Select("name").Preload("Addresses").Find(&users).Error; err != nil {
    // 显示错误消息
} else {
    //
}

结果:

[
    {
        "created_at": "2017-04-02T00:07:59Z",
        "updated_at": "2017-04-02T00:07:59Z",
        "name": "Richard"
    }
]

SQL 日志:

SELECT * FROM `users` WHERE `users`.deleted_at IS NULL AND ((1 <> 1))

期望结果:

[
    {
        "created_at": "2017-04-02T00:07:59Z",
        "updated_at": "2017-04-02T00:07:59Z",
        "name": "Richard",
        "addresses": [
            {
                "address": "No 1, Street 5",
                "city": "New York",
                "country": "US"
            }
        ]
    }
]

我已经在StackOverflow上研究了一些其他帖子,但没有看到任何解决方案。

英文:

I'm using gorm.

I have an issue bellow, hope somebody can help me resolve or explain more about that

type User struct {
    ID        int
    Name      string
    Addresses []Address
}

type Address struct {
    UserID   int `gorm:&quot;index;not null&quot;`
    Address string
    City     string
    Zipcode  int
    Country  string
}

if err := db.Select(&quot;name&quot;).Preload(&quot;Addresses&quot;).Find(&amp;users).Error; err != nil {
    // Display error message
} else {
    //
}

Result:

[
    {
      &quot;created_at&quot;: &quot;2017-04-02T00:07:59Z&quot;,
      &quot;updated_at&quot;: &quot;2017-04-02T00:07:59Z&quot;,
      &quot;name&quot;: &quot;Richard&quot;
    }
  ]

SQL log:

SELECT * FROM `users` WHERE `users`.deleted_at IS NULL AND ((1 &lt;&gt; 1))

Expect result:

[
  {
    &quot;created_at&quot;: &quot;2017-04-02T00:07:59Z&quot;,
    &quot;updated_at&quot;: &quot;2017-04-02T00:07:59Z&quot;,
    &quot;name&quot;: &quot;Richard&quot;,
    &quot;addresses&quot;: [
      {
        &quot;address&quot;: &quot;No 1, Street 5&quot;,
        &quot;city&quot;: &quot;New York&quot;,
        &quot;country&quot;: &quot;US&quot;
      }
    ]
  }
]

I have researched some other post on StackOverflow but don't see any solutions

答案1

得分: 4

选择缺失的id字段,以便不应用预加载。下面的代码将解决这个问题。

if err := db.Select("id, name").Preload("Addresses").Find(&users).Error; err != nil {
    // 显示错误消息
} else {
    //
}
英文:

Select missing id fields so preloading was not applied. Bellow code will be resolve issue

if err := db.Select(&quot;id, name&quot;).Preload(&quot;Addresses&quot;).Find(&amp;users).Error; err != nil {
    // Display error message
} else {
    //
}

huangapple
  • 本文由 发表于 2017年4月4日 01:43:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/43191051.html
匿名

发表评论

匿名网友

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

确定