ds0409 2017-07-19 08:37
浏览 50
已采纳

go-pg-pg:找不到型号id =“,”的dst值

getting pg: can't find dst value for model id=","

I have defined the following models

// omitting fields which don't seem relevant to the issue
// corresponding queries also shortened as appropriate
type GrProduct struct {
    tableName        struct{} `sql:"gr_product"`
    ID               int64
    Name             string
// fk:Product,joinFK:Category given so that joins are made on category_id and product_id with gr_product_category_mapping
    Categories       []*GrCategory               `pg:",many2many:gr_product_category_mapping,fk:Product,joinFK:Category"`
    CategoryMappings []*GrProductCategoryMapping `pg:",fk:Product"`
}

type GrProductCategoryMapping struct {
    tableName  struct{} `sql:"gr_product_category_mapping"`
    ID         int64
    ProductID  int64 // product_id in db instead of gr_product_id
    CategoryID int // category id in db instead of gr_category_id
    IsPrimary  bool
}

type GrCategory struct {
    tableName                struct{} `sql:"gr_category"`
    ID                       int
    Name                     string
    Products                 []*GrProduct `pg:",many2many:gr_product_category_mapping,fk:Category,joinFK:Product"`
}

on trying this -

p := models.GrProduct{}
if err := models.DB.Model(&p).
    Column("Categories").
    Where("id = ?", 10).
    Select(); err != nil {
    panic(err)
}

These are the queries that are made

SELECT
    "gr_product"."id",
    "gr_product"."name"  
FROM
    gr_product AS "gr_product" 
WHERE
    (
        id= 10
    );

SELECT
    gr_product_category_mapping.*,
    "gr_category"."id",
    "gr_category"."name"

FROM
    gr_category AS "gr_category" 
JOIN
    gr_product_category_mapping AS gr_product_category_mapping 
        ON (
            gr_product_category_mapping."product_id"
        ) IN (
            (
                10
            )
        ) 
WHERE
    (
        "gr_category"."id" = gr_product_category_mapping."category_id"
    );

I get panic: pg: can't find dst value for model id="," on line https://github.com/go-pg/pg/blob/master/orm/model_table_m2m.go#L53 I think. On trying to dig deeper with delve I found that that the 'prefix' m.baseTable.ModelName+"_" evaluates to gr_product_, but instead probably should be product_, since columns contains

map[string]string [
        "product_id": "10",
        "category_id": "48",
        "is_primary": "t",
]

I haven't been able to figure out how to override this default behaviour(new to both Golang and go-pg), any help would be appreciated, thanks

  • 写回答

2条回答 默认 最新

  • duandanbeng1829 2017-07-20 09:09
    关注

    this was a bug in which is fixed in v6.4.6. here's the relevant issue - https://github.com/go-pg/pg/issues/583

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题