Go-Gorm 有一个 Child Table 总是空的
Go-Gorm has one Child Table Always Empty
我尝试用 Golang 开发 Restfull API,
但我对 gorm 关系有疑问 has-one.
我是golang新手go-gorm,我按照官网的struct例子https://gorm.io/docs/has_one.html
这里是代码片段:
type Server struct {
DB *gorm.DB
Router *mux.Router
}
type CreditCard struct {
gorm.Model
Number string
UserID uint
}
type User struct {
gorm.Model
Name string
Card CreditCard
}
func (u *User) FindUsersWithRelation(db *gorm.DB) (*[]User, error) {
var err error
users := []User{}
err = db.Debug().Model(&User{}).Limit(100).Find(&users).Error
if err != nil {
return &[]User{}, err
}
return &users, err
}
/============
func (s *Server) GetUsersWithRelation(w http.ResponseWriter, r *http.Request) {
user := models.User{}
users, err := user.FindUsersWithRelation(s.DB)
if err != nil {
responses.ERROR(w, http.StatusInternalServerError, err)
return
}
responses.JSON(w, http.StatusOK, users)
}
//============
func (s *Server) InitializeRoutes() {
s.Router.HandleFunc("/test", middlewares.SetMiddlewareJSON(s.GetUsersWithRelation)).Methods("GET")
}
用户:
+----+---------------------+---------------------+------------+------+
| id | created_at | updated_at | deleted_at | name |
+----+---------------------+---------------------+------------+------+
| 1 | 2020-01-22 11:51:42 | 2020-01-22 11:51:42 | NULL | John |
| 2 | 2020-01-22 11:51:43 | 2020-01-22 11:51:43 | NULL | Ann |
+----+---------------------+---------------------+------------+------+
credit_cards:
+----+---------------------+---------------------+------------+------------------+---------+
| id | created_at | updated_at | deleted_at | number | user_id |
+----+---------------------+---------------------+------------+------------------+---------+
| 1 | 2020-01-22 11:51:42 | 2020-01-22 11:51:42 | NULL | 123-456-90123 | 1 |
| 2 | 2020-01-22 11:51:43 | 2020-01-22 11:51:43 | NULL | 098-765-432-1010 | 2 |
+----+---------------------+---------------------+------------+------------------+---------+
这是从 Postman 获取测试端点的结果:
[
{
"ID": 1,
"CreatedAt": "2020-01-22T11:51:42+07:00",
"UpdatedAt": "2020-01-22T11:51:42+07:00",
"DeletedAt": null,
"Name": "John",
"Card": {
"ID": 0,
"CreatedAt": "0001-01-01T00:00:00Z",
"UpdatedAt": "0001-01-01T00:00:00Z",
"DeletedAt": null,
"Number": "",
"UserID": 0
}
},
{
"ID": 2,
"CreatedAt": "2020-01-22T11:51:43+07:00",
"UpdatedAt": "2020-01-22T11:51:43+07:00",
"DeletedAt": null,
"Name": "Ann",
"Card": {
"ID": 0,
"CreatedAt": "0001-01-01T00:00:00Z",
"UpdatedAt": "0001-01-01T00:00:00Z",
"DeletedAt": null,
"Number": "",
"UserID": 0
}
}
]
为什么每个用户中的名片总是空的?
如果你想获取相关的东西,你需要使用preload:
db.Model(&User{}).Limit(100).Preload("CreditCards").Find(&users)
它返回的是空的,因为您没有获取数据,Go 将默认使用结构的 zero value。
我尝试用 Golang 开发 Restfull API, 但我对 gorm 关系有疑问 has-one.
我是golang新手go-gorm,我按照官网的struct例子https://gorm.io/docs/has_one.html
这里是代码片段:
type Server struct {
DB *gorm.DB
Router *mux.Router
}
type CreditCard struct {
gorm.Model
Number string
UserID uint
}
type User struct {
gorm.Model
Name string
Card CreditCard
}
func (u *User) FindUsersWithRelation(db *gorm.DB) (*[]User, error) {
var err error
users := []User{}
err = db.Debug().Model(&User{}).Limit(100).Find(&users).Error
if err != nil {
return &[]User{}, err
}
return &users, err
}
/============
func (s *Server) GetUsersWithRelation(w http.ResponseWriter, r *http.Request) {
user := models.User{}
users, err := user.FindUsersWithRelation(s.DB)
if err != nil {
responses.ERROR(w, http.StatusInternalServerError, err)
return
}
responses.JSON(w, http.StatusOK, users)
}
//============
func (s *Server) InitializeRoutes() {
s.Router.HandleFunc("/test", middlewares.SetMiddlewareJSON(s.GetUsersWithRelation)).Methods("GET")
}
用户:
+----+---------------------+---------------------+------------+------+
| id | created_at | updated_at | deleted_at | name |
+----+---------------------+---------------------+------------+------+
| 1 | 2020-01-22 11:51:42 | 2020-01-22 11:51:42 | NULL | John |
| 2 | 2020-01-22 11:51:43 | 2020-01-22 11:51:43 | NULL | Ann |
+----+---------------------+---------------------+------------+------+
credit_cards:
+----+---------------------+---------------------+------------+------------------+---------+
| id | created_at | updated_at | deleted_at | number | user_id |
+----+---------------------+---------------------+------------+------------------+---------+
| 1 | 2020-01-22 11:51:42 | 2020-01-22 11:51:42 | NULL | 123-456-90123 | 1 |
| 2 | 2020-01-22 11:51:43 | 2020-01-22 11:51:43 | NULL | 098-765-432-1010 | 2 |
+----+---------------------+---------------------+------------+------------------+---------+
这是从 Postman 获取测试端点的结果:
[
{
"ID": 1,
"CreatedAt": "2020-01-22T11:51:42+07:00",
"UpdatedAt": "2020-01-22T11:51:42+07:00",
"DeletedAt": null,
"Name": "John",
"Card": {
"ID": 0,
"CreatedAt": "0001-01-01T00:00:00Z",
"UpdatedAt": "0001-01-01T00:00:00Z",
"DeletedAt": null,
"Number": "",
"UserID": 0
}
},
{
"ID": 2,
"CreatedAt": "2020-01-22T11:51:43+07:00",
"UpdatedAt": "2020-01-22T11:51:43+07:00",
"DeletedAt": null,
"Name": "Ann",
"Card": {
"ID": 0,
"CreatedAt": "0001-01-01T00:00:00Z",
"UpdatedAt": "0001-01-01T00:00:00Z",
"DeletedAt": null,
"Number": "",
"UserID": 0
}
}
]
为什么每个用户中的名片总是空的?
如果你想获取相关的东西,你需要使用preload:
db.Model(&User{}).Limit(100).Preload("CreditCards").Find(&users)
它返回的是空的,因为您没有获取数据,Go 将默认使用结构的 zero value。