{"id":158884,"date":"2020-09-18T12:23:58","date_gmt":"2020-09-18T04:23:58","guid":{"rendered":"http:\/\/4563.org\/?p=158884"},"modified":"2020-09-18T12:23:58","modified_gmt":"2020-09-18T04:23:58","slug":"%e8%af%b7%e9%97%ae-gorm-%e5%a6%82%e4%bd%95%e8%8e%b7%e5%8f%96%e5%88%b0%e6%9b%b4%e6%96%b0%e5%90%8e%e7%9a%84%e5%80%bc","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=158884","title":{"rendered":"\u8bf7\u95ee gorm \u5982\u4f55\u83b7\u53d6\u5230\u66f4\u65b0\u540e\u7684\u503c"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u8bf7\u95ee gorm \u5982\u4f55\u83b7\u53d6\u5230\u66f4\u65b0\u540e\u7684\u503c               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : swxc <\/span>  <span><i><\/i> 3<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<pre><code>type Counter struct {  BizName   string `gorm:\"primary_key\"`  NextId    string  CreatedAt time.Time }   counter := model.Counter{BizName: \"class\"}  err = d.Model(&amp;counter).Update(ctx, \"next_id\", gorm.Expr(\"next_id  + ?\",1)).Error  <\/code><\/pre>\n<p>\u60f3\u5b9e\u73b0\u4e00\u4e2a\u7b80\u6613\u7684\u53d1\u53f7\u5668\uff0c\u60f3\u5229\u7528 mysql update \u7684\u539f\u5b50\u6027\uff0c\u66f4\u65b0 NextId \u5e76\u83b7\u53d6\u5230\u66f4\u65b0\u540e\u7684\u503c\uff0c\u8bf7\u95ee\u5982\u4f55\u641e\u5b9a<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>4<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"3409596\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : wleven <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             d.Model(&amp;counter).Update(ctx, &#8220;next_id&#8221;, gorm.Expr(&#8220;next_id + ?&#8221;,1)).Find(&amp;counter).Error                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3409597\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : crclz <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5229\u7528 postgresql \u7684 Returning \u5173\u952e\u5b57\u3002mysql \u6216\u8005\u5176\u4ed6\u4f30\u8ba1\u4e5f\u6709\u5bf9\u5e94\u7684\u89e3\u51b3\u65b9\u6848\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3409598\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ghjacky <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             d.Model(&amp;counter).Debug().Update(ctx, &#8220;next_id&#8221;, gorm.Expr(&#8220;next_id + ?&#8221;,1))                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3409599\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : xuegj1010 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             d.Model(&amp;counter).Update(ctx, &#8220;next_id&#8221;, gorm.Expr(&#8220;next_id + ?&#8221;,1)).Find(&amp;counter).Scan(&amp;counter).Error                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u8bf7\u95ee gorm \u5982\u4f55\u83b7\u53d6\u5230\u66f4\u65b0\u540e\u7684&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\/158884"}],"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=158884"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/158884\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=158884"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=158884"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=158884"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}