我正在用 gorm 为 mysql 创建一个 Go 项目。使用gorm创建mysql表时,需要为一张表创建多个外键。到目前为止,它是像下面的代码一样创建的这样的桌子type Members struct { ID int32 `gorm:"AUTO_INCREMENT" json:"id" from:"id"` MyselfUserID int32 `gorm:"NOT NULL" json:"myself_user_id" from:"myself_user_id"` MemberUserID int32 `gorm:"NOT NULL" json:"member_user_id" from:"family_user_id"` FirstName string `json:"first_name" from:"first_name"` LastName string `json:"last_name" from:"last_name"` Relationship string `json:"relationship" from:"relationship"` Image string `json:"image" from:"image"` Version int32 `gorm:"column:version" json:"version" form:"version"` UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at" form:"updated_at"` CreatedAt time.Time `gorm:"column:created_at" json:"created_at" form:"created_at"` }我有一个像这样的外键MgtDB.AutoMigrate(&Members{}).AddForeignKey("myself_user_id", "users(user_id)", "RESTRICT", "RESTRICT").AddForeignKey("member_user_id", "users(user_id)", "RESTRICT", "RESTRICT")我觉得有点冗长请告诉我是否还有其他类似的方法谢谢
1 回答
泛舟湖上清波郎朗
TA贡献1818条经验 获得超3个赞
type Members struct {
...
MemberUserID int32
MemberUser User `gorm:"foreignkey:MemberUserID"`
}
- 1 回答
- 0 关注
- 153 浏览
添加回答
举报
0/150
提交
取消