GORM不会创建多对多关联。

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

GORM don't create many2many associations

问题

我有一个模型:

  1. type Book struct {
  2. gorm.Model
  3. Title string `json:"title"`
  4. Author string `json:"author"`
  5. Description string `json:"description"`
  6. Category string `json:"Category"`
  7. Publisher string `json:"publisher"`
  8. AuthorsCard []*AuthorsCard `gorm:"many2many:book_authorscard;" json:"authorscard"`
  9. }
  10. type AuthorsCard struct {
  11. gorm.Model
  12. Name string `json:"name"`
  13. Age int `json:"age"`
  14. YearOfBirth int `json:"year"`
  15. Biography string `json:"biography"`
  16. }

在执行 db.AutoMigrate(&models.Book{}, &models.AuthorsCard{}) 后,我有一个创建函数:

  1. func TestCreate() {
  2. var testbook = models.Book{
  3. Title: "Test",
  4. Author: "tst",
  5. AuthorsCard: []*models.AuthorsCard{
  6. {
  7. Age: 23,
  8. Name: "test",
  9. YearOfBirth: 1999,
  10. Biography: "23fdgsdddTEST",
  11. },
  12. },
  13. Description: "something",
  14. }
  15. db.Preload("AuthorsCard").Find(&models.Book{})
  16. db.Create(&testbook)
  17. }

尽管 AuthorsCard 中有一些数据,但我收到的响应是:

  1. {
  2. "ID": 78,
  3. "CreatedAt": "2022-06-23T13:10:58.01629+03:00",
  4. "UpdatedAt": "2022-06-23T13:10:58.01629+03:00",
  5. "DeletedAt": null,
  6. "title": "Test",
  7. "author": "tst",
  8. "description": "something",
  9. "Category": "",
  10. "publisher": "",
  11. "authorscard": null
  12. }

如你所见,"authorscard" 是 null。

我该如何将 "authorscard" 保存到数据库中?
我使用的是 Gorm + PostgreSQL,我还用 Postman 发送了一些请求,结果都是一样的 - Authors card 是 null。

英文:

I have model:

  1. type Book struct {
  2. gorm.Model
  3. Title string `json:"title"`
  4. Author string `json:"author"`
  5. Description string `json:"description"`
  6. Category string `json:"Category"`
  7. Publisher string `json:"publisher"`
  8. AuthorsCard []*AuthorsCard `gorm:"many2many:book_authorscard; "json:"authorscard"`
  9. }
  10. type AuthorsCard struct {
  11. gorm.Model
  12. Name string `json:"name"`
  13. Age int `json:"age"`
  14. YearOfBirth int `json:"year"`
  15. Biography string `json:"biography"`
  16. }

After db.AutoMigrate(&models.Book{}, &models.AuthorsCard{})
I have a creation function:

  1. func TestCreate() {
  2. var testbook = models.Book{
  3. Title: "Test",
  4. Author: "tst",
  5. AuthorsCard: []*models.AuthorsCard{
  6. {
  7. Age: 23,
  8. Name: "test",
  9. YearOfBirth: 1999,
  10. Biography: "23fdgsdddTEST",
  11. },
  12. },
  13. Description: "something",
  14. }
  15. db.Preload("AuthorsCard").Find(&models.Book{})
  16. db.Create(&testbook)
  17. }

despite That AuthorsCard has some data in it, I receive this as a response:

  1. {
  2. "ID": 78,
  3. "CreatedAt": "2022-06-23T13:10:58.01629+03:00",
  4. "UpdatedAt": "2022-06-23T13:10:58.01629+03:00",
  5. "DeletedAt": null,
  6. "title": "Test",
  7. "author": "tst",
  8. "description": "something",
  9. "Category": "",
  10. "publisher": "",
  11. "authorscard": null
  12. }

As you can see "authorscard" is null.

How can I save "authorscard" to the database?
I used Gorm + postgresql and also I sent a few request with postman, results are the same - Authors card is null

答案1

得分: 2

代码在正确的顺序下调用时是有效的:

  1. func TestCreate() {
  2. db := getDB()
  3. db.AutoMigrate(&Book{}, AuthorsCard{})
  4. var testbook = Book{
  5. Title: "Test",
  6. Author: "tst",
  7. AuthorsCard: []*AuthorsCard{
  8. {
  9. Age: 23,
  10. Name: "test",
  11. YearOfBirth: 1999,
  12. Biography: "23fdgsdddTEST",
  13. },
  14. },
  15. Description: "something",
  16. }
  17. // 1. 创建 testbook。
  18. db.Create(&testbook)
  19. // 2. 将其存储到变量中:
  20. var b1 *Book
  21. db.Preload("AuthorsCard").Find(&b1)
  22. fmt.Println(b1.AuthorsCard[0].Age)
  23. fmt.Println(b1.AuthorsCard[0].Name)
  24. fmt.Println(b1.AuthorsCard[0].YearOfBirth)
  25. fmt.Println(b1.AuthorsCard[0].Biography)
  26. }

输出结果:

  1. > 23
  2. > test
  3. 1999
  4. 23fdgsdddTEST

此外,由于你传递了一个 AuthorsCard 的指针,并且在这些情况下编组不总是正常工作,所以你的 JSON 导出可能会失败。然而,GORM 在这里做得很好。

静态检查还给了我一些提示:

  1. type Book struct {
  2. gorm.Model
  3. Title string `json:"title"`
  4. Author string `json:"author"`
  5. Description string `json:"description"`
  6. Category string `json:"category"`
  7. Publisher string `json:"publisher"`
  8. AuthorsCard []*AuthorsCard `gorm:"many2many:book_authorscard" json:"authorscard"` // 错误的空格
  9. }
英文:

The code is working when it's called in the correct order:

  1. func TestCreate() {
  2. db := getDB()
  3. db.AutoMigrate(&Book{}, AuthorsCard{})
  4. var testbook = Book{
  5. Title: "Test",
  6. Author: "tst",
  7. AuthorsCard: []*AuthorsCard{
  8. {
  9. Age: 23,
  10. Name: "test",
  11. YearOfBirth: 1999,
  12. Biography: "23fdgsdddTEST",
  13. },
  14. },
  15. Description: "something",
  16. }
  17. // 1. Create your testbook.
  18. db.Create(&testbook)
  19. // 2. Store it into a variable:
  20. var b1 *Book
  21. db.Preload("AuthorsCard").Find(&b1)
  22. fmt.Println(b1.AuthorsCard[0].Age)
  23. fmt.Println(b1.AuthorsCard[0].Name)
  24. fmt.Println(b1.AuthorsCard[0].YearOfBirth)
  25. fmt.Println(b1.AuthorsCard[0].Biography)
  26. }

prints:

> 23
> test
1999
23fdgsdddTEST

Also your JSON export might fail since you pass a pointer to AuthorCard and the marshalling not always works properly in those cases. However, GORM does the right job here.

Static check also gave me some hints here:

  1. type Book struct {
  2. gorm.Model
  3. Title string `json:"title"`
  4. Author string `json:"author"`
  5. Description string `json:"description"`
  6. Category string `json:"Category"`
  7. Publisher string `json:"publisher"`
  8. AuthorsCard []*AuthorsCard `gorm:"many2many:book_authorscard" json:"authorscard"` // wrong space
  9. }

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

发表评论

匿名网友

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

确定