簡體   English   中英

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行上panic: pg: can't find dst value for model id="," 在嘗試深入研究時,我發現' 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
}

在這里閱讀更多。

這是v6.4.6中修復的錯誤。 這是相關的問題-https://github.com/go-pg/pg/issues/583

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM