I'm making an http service on Go, using Gorm with PostgreSQL, and I've encountered something weird. I've got a three level nested model for a library:
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"`
}
As you can see, books belong to shelves and pages belong to books. Then, I add the following json to test it out:
{
"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"
}
]
}
]
}
And while books actually add to the database along with the shelves, pages... just don't. Without any error messages, panics, anything - even though I think I check for errors quite rigorously. They all seem to be connected in exactly the same way - why wouldn't it add? Here's the adding function, even though I doubt that's the problem.
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
}
}
I'd google what the problem is, but to be perfectly honest... I don't even have any idea where to start. Is it something with my json? Doesn't seem to be the case. My structs? Look bloody identical. This is seriously confusing.
It was indeed a bug, and the bug was in the Book struct. It had no field to read pages into, so the pages in json were indeed simply ignored on a decode level.
Book should look like this to work properly:
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
}
Well that was a dumb mistake, sorry to bother you.