无法在 gobuffalo 中验证和创建模型

Cannot ValidateAndCreate model in gobuffalo

我在 gobuffalo 中使用 pop.Connection#ValidateAndCreate 时遇到问题。

    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 是类型 *github.com/gobuffalo/pop.Connection

我收到错误:reflect: call of reflect.Value.Elem on struct Valueverrs, err := tx.ValidateAndCreate(item)

ValidateAndCreate 需要项目作为指针,因为它需要更新 ID 属性 以防它自动生成。 Pop 也管理 CreatedAtUpdatedAt 属性,因此它也必须更改这些属性。

根据 mkopriva 的建议,您可以将 ValidateAndCreate 调用更改为:

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