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

1.3k Views Asked by At

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

There are 2 best solutions below

0
On BEST ANSWER

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

2
On

You should be able to use the sql tag to override the default column names.

type GrProductCategoryMapping struct {
    tableName  struct{} `sql:"gr_product_category_mapping"`
    ID         int64
    ProductID  int64 `sql:"product_id"`
    CategoryID int   `sql:"category_id"`
    IsPrimary  bool
}

Read more here.