乐趣区

关于go:unsupported-relations-for-schema

预加载 Preload() 的参数是构造体的字段名称,而不是表名,如果写成了表名就会报 unsupported relations for schema 谬误。

type User struct {
  gorm.Model
  Username string
  Orders   []Order}

type Order struct {
  gorm.Model
  UserID uint
  Price  float64
}

// 查找 user 时预加载相干 Order
db.Preload("Orders").Find(&users)
// SELECT * FROM users;
// SELECT * FROM orders WHERE user_id IN (1,2,3,4);
退出移动版