简体   繁体   English

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

[英]Cannot ValidateAndCreate model in gobuffalo

I am having trouble using pop.Connection#ValidateAndCreate 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 is type *github.com/gobuffalo/pop.Connection tx是* 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) 我收到错误消息: reflect: call of reflect.Value.Elem on struct Valuereflect: call of reflect.Value.Elem on struct Value verrs, err := tx.ValidateAndCreate(item)

ValidateAndCreate requires the item as a pointer, since it needs to update the ID property in case it's auto-generated. ValidateAndCreate要求该项目作为指针,因为它需要在自动生成的情况下更新ID属性。 Pop manages the CreatedAt and UpdatedAt attributes too, so it have to change those too. Pop也管理CreatedAtUpdatedAt属性,因此它也必须更改它们。

As proposed by mkopriva, you can change the ValidateAndCreate call to: 根据mkopriva的建议,您可以将ValidateAndCreate调用更改为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM