{"id":206717,"date":"2025-06-12T14:11:21","date_gmt":"2025-06-12T06:11:21","guid":{"rendered":"https:\/\/server.hk\/cnblog\/206717\/"},"modified":"2025-06-12T14:11:21","modified_gmt":"2025-06-12T06:11:21","slug":"gorm-postgres%ef%bc%9a%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e8%87%aa%e5%ae%9a%e4%b9%89%e7%b1%bb%e5%9e%8b%e4%b8%bb%e9%94%ae%e7%9a%84%e8%87%aa%e5%a2%9e%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/206717\/","title":{"rendered":"Gorm Postgres\uff1a\u5982\u4f55\u5b9e\u73b0\u81ea\u5b9a\u4e49\u7c7b\u578b\u4e3b\u952e\u7684\u81ea\u589e\uff1f"},"content":{"rendered":"<p>\u6700\u8fd1\u53d1\u73b0\u4e0d\u5c11\u5c0f\u4f19\u4f34\u90fd\u5bf9<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">GOLANG<\/span>\u5f88\u611f\u5174\u8da3\uff0c\u6240\u4ee5\u4eca\u5929\u7ee7\u7eed\u7ed9\u5927\u5bb6\u4ecb\u7ecd<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">GOLANG<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\uff0c\u672c\u6587<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aGorm Postgres\uff1a\u5982\u4f55\u5b9e\u73b0\u81ea\u5b9a\u4e49\u7c7b\u578b\u4e3b\u952e\u7684\u81ea\u589e\uff1f\u300b<\/span>\u4e3b\u8981\u5185\u5bb9\u6d89\u53ca\u5230<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u5e2e\u5230\u4f60\uff01\u5f53\u7136\u5982\u679c\u9605\u8bfb\u672c\u6587\u65f6\u5b58\u5728\u4e0d\u540c\u60f3\u6cd5\uff0c\u53ef\u4ee5\u5728\u8bc4\u8bba\u4e2d\u8868\u8fbe\uff0c\u4f46\u662f\u8bf7\u52ff\u4f7f\u7528\u8fc7\u6fc0\u7684\u63aa\u8f9e~,<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241120\/1732093245673da53df208e.jpg\" class=\"aligncenter\">,<br \/>\n<strong>gorm postgres\uff1a\u81ea\u5b9a\u4e49\u7c7b\u578b\u4e3b\u952e\u5f00\u542f\u81ea\u589e<\/strong>,\u5728\u4f7f\u7528 gorm \u5bf9 postgres \u6570\u636e\u5e93\u8fdb\u884c\u64cd\u4f5c\u65f6\uff0c\u81ea\u5b9a\u4e49\u7c7b\u578b\u7684\u4e3b\u952e\u65e0\u6cd5\u81ea\u52a8\u8bbe\u7f6e\u81ea\u589e\u3002\u672c\u6587\u8be6\u7ec6\u9610\u8ff0\u8be5\u95ee\u9898\u7684\u539f\u56e0\u53ca\u89e3\u51b3\u65b9\u6848\u3002,<br \/>\n<strong>\u95ee\u9898\u539f\u56e0\uff1a<\/strong>,\u81ea\u5b9a\u4e49\u7c7b\u578b\u5bf9\u5e94\u7684\u4e3b\u952e\u5b57\u6bb5\u5728\u8868\u81ea\u52a8\u8fc1\u79fb\u65f6\u65e0\u6cd5\u88ab\u8bc6\u522b\u4e3a\u81ea\u589e\u3002\u8fd9\u662f\u56e0\u4e3a gorm \u7684\u9ed8\u8ba4\u884c\u4e3a\u662f\u6839\u636e\u5b57\u6bb5\u7c7b\u578b\u6765\u5224\u65ad\u662f\u5426\u81ea\u589e\uff08\u4f8b\u5982\uff0cbigint \u7c7b\u578b\u9ed8\u8ba4\u8bbe\u7f6e\u4e3a\u81ea\u589e\uff09\u3002\u800c\u81ea\u5b9a\u4e49\u7c7b\u578b\u5e76\u4e0d\u5c5e\u4e8e gorm \u652f\u6301\u7684\u9ed8\u8ba4\u7c7b\u578b\u3002,<br \/>\n<strong>\u89e3\u51b3\u65b9\u6848\uff1a<\/strong>,\u901a\u8fc7\u4fee\u6539 gormdbdatatype \u65b9\u6cd5\uff0c\u53ef\u4ee5\u6307\u5b9a\u81ea\u5b9a\u4e49\u7c7b\u578b\u5bf9\u5e94\u5b57\u6bb5\u7684\u6570\u636e\u7c7b\u578b\u4ee5\u53ca\u81ea\u589e\u5c5e\u6027\u3002\u5728\u4ee3\u7801\u4e2d\uff0c\u53ea\u9700\u6839\u636e\u4e0d\u540c\u6570\u636e\u5e93\uff08\u4f8b\u5982 postgres\uff09\u5224\u65ad\u5b57\u6bb5\u662f\u5426\u81ea\u589e\uff0c\u5982\u679c\u662f\u81ea\u589e\uff0c\u5219\u8fd4\u56de bigserial \u7c7b\u578b\u3002,\u4ee5\u4e0b\u4e3a\u4fee\u6539\u540e\u7684\u4ee3\u7801\uff1a,<br \/>\n<strong>\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong>,\u81ea\u5b9a\u4e49\u7c7b\u578b\u9700\u8981\u5b9e\u73b0 gormdbdatatype \u63a5\u53e3\u3002\u5728\u8868\u521b\u5efa\u65f6\uff0cgorm \u4f1a\u76f4\u63a5\u4f7f\u7528 gormdbdatatype \u8fd4\u56de\u7684\u7c7b\u578b\u6765\u521b\u5efa\u8868\u7684\u5b57\u6bb5\u3002,\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301GOLANG\uff01\u66f4\u591a\u5173\u4e8eGOLANG\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8GOLANG\u516c\u4f17\u53f7\u3002,gorm postgres\uff1a\u81ea\u5b9a\u4e49\u7c7b\u578b\u4e3b\u952e\u5f00\u542f\u81ea\u589e,\u6700\u8fd1\u53d1\u73b0\u4e0d\u5c11\u5c0f\u4f19\u4f34\u90fd\u5bf9<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">GOLANG<\/span>\u5f88\u611f\u5174\u8da3\uff0c\u6240\u4ee5\u4eca\u5929\u7ee7\u7eed\u7ed9\u5927\u5bb6\u4ecb\u7ecd<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">GOLANG<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\uff0c\u672c\u6587<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aGorm Postgres\uff1a\u5982\u4f55\u5b9e\u73b0\u81ea\u5b9a\u4e49\u7c7b\u578b\u4e3b\u952e\u7684\u81ea\u589e\uff1f\u300b<\/span>\u4e3b\u8981\u5185\u5bb9\u6d89\u53ca\u5230<br \/>\n<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u5e2e\u5230\u4f60\uff01\u5f53\u7136\u5982\u679c\u9605\u8bfb\u672c\u6587\u65f6\u5b58\u5728\u4e0d\u540c\u60f3\u6cd5\uff0c\u53ef\u4ee5\u5728\u8bc4\u8bba\u4e2d\u8868\u8fbe\uff0c\u4f46\u662f\u8bf7\u52ff\u4f7f\u7528\u8fc7\u6fc0\u7684\u63aa\u8f9e~,<br \/>\n<b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span> <span>Gorm Postgres\uff1a\u5982\u4f55\u5b9e\u73b0\u81ea\u5b9a\u4e49\u7c7b\u578b\u4e3b\u952e\u7684\u81ea\u589e\uff1f<\/span><\/p>\n<h1>Gorm Postgres\uff1a\u5982\u4f55\u5b9e\u73b0\u81ea\u5b9a\u4e49\u7c7b\u578b\u4e3b\u952e\u7684\u81ea\u589e\uff1f<\/h1>\n<p><span>2024-11-20 17:01:01<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u6700\u8fd1\u53d1\u73b0\u4e0d\u5c11\u5c0f\u4f19\u4f34\u90fd\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">GOLANG<\/span>\u5f88\u611f\u5174\u8da3\uff0c\u6240\u4ee5\u4eca\u5929\u7ee7\u7eed\u7ed9\u5927\u5bb6\u4ecb\u7ecd<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">GOLANG<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\uff0c\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aGorm Postgres\uff1a\u5982\u4f55\u5b9e\u73b0\u81ea\u5b9a\u4e49\u7c7b\u578b\u4e3b\u952e\u7684\u81ea\u589e\uff1f\u300b<\/span>\u4e3b\u8981\u5185\u5bb9\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u5e2e\u5230\u4f60\uff01\u5f53\u7136\u5982\u679c\u9605\u8bfb\u672c\u6587\u65f6\u5b58\u5728\u4e0d\u540c\u60f3\u6cd5\uff0c\u53ef\u4ee5\u5728\u8bc4\u8bba\u4e2d\u8868\u8fbe\uff0c\u4f46\u662f\u8bf7\u52ff\u4f7f\u7528\u8fc7\u6fc0\u7684\u63aa\u8f9e~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241120\/1732093245673da53df208e.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>gorm postgres\uff1a\u81ea\u5b9a\u4e49\u7c7b\u578b\u4e3b\u952e\u5f00\u542f\u81ea\u589e<\/strong><\/p>\n<p>\u5728\u4f7f\u7528 gorm \u5bf9 postgres \u6570\u636e\u5e93\u8fdb\u884c\u64cd\u4f5c\u65f6\uff0c\u81ea\u5b9a\u4e49\u7c7b\u578b\u7684\u4e3b\u952e\u65e0\u6cd5\u81ea\u52a8\u8bbe\u7f6e\u81ea\u589e\u3002\u672c\u6587\u8be6\u7ec6\u9610\u8ff0\u8be5\u95ee\u9898\u7684\u539f\u56e0\u53ca\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<p><strong>\u95ee\u9898\u539f\u56e0\uff1a<\/strong><\/p>\n<p>\u81ea\u5b9a\u4e49\u7c7b\u578b\u5bf9\u5e94\u7684\u4e3b\u952e\u5b57\u6bb5\u5728\u8868\u81ea\u52a8\u8fc1\u79fb\u65f6\u65e0\u6cd5\u88ab\u8bc6\u522b\u4e3a\u81ea\u589e\u3002\u8fd9\u662f\u56e0\u4e3a gorm \u7684\u9ed8\u8ba4\u884c\u4e3a\u662f\u6839\u636e\u5b57\u6bb5\u7c7b\u578b\u6765\u5224\u65ad\u662f\u5426\u81ea\u589e\uff08\u4f8b\u5982\uff0cbigint \u7c7b\u578b\u9ed8\u8ba4\u8bbe\u7f6e\u4e3a\u81ea\u589e\uff09\u3002\u800c\u81ea\u5b9a\u4e49\u7c7b\u578b\u5e76\u4e0d\u5c5e\u4e8e gorm \u652f\u6301\u7684\u9ed8\u8ba4\u7c7b\u578b\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848\uff1a<\/strong><\/p>\n<p>\u901a\u8fc7\u4fee\u6539 gormdbdatatype \u65b9\u6cd5\uff0c\u53ef\u4ee5\u6307\u5b9a\u81ea\u5b9a\u4e49\u7c7b\u578b\u5bf9\u5e94\u5b57\u6bb5\u7684\u6570\u636e\u7c7b\u578b\u4ee5\u53ca\u81ea\u589e\u5c5e\u6027\u3002\u5728\u4ee3\u7801\u4e2d\uff0c\u53ea\u9700\u6839\u636e\u4e0d\u540c\u6570\u636e\u5e93\uff08\u4f8b\u5982 postgres\uff09\u5224\u65ad\u5b57\u6bb5\u662f\u5426\u81ea\u589e\uff0c\u5982\u679c\u662f\u81ea\u589e\uff0c\u5219\u8fd4\u56de bigserial \u7c7b\u578b\u3002<\/p>\n<p>\u4ee5\u4e0b\u4e3a\u4fee\u6539\u540e\u7684\u4ee3\u7801\uff1a<\/p>\n<pre>func (bigint BigInt) GormDBDataType(db *gorm.DB, field *schema.Field) string {\n    \/\/ \u6839\u636e\u4e0d\u540c\u7684\u6570\u636e\u5e93\u9a71\u52a8\u8fd4\u56de\u4e0d\u540c\u7684\u6570\u636e\u7c7b\u578b\n    switch db.Dialector.Name() {\n    case \"mysql\", \"sqlite\":\n        return \"bigint\"\n    case \"postgres\":\n        if field.AutoIncrement {\n            return \"bigserial\"\n        }\n        return \"bigint\"\n    }\n    return \"bigint\"\n}<\/pre>\n<p><strong>\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong><\/p>\n<p>\u81ea\u5b9a\u4e49\u7c7b\u578b\u9700\u8981\u5b9e\u73b0 gormdbdatatype \u63a5\u53e3\u3002\u5728\u8868\u521b\u5efa\u65f6\uff0cgorm \u4f1a\u76f4\u63a5\u4f7f\u7528 gormdbdatatype \u8fd4\u56de\u7684\u7c7b\u578b\u6765\u521b\u5efa\u8868\u7684\u5b57\u6bb5\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301GOLANG\uff01\u66f4\u591a\u5173\u4e8eGOLANG\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8GOLANG\u516c\u4f17\u53f7\u3002<\/p>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   win10\u91cd\u7f6e\u6b64\u7535\u8111\u9009\u9879\u662f\u91cd\u7f6ec\u76d8\u8fd8\u662f\u6240\u6709\u4e1c\u897f\u90fd\u6ca1\u6709\uff1f\n <\/dd>\n<\/dl>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   Win11\u5e94\u7528\u7a0b\u5e8f\u5378\u8f7d\u65b9\u6cd5\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u53d1\u73b0\u4e0d\u5c11\u5c0f\u4f19\u4f34\u90fd\u5bf9 GOLA&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-206717","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/206717","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=206717"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/206717\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=206717"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=206717"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=206717"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}