0

I am using GORM in combination with Fiber. When querying all users with preloaded clause.Associations I get the following error:

can't preload field @@@as@@@ for entities.User

What does that mean? Without the preload of clause.Associations it works normally and but it does not show one-to-one associations.

func UsersGetAll(c *fiber.Ctx) error {
    db := database.DBConn
    users := []entities.User{}
    records := db.Preload(clause.Associations).Find(&users)
    if records.Error != nil {
        log.Println(records.Error)
        return c.Status(500).SendString(records.Error.Error())
    }

    return c.JSON(records.Value)
}
4

1 回答 1

0

看起来像gorm标签的问题:我想结构是这样的:

type Address struct {
ID int `gorm:"column:id;primaryKey"`
}
type User struct {
Name string `gorm:"column:name"`
Age string `gorm:"column:age"`
Address Address `gorm:"references:ID"`
}

和代码:

user:=User{}
if err:=db.Preload("Address").Find(&user).Error {
panic(err)
}

我认为你也应该看看这个:Gorm 关系错误:需要为关系定义一个有效的外键或者它需要实现 Valuer/Scanner 接口

于 2021-07-28T16:21:02.907 回答