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

相关推荐 更多相似问题