doulutian4843 2018-05-11 03:39
浏览 9
已采纳

无法在Gobuffalo中验证并创建模型

I am having trouble using pop.Connection#ValidateAndCreate in gobuffalo.

    purchaseOrder.Items = models.OrderItems{}

    ... fill purchaseOrder.Items ...

    for _, item := range purchaseOrder.Items {

        verrs, err := tx.ValidateAndCreate(item)
        if err != nil {
            return errors.WithStack(err)
        }

        if verrs != nil {
            // show error
        }
    }

tx is type *github.com/gobuffalo/pop.Connection

I get the error: reflect: call of reflect.Value.Elem on struct Value on the line verrs, err := tx.ValidateAndCreate(item)

  • 写回答

1条回答 默认 最新

  • doutongya8378 2018-05-11 09:24
    关注

    ValidateAndCreate requires the item as a pointer, since it needs to update the ID property in case it's auto-generated. Pop manages the CreatedAt and UpdatedAt attributes too, so it have to change those too.

    As proposed by mkopriva, you can change the ValidateAndCreate call to:

    verrs, err := tx.ValidateAndCreate(&item)
    if err != nil {
        return errors.WithStack(err)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答