登录
首页 >  Golang >  Go问答

当 GORM 在未嵌入 "gorm.Model" 时会导致恐慌

来源:stackoverflow

时间:2024-02-09 20:30:24 108浏览 收藏

对于一个Golang开发者来说,牢固扎实的基础是十分重要的,golang学习网就来带大家一点点的掌握基础知识点。今天本篇文章带大家了解《当 GORM 在未嵌入 "gorm.Model" 时会导致恐慌》,主要介绍了,希望对大家的知识积累有所帮助,快点收藏起来吧,否则需要时就找不到了!

问题内容

这是我的模型。请注意,我不想将 gorm.model 嵌入到我的结构中。

type order struct {
    orderid   uint64     `gorm:"column:order_id" gorm:"primarykey" gorm:"unique"`
    cart      cart       `gorm:"foreignkey:userid"`
    createdby userid     `gorm:"index"`
    createdat *time.time `gorm:"autocreatetime:nano"`
    updatedat *time.time `gorm:"autoupdatetime:nano"`
    deletedat *time.time
}

type category struct {
    id        uint64     `gorm:"column:category_id" gorm:"primarykey" gorm:"unique"`
    name      string     `gorm:"index"`
    image     string     
    products  []product  `gorm:"foreignkey:product_id" gorm:"constraint:onupdate:cascade,ondelete:cascade;"`
    createdby userid     `gorm:"index" gorm:"type:bytea"`
    createdat *time.time `gorm:"autocreatetime:nano"`
    updatedat *time.time `gorm:"autoupdatetime:nano"`
    deletedat *time.time 
}

type cart struct {
    userid    userid     `gorm:"column:user_id" gorm:"primarykey"`
    products  []product  `gorm:"foreignkey:id"`
    price     float64   
    createdat *time.time `gorm:"autocreatetime:nano"`
    updatedat *time.time `gorm:"autoupdatetime:nano"`
    deletedat *time.time 
}

type product struct {
    id        uint64     `gorm:"column:product_id" gorm:"primarykey" gorm:"unique"`
    name      string     `gorm:"index"`
    price     float64    `gorm:"index"`
    rating    uint       `gorm:"index"`
    image     string     
    createdby userid     `gorm:"index" gorm:"type:bytea"`
    createdat *time.time `gorm:"autocreatetime:nano"`
    updatedat *time.time `gorm:"autoupdatetime:nano"`
    deletedat *time.time 
}

当我使用 automigrate 时,出现以下错误:

[error] invalid field found for struct moonspace/model.cart's field products: define a valid foreign key for relations or implement the valuer/scanner interface

我尝试将 foreignkey:userid 更改为 foreignkey:user_id 但错误仍然相同。

这是我的自动迁移:

func createPostgresCLI(cfg types.Config, config ...any) *gorm.DB {
    db, err := gorm.Open(postgres.Open(cfg.Url))
    if err != nil {
        panic(err)
    }

    err = db.AutoMigrate(&model.Order{}, &model.Cart{}, &model.Category{}, &model.Product{})
    if err != nil {
        panic(fmt.Errorf("Error creating database: %w", err))
    }

    return db
}

我做错了什么? 另外,当我使用 gorm.model 时,并且当我尝试将 product 插入 category 时,我收到外键约束错误。我正在尝试通过关联添加它。


正确答案


类别 <==> 产品:外键列需要包含在将引用类别 id 的产品中。当前配置工作时会用类别id更新产品id,这是错误的

type category struct {
    id       uint64 `gorm:"column:category_id;primarykey"`
    products  []product  `gorm:"foreignkey:categoryid" gorm:"constraint:onupdate:cascade,ondelete:cascade;"`
    ....
}

type product struct {
    id        uint64     `gorm:"column:product_id" gorm:"primarykey" gorm:"unique"`
    categoryid uint64 // new field to hold category id.
    ....
}

购物车 <===> 产品关系可以建模为 many2many,并使用连接表来存储产品和购物车的 id,此处可能不需要外键。

type Cart struct {
    CartID   uint64 `gorm:"primaryKey"`
    Products []Product `gorm:"many2many:cart_products"`
    ....
}

另外您可以将所有 gorm 标签合并为一个,无需重复。

以上就是《当 GORM 在未嵌入 "gorm.Model" 时会导致恐慌》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>