无法在 GORM 中设置 has-many 关联

Can't set up has-many association in GORM

我正在尝试在 UserPredictionsBag 之间建立关联。我的问题是,如果我使用 GORM 的假定名称来引用对象,一切正常,但我想稍微更改一下名称。

type User struct {
    gorm.Model
    //  We’ll try not using usernames for now

    Email        string `gorm:"not null;unique_index"`
    Password     string `gorm:"-"`
    PasswordHash string `gorm:"not null"`
    Remember     string `gorm:"-"` // A user’s remember token.
    RememberHash string `gorm:"not null;unique_index"`

    Bags []PredictionsBag
}

当然,每个用户都拥有零个或多个 PredictionsBags:

type PredictionsBag struct {
    gorm.Model

    UserID uint // I want this to be "OwnerID"

    Title        string
    NotesPublic  string `gorm:"not null"` // Markdown field. May be published.
    NotesPrivate string `gorm:"not null"` // Markdown field. Only for (private) viewing and export.

    Predictions []Prediction
}

我想让 .Related() 以通常的方式工作:

func (ug *userGorm) ByEmail(email string) (*User, error) {
    var ret User

    matchingEmail := ug.db.Where("email = ?", email)

    err := first(matchingEmail, &ret)
    if err != nil {
        return nil, err
    }

    var bags []PredictionsBag
    if err := ug.db.Model(&ret).Related(&bags).Error; err != nil {
        return nil, err
    }
    ret.Bags = bags

    return &ret, nil
}

我的问题是我无法找到一种方法将 PredictionsBag.UserID 更改为其他任何内容,并且仍然让 GORM 弄清楚所涉及的关系。我一直在阅读 http://gorm.io/docs/has_many.html#Foreign-Key,如果我将相关行更改为

type User struct {
    // …
    Bags []PredictionsBag `gorm:"foreignkey:OwnerID"`
}

type PredictionsBag struct {
    // …
    OwnerID uint
    // …
}

我收到这个错误:

[2019-07-28 14:23:49]  invalid association [] 

我做错了什么?我也一直在阅读 http://gorm.io/docs/belongs_to.html,但我不确定应该更仔细地阅读哪一页。

我回家后必须检查 Related(),但我认为您正在寻找的是 Preload() 这是我的示例,可以满足您的需求。

package main

import (
    "errors"
    "fmt"
    _ "github.com/go-sql-driver/mysql"
    "github.com/jinzhu/gorm"
    _ "github.com/jinzhu/gorm/dialects/mysql"
    "log"
)

var DB *gorm.DB

func init() {
    var err error

    DB, err = gorm.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s:3306)/%s?&parseTime=True&loc=Local", "root", "root", "localhost", "testing"))
    if err != nil {
        log.Fatal(err)
    }

    DB.DropTableIfExists(&User{}, &PredictionsBag{})
    DB.AutoMigrate(&User{}, &PredictionsBag{})
    user := User{Email:"dave@example.com"}
    user.Bags = append(user.Bags, PredictionsBag{OwnerID: user.ID, NotesPrivate: "1", NotesPublic: "1"})
    DB.Create(&user)
}

func main()  {

    user := User{Email:"dave@example.com"}
    err := user.ByEmail()
    if err != nil {
        log.Println(err)
    }
    fmt.Println(user.ID, user.Email, "Bags:", len(user.Bags))

    DB.Close()
}


type User struct {
    gorm.Model
    //  We’ll try not using usernames for now

    Email        string `gorm:"not null;unique_index"`
    Password     string `gorm:"-"`
    PasswordHash string `gorm:"not null"`
    Remember     string `gorm:"-"` // A user’s remember token.
    RememberHash string `gorm:"not null;unique_index"`

    Bags []PredictionsBag `gorm:"foreignkey:OwnerID"`
}

type PredictionsBag struct {
    gorm.Model
    OwnerID uint

    Title        string
    NotesPublic  string `gorm:"not null"` // Markdown field. May be published.
    NotesPrivate string `gorm:"not null"` // Markdown field. Only for (private) viewing and export.

}

func (ug *User) ByEmail() error {

    DB.Where("email = ?", ug.Email).Preload("Bags").Limit(1).Find(&ug)
    if ug.ID == 0 {
        return errors.New("no user found")
    }
    return nil
}

使用这个可能与相关的一起使用,但我不确定还需要更改什么:

    Bags []PredictionsBag `gorm:"foreignkey:OwnerID;association_foreignkey:ID"`

更新:

我可以让 Related() 方法工作,如果你像下面这样声明 ForeignKey:

    DB.Where("email = ?", ug.Email).Limit(1).Find(&ug)
    if ug.ID == 0 {
        return errors.New("no user found")
    }
    if err := DB.Model(&ug).Related(&ug.Bags, "owner_id").Error; err != nil {
        return err
    }