在创建后,Gorm只读字段的读取

huangapple go评论104阅读模式
英文:

Gorm read only field after create

问题

我需要在创建后将模型的一个属性设置为只读。我的代码可以正常工作,但是当我尝试更新时不会抛出任何错误。

请问你能帮我解决如何在更新时获取错误吗?

  1. package main
  2. import (
  3. "fmt"
  4. "gorm.io/gorm"
  5. )
  6. type Product struct {
  7. gorm.Model
  8. ProductID int `gorm:"primaryKey"`
  9. Code string `gorm:"<-:create"`
  10. Price uint
  11. }
  12. // TestSuite 是所有测试的代码,与数据库无关
  13. func TestSuite(db *gorm.DB) { // 迁移模式
  14. db.AutoMigrate(&Product{})
  15. // 创建
  16. db.Create(&Product{Code: "D4222", Price: 1000, ProductID: 3})
  17. // 读取
  18. var product Product
  19. db.First(&product, "product_id = ?", 3) // 查找 product_id 为 3 的产品
  20. fmt.Println("创建后的产品代码:", product.Code)
  21. fmt.Println("创建后的产品价格:", product.Price)
  22. // 更新
  23. err := db.Model(&product).Where("product_id = ?", 3).Updates(Product{Price: 400, Code: "F42"}).Error
  24. if err != nil {
  25. fmt.Println(err)
  26. }
  27. // 更新后读取
  28. var updateProd Product
  29. db.First(&updateProd, "product_id = ?", 3) // 查找 product_id 为 3 的产品
  30. fmt.Println("更新后的产品代码:", updateProd.Code)
  31. fmt.Println("更新后的产品价格:", updateProd.Price)
  32. // 删除 - 删除产品
  33. db.Unscoped().Delete(&updateProd, 3)
  34. }

输出:
创建后的产品代码: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?

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;gorm.io/gorm&quot;
  5. )
  6. type Product struct {
  7. gorm.Model
  8. ProductID int `gorm:&quot;primaryKey&quot;`
  9. Code string `gorm:&quot;-&gt;;&lt;-:create&quot;`
  10. Price uint
  11. }
  12. // TestSuite is code to all tests, independent of database
  13. func TestSuite(db *gorm.DB) { // Migrate the schema
  14. db.AutoMigrate(&amp;Product{})
  15. // Create
  16. db.Create(&amp;Product{Code: &quot;D4222&quot;, Price: 1000, ProductID: 3})
  17. // Read
  18. var product Product
  19. db.First(&amp;product, &quot;product_id = ?&quot;, 3) // find product with product_id 2
  20. fmt.Println(&quot;Product Code After Creation: &quot;, product.Code)
  21. fmt.Println(&quot;Product Price After Creation: &quot;, product.Price)
  22. //Update
  23. err := db.Model(&amp;product).Where(&quot;product_id = ?&quot;, 3).Updates(Product{Price: 400, Code: &quot;F42&quot;}).Error
  24. if err != nil {
  25. fmt.Println(err)
  26. }
  27. // Read after update
  28. var updateProd Product
  29. db.First(&amp;updateProd, &quot;product_id = ?&quot;, 3) // find product with product_id 2
  30. fmt.Println(&quot;Product Code After Update: &quot;, updateProd.Code)
  31. fmt.Println(&quot;Product Price After Update: &quot;, updateProd.Price)
  32. // Delete - delete product
  33. db.Unscoped().Delete(&amp;updateProd, 3)
  34. }
  1. Output:
  2. Product Code After Creation: D4222
  3. Product Price After Creation: 1000
  4. Product Code After Update: D4222
  5. Product Price After Update: 400

答案1

得分: 1

如果你的表还没有创建,那么你没有任何问题。

如果你的表已经创建,并且你想将模型的两个属性设置为只读,请按照以下步骤进行操作:

  1. type Test struct {
  2. EnvID string `json:"env_id" gorm:"-"`
  3. PartID string `json:"part_id" gorm:"index:depl_part;unique;priority:3;-"`
  4. }

然后你需要运行以下代码:

  1. 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:

  1. type Test struct {
  2. EnvID string `json:&quot;env_id&quot; gorm:&quot;-&gt;&quot;`
  3. PartID string `json:&quot;part_id&quot; gorm:&quot;index:depl_part;unique;priority:3;-&gt;&quot;`
  4. }

then you must be to run following code

  1. err := Client.AutoMigrate(&amp;Test{})

now EnvID and PartID are READ-ONLY until you delete -> from GORM and run top code.

huangapple
  • 本文由 发表于 2022年6月23日 13:13:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/72724841.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定