go - How to save and retrieve Gorm's many2many relationship using Go
问题描述
I want to create a rest API to manage casts using Gorm and Gin. When I add a relation between two of my models and submitting those items to the API, I cannot make that expected relationship between those. and the BornLocations
and Nationalities
fields are null. Have I missed something here?
My question is: How can I store and retrieve that relationship in my responses?
My Models:
type Cast struct {
ID string `sql:"type:uuid;primary_key;default:uuid_generate_v4()"`
FullName string `gorm:"size:150;not null" json:"full_name"`
NickNames string `gorm:"size:250;null;" json:"nick_names"`
BornLocation Country `gorm:"many2many:CastBornLocation;association_foreignkey:ID;foreignkey:ID" json:"born_location"`
Nationalities []Country `gorm:"many2many:Castnationalities;association_foreignkey:ID;foreignkey:ID" json:"cast_nationalities"`
MiniBio string `gorm:"size:1000;null;" json:"mini_bio"`
UserRefer string
}
type Country struct {
ID string `sql:"type:uuid;primary_key;default:uuid_generate_v4()"`
Title string `gorm:"size:100;not null" json:"title"`
CreatedAt time.Time `gorm:"default:CURRENT_TIMESTAMP" json:"created_at"`
UpdatedAt time.Time `gorm:"default:CURRENT_TIMESTAMP" json:"updated_at"`
}
And here is the controller:
func (server *Server) CreateCast(c *gin.Context) {
errList = map[string]string{}
item := models.Cast{}
err = json.Unmarshal(body, &item)
if err != nil {
errList["Unmarshal_error"] = "Cannot unmarshal body"
c.JSON(http.StatusUnprocessableEntity, gin.H{
"status": http.StatusUnprocessableEntity,
"error": errList,
})
return
}
// check the token
uid, err := auth.ExtractTokenID(c.Request)
if err != nil {
errList["Unauthorized"] = "Unauthorized"
c.JSON(http.StatusUnauthorized, gin.H{
"status": http.StatusUnauthorized,
"error": errList,
})
return
}
user := models.User{}
err = server.DB.Debug().Model(models.User{}).Where("id = ?", uid).Take(&user).Error
if err != nil {
errList["Unauthorized"] = "Unauthorized"
c.JSON(http.StatusUnauthorized, gin.H{
"status": http.StatusUnauthorized,
"error": errList,
})
return
}
item.UserRefer = uid
item.Prepare()
errorMessages := item.Validate("")
if len(errorMessages) > 0 {
errList = errorMessages
c.JSON(http.StatusUnprocessableEntity, gin.H{
"status": http.StatusUnprocessableEntity,
"error": errList,
})
return
}
itemCreated, err := item.SaveCast(server.DB) //Saving the cast!
if err != nil {
formattedError := formaterror.FormatError(err.Error())
errList = formattedError
c.JSON(http.StatusInternalServerError, gin.H{
"status": http.StatusInternalServerError,
"error": errList,
})
return
}
c.JSON(http.StatusCreated, gin.H{
"status": http.StatusCreated,
"response": itemCreated,
})
}
And this is the JSON body I'm submitting to the API:
{
"full_name": "Cast full_name",
"nick_names": "nickname1, nickname2",
"born_location": {
"ID" :"caf2d6af-c360-4777-85d4-32541094b8be"
},
"cast_nationalities": [
{
"ID" :"caf2d6af-c360-4777-85d4-32541094b8be"
},
{
"ID" :"064058f4-f4ea-4d28-ad22-f9cf92df3513"
}
],
"mini_bio": "this is the mini bio of the cast"
}
And now, this is the response:
{
"response": {
"ID": "09f6a184-9b6e-4487-85f0-c2dbbce7ee5b",
"full_name": "Cast full_name",
"nick_names": "nickname1, nickname2",
"born": "",
"born_location": {
"ID": "e5c431ed-3158-4532-81b6-52d78e5539dc",
"title": "",
"created_at": "2020-07-16T13:23:32.579892866Z",
"updated_at": "2020-07-16T13:23:32.578216004Z",
"UserRefer": ""
},
"cast_nationalities": [
{
"ID": "caf2d6af-c360-4777-85d4-32541094b8be",
"title": "",
"created_at": "0001-01-01T00:00:00Z",
"updated_at": "2020-07-16T13:23:32.580620065Z",
"UserRefer": ""
},
{
"ID": "064058f4-f4ea-4d28-ad22-f9cf92df3513",
"title": "",
"created_at": "0001-01-01T00:00:00Z",
"updated_at": "2020-07-16T13:23:32.58249818Z",
"UserRefer": ""
}
],
"mini_bio": "this is the mini bio of the cast",
"bio": "",
"avatar": "",
"height": "",
"weight": "",
"created_at": "2020-07-16T13:23:32.576892187Z",
"updated_at": "2020-07-16T13:23:32.576892346Z",
"UserRefer": "c0223913-8679-4103-bcb6-66db0b2fed21"
},
"status": 201
}
And this is the controller and response for getting all the casts:
func (server *Server) GetCasts(c *gin.Context) {
//clear previous error if any
errList = map[string]string{}
item := models.Cast{}
// check the token
uid, err := auth.ExtractTokenID(c.Request)
if err != nil {
errList["Unauthorized"] = "Unauthorized"
c.JSON(http.StatusUnauthorized, gin.H{
"status": http.StatusUnauthorized,
"error": errList,
})
return
}
items, err := item.FindCasts(server.DB, uid)
if err != nil {
errList["No_Items"] = "No Items Found"
c.JSON(http.StatusInternalServerError, gin.H{
"status": http.StatusInternalServerError,
"error": errList,
})
return
}
c.JSON(http.StatusOK, gin.H{
"status": http.StatusOK,
"response": items,
})
}
func (u *Cast) FindCasts(db *gorm.DB, uid string) (*[]Cast, error) {
var err error
casts := []Cast{}
err = db.Debug().Model(&Cast{}).Where("user_refer = ?", uid).Limit(100).Find(&casts).Error
if err != nil {
return &[]Cast{}, err
}
return &casts, err
}
And the final response for the casts:
{
"response": [
{
"ID": "09f6a184-9b6e-4487-85f0-c2dbbce7ee5b",
"full_name": "Cast full_name",
"nick_names": "nickname1, nickname2",
"born": "",
"born_location": {
"ID": "",
"title": "",
"created_at": "0001-01-01T00:00:00Z",
"updated_at": "0001-01-01T00:00:00Z",
"UserRefer": ""
},
"nationalities": null,
"mini_bio": "this is the mini bio of the cast",
"bio": "",
"avatar": "",
"height": "",
"weight": "",
"created_at": "2020-07-16T13:23:32.576892Z",
"updated_at": "2020-07-16T13:23:32.576892Z",
"UserRefer": "c0223913-8679-4103-bcb6-66db0b2fed21"
}
],
"status": 200
解决方案
For fetching data you can use preload child data in gorm
err = db.Preload("BornLocation").Preload("Nationalities").Model(&Cast{})
.Where("user_refer = ?", uid).Limit(100).Find(&casts).Error
Or you can use flag
db.Set("gorm:auto_preload", true).Find(&casts).Error
Here you BornLocation
seems like OneToMany you can do like this
type Cast struct {
...
BornLocationId string
BornLocation Country `gorm:"association_foreignkey:ID;foreignkey:BornLocationId" json:"born_location"`
...
}
推荐阅读
- android - 从另一个活动 onBackpressed 更新 RecyclerView
- angular - 垫输入的动态负载颜色
- android - Android Espresso:检查 TextView 是否显示来自资源的格式化字符串
- github - gp-pages 无法更新现有分支
- json - 用 groovy 测量 json 响应的大小
- c# - Java Access Bridge c#无法激活字段、设置文本、单击等
- javascript - 按下第二个按钮时第一次停止 setInterval
- javascript - 如何在具有 Angular 8 的 FullCalendar 中使用 gotoDate()
- javascript - JS中没有任何其他字符的十进制验证
- java - 找不到表