我对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";";产品";桌子因此,正如我们所知,我们无法插入或编辑生成的列!
注意:我正在使用SQLite
我的请求解决了这个问题
https://github.com/go-gorm/sqlite/pull/109
接受拉力