【问题标题】:Can't set up has-many association in GORM无法在 GORM 中设置 has-many 关联
【发布时间】:2019-07-30 05:41:42
【问题描述】:

我正在尝试在Users 和PredictionsBags 之间建立关联。我的问题是,如果我使用 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,但我不确定要更密切关注哪个页面。

【问题讨论】:

    标签: go go-gorm


    【解决方案1】:

    我回家后必须检查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"`
    
    

    更新:

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

        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
        }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-17
      • 1970-01-01
      • 2015-02-17
      • 2014-04-15
      相关资源
      最近更新 更多