gorm.DB不能为model.Currency预加载字段货币

时间:2018-01-21 11:25:03

标签: database go go-gorm

我正在查看gorm.DB的文档和来源,但似乎无法理解Preload的目的。

我认为这是"预加载的架构/表格/行"你以后可以使用"但不能以某种方式使用它。

例如,我有以下struct

package model

type Currency struct {
  ID           uint64 `gorm:"primary_key"`
  CurrencyCode string `gorm:"size:3"`
}

但是当我做这样的事情来比较FindPreload

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(&currencies)
for i, curr := range currencies {
    // stuff
}

我得到以下(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 ]

数据库架构:

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)

0 个答案:

没有答案