doz97171 2016-05-15 08:28
浏览 188
已采纳

在Go GORM中显示外键的外键

I was able to partially solve this with this case

Unfortunately, the Preload() function doesn't seem to work with delving further down in the related object set.

To clarify, I have the following models:

    type Room struct {
        gorm.Model
        Name    string
        Games   []Game `gorm:"ForeignKey:RoomID"`
    }

    type Game struct {
        gorm.Model
        RoomID   int `gorm:"index"`
        Players  []Player `gorm:"ForeignKey:GameID"`
    }

    type Player struct {
        gorm.Model
        Name   string
        GameID int `gorm:"index"`
    }

When I create a new object with a new Room, Game, and Player object created I get the following data returned (json encoded):

    {"Value":{"ID":26,"CreatedAt":"2016-05-15T01:21:22.426234189-07:00","UpdatedAt":"2016-05-15T01:21:22.426234189-07:00","DeletedAt":null,"Name":"foo","Games":[{"ID":17,"CreatedAt":"2016-05-15T01:21:22.427026134-07:00","UpdatedAt":"2016-05-15T01:21:22.427026134-07:00","DeletedAt":null,"RoomID":26,"Turns":null,"Players":[{"ID":4,"CreatedAt":"2016-05-15T01:21:22.427560561-07:00","UpdatedAt":"2016-05-15T01:21:22.427560561-07:00","DeletedAt":null,"Name":"TestPlayer","GameID":17}],"Counter":1,"Assigned":false}],"Testing":false},"Error":null,"RowsAffected":1}

But if I try to query the structure with a preload() function, I get the following:

    {"Value":{"ID":26,"CreatedAt":"2016-05-15T01:21:22.426234189-07:00","UpdatedAt":"2016-05-15T01:21:22.426234189-07:00","DeletedAt":null,"Name":"foo","Games":[{"ID":17,"CreatedAt":"2016-05-15T01:21:22.427026134-07:00","UpdatedAt":"2016-05-15T01:21:22.427026134-07:00","DeletedAt":null,"RoomID":26,"Turns":null,"Players":null,"Counter":1,"Assigned":false}],"Testing":false},"Error":null,"RowsAffected":1}

Note that the Players section is now null. Here's my room registration and room query functions:

    func RegisterRoom(w http.ResponseWriter, r *http.Request) {
        err := r.ParseForm()

        if err != nil {
            // Handle error
        }
        // r.PostForm is a map of our POST form values
        room := Room{
            Name: r.PostFormValue("label"),
            Games: []Game{{
                Counter: 1,
                Players: []Player{{Name: r.PostFormValue("username")}},
            }},
        }
        t := db.Create(&room)
        if err := json.NewEncoder(w).Encode(t); err != nil {
            panic(err)
        }
    }

func RoomShow(w http.ResponseWriter, r *http.Request) {
    var rm Room
    vars := mux.Vars(r)
    roomId := vars["roomId"]
    id, _ := strconv.Atoi(roomId)
    room := db.Preload("Games").First(&rm, id)
    result := db.Find(&room)
    json.NewEncoder(w).Encode(result)
}
  • 写回答

1条回答 默认 最新

  • dpd7195 2016-05-15 08:53
    关注

    Solved it! Answer was under Nested Preloading smacks forehead

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?