首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Golang:插入或更新表上的Gorm错误违反了外键禁忌。

Golang:插入或更新表上的Gorm错误违反了外键禁忌。
EN

Stack Overflow用户
提问于 2022-02-15 15:33:49
回答 2查看 2.4K关注 0票数 1

我试图使用戈姆创建一个REST服务,它在启动时将从Postgres中删除数据库表,然后创建并填充测试数据。我所使用的实体如下:

代码语言:javascript
复制
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"`
}

在启动时,当删除表时,将使用以下代码重新创建和填充表:

代码语言:javascript
复制
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),
        },
        
    }
    for index := range sessions {
        db.Model(&group).Association("Sessions").Append(&sessions[index])
        // db.Create(&sessions[index])
    }

    // Make player >-< groups connections
    for index := range players {
        db.Model(&players[index]).Association("Groups").Append(&group)
    }

    // Make player >-< session connections
    for index := range players {
        db.Model(&players[index]).Association("Sessions").Append(&sessions[0])
        if index%2 == 0 {
            db.Model(&players[index]).Association("Sessions").Append(&sessions[1])
        }
    }
}

我遇到的问题是,当插入测试数据时,它返回以下错误: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的了解还不足以理解我做错了什么。任何帮助都将不胜感激!

EN

Stack Overflow用户

发布于 2022-05-02 10:16:59

如果这是您的数据库模式设计,则应该是这样的。

代码语言:javascript
复制
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;"`
}
票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71129151

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档