i have an issue with the generated columns value with 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
}
This is the error
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`
It seems like GORM has created a temporary table "products__temp" a clone of "products" table. So as we can know we cannot insert or edit the generated columns!
Note: i'm working wih SQLite
This issue is fixed by my pull request
https://github.com/go-gorm/sqlite/pull/109
the pull is accepted 👌🏻