登录
首页 >  Golang >  Go问答

Go-Gorm中的复杂嵌套数据结构

来源:stackoverflow

时间:2024-02-27 09:00:25 123浏览 收藏

小伙伴们对Golang编程感兴趣吗?是否正在学习相关知识点?如果是,那么本文《Go-Gorm中的复杂嵌套数据结构》,就很适合你,本篇文章讲解的知识点主要包括。在之后的文章中也会多多分享相关知识点,希望对大家的知识积累有所帮助!

问题内容

我正在 go 上创建一个 http 服务,使用 gorm 和 postgresql,我遇到了一些奇怪的事情。 我有一个库的三层嵌套模型:

type page struct {
    id int64 `sql:"auto_increment" json:"-"`
    number int64 `json:"number"`
    book book `gorm:"foreignkey:book_id" json:"-"`
    bookid int64 `json:"book_id"`
    text string `json:"text"`
}

type book struct {
    id int64 `sql:"auto_increment" json:"-"`
    shelfplace int64 `json:"shelf_place"`
    shelf shelf `gorm:"foreignkey:shelf_id" json:"-"`
    shelfid int64 `json:"shelf_id"`
    author string `json:"author"`
    publisher string `json:"publisher"`
    pagesamount int64 `json:"pages_amount"`
}

type shelf struct {
    id int64 `sql:"auto_increment" json:"-"`
    number int64 `json:"number"`
    booksamount int64 `json:"books_amount"`
    book []book `json:"books"`
}

如您所见,书籍属于书架,页面属于书籍。 然后,我添加以下 json 来测试它:

{
  "number": 1,
  "books": [
    {
      "shelf_place": 5,
      "author": "lewis carroll",
      "publisher": "ea",
      "pages_amount": 2,
      "pages": [
        {
          "number": 2,
          "text": "lorem ipsum"
        },
        {
          "number": 4,
          "text": "dolor sit amet"
        }
      ]
    },
    {
      "shelf_place": 7,
      "author": "mark twain",
      "publisher": "activision",
      "pages_amount": 3,
      "pages": [
        {
          "number": 1,
          "text": "this is"
        },
        {
          "number": 3,
          "text": "a test"
        },
        {
          "number": 6,
          "text": "of json"
        }
      ]
    }
  ]
}

虽然书籍实际上与书架一起添加到数据库中,但页面...只是没有。没有任何错误消息,恐慌,任何东西 - 尽管我认为我非常严格地检查错误。 它们似乎都以完全相同的方式连接 - 为什么不添加呢? 这是添加函数,尽管我怀疑这就是问题所在。

func requestShelfAdd(w http.ResponseWriter, r *http.Request) {
    var newShelf Shelf
    decoder := json.NewDecoder(r.Body)
    err := decoder.Decode(&newShelf)
    if err != nil {
        w.WriteHeader(http.StatusBadRequest)
        fmt.Println("Error when decoding JSON: %v", err)
        fmt.Fprintf(w, "Error when decoding JSON: %v", err)
        return
    }
    err = shelfStore.Create(&newShelf) //shelfStore just stores current DB connection. Not much there, but I can give it to you if you want it
    if err != nil {
        w.WriteHeader(http.StatusInternalServerError)
        fmt.Fprintf(w, "Error when writing to database: %v", err)
        fmt.Println("Error when writing to database: %v", err)
        return
    }
    encoder := json.NewEncoder(w)
    err = encoder.Encode(newShelf)
    if err != nil {
        w.WriteHeader(http.StatusInternalServerError)
        fmt.Fprintf(w, "Error when encoding JSON: %v", err)
        fmt.Println("Error when encoding JSON: %v", err)
        return
    }
}

我会用谷歌搜索问题是什么,但说实话......我什至不知道从哪里开始。是我的json有问题吗?看来并非如此。我的结构?看起来一模一样。这非常令人困惑。


解决方案


这确实是一个错误,并且该错误位于 book 结构中。它没有用于读取页面的字段,因此 json 中的页面确实在解码级别上被简单地忽略了。

书应该看起来像这样才能正常工作:

type Book struct {
    ID int64 `sql:"auto_increment" json:"-"`
    ShelfPlace int64 `json:"shelf_place"`
    Shelf Shelf `gorm:"foreignkey:shelf_id" json:"-"`
    ShelfID int64 `json:"shelf_id"`
    Author string `json:"author"`
    Publisher string `json:"publisher"`
    PagesAmount int64 `json:"pages_amount"`
    Page []Page `json:"pages"`         //This was missing
}

这是一个愚蠢的错误,很抱歉打扰您。

文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《Go-Gorm中的复杂嵌套数据结构》文章吧,也可关注golang学习网公众号了解相关技术文章。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>