如何在Gorm中使用插入冲突时更新嵌套字段?

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

How to update nested fields on insert conflict with Gorm?

问题

我已经翻译了你提供的内容,请查看以下翻译结果:

我有两个模型,Dictionary(字典)和DictionaryRecord(字典记录),其中DictionaryRecord有一个指向Dictionary的外键。

  1. type DictionaryRecord struct {
  2. Id string `gorm:"primaryKey"`
  3. Name string
  4. DictionaryId string
  5. }
  6. type Dictionary struct {
  7. Id string `gorm:"primaryKey"`
  8. Name string
  9. Records []DictionaryRecord
  10. }

我想确保在创建一个带有嵌套记录的字典时,对记录进行的更改能够在upsert(插入或更新)期间反映出来。

  1. package main
  2. import (
  3. // 基于GGO的Sqlite驱动
  4. "gorm.io/driver/sqlite"
  5. "gorm.io/gorm"
  6. "gorm.io/gorm/clause"
  7. )
  8. func main() {
  9. db, err := gorm.Open(sqlite.Open("gorm.db"), &gorm.Config{})
  10. if err != nil {
  11. panic(err)
  12. }
  13. db.AutoMigrate(
  14. &DictionaryRecord{},
  15. &Dictionary{},
  16. )
  17. db.Clauses(clause.OnConflict{
  18. Columns: []clause.Column{{Name: "id"}},
  19. DoUpdates: clause.AssignmentColumns([]string{"name"}),
  20. }).Create(&Dictionary{
  21. Name: "AAAAA",
  22. Id: "e831ab86-db60-4a71-ba63-f20a181cd69b",
  23. Records: []DictionaryRecord{
  24. {
  25. Id: "66f73e9b-61b8-4bc9-941d-80d7fd80f8f4",
  26. Name: "将被更新",
  27. },
  28. },
  29. })
  30. }

你如何指定DictionaryRecord中的列名必须被更新?

英文:

I've got two models, Dictionary and DictionaryRecord with a foreign key on Dictionary.

  1. type DictionaryRecord struct {
  2. Id string `gorm:"primaryKey"`
  3. Name string
  4. DictionaryId string
  5. }
  6. type Dictionary struct {
  7. Id string `gorm:"primaryKey"`
  8. Name string
  9. Records []DictionaryRecord
  10. }

I'd like to make sure that when creating a dictionary with a nested record changed, to have the change reflected during the upsert.

  1. package main
  2. import (
  3. // Sqlite driver based on GGO
  4. "gorm.io/driver/sqlite"
  5. "gorm.io/gorm"
  6. "gorm.io/gorm/clause"
  7. )
  8. func main() {
  9. db, err := gorm.Open(sqlite.Open("gorm.db"), &gorm.Config{})
  10. if err != nil {
  11. panic(err)
  12. }
  13. db.AutoMigrate(
  14. &DictionaryRecord{},
  15. &Dictionary{},
  16. )
  17. db.Clauses(clause.OnConflict{
  18. Columns: []clause.Column{{Name: "id"}},
  19. DoUpdates: clause.AssignmentColumns([]string{"name"}),
  20. }).Create(&Dictionary{
  21. Name: "AAAAA",
  22. Id: "e831ab86-db60-4a71-ba63-f20a181cd69b",
  23. Records: []DictionaryRecord{
  24. {
  25. Id: "66f73e9b-61b8-4bc9-941d-80d7fd80f8f4",
  26. Name: "will be updated",
  27. },
  28. },
  29. })
  30. }

How do you specify that the column name in the DictionaryRecord must be updated?

答案1

得分: 2

你可以尝试使用指针:

  1. type DictionaryRecord struct {
  2. Id string `gorm:"primaryKey"`
  3. Name string
  4. DictionaryId string
  5. }
  6. type Dictionary struct {
  7. Id string `gorm:"primaryKey"`
  8. Name string
  9. Records []*DictionaryRecord // 在这里使用指针
  10. }
英文:

can you try use pointer

  1. type DictionaryRecord struct {
  2. Id string `gorm:"primaryKey"`
  3. Name string
  4. DictionaryId string
  5. }
  6. type Dictionary struct {
  7. Id string `gorm:"primaryKey"`
  8. Name string
  9. Records []*DictionaryRecord //on this records
  10. }

huangapple
  • 本文由 发表于 2022年4月30日 21:04:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/72068752.html
匿名

发表评论

匿名网友

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

确定