I was looking through gorm.DB
's docs and sources but can't seem to understand the purpose of Preload
.
I thought that is the "preloaded schema/tables/rows" that you can use afterwards" but cannot somehow use it that way.
For instance I have the following struct
package model
type Currency struct {
ID uint64 `gorm:"primary_key"`
CurrencyCode string `gorm:"size:3"`
}
but when I do something like this to compare Find
and Preload
logger.Info("Preload")
var preloadCurrencies []model.Currency
dbMySQL.Preload("currencies").Find(&preloadCurrencies)
for i, curr := range preloadCurrencies {
// stuff
}
logger.Info("find")
var currencies []model.Currency
dbMySQL.Find(¤cies)
for i, curr := range currencies {
// stuff
}
I get the following (with ):
INFO[0000] Preload
(/go/src/SOMESOURCES/main.go:28)
[2018-01-21 11:08:47] [1.02ms] SELECT * FROM `currencies`
[168 rows affected or returned ]
(/go/src/SOMESOURCES/main.go:28)
[2018-01-21 11:08:47] can't preload field currencies for model.Currency
INFO[0000] find
(/go/src/SOMESOURCES/main.go:37)
[2018-01-21 11:08:47] [0.90ms] SELECT * FROM `currencies`
[168 rows affected or returned ]
DB schema:
show columns from currencies;
+---------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+---------------------+------+-----+---------+----------------+
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| currency_code | char(3) | NO | | NULL | |
+---------------+---------------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)