【问题标题】:Deep nested structures in Go-GormGo-Gorm 中的深层嵌套结构
【发布时间】:2019-12-13 12:28:06
【问题描述】:

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

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有什么问题吗?似乎并非如此。我的结构?看起来一模一样。这非常令人困惑。

【问题讨论】:

    标签: json postgresql http go go-gorm


    【解决方案1】:

    这确实是一个错误,并且该错误在 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
    }
    

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

    【讨论】: