gORM belongs to association doesnt preload

Viewed 139

I am trying to make a relational database with gORM and I have a problem with the belongs to method that I can't get to work.

I am expecting to see the booking with the table it is related to but the booking just shows a table with nil values. and the TableID that it should refer to is also null.

I have created a small test which creates some data and prints out the results as json.

package main

import (
    "encoding/json"
    "fmt"

    "gorm.io/driver/sqlite"
    "gorm.io/gorm"
)

type User struct {
    gorm.Model
    Username string
    Password string
    Bookings []Booking `gorm:"polymorphic:Owner;"`
}

type Booking struct {
    gorm.Model
    TableID   *int
    Table     Table
    OwnerID   uint
    Time      int
    OwnerType string
}
type Table struct {
    gorm.Model
    Number int
    Price  int
}

func DatabaseConnection() *gorm.DB {
    db, err := gorm.Open(sqlite.Open("./test.db"), &gorm.Config{})
    if err != nil {
        panic(err)
    }

    db.AutoMigrate(&Booking{})
    db.AutoMigrate(&Table{})
    db.AutoMigrate(&User{})

    return db
}

func main() {
    fmt.Println("creating stuff..")
    createStuff()

    fmt.Println("checking table..")
    checkTable()

    fmt.Println("checking booking..")
    checkBooking()

    fmt.Println("checking user..")
    checkUser()
}

func checkTable() {
    db := DatabaseConnection()

    var table Table
    db.First(&table)
    u, _ := json.Marshal(&table)
    fmt.Println(string(u))
}

func checkUser() {
    db := DatabaseConnection()

    var user User
    db.First(&user)
    db.Preload("Bookings.Table").Preload("Bookings").First(&user)
    u, _ := json.Marshal(&user)
    fmt.Println(string(u))
}

func checkBooking() {
    db := DatabaseConnection()

    var booking Booking
    db.Preload("Table").First(&booking)
    u, _ := json.Marshal(&booking)
    fmt.Println(string(u))
}

func createStuff() {
    db := DatabaseConnection()
    tables := []Table{
        {Number: 1, Price: 100},
        {Number: 2, Price: 100},
        {Number: 3, Price: 100},
        {Number: 4, Price: 100},
        {Number: 5, Price: 100},
        {Number: 6, Price: 100},
        {Number: 7, Price: 100},
    }

    user := User{
        Username: "martin",
        Password: "secret",
    }

    db.Create(&user)
    db.Create(&tables)

    booking := Booking{Time: 1234}

    db.Model(&tables[0]).Association("Table").Append(
        &booking,
    )

    db.Model(&user).Association("Bookings").Append(
        &booking,
    )
}

I get the following output from my test:

checking table..
{
  "ID": 1,
  "CreatedAt": "2021-12-06T14:56:27.169847346+01:00",
  "UpdatedAt": "2021-12-06T14:56:27.169847346+01:00",
  "DeletedAt": null,
  "Number": 1,
  "Price": 100
}
checking booking..
{
  "ID": 1,
  "CreatedAt": "2021-12-06T14:56:27.173323491+01:00",
  "UpdatedAt": "2021-12-06T14:56:27.173323491+01:00",
  "DeletedAt": null,
  "TableID": null,
  "Table": {
    "ID": 0,
    "CreatedAt": "0001-01-01T00:00:00Z",
    "UpdatedAt": "0001-01-01T00:00:00Z",
    "DeletedAt": null,
    "Number": 0,
    "Price": 0
  },
  "OwnerID": 1,
  "Time": 1234,
  "OwnerType": "users"
}
checking user..
{
  "ID": 1,
  "CreatedAt": "2021-12-06T14:56:27.166274122+01:00",
  "UpdatedAt": "2021-12-06T14:56:27.17309057+01:00",
  "DeletedAt": null,
  "Username": "martin",
  "Password": "secret",
  "Bookings": [
    {
      "ID": 1,
      "CreatedAt": "2021-12-06T14:56:27.173323491+01:00",
      "UpdatedAt": "2021-12-06T14:56:27.173323491+01:00",
      "DeletedAt": null,
      "TableID": null,
      "Table": {
        "ID": 0,
        "CreatedAt": "0001-01-01T00:00:00Z",
        "UpdatedAt": "0001-01-01T00:00:00Z",
        "DeletedAt": null,
        "Number": 0,
        "Price": 0
      },
      "OwnerID": 1,
      "Time": 1234,
      "OwnerType": "users"
    }
  ]
}
0 Answers
Related