GORM的.Save方法无法将”has one”关系保存到数据库中。

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

GORM .Save don't save "has one" relation to the database

问题

我有一个结构体:

  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:"foreignKey:BookID" json:"authorscard"`
  9. }
  10. type AuthorsCard struct {
  11. //gorm.Model // 我故意不想使用 gorm.model
  12. ID uint `gorm:"primarykey"`
  13. BookID uint
  14. Name string `json:"name"`
  15. Age int `json:"age"`
  16. YearOfBirth int `json:"year"`
  17. Biography string `json:"biography"`
  18. }

我试图创建一个更新函数:

  1. // controller.go
  2. func UpdateBook(ctx *gin.Context) {
  3. enableCors(&ctx.Writer)
  4. id := ctx.Param("ID")
  5. var updateBook = &models.Book{}
  6. if err := ctx.BindJSON(updateBook); err != nil {
  7. ctx.AbortWithStatus(http.StatusBadRequest)
  8. log.Fatal(err)
  9. } else {
  10. repository.UpdateBook(updateBook, id)
  11. ctx.JSON(http.StatusOK, updateBook)
  12. log.Println(updateBook)
  13. }
  14. }
  1. //repository.go
  2. func UpdateBook(updateBook *models.Book, ID string) {
  3. book, db := GetBookById(ID)
  4. if updateBook.Title != "" {
  5. book.Title = updateBook.Title
  6. }
  7. if updateBook.Author != "" {
  8. book.Author = updateBook.Author
  9. }
  10. if updateBook.Publisher != "" {
  11. book.Publisher = updateBook.Publisher
  12. }
  13. if updateBook.Description != "" {
  14. book.Description = updateBook.Description
  15. }
  16. if updateBook.Category != "" {
  17. book.Category = updateBook.Category
  18. }
  19. if updateBook.AuthorsCard.Name != "" {
  20. book.AuthorsCard.Name = updateBook.AuthorsCard.Name
  21. }
  22. if updateBook.AuthorsCard.Age != 0 {
  23. book.AuthorsCard.Age = updateBook.AuthorsCard.Age
  24. }
  25. if updateBook.AuthorsCard.YearOfBirth != 0 {
  26. book.AuthorsCard.YearOfBirth = updateBook.AuthorsCard.YearOfBirth
  27. }
  28. if updateBook.AuthorsCard.Biography != "" {
  29. book.AuthorsCard.Biography = updateBook.AuthorsCard.Biography
  30. }
  31. db.Save(&book)
  32. // 同样可以使用 db.Preload("AuthorsCard").Save(&book)
  33. }

问题是:当我发出 PUT 请求时,我收到了完全更新的数据。当我尝试发出 GET 请求时,除了相关的 AuthorsCard 字段之外,所有字段都已更新。

PUT 响应:
200 状态码

  1. {
  2. "ID": 0,
  3. "CreatedAt": "0001-01-01T00:00:00Z",
  4. "UpdatedAt": "0001-01-01T00:00:00Z",
  5. "DeletedAt": null,
  6. "title": "Test",
  7. "author": "author",
  8. "description": "something",
  9. "Category": "Category",
  10. "publisher": "PB",
  11. "authorscard": {
  12. "ID": 0,
  13. "BookID": 0,
  14. "name": "Updated",
  15. "age": 22,
  16. "year": 1999,
  17. "biography": "biography Updated"
  18. }
  19. }

之后的 GET 响应:

  1. [
  2. {
  3. "ID": 1,
  4. "CreatedAt": "2022-06-29T14:57:37.489639+03:00",
  5. "UpdatedAt": "2022-06-29T15:50:11.578724+03:00",
  6. "DeletedAt": null,
  7. "title": "Test",
  8. "author": "author",
  9. "description": "something",
  10. "Category": "Category",
  11. "publisher": "PB",
  12. "authorscard": {
  13. "ID": 1,
  14. "BookID": 1,
  15. "name": "test",
  16. "age": 23,
  17. "year": 1999,
  18. "biography": "23fdgsdddTEST"
  19. }
  20. }
  21. ]

如你所见,"authorscard" 没有改变。请问,有人可以告诉我我做错了什么吗?

英文:

I have struct:

  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:"foreignKey:BookID" json:"authorscard"`
  9. }
  10. type AuthorsCard struct {
  11. //gorm.Model // I purposely Don't want to use gorm.model
  12. ID uint `gorm:"primarykey"`
  13. BookID uint
  14. Name string `json:"name"`
  15. Age int `json:"age"`
  16. YearOfBirth int `json:"year"`
  17. Biography string `json:"biography"`
  18. }

And I'm trying to make update function:

  1. // controller.go
  2. func UpdateBook(ctx *gin.Context) {
  3. enableCors(&ctx.Writer)
  4. id := ctx.Param("ID")
  5. var updateBook = &models.Book{}
  6. if err := ctx.BindJSON(updateBook); err != nil {
  7. ctx.AbortWithStatus(http.StatusBadRequest)
  8. log.Fatal(err)
  9. } else {
  10. repository.UpdateBook(updateBook, id)
  11. ctx.JSON(http.StatusOK, updateBook)
  12. log.Println(updateBook)
  13. }
  14. }
  1. //repository.go
  2. func UpdateBook(updateBook *models.Book, ID string) {
  3. book, db := GetBookById(ID)
  4. if updateBook.Title != "" {
  5. book.Title = updateBook.Title
  6. }
  7. if updateBook.Author != "" {
  8. book.Author = updateBook.Author
  9. }
  10. if updateBook.Publisher != "" {
  11. book.Publisher = updateBook.Publisher
  12. }
  13. if updateBook.Description != "" {
  14. book.Description = updateBook.Description
  15. }
  16. if updateBook.Category != "" {
  17. book.Category = updateBook.Category
  18. }
  19. if updateBook.AuthorsCard.Name != "" {
  20. book.AuthorsCard.Name = updateBook.AuthorsCard.Name
  21. }
  22. if updateBook.AuthorsCard.Age != 0 {
  23. book.AuthorsCard.Age = updateBook.AuthorsCard.Age
  24. }
  25. if updateBook.AuthorsCard.YearOfBirth != 0 {
  26. book.AuthorsCard.YearOfBirth = updateBook.AuthorsCard.YearOfBirth
  27. }
  28. if updateBook.AuthorsCard.Biography != "" {
  29. book.AuthorsCard.Biography = updateBook.AuthorsCard.Biography
  30. }
  31. db.Save(&book)
  32. // same with db.Preload("AuthorsCard").Save(&book)
  33. }

The issue is: When I make an PUT request, I receive Fully updated data. And when I'm trying to make GET request all my fields, except related AuthorsCard, are been updated.

PUT response:
200 Code

  1. {
  2. "ID": 0,
  3. "CreatedAt": "0001-01-01T00:00:00Z",
  4. "UpdatedAt": "0001-01-01T00:00:00Z",
  5. "DeletedAt": null,
  6. "title": "Test",
  7. "author": "author",
  8. "description": "something",
  9. "Category": "Category",
  10. "publisher": "PB",
  11. "authorscard": {
  12. "ID": 0,
  13. "BookID": 0,
  14. "name": "Updated",
  15. "age": 22,
  16. "year": 1999,
  17. "biography": "biography Updated"
  18. }
  19. }

Get response after that:

  1. [
  2. {
  3. "ID": 1,
  4. "CreatedAt": "2022-06-29T14:57:37.489639+03:00",
  5. "UpdatedAt": "2022-06-29T15:50:11.578724+03:00",
  6. "DeletedAt": null,
  7. "title": "Test",
  8. "author": "author",
  9. "description": "something",
  10. "Category": "Category",
  11. "publisher": "PB",
  12. "authorscard": {
  13. "ID": 1,
  14. "BookID": 1,
  15. "name": "test",
  16. "age": 23,
  17. "year": 1999,
  18. "biography": "23fdgsdddTEST"
  19. }
  20. }
  21. ]

As you can see "authorscard" hasn't changed. Please, can someone tell me what I'm doing wrong?

答案1

得分: 3

你需要明确告诉 Gorm 存储/更新关联关系。

  1. db.Session(&gorm.Session{FullSaveAssociations: true}).Updates(&book)
英文:

You need to explicitly tell Gorm to store/update associations.

  1. db.Session(&gorm.Session{FullSaveAssociations: true}).Updates(&book)

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

发表评论

匿名网友

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

确定