{"id":156971,"date":"2020-09-13T07:53:50","date_gmt":"2020-09-12T23:53:50","guid":{"rendered":"http:\/\/4563.org\/?p=156971"},"modified":"2020-09-13T07:53:50","modified_gmt":"2020-09-12T23:53:50","slug":"gorm-%e5%a6%82%e4%bd%95%e5%ad%98%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=156971","title":{"rendered":"gorm \u5982\u4f55\u5b58\u6570\u7ec4"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  gorm \u5982\u4f55\u5b58\u6570\u7ec4               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : richzhu <\/span>  <span><i><\/i> 11<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u5404\u4f4d\u5927\u4f6c\uff0c\u8bf7\u95ee\u5982\u4f55\u4f7f\u7528 gorm \u4fdd\u5b58\u5207\u7247\uff0c\u4e0b\u9762\u662f\u6211\u51fa\u9519\u7684\u4ee3\u7801<\/p>\n<h3>\u7ed3\u6784\u4f53<\/h3>\n<pre><code>type Node struct {  ID     int    `json:\"id\"`  Name   string `json:\"name\"`  Author string `json:\"author\"`  IP   []string `json:\"ip\"` } <\/code><\/pre>\n<h3>Gorm \u4fdd\u5b58<\/h3>\n<pre><code>ips:=[]string{\"1.1.1.1\",\"2.2.2.2\",\"3.3.3.3\"} body := Node{Name: \"testName\", Author: \"testAuthor\", Node: ips} if err := DB.Create(&amp;body).Error; err != nil {  return false } <\/code><\/pre>\n<h3>\u5f97\u5230\u62a5\u9519<\/h3>\n<pre><code>unsupported data type: &amp;[] <\/code><\/pre>\n<p>\u6211\u60f3\u5b58\u5165\u6570\u636e\u5e93\u7684\u6570\u636e\u5f62\u5f0f\u662f\uff1a<\/p>\n<pre><code>\"1.1.1.1\",\"2.2.2.2\",\"3.3.3.3\" <\/code><\/pre>\n<p>\u524d\u7aef\u53d1\u6765\u7684\u6570\u636e\uff0c\u53ea\u80fd\u662f\u4e00\u4e2a\u6570\u7ec4\u5305\u542b\u7740 ip\uff0c\u8fd8\u4e0d\u80fd\u6539\u3002\u3002\u3002 \u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u95ee\u4e0b\u5927\u4f6c\u4eec\u6211\u8be5\u548b\u6574 T_T<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>10<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"3272359\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zgw0 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6211\u4e4b\u524d\u7684\u505a\u6cd5\u662f\u8f6c\u4e86\u5b57\u7b26\u4e32\u5b58\u8d77\u6765\uff0c\u7136\u540e\u7528\u7684\u65f6\u5019\u518d\u8f6c\u56de\u6570\u7ec4                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3272360\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : z0wjqnxi <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             &#8220;` golang<br \/>type Node struct {<br \/> &#8230;<br \/> IPList ipList `json:&#8221;ip&#8221; binding:\u201dgt=0,dive,ipv4\u201c ` <br \/>}<\/p>\n<p>type ipList []string<\/p>\n<p>\/\/ gorm \u81ea\u5b9a\u4e49\u7ed3\u6784\u9700\u8981\u5b9e\u73b0 Value Scan \u4e24\u4e2a\u65b9\u6cd5<br \/>\/\/ Value \u5b9e\u73b0\u65b9\u6cd5<br \/>func (p ipList ) Value() (driver.Value, error) {<br \/> return json.Marshal(p)<br \/>}<\/p>\n<p>\/\/ Scan \u5b9e\u73b0\u65b9\u6cd5<br \/>func (p *ipList ) Scan(data interface{}) error {<br \/> return json.Unmarshal(data.([]byte), &amp;p)<br \/>}<\/p>\n<p>\u81ea\u5b9a\u4e49\u7ed3\u6784\u6211\u662f\u8fd9\u4e48\u5b9e\u73b0\u7684                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3272361\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : richzhu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @zgw0 \u8c22\u8c22\u8001\u54e5\uff0c\u54b1\u4fe9\u60f3\u7684\u4e00\u6837\uff0c\u4f46\u662f\u6211\u8fd9\u60c5\u51b5\u6709\u70b9\u7279\u6b8a\uff0c\u524d\u7aef\u53d1\u6765\u7684\u6570\u636e\u7ed3\u6784\u4f53\u5fc5\u987b\u8981\u4fdd\u6301[]string \u7684\u7c7b\u578b\u624d\u53ef\u4ee5\u6536\u5230\uff0c\u53c8\u4e0d\u60f3\u5927\u6539\uff0c\u5c31\u6ca1\u7528\u8fd9\u4e2a\u65b9\u6cd5<\/p>\n<p>@z0wjqnxi \u611f\u8c22\u5927\u4f6c\uff0c\u65e0\u8111\u590d\u5236\u60a8\u7684\u4ee3\u7801\uff0c\u628a node \u6539\u6210 ipList \u7c7b\u578b\u540e\uff0c\u4e00\u4e0b\u5c31\u6210\u529f\u4e86\uff0c\u723d\u7684\u4e0d\u8981\u4e0d\u8981\u7684~                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3272362\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : richzhu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             sql: Scan error on column index 5, name &#8220;node&#8221;: invalid character &#8216;.&#8217; after top-level value; <\/p>\n<p>@z0wjqnxi <br \/>\u5927\u4f6c\u597d\uff0c\u4f7f\u7528\u8fd9\u4e2a\u65b9\u6cd5\u4e4b\u540e\uff0c\u4e0d\u77e5\u9053\u4e3a\u4ec0\u4e48\uff0c\u4e0d\u80fd\u8bfb\u4e86\uff0c\u8bfb\u53d6\u6570\u636e\u7684\u65f6\u5019\uff0c\u62a5\u4e0a\u9762\u8fd9\u4e2a\u9519\u8bef                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3272363\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : richzhu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @z0wjqnxi \u77e5\u9053\u4e86 \u54c8\u54c8\uff0c\u662f\u8868\u4e2d\u8001\u6570\u636e\u7684\u95ee\u9898\uff0c\u6253\u6270\u5927\u4f6c\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3272364\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Yoock <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             AfterFind hook                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3272365\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : cszchen <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7528 postgres.jsonb \u7c7b\u578b                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3272366\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : reus <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8981\u5bf9 ip \u505a\u67e5\u8be2\u505a\u7d22\u5f15\u65f6\u4f60\u5c31\u77e5\u9519<\/p>\n<p>\u5e94\u8be5\u53e6\u5916\u5efa\u8868\uff0c\u5b58\u6210\u591a\u884c                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3272367\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : adrianduan <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e0d\u77e5\u9053\u4f60\u7528\u7684\u662f gorm V1 \u8fd8\u662f V2\uff0c\u5982\u679c\u4f7f\u7528\u7684 PostgreSQL \u4e14\u7248\u672c\u4e3a V1 \u7684\u8bdd\u53ef\u4ee5\u76f4\u63a5\u5b9a\u4e49\u7c7b\u578b\u4e3a pq.StringArray\uff0cV2 \u597d\u50cf\u6539\u4e86 PostgreSQL \u7684 driver \u4ece pq \u5230 pgx\uff0c\u8fd9\u79cd\u5199\u6cd5\u5c31\u4e0d\u884c\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3272368\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : richzhu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @reus \u5927\u4f6c\u597d\uff0c\u8bf7\u95ee\u5b58\u6210\u591a\u884c\u662f\u4ec0\u4e48\u610f\u601d<\/p>\n<p>\u662f\u8bf4\u65b0\u5efa\u4e00\u5f20\u8868 2 \u4e2a\u5b57\u6bb5 app ip<br \/>\u6bd4\u5982\u4e00\u4e2a app \u540d\u5b57\u53eb test \u6709 3 \u4e2a ip\uff0c\u53e6\u4e00\u4e2a app \u540d\u5b57\u662f test1\uff0c\u6709 2 \u4e2a ip\uff0c\u90a3\u8868\u4e2d\u5185\u5bb9\u5c31\u662f<\/p>\n<p>test 1.1.1.1<br \/>test 2.2.2.2<br \/>test 3.3.3.3<br \/>test1 4.4.4.4<br \/>test1 5.5.5.5<\/p>\n<p>\u7136\u540e\u5bf9 ip \u5b57\u6bb5\u505a\u7d22\u5f15\uff0c\u662f\u8fd9\u6837\u561b\uff1f                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>gorm \u5982\u4f55\u5b58\u6570\u7ec4 \u8cc7\u6df1\u5927\u4f6c :&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\/156971"}],"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=156971"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/156971\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=156971"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=156971"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=156971"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}