{"id":41434,"date":"2024-12-01T13:41:27","date_gmt":"2024-12-01T05:41:27","guid":{"rendered":"https:\/\/fwq.ai\/blog\/41434\/"},"modified":"2024-12-01T13:41:27","modified_gmt":"2024-12-01T05:41:27","slug":"gorm-%e8%87%aa%e5%8a%a8%e8%bf%81%e7%a7%bb%e5%88%9b%e5%bb%ba%e4%b8%80%e4%b8%aa%e6%b2%a1%e6%9c%89%e7%94%a8%e6%88%b7%e5%ae%9a%e4%b9%89%e5%b1%9e%e6%80%a7%e7%9a%84%e8%a1%a8-postgresql","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/41434\/","title":{"rendered":"Gorm \u81ea\u52a8\u8fc1\u79fb\u521b\u5efa\u4e00\u4e2a\u6ca1\u6709\u7528\u6237\u5b9a\u4e49\u5c5e\u6027\u7684\u8868 (postgresql)"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span> <span>Gorm \u81ea\u52a8\u8fc1\u79fb\u521b\u5efa\u4e00\u4e2a\u6ca1\u6709\u7528\u6237\u5b9a\u4e49\u5c5e\u6027\u7684\u8868 (postgresql)<\/span><\/p>\n<h1>Gorm \u81ea\u52a8\u8fc1\u79fb\u521b\u5efa\u4e00\u4e2a\u6ca1\u6709\u7528\u6237\u5b9a\u4e49\u5c5e\u6027\u7684\u8868 (postgresql)<\/h1>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-20 09:54:16<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>Golang\u5c0f\u767d\u4e00\u679a\uff0c\u6b63\u5728\u4e0d\u65ad\u5b66\u4e60\u79ef\u7d2f\u77e5\u8bc6\uff0c\u73b0\u5c06\u5b66\u4e60\u5230\u7684\u77e5\u8bc6\u8bb0\u5f55\u4e00\u4e0b\uff0c\u4e5f\u662f\u5c06\u6211\u7684\u6240\u5f97\u5206\u4eab\u7ed9\u5927\u5bb6\uff01\u800c\u4eca\u5929\u8fd9\u7bc7\u6587\u7ae0\u300aGorm \u81ea\u52a8\u8fc1\u79fb\u521b\u5efa\u4e00\u4e2a\u6ca1\u6709\u7528\u6237\u5b9a\u4e49\u5c5e\u6027\u7684\u8868 (postgresql)\u300b\u5e26\u5927\u5bb6\u6765\u4e86\u89e3\u4e00\u4e0b##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p><\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n\n    _ \"github.com\/jinzhu\/gorm\/dialects\/postgres\"\n    \"gorm.io\/driver\/postgres\"\n    \"gorm.io\/gorm\"\n)\n\ntype Books struct {\n    gorm.Model\n    ID              uint\n    title           string\n    author          string\n    description     string\n    display_picture byte\n}\n\n\nfunc main() {\n    \n    dsn := \/\/successful create connection string\n    \n    db, err := gorm.Open(postgres.Open(dsn), &amp;gorm.Config{})\n\n    if err != nil {\n        \/\/ fmt.Println(err)\n        panic(err)\n    }\n    b := Books{}\n    db.AutoMigrate(&amp;b)\n    data := Books{\n        title:       \"Invisible Cities\",\n        author:      \"Italio Calvino\",\n        description: \"This book shows you the power of prose. It's a beautiful read which will make you feel like you're floating on a cloud.\",\n    }\n    db.Create(&amp;data)\n    fmt.Println(data)\n}<\/pre>\n<p>\u8fde\u63a5\u5b57\u7b26\u4e32\u662f\u6b63\u786e\u7684\uff0c\u56e0\u4e3a db.automitrate(&amp;b) \u8fde\u63a5\u6570\u636e\u5e93\u5e76\u5b9e\u73b0 id \u548c gorm.model \u5c5e\u6027\uff08createdat \u7b49\uff09\uff0c\u4f46\u5b83\u6ca1\u6709\u6dfb\u52a0\u6211\u7684\u5c5e\u6027\u6807\u9898\u3001\u4f5c\u8005\u548c\u63cf\u8ff0\u3002 p&gt; <\/p>\n<p>\u6211\u82b1\u4e86\u4e00\u6574\u5929\u7684\u65f6\u95f4\u5728\u8c37\u6b4c\u4e0a\u641c\u7d22\uff0c\u4f46\u5728\u5176\u4ed6\u5730\u65b9\u627e\u4e0d\u5230\u8fd9\u4e2a\u9519\u8bef\u3002\u6709\u4eba\u53ef\u4ee5\u5e2e\u5fd9\u5417\uff1f\u53e6\u5916\uff0c\u6211\u5728\u8fd0\u884c\u811a\u672c\u4e4b\u524d\u5220\u9664\u4e86 postgres \u4e2d\u7684 books \u8868\u3002<\/p>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<ul>\n<li>\u5c06\u60a8\u7684\u4ee3\u7801\u91cd\u5199\u4e3a\u6b64\uff0c\u5e76\u59cb\u7ec8\u8bb0\u4f4f\u5728 gorm \u4e2d\u6211\u4eec\u9700\u8981\u5c06\u6a21\u578b\u5b57\u6bb5\u5927\u5199<\/li>\n<\/ul>\n<p> 855815536\u200b\u200b526 <\/p>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300aGorm \u81ea\u52a8\u8fc1\u79fb\u521b\u5efa\u4e00\u4e2a\u6ca1\u6709\u7528\u6237\u5b9a\u4e49\u5c5e\u6027\u7684\u8868 (postgresql)\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u7c73\u4e91\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03Golang\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &gt; &gt; Gorm \u81ea\u52a8\u8fc1\u79fb\u521b\u5efa\u4e00\u4e2a\u6ca1\u6709\u7528\u6237\u5b9a\u4e49\u5c5e\u6027\u7684\u8868 (postgresql) Gorm \u81ea\u52a8\u8fc1\u79fb\u521b\u5efa\u4e00\u4e2a\u6ca1\u6709\u7528\u6237\u5b9a\u4e49\u5c5e\u6027\u7684\u8868 (postgresql) \u6765\u6e90\uff1astackoverflow 2024-04-20 09:54:16 0\u6d4f\u89c8 \u6536\u85cf Golang\u5c0f\u767d\u4e00\u679a\uff0c\u6b63\u5728\u4e0d\u65ad\u5b66\u4e60\u79ef\u7d2f\u77e5\u8bc6\uff0c\u73b0\u5c06\u5b66\u4e60\u5230\u7684\u77e5\u8bc6\u8bb0\u5f55\u4e00\u4e0b\uff0c\u4e5f\u662f\u5c06\u6211\u7684\u6240\u5f97\u5206\u4eab\u7ed9\u5927\u5bb6\uff01\u800c\u4eca\u5929\u8fd9\u7bc7\u6587\u7ae0\u300aGorm \u81ea\u52a8\u8fc1\u79fb\u521b\u5efa\u4e00\u4e2a\u6ca1\u6709\u7528\u6237\u5b9a\u4e49\u5c5e\u6027\u7684\u8868 (postgresql)\u300b\u5e26\u5927\u5bb6\u6765\u4e86\u89e3\u4e00\u4e0b##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01 \u95ee\u9898\u5185\u5bb9 package main import ( &#8220;fmt&#8221; _ &#8220;github.com\/jinzhu\/gorm\/dialects\/postgres&#8221; &#8220;gorm.io\/driver\/postgres&#8221; &#8220;gorm.io\/gorm&#8221; ) type Books struct { gorm.Model ID uint title string author string description string display_picture byte } func main() { dsn := \/\/successful create connection string [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-41434","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/41434","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=41434"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/41434\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=41434"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=41434"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=41434"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}