duandie0921 2018-08-08 08:10
浏览 32
已采纳

go-pg“属于”同一表的2个字段

I am using https://github.com/go-pg/pg to handle this stuff, having huge issues making a basic belongs to relation.

So basically i have one table that has columns receipient_id and sender_id, both point to the same users table. This is my code and result:

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)
 ...

And this is the actual db:

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              | $2a$10$ABBHfgwhnepqKitBoiQ2bOcPfGjUIjio33uL52R88Lk2XhcfGwn26
user_type             | 2
profile_id            | 1
status                | 2
confirm_token         |
recover_token         |
recover_token_expiry  |
recover_secret_answer | $2a$10$xPwZk2LanxXf1lvCvp29IuyHav88m5dGXo8Ao0f5A1aV1zwh5t6le
-[ RECORD 2 ]---------+-------------------------------------------------------------
id                    | 24
email                 | anotehrtsts@gmail.com
username              | removeusername
password              | $2a$10$1tmCvZy5xo5zGz9C3g6YeeV8Wb149VwSTFhT1pcjb0zVasT.fbTKy
user_type             | 2
profile_id            | 1
status                | 2
confirm_token         |
recover_token         |
recover_token_expiry  |
recover_secret_answer | $2a$10$CeS5qgITVGBgWX7cumhbdOoAPOhg/Lc2NXDu.nY5RzK3PGCW26Ik2
  • 写回答

1条回答 默认 最新

  • doutan3040 2018-08-10 17:31
    关注

    I forgot to add Id columns, here's a working example:

    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"`
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊