Go-Gorm中的深层嵌套结构

时间:2019-08-06 01:26:12

标签: json postgresql http go go-gorm

我正在Gorm上使用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
    }
}

我会用Google搜索问题所在,但是老实说……我什至不知道从哪里开始。我的json有问题吗?似乎并非如此。我的结构?看起来很血腥。这很令人困惑。

1 个答案:

答案 0 :(得分:0)

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

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