go-pg "belongs to" 2 个字段相同 table
go-pg "belongs to" 2 fields to same table
我正在使用 https://github.com/go-pg/pg 来处理这些东西,在制作基本属于关系时遇到了很大的问题。
所以基本上我有一个 table,其中包含列 receipient_id 和 sender_id,两者都指向相同的用户 table。这是我的代码和结果:
type Transaction struct {
Id int
Receipient *User `json:"receipient_id" sql:"-" validate:"required"`
Sender *User `json:"sender_id" sql:"-" validate:"required"`
TransactionType int `json:"transaction_type" validate:"required"`
Status int `json:"status" validate:"required"`
Ammount int `json:"ammount" validate:"required"`
CreatedAt string `json:"created_at" validate:"required"`
}
err = database.DBCon.Model(&transactions).Select()
if err != nil {
return transactions, err
}
16:51:15.754 V1TransactionListPOST ▶ ERRO 006 supppers [Transaction<id:1 receipient:<nil> sender:<nil> transaction_type:0 status:1 ammount:100 created_at:1533650239
]
2018/08/07 16:51:15 http: panic serving [::1]:50157: runtime error: invalid memory address or nil pointer dereference
goroutine 5 [running]:
net/http.(*conn).serve.func1(0xc42036fd60)
...
这是实际的数据库:
SELECT * FROM transactions;
-[ RECORD 1 ]----+-----------
id | 1
receipient_id | 23
sender_id | 24
transaction_type | 0
status | 1
ammount | 100
created_at | 1533650239
SELECT * FROM users;
-[ RECORD 1 ]---------+-------------------------------------------------------------
id | 23
email | testss@gmail.com
username | damn here
password | a$ABBHfgwhnepqKitBoiQ2bOcPfGjUIjio33uL52R88Lk2XhcfGwn26
user_type | 2
profile_id | 1
status | 2
confirm_token |
recover_token |
recover_token_expiry |
recover_secret_answer | a$xPwZk2LanxXf1lvCvp29IuyHav88m5dGXo8Ao0f5A1aV1zwh5t6le
-[ RECORD 2 ]---------+-------------------------------------------------------------
id | 24
email | anotehrtsts@gmail.com
username | removeusername
password | atmCvZy5xo5zGz9C3g6YeeV8Wb149VwSTFhT1pcjb0zVasT.fbTKy
user_type | 2
profile_id | 1
status | 2
confirm_token |
recover_token |
recover_token_expiry |
recover_secret_answer | a$CeS5qgITVGBgWX7cumhbdOoAPOhg/Lc2NXDu.nY5RzK3PGCW26Ik2
我忘了添加 Id 列,这是一个工作示例:
type Transaction struct {
Id int
RecipientId int `json:"recipient_id"`
Recipient *User `pg:"fk:recipient_id"`
SenderId int `json:"sender_id"`
Sender *User `pg:"fk:sender_id"`
TransactionType int `json:"transaction_type" validate:"required"`
Status int `json:"status" validate:"required"`
Ammount int `json:"ammount" validate:"required"`
CreatedAt string `json:"created_at" validate:"required"`
}
我正在使用 https://github.com/go-pg/pg 来处理这些东西,在制作基本属于关系时遇到了很大的问题。
所以基本上我有一个 table,其中包含列 receipient_id 和 sender_id,两者都指向相同的用户 table。这是我的代码和结果:
type Transaction struct {
Id int
Receipient *User `json:"receipient_id" sql:"-" validate:"required"`
Sender *User `json:"sender_id" sql:"-" validate:"required"`
TransactionType int `json:"transaction_type" validate:"required"`
Status int `json:"status" validate:"required"`
Ammount int `json:"ammount" validate:"required"`
CreatedAt string `json:"created_at" validate:"required"`
}
err = database.DBCon.Model(&transactions).Select()
if err != nil {
return transactions, err
}
16:51:15.754 V1TransactionListPOST ▶ ERRO 006 supppers [Transaction<id:1 receipient:<nil> sender:<nil> transaction_type:0 status:1 ammount:100 created_at:1533650239
]
2018/08/07 16:51:15 http: panic serving [::1]:50157: runtime error: invalid memory address or nil pointer dereference
goroutine 5 [running]:
net/http.(*conn).serve.func1(0xc42036fd60)
...
这是实际的数据库:
SELECT * FROM transactions;
-[ RECORD 1 ]----+-----------
id | 1
receipient_id | 23
sender_id | 24
transaction_type | 0
status | 1
ammount | 100
created_at | 1533650239
SELECT * FROM users;
-[ RECORD 1 ]---------+-------------------------------------------------------------
id | 23
email | testss@gmail.com
username | damn here
password | a$ABBHfgwhnepqKitBoiQ2bOcPfGjUIjio33uL52R88Lk2XhcfGwn26
user_type | 2
profile_id | 1
status | 2
confirm_token |
recover_token |
recover_token_expiry |
recover_secret_answer | a$xPwZk2LanxXf1lvCvp29IuyHav88m5dGXo8Ao0f5A1aV1zwh5t6le
-[ RECORD 2 ]---------+-------------------------------------------------------------
id | 24
email | anotehrtsts@gmail.com
username | removeusername
password | atmCvZy5xo5zGz9C3g6YeeV8Wb149VwSTFhT1pcjb0zVasT.fbTKy
user_type | 2
profile_id | 1
status | 2
confirm_token |
recover_token |
recover_token_expiry |
recover_secret_answer | a$CeS5qgITVGBgWX7cumhbdOoAPOhg/Lc2NXDu.nY5RzK3PGCW26Ik2
我忘了添加 Id 列,这是一个工作示例:
type Transaction struct {
Id int
RecipientId int `json:"recipient_id"`
Recipient *User `pg:"fk:recipient_id"`
SenderId int `json:"sender_id"`
Sender *User `pg:"fk:sender_id"`
TransactionType int `json:"transaction_type" validate:"required"`
Status int `json:"status" validate:"required"`
Ammount int `json:"ammount" validate:"required"`
CreatedAt string `json:"created_at" validate:"required"`
}