Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are possible to make db.Preload() in GORM to be Auto-preload?

Tags:

go

go-gorm

model.go:


type First struct {
    ID            int     `json:"id" gorm:"column:id;primary_key"`
    Status        string  `json:"status" gorm:"column:status"`
    SecondID      int     `json:"second_id" gorm:"column:second_id"`
    SecondData    Second  `json:"second_data" gorm:"foreignKey:SecondID;references:ID"`
}

type Second struct {
    ID            int     `json:"id" gorm:"column:second_id;primary_key"`
    Status        string  `json:"status" gorm:"column:status"`
    Description   string  `json:"description" gorm:"column:description"`
}

var res []model.First

db.Raw("first.*, second.* FROM first LEFT JOIN second ON first.second_id = second.second_id")
db.Preload("SecondData").Find(&res).Error

Output:

{
    "id": 1,
    "status": "A",
    "second_id": 1
    "second_data": {
        "id": 1
        "status": "B",
        "description": "blablabla"
    }
}

I don't really know how db.Preload() works. Why i should use db.Preload() to get "SecondData" every time i need do nested struct ? Are it's possible only use db.Row() or db.Table().Joins().Where().Find(), i mean's without db.Preload()?

like image 854
skyccrfxcyr Avatar asked Apr 22 '21 02:04

skyccrfxcyr


People also ask

What is DB model in Gorm?

db. Model is the ordinary way of doing things. It allows you to tell gorm which model struct this operation relates to. It isn't always needed, as for example a simple Find with the right struct type will infer the model automatically.

Is Gorm case sensitive?

Tags are optional to use when declaring models, GORM supports the following tags: Tags are case insensitive, however camelCase is preferred.


1 Answers

If you want SecondData loaded every time when the First struct is loaded without using Preload, you might consider using hooks.

It might look something like this:

func (f *First) AfterFind(tx *gorm.DB) error {
  return tx.First(&f.SecondData, f.SecondID).Error
}

So, when you load the First data, the AfterFind hook should be triggered.

like image 132
Emin Laletovic Avatar answered Nov 09 '22 04:11

Emin Laletovic