英文:
Gorm read only field after create
问题
我需要在创建后将模型的一个属性设置为只读。我的代码可以正常工作,但是当我尝试更新时不会抛出任何错误。
请问你能帮我解决如何在更新时获取错误吗?
package main
import (
"fmt"
"gorm.io/gorm"
)
type Product struct {
gorm.Model
ProductID int `gorm:"primaryKey"`
Code string `gorm:"<-:create"`
Price uint
}
// TestSuite 是所有测试的代码,与数据库无关
func TestSuite(db *gorm.DB) { // 迁移模式
db.AutoMigrate(&Product{})
// 创建
db.Create(&Product{Code: "D4222", Price: 1000, ProductID: 3})
// 读取
var product Product
db.First(&product, "product_id = ?", 3) // 查找 product_id 为 3 的产品
fmt.Println("创建后的产品代码:", product.Code)
fmt.Println("创建后的产品价格:", product.Price)
// 更新
err := db.Model(&product).Where("product_id = ?", 3).Updates(Product{Price: 400, Code: "F42"}).Error
if err != nil {
fmt.Println(err)
}
// 更新后读取
var updateProd Product
db.First(&updateProd, "product_id = ?", 3) // 查找 product_id 为 3 的产品
fmt.Println("更新后的产品代码:", updateProd.Code)
fmt.Println("更新后的产品价格:", updateProd.Price)
// 删除 - 删除产品
db.Unscoped().Delete(&updateProd, 3)
}
输出:
创建后的产品代码:D4222
创建后的产品价格:1000
更新后的产品代码:D4222
更新后的产品价格:400
英文:
I need to make one of the property of model as read only after create.
My code in working but when I try to update It will not throw any error.
Could you please help me in how to get error when we try to Update?
package main
import (
"fmt"
"gorm.io/gorm"
)
type Product struct {
gorm.Model
ProductID int `gorm:"primaryKey"`
Code string `gorm:"->;<-:create"`
Price uint
}
// TestSuite is code to all tests, independent of database
func TestSuite(db *gorm.DB) { // Migrate the schema
db.AutoMigrate(&Product{})
// Create
db.Create(&Product{Code: "D4222", Price: 1000, ProductID: 3})
// Read
var product Product
db.First(&product, "product_id = ?", 3) // find product with product_id 2
fmt.Println("Product Code After Creation: ", product.Code)
fmt.Println("Product Price After Creation: ", product.Price)
//Update
err := db.Model(&product).Where("product_id = ?", 3).Updates(Product{Price: 400, Code: "F42"}).Error
if err != nil {
fmt.Println(err)
}
// Read after update
var updateProd Product
db.First(&updateProd, "product_id = ?", 3) // find product with product_id 2
fmt.Println("Product Code After Update: ", updateProd.Code)
fmt.Println("Product Price After Update: ", updateProd.Price)
// Delete - delete product
db.Unscoped().Delete(&updateProd, 3)
}
Output:
Product Code After Creation: D4222
Product Price After Creation: 1000
Product Code After Update: D4222
Product Price After Update: 400
答案1
得分: 1
如果你的表还没有创建,那么你没有任何问题。
如果你的表已经创建,并且你想将模型的两个属性设置为只读,请按照以下步骤进行操作:
type Test struct {
EnvID string `json:"env_id" gorm:"-"`
PartID string `json:"part_id" gorm:"index:depl_part;unique;priority:3;-"`
}
然后你需要运行以下代码:
err := Client.AutoMigrate(&Test{})
现在,EnvID和PartID属性是只读的,直到你从GORM中删除"->"并运行上述代码。
英文:
if your table not created you don't have any problem
if your table created and you wanna to make two of the property of model as read only following below:
type Test struct {
EnvID string `json:"env_id" gorm:"->"`
PartID string `json:"part_id" gorm:"index:depl_part;unique;priority:3;->"`
}
then you must be to run following code
err := Client.AutoMigrate(&Test{})
now EnvID and PartID are READ-ONLY until you delete -> from GORM and run top code.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论