go-pg - pg:找不到模型 id="," 的 dst 值
go-pg - pg: can't find dst value for model id=","
得到pg: can't find dst value for model id=","
我定义了以下模型
// 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"`
}
尝试这个 -
p := models.GrProduct{}
if err := models.DB.Model(&p).
Column("Categories").
Where("id = ?", 10).
Select(); err != nil {
panic(err)
}
这些是进行的查询
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"
);
我想 panic: pg: can't find dst value for model id=","
在线 https://github.com/go-pg/pg/blob/master/orm/model_table_m2m.go#L53。
在尝试使用 delve 进行更深入的挖掘时,我发现 'prefix' m.baseTable.ModelName+"_"
的计算结果为 gr_product_
,但可能应该是 product_
,因为 columns
包含
map[string]string [
"product_id": "10",
"category_id": "48",
"is_primary": "t",
]
我一直无法弄清楚如何覆盖此默认行为(Golang 和 go-pg 的新行为),我们将不胜感激,谢谢
您应该能够使用 sql
标记来覆盖默认列名。
type GrProductCategoryMapping struct {
tableName struct{} `sql:"gr_product_category_mapping"`
ID int64
ProductID int64 `sql:"product_id"`
CategoryID int `sql:"category_id"`
IsPrimary bool
}
阅读更多here。
这是一个错误,已在 v6.4.6 中修复。
这是相关问题 - https://github.com/go-pg/pg/issues/583
得到pg: can't find dst value for model id=","
我定义了以下模型
// 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"`
}
尝试这个 -
p := models.GrProduct{}
if err := models.DB.Model(&p).
Column("Categories").
Where("id = ?", 10).
Select(); err != nil {
panic(err)
}
这些是进行的查询
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"
);
我想 panic: pg: can't find dst value for model id=","
在线 https://github.com/go-pg/pg/blob/master/orm/model_table_m2m.go#L53。
在尝试使用 delve 进行更深入的挖掘时,我发现 'prefix' m.baseTable.ModelName+"_"
的计算结果为 gr_product_
,但可能应该是 product_
,因为 columns
包含
map[string]string [
"product_id": "10",
"category_id": "48",
"is_primary": "t",
]
我一直无法弄清楚如何覆盖此默认行为(Golang 和 go-pg 的新行为),我们将不胜感激,谢谢
您应该能够使用 sql
标记来覆盖默认列名。
type GrProductCategoryMapping struct {
tableName struct{} `sql:"gr_product_category_mapping"`
ID int64
ProductID int64 `sql:"product_id"`
CategoryID int `sql:"category_id"`
IsPrimary bool
}
阅读更多here。
这是一个错误,已在 v6.4.6 中修复。 这是相关问题 - https://github.com/go-pg/pg/issues/583