gpt4 book ai didi

GORM : cannot INSERT into generated column

转载 作者:行者123 更新时间:2023-12-05 04:25:24 27 4
gpt4 key购买 nike

我对使用 GORM 生成的列值有疑问。

我的结构
type Product struct {
gorm.Model

Name string `json:"name" gorm:"not null"`
Quantity uint `json:"quantity" gorm:"not null"`
Price float64 `json:"price" gorm:"not null"`
Gain float64 `json:"gain" gorm:"not null"`

Total float64 `json:"total" gorm:"->;type:GENERATED ALWAYS AS (quantity*price);"` // generated total price
TotalGain float64 `json:"total_gain" gorm:"->;type:GENERATED ALWAYS AS (quantity*gain);"` // generated total gain

ProcessID uint // one-to-many
}
自动迁移
    err = database.AutoMigrate(&models.Process{}, &models.Product{})
if err != nil {
return err
}

这是错误

cannot INSERT into generated column "total"

[0.021ms] [rows:0] INSERT INTO `products__temp`(`id`,`created_at`,`updated_at`,`deleted_at`,`name`,`quantity`,`price`,`gain`,`total`,`total_gain`,`process_id`) SELECT `id`,`created_at`,`updated_at`,`deleted_at`,`name`,`quantity`,`price`,`gain`,`total`,`total_gain`,`process_id` FROM `products`

GORM 似乎创建了一个临时表“products__temp”,它是“products”表的克隆。因此我们知道我们无法插入或编辑生成的列!

Note: i'm working wih SQLite

最佳答案

这个问题已经被我的 pull request 修复了

https://github.com/go-gorm/sqlite/pull/109

拉动被接受👌🏻

关于GORM : cannot INSERT into generated column,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73286022/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com