[英]Gorm insert into many to many not adding to join table
I have two tables: Meeting and Participant .我有两个表: Meeting和Participant 。 A meeting can have multiple participants while a participant can attend multiple meetings.
一个会议可以有多个参与者,而一个参与者可以参加多个会议。 This many2many relationship is structured in gorm as follow:
这种many2many关系在gorm中的结构如下:
type Meeting struct {
Id uint `json:"id" gorm:"primary_key"`
MeetingName string `json:"meetingName" binding:"required"`
Participants []Participant `json:"participants" gorm:"many2many:meeting_participants" binding:"required"`
}
type Participant struct {
Id uint `json:"id" gorm:"primary_key"`
Name string `json:"name" validate:"required"`
Meetings []Meeting `json:"meetings" gorm:"many2many:meeting_participants"`
}
The issue is that when I add a new participant, gorm is not adding to the join table (meeting_participants).问题是当我添加一个新参与者时,gorm 没有添加到连接表 (meeting_participants)。 A new participant is added but the join table has no new record.
添加了一个新参与者,但连接表没有新记录。 The script to add participant is as follow:
添加参与者的脚本如下:
func InsertParticipant(c *gin.Context) (string, error) {
input, err := checkParticipantInput(c)
if err != nil {
return "", err
}
newParticipant := DB.Create(&input)
DB.Save(&input)
return input.Id, newParticipant.Error
}
func checkParticipantInput(c *gin.Context) (Participant, error) {
var input Participant
err := c.ShouldBindJSON(&input)
if err != nil {
return Participant{}, err
}
return input, err
}
Json input: Json 输入:
{
"name":"testParticipant",
"meetings": [{"id": 40}]
}
Any idea what went wrong?知道出了什么问题吗? Thanks in advance.
提前致谢。
I have two tables: Meeting and Participant .我有两个表: Meeting和Participant 。 A meeting can have multiple participants while a participant can attend multiple meetings.
一个会议可以有多个参与者,而一个参与者可以参加多个会议。 This many2many relationship is structured in gorm as follow:
这种many2many关系在gorm中的结构如下:
type Meeting struct {
Id uint `json:"id" gorm:"primary_key"`
MeetingName string `json:"meetingName" binding:"required"`
Participants []Participant `json:"participants" gorm:"many2many:meeting_participants" binding:"required"`
}
type Participant struct {
Id uint `json:"id" gorm:"primary_key"`
Name string `json:"name" validate:"required"`
Meetings []Meeting `json:"meetings" gorm:"many2many:meeting_participants"`
}
The issue is that when I add a new participant, gorm is not adding to the join table (meeting_participants).问题是当我添加一个新参与者时,gorm 没有添加到连接表 (meeting_participants)。 A new participant is added but the join table has no new record.
添加了一个新参与者,但连接表没有新记录。 The script to add participant is as follow:
添加参与者的脚本如下:
func InsertParticipant(c *gin.Context) (string, error) {
input, err := checkParticipantInput(c)
if err != nil {
return "", err
}
newParticipant := DB.Create(&input)
DB.Save(&input)
return input.Id, newParticipant.Error
}
func checkParticipantInput(c *gin.Context) (Participant, error) {
var input Participant
err := c.ShouldBindJSON(&input)
if err != nil {
return Participant{}, err
}
return input, err
}
Any idea what went wrong?知道出了什么问题吗? Thanks in advance.
提前致谢。
Not so sure why but I restarted my server and it works.不太清楚为什么,但我重新启动了我的服务器并且它可以工作。 Perhaps something to do with automigrate not working when the server hot reloads.
当服务器热重新加载时,可能与自动迁移不起作用有关。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.