英文:
go-pg - pg: can't find dst value for model id=","
问题
获取pg: can't find dst value for model id=","
我定义了以下模型
// 省略不相关的字段
// 相应的查询也适当缩短
type GrProduct struct {
tableName struct{} sql:"gr_product"
ID int64
Name string
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 而不是 gr_product_id
CategoryID int // 在数据库中是 category id 而不是 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"
);
我在 https://github.com/go-pg/pg/blob/master/orm/model_table_m2m.go#L53 的这一行上得到了 panic: pg: can't find dst value for model id=","
。尝试使用 delve 进一步调试时,我发现 'prefix' m.baseTable.ModelName+"_"
的计算结果为 gr_product_
,但实际上可能应该是 product_
,因为 columns
包含
map[string]string [
"product_id": "10",
"category_id": "48",
"is_primary": "t",
]
我还没有找到如何覆盖这种默认行为的方法(对 Golang 和 go-pg 都不熟悉),希望能得到帮助,谢谢。
英文:
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
答案1
得分: 1
你应该可以使用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
}
在这里阅读更多信息:链接。
英文:
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.
答案2
得分: 1
这是一个在v6.4.6版本中修复的错误。
这是相关问题的链接-https://github.com/go-pg/pg/issues/583
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论