douhan8892 2017-10-24 00:13
浏览 191

转到GORM many2many问题

I can't work out the best way to add an association to a model. I have the following structs

type Beer struct {
        ID             uint       `json:"id"`
    Name           string     `json:"name" gorm:"not null;" sql:"unique"`
    Description    string     `json:"description" gorm:"not null;"`
    ImageURL       string     `json:"image_url"`
    AlcoholContent float64    `json:"alcohol_content, default:0"`
    Featured       bool       `json:"featured"`
    BrewStart      time.Time  `json:"brew_start"`
    BrewEnd        time.Time  `json:"brew_end"` 
    Brewers        []Brewer   `gorm:"many2many:beer_brewers" json:"brewers"`
}

type Brewer struct {
    ID        uint       `json:"id"`
    FirstName string     `json:"first_name"`
    LastName  string     `json:"last_name"`
    Title     string     `json:"title"`
    Featured  bool       `json:"featured"`
    Beers     []Beer    `gorm:"many2many:beer_brewers" json:"beers"`
}

Below is an example of data i have seeded the DB with

Beer{
    Name:           "some pale ale",
    Description:    "a description of some pale ale",
    ImageURL:       "http://via.placeholder.com/350x150",
    AlcoholContent: 4.5,
    Featured:       false,
    BrewStart:      utils.ParseTime("30-10-2017 13:00 (AEDT)"),
    BrewEnd:        utils.ParseTime("14-11-2017 13:00 (AEDT)"),
    Brewers: []Brewer{
        Brewer{FirstName: "john", LastName: "smith", Title: "bottle shaker", Featured: false},
        Brewer{FirstName: "joe", LastName: "bloggs", Title: "bottle maker", Featured: true},
    },
},
Beer{
    Name:           "some lager",
    Description:    "a description of some pale ale",
    ImageURL:       "http://via.placeholder.com/350x150",
    AlcoholContent: 4.5,
    Featured:       false,
    BrewStart:      utils.ParseTime("30-10-2017 13:00 (AEDT)"),
    BrewEnd:        utils.ParseTime("14-11-2017 13:00 (AEDT)"),
    Brewers: []Brewer{
        Brewer{FirstName: "john", LastName: "smith", Title: "bottle shaker", Featured: false},
        Brewer{FirstName: "joe", LastName: "bloggs", Title: "bottle maker", Featured: true},
    },
},

However the above creates duplicate Brewers in the Brewer table. My question is, what is the best way to reference a Brewer that already exists but not create another Brewer item in the Brewer table?..and also what is the best way to Append a new Brewer into a Beer collection?

Thanks, Justin

  • 写回答

4条回答 默认 最新

  • dongling2038 2017-10-24 03:30
    关注

    Try create first beer, then the brewers and usage the .append method to append the brewers in beer.

    http://jinzhu.me/gorm/associations.html#association-mode

    // Start Association Mode
    var user User
    db.Model(&user).Association("Languages")
    // `user` is the source, it need to be a valid record (contains primary key)
    // `Languages` is source's field name for a relationship.
    // If those conditions not matched, will return an error, check it with:
    // db.Model(&user).Association("Languages").Error
    
    
    // Query - Find out all related associations
    db.Model(&user).Association("Languages").Find(&languages)
    
    
    // Append - Append new associations for many2many, has_many, will replace current association for has_one, belongs_to
    db.Model(&user).Association("Languages").Append([]Language{languageZH, languageEN})
    db.Model(&user).Association("Languages").Append(Language{Name: "DE"})
    
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决