Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql Gorm不需要`属于`_Mysql_Go_Gorm - Fatal编程技术网

Mysql Gorm不需要`属于`

Mysql Gorm不需要`属于`,mysql,go,gorm,Mysql,Go,Gorm,我有这个模型: type Order struct { ID uint64 `gorm:"primarykey" json:"id"` UserPaymentMethodID uint64 `json:"payment_method_id"` UserPaymentMethod *UserPaymentMethod

我有这个模型:

type Order struct {
    ID                    uint64             `gorm:"primarykey" json:"id"`
    UserPaymentMethodID   uint64             `json:"payment_method_id"`
    UserPaymentMethod     *UserPaymentMethod
}

对于UserPaymentMethod列,是否可能没有必需的
属于
?我不必为这个专栏设置约束。

另外,我想对其他列使用约束。Gorm默认情况下使列为空,这意味着不需要“属于”关系,除非您将该字段标记为非空

但是,当使用零值的UserPaymentMethodID创建订单时,您会得到一个错误,因为UserPaymentMethod0当然不存在。您需要的只是通过将此字段设置为指针来允许其为空值:

类型订单结构{
ID uint64`gorm:“primarykey”json:“ID”`
UserPaymentMethodID*uint64`json:“付款方法id”`
UserPaymentMethod*UserPaymentMethod
}
err:=db.Create(&Order{})。错误//应该可以工作