duangangmo0583
duangangmo0583
2018-11-03 14:55

创建新结构并将其以一对多关系添加到数组

已采纳

I'm trying to append a new item to an array in a one-to-many relationship. The problem is that one of the IDs is always undefined and the model I want to append to does not get updated.

I have the following models:

type Station struct {
    gorm.Model
    Name         string
    Measurements []Measurement
    PlantID      uint64
    Plant        Plant
}

type Measurement struct {
    ID             uint64 `gorm:"primary_key"`
    StationID      uint64
    TempSensor     float32
    LightSensor    float32
    HumiditySensor float32
    CreatedAt      time.Time
}

type Plant struct {
    gorm.Model
    Name string
}

This is the route to which I'm sending the post request:

/stations/:id/measurements

This is the current route handler that I have:

func CreateMeasurement(c *gin.Context) {
    id := c.Params.ByName("id")
    var station Station
    if err := db.Where("id = ?", id).First(&station).Error; err != nil {
        c.AbortWithStatus(404)
        fmt.Println(err)
    } else {
        var measurement Measurement
        c.BindJSON(&measurement)

        // Convert params string to uint
        convertedID, err := strconv.ParseUint(id, 10, 64)
        if err != nil {
            fmt.Println(err)
        }
        measurement.StationID = convertedID

        db.Model(&station).Association("Measurements").Append(&measurement)
        db.Save(&station)
        c.JSON(200, station)
    }
}

Question: How can I create a new Measurement item and append it to the []Measurement array in a specific Station which is specified by the route parameters?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • duanlaiquan8174 duanlaiquan8174 3年前

    Solved the problem. It turns out there was some problem with the database table. Although I got auto migrate enable, there was a problem with some ids that were null.

    Here is the working route:

    func CreateMeasurement(c *gin.Context) {
        id := c.Params.ByName("id")
        var station Station
        if err := db.Where("id = ?", id).First(&station).Error; err != nil {
            c.AbortWithStatus(404)
            fmt.Println(err)
        } else {
            var measurement Measurement
            c.BindJSON(&measurement)
    
            db.Model(&station).Association("Measurements").Append(&measurement)
            c.JSON(200, station)
        }
    }
    
    点赞 评论 复制链接分享

为你推荐