首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当`gorm.Model`未嵌入时GORM恐慌

当`gorm.Model`未嵌入时GORM恐慌
EN

Stack Overflow用户
提问于 2022-10-30 21:48:31
回答 1查看 43关注 0票数 1

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

代码语言:javascript
复制
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时,我会得到以下错误:

代码语言:javascript
复制
[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,但是错误仍然是一样的。

这是我的自动迁移:

代码语言:javascript
复制
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中时,我会得到一个外键约束错误。我正试图通过联想来添加它。

EN

回答 1

Stack Overflow用户

发布于 2022-10-31 05:03:16

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

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

Cart <===> products关系可以建模为带有连接表的many2many,以存储产品和手推车的ID,这里可能不需要外键。

代码语言:javascript
复制
type Cart struct {
    CartID   uint64 `gorm:"primaryKey"`
    Products []Product `gorm:"many2many:cart_products"`
    ....
}

还可以使用将所有gorm标记合并为一个,不需要重复。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74257010

复制
相关文章

相似问题

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