{"id":151989,"date":"2020-09-01T15:05:08","date_gmt":"2020-09-01T07:05:08","guid":{"rendered":"http:\/\/4563.org\/?p=151989"},"modified":"2020-09-01T15:05:08","modified_gmt":"2020-09-01T07:05:08","slug":"gorm-%e8%87%aa%e5%ae%9a%e4%b9%89%e9%a2%84%e5%8a%a0%e8%bd%bd%e7%9a%84-%e6%ad%a3%e7%a1%ae%e7%94%a8%e6%b3%95%e6%98%af%e4%bb%80%e4%b9%88%ef%bc%9f%e4%b8%80%e7%9b%b4%e6%8a%a5%e9%94%99%ef%bc%9a-invalid-query","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=151989","title":{"rendered":"gorm \u81ea\u5b9a\u4e49\u9884\u52a0\u8f7d\u7684 \u6b63\u786e\u7528\u6cd5\u662f\u4ec0\u4e48\uff1f\u4e00\u76f4\u62a5\u9519\uff1a invalid query condition"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  gorm \u81ea\u5b9a\u4e49\u9884\u52a0\u8f7d\u7684 \u6b63\u786e\u7528\u6cd5\u662f\u4ec0\u4e48\uff1f\u4e00\u76f4\u62a5\u9519\uff1a invalid query condition               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ysongyang <\/span>  <span><i><\/i> 4<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<pre><code>\/\/ \u9996\u9875\u6d3b\u52a8\u6a21\u5757\u533a\u57df\u5206\u7c7b\u8868\u6a21\u578b type LabelType struct {     Model     Id           int    `json:\"id\";gorm:\"primary_key\"`     MallId       int    `json:\"mall_id\"`     Name         string `json:\"name\"`     Weigh        int    `json:\"weigh\"`  \/\/ \u6743\u91cd     GoodsList []Goods `gorm:\"ForeignKey:LabelId\" json:\"goods_list\"` \/\/\u67e5\u8be2\u5f53\u524d\u5206\u7c7b\u4e0b\u7684\u5546\u54c1\u96c6\u5408 } <\/code><\/pre>\n<pre><code>type Goods struct {     Model     GoodsId         int               `json:\"goods_id\" gorm:\"primary_key\"`     MallId          int               `json:\"mall_id\"`     \/\/ \u5546\u57ce id     LabelId         int               `json:\"label_id\"`    \/\/ \u5173\u8054 mall_label_type     GoodsName       string            `json:\"goods_name\"`  \/\/ \u5546\u54c1\u540d\u79f0     CategoryId      int               `json:\"category_id\"` \/\/ \u5206\u7c7b id     SmallImage      string            `json:\"small_image\"` \/\/ \u5546\u54c1\u7f29\u7565\u56fe     Images          string            `json:\"images\"`     Category        Category `gorm:\"foreignkey:CategoryID\" json:\"category\"` \/\/\u5206\u7c7b\u8868 } <\/code><\/pre>\n<pre><code>func (labelModel *LabelType) GetAll(params *request.IndexParams) (labelTypes []*LabelType) { err := db.Debug().Model(&amp;labelTypes).         Preload(\"GoodsList\", func(query *gorm.DB) *gorm.DB {             return query.Order(\"goods_id desc\")         }).         Preload(\"GoodsList.Category\").         Where(\"mall_id = ? and status = ?\", params.MallId, \"normal\").Order(\"weigh desc\").         Find(&amp;labelTypes).Error     if err != nil &amp;&amp; err != gorm.ErrRecordNotFound {         return nil     }     return labelTypes } <\/code><\/pre>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>7<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"3178893\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : ysongyang <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             LabelType \u4ee3\u7801\u8865\u5168<br \/>&#8220;`<br \/>\/\/ \u9996\u9875\u6d3b\u52a8\u6a21\u5757\u533a\u57df\u5206\u7c7b\u8868\u6a21\u578b<br \/>type LabelType struct {<br \/> Model<br \/> Id int `gorm:&#8221;primary_key&#8221; json:&#8221;id&#8221;`<br \/> MallId int `json:&#8221;mall_id&#8221;`<br \/> Name string `json:&#8221;name&#8221;`<br \/> Images string `json:&#8221;images&#8221;` \/\/ \u80cc\u666f\u56fe<br \/> Status string `json:&#8221;-&#8220;` \/\/ normal \u5f00\u542f hidden \u5173\u95ed<br \/> Weigh int `json:&#8221;weigh&#8221;` \/\/ \u6743\u91cd\uff0c \u8d8a\u5927\u8d8a\u9760\u524d<br \/> CreateTime int `json:&#8221;-&#8220;`<br \/> UpdateTime int `json:&#8221;-&#8220;`<br \/> GoodsList []LitestoreGoods `gorm:&#8221;ForeignKey:LabelId&#8221; json:&#8221;goods_list&#8221;` \/\/\u67e5\u8be2\u5f53\u524d\u5206\u7c7b\u4e0b\u7684\u5546\u54c1\u96c6\u5408<br \/>}<br \/>&#8220;`                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3178894\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : ysongyang <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u52a0 func(query gorm.DB) gorm.DB { return query.Order(&#8220;goods_id desc&#8221;)} \u5c31\u4f1a\u62a5\u9519<br \/>invalid query condition:<br \/>\u4e0d\u52a0\u8fd9\u4e2a\u662f\u53ef\u4ee5\u6b63\u5e38\u67e5\u8be2\u7684                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3178895\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : EileenJ <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u53bb\u6389 Model(&amp;labelTypes)\u8bd5\u4e0b                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3178896\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : ysongyang <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @EileenJ \u6ca1\u7528\u7684\u3002\u80fd\u4f7f\u7684\u65b9\u6cd5\u90fd\u8bd5\u8fc7\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3178897\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Carlgao <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4f60\u7684 ForeignKey:LabelId \u5199\u9519\u4e86\u5427\uff0c\u96be\u9053\u4e0d\u662f\u5e94\u8be5\u662f GoodId \u5417\uff0c\u7136\u800c\u5728\u4f60\u7684 LabelType \u7ed3\u6784\u4f53\u91cc\u9762\u6ca1\u6709\u770b\u5230\u8fd9\u4e2a\u5b57\u6bb5                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3178898\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : ysongyang <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Carlgao \u6211\u67e5\u8be2\u7684\u662f \u6a21\u5757\u5206\u7c7b\u5217\u8868\uff0c\u7136\u540e\u5217\u51fa \u6a21\u5757\u5206\u7c7b\u4e0b\u7684\u5546\u54c1\u96c6\u5408\uff0c\u6ca1\u6bdb\u75c5\u5427\u3002\u800c\u4e14\u6211\u4e5f\u8bf4\u4e86\u5440 \u52a0 func(query gorm.DB) gorm.DB { return query.Order(&#8220;goods_id desc&#8221;)} \u5c31\u4f1a\u62a5\u9519\uff0c \u4e0d\u52a0\u8fd9\u4e2a \u662f\u53ef\u4ee5\u6b63\u5e38\u67e5\u51fa\u6765\u6211\u60f3\u8981\u7684\u6570\u636e\u7684\u5440\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3178899\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : ysongyang <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u95ee\u9898\u5df2\u89e3\u51b3\uff01                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>gorm \u81ea\u5b9a\u4e49\u9884\u52a0\u8f7d\u7684 \u6b63\u786e\u7528\u6cd5&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/151989"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=151989"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/151989\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=151989"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=151989"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=151989"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}