```go
type User struct {
BaseModel
Name string `gorm:"type:varchar(20);default:'卢本伟'"`
Phone string `gorm:"index:idx_phone;unique;type:varchar(11);not null"`
Avatar string `gorm:"type:varchar(100);default:'/static/images/login/logo.jpg'"`
//PassWord string `gorm:"type:varchar(100);not null;column:password"`
Gender string `gorm:"column:gender;default:male;type:varchar(6) comment 'female表示女,male表示男'"`
Role uint8 `gorm:"column:role;default 1;type:int comment '1表示普通用户,2表示超级管理员'"`
Articles []Article `gorm:"foreignKey:UserID"`
Comments []Comment `gorm:"foreignKey:UserID"`
Upvotes []Upvote `gorm:"foreignKey:UserID"`
Collects []Collect `gorm:"foreignKey:UserID"`
Messages []Message `gorm:"foreignKey:UserID"`
}
type Message struct {
BaseModel
Content string `gorm:"type:varchar(255)"`
UserID uint32
MessageNotice []MessageNotice `gorm:"foreignKey:MessageID"`
}
初学gorm,请教一下 查所有message的时候怎么反向查询连User信息一并查询出来。
{
"ID": 1,
"CreatedAt": "2021-10-09T18:29:26+08:00",
"UpdatedAt": "2021-10-09T18:29:28+08:00",
"DeletedAt": null,
"IsDeleted": false,
"Content": "哈哈哈哈",
"UserID": 6,
"MessageNotice": null
}
```
有疑问加站长微信联系(非本文作者)