为了账号安全,请及时绑定邮箱和手机立即绑定

Golang:在表上插入或更新时出现 Gorm 错误违反外键约束

Golang:在表上插入或更新时出现 Gorm 错误违反外键约束

Go
慕尼黑5688855 2022-11-08 16:03:17
我尝试使用Gorm创建一个 REST 服务,该服务在启动时会从我的 Postgres DB 中删除数据库表,然后使用测试数据创建和填充它们。我使用的实体如下:type Group struct {    ID       uuid.UUID `gorm:"PrimaryKey" json:"id"`    Name     string     `json:"name"`    Sessions []Session `gorm:"foreignKey:ID" json:"sessions"`}type Session struct {    ID      uuid.UUID `gorm:"PrimaryKey"`    GroupID uuid.UUID `gorm:"foreignKey:ID"`    Name    string    From    time.Time    To      time.Time}type Player struct {    ID       uuid.UUID `gorm:"PrimaryKey" json:"id"`    Sessions []Session `gorm:"many2many:players_sessions" json:"sessions"`    Groups   []Group   `gorm:"many2many:players_groups" json:"groups"`    Username string    `gorm:"type:varchar;NOT NULL" json:"username"`    Cookie   string    `json:"cookie"`}在启动时,当表被删除时,它们会使用以下代码重新创建和填充:func PopulateDB(db *gorm.DB) {    db.AutoMigrate(&entities.Group{}, &entities.Player{}, &entities.Session{})    players := []entities.Player{        {ID: uuid.New(), Username: "Player 1", Cookie: ""},        {ID: uuid.New(), Username: "Player 2", Cookie: ""},        {ID: uuid.New(), Username: "Player 3", Cookie: ""},    }    for index := range players {        db.Create(&players[index])    }    group := entities.Group{ID: uuid.New(), Name: "My Group"}    db.Create(&group)    sessions := []entities.Session{        {            ID: uuid.New(),             GroupID: group.ID,            Name: "Session 1",             From: time.Now(),             To: time.Now().Add(12 * time.Hour),        },        {            ID:      uuid.New(),            GroupID: group.ID,            Name:    "Session 2",            From:    time.Now().Add(24 * time.Hour),            To:      time.Now().Add(36 * time.Hour),        },            }我遇到的问题是,当插入测试数据时,它返回以下错误:ERROR: insert or update on table "sessions" violates foreign key constraint "fk_groups_sessions" (SQLSTATE 23503). 出现这种情况就行了db.Model(&group).Association("Sessions").Append(&sessions[index])。我的印象是,这是由于尝试插入没有有效组外键的会话引起的,但我对 Gorm 的了解不足,无法理解我做错了什么。任何帮助将非常感激!
查看完整描述

2 回答

?
LEATH

TA贡献1936条经验 获得超7个赞

type Group struct {

    ID       uuid.UUID `gorm:"PrimaryKey" json:"id"`

    Name     string     `json:"name"`

    Sessions []Session `gorm:"foreignKey:GroupID" json:"sessions"

}


type Session struct {

    ID      uuid.UUID `gorm:"PrimaryKey"`

    GroupID uuid.UUID `gorm:"foreignKey:ID"`

    Name    string

    From    time.Time

    To      time.Time

}

这应该修复它,引用从 ID 更改为 GroupID


查看完整回答
反对 回复 2022-11-08
?
缥缈止盈

TA贡献2041条经验 获得超4个赞

如果这是您的数据库架构设计,它应该是这样的。


type Group struct {

    ID       uuid.UUID `gorm:"primaryKey"`

    Name     string    `gorm:"column:name"`

}


type Session struct {

    ID      uuid.UUID `gorm:"primaryKey"`

    GroupID uuid.UUID `gorm:"column:group_id"

    Group   Group     `gorm:"foreignKey:ID"`

    Name    string    `gorm:"column:name"`

    From    time.Time `gorm:"column:from"`

    To      time.Time `gorm:"column:to"`

}


type Player struct {

    ID        uuid.UUID `gorm:"primaryKey"`

    Username  string    `gorm:"column:username;type:varchar;NOT NULL"`

    Cookie    string    `gorm:"column:cookie"`

    SessionID uuid.UUID `gorm:"column:session_id"`

    GroupID   uuid.UUID `gorm:"column:group_id"`

    Sessions  []Session `gorm:"many2many:players_sessions;foreignKey:SessionID;references:ID;"`

    Groups    []Group   `gorm:"many2many:players_groups;foreignKey:GroupID;references:ID;"`

}


查看完整回答
反对 回复 2022-11-08
  • 2 回答
  • 0 关注
  • 447 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号