首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >go-pg - pg:找不到模型id=的DST值“,”

go-pg - pg:找不到模型id=的DST值“,”
EN

Stack Overflow用户
提问于 2017-07-19 16:37:07
回答 2查看 965关注 0票数 0

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

我定义了以下模型

代码语言:javascript
复制
// 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"`
}

在尝试这个时-

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

以下是所做的查询

代码语言:javascript
复制
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更深入地挖掘时,我发现‘前缀’m.baseTable.ModelName+"_"的计算结果是gr_product_,但实际上可能应该是product_,因为columns包含

代码语言:javascript
复制
map[string]string [
        "product_id": "10",
        "category_id": "48",
        "is_primary": "t",
]

我还没有弄清楚如何覆盖这个默认行为(对于Golang和go-pg来说都是新的),任何帮助都将不胜感激,谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-07-20 17:09:24

这是一个错误,已在v6.4.6中修复。下面是相关的问题- https://github.com/go-pg/pg/issues/583

票数 0
EN

Stack Overflow用户

发布于 2017-07-19 17:34:04

您应该能够使用sql标记来覆盖默认的列名。

代码语言:javascript
复制
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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45185191

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档