{"id":822,"date":"2024-11-07T09:53:44","date_gmt":"2024-11-07T01:53:44","guid":{"rendered":"https:\/\/fwq.ai\/blog\/822\/"},"modified":"2024-11-07T09:53:44","modified_gmt":"2024-11-07T01:53:44","slug":"gorm%e4%b8%ad%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e4%b8%80%e5%af%b9%e4%b8%80%e5%85%b3%e8%81%94%e6%9f%a5%e8%af%a2%ef%bc%8c%e5%90%8c%e6%97%b6%e8%8e%b7%e5%8f%96%e5%85%b3%e8%81%94%e8%a1%a8%e7%9a%84","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/822\/","title":{"rendered":"Gorm\u4e2d\u5982\u4f55\u5b9e\u73b0\u4e00\u5bf9\u4e00\u5173\u8054\u67e5\u8be2\uff0c\u540c\u65f6\u83b7\u53d6\u5173\u8054\u8868\u7684\u6570\u636e\uff1f"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1082\" src=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/172991413244242.jpg\" width=\"800\" height=\"320\" srcset=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/172991413244242.jpg 800w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/172991413244242-300x120.jpg 300w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/172991413244242-768x307.jpg 768w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/172991413244242-670x268.jpg 670w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" title=\"Gorm\u4e2d\u5982\u4f55\u5b9e\u73b0\u4e00\u5bf9\u4e00\u5173\u8054\u67e5\u8be2\uff0c\u540c\u65f6\u83b7\u53d6\u5173\u8054\u8868\u7684\u6570\u636e\uff1f\u63d2\u56fe\" alt=\"Gorm\u4e2d\u5982\u4f55\u5b9e\u73b0\u4e00\u5bf9\u4e00\u5173\u8054\u67e5\u8be2\uff0c\u540c\u65f6\u83b7\u53d6\u5173\u8054\u8868\u7684\u6570\u636e\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>gorm\u5982\u4f55\u5b9e\u73b0\u4e00\u5bf9\u4e00\u5bf9\u5173\u8054<\/strong><\/p>\n<p>\u8003\u8651\u4ee5\u4e0b\u4e24\u4e2a\u8868\uff1auser\u8868\u548cuser_info\u8868\uff0c\u4e8c\u8005\u7684\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n<p><strong>user\u8868<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>\u5b57\u6bb5<\/th>\n<th>\u540d\u79f0<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>id<\/td>\n<td>1001<\/td>\n<\/tr>\n<tr>\n<td>username<\/td>\n<td>wang<\/td>\n<\/tr>\n<tr>\n<td>status<\/td>\n<td>1<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong>user_info\u8868<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>\u5b57\u6bb5<\/th>\n<th>\u540d\u79f0<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>id<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>user_id<\/td>\n<td>1001<\/td>\n<\/tr>\n<tr>\n<td>age<\/td>\n<td>40<\/td>\n<\/tr>\n<tr>\n<td>openid<\/td>\n<td>xxxxx<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8981\u5b9e\u73b0\u53ea\u67e5\u8be2user\u8868\u5c31\u80fd\u540c\u65f6\u83b7\u53d6\u5173\u8054\u7684user_info\u8868\u4e2d\u7684\u6570\u636e\uff0c\u9700\u8981\u5bf9\u7ed3\u6784\u4f53\u8fdb\u884c\u5982\u4e0b\u8bbe\u7f6e\uff1a<\/p>\n<p><strong>user\u7ed3\u6784\u4f53<\/strong><\/p>\n<pre>type user struct {\n    uid  uint64  `gorm:\"column:uid;primary_key\"`\n    username string `gorm:\"column:username;type:varchar(50);\"`\n    status bool `gorm:\"column:status;type:int(10)\"`\n    userinfo userinfo  `gorm:\"foreignkey:uid;associationforeignkey:userid\"`\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>userinfo\u7ed3\u6784\u4f53<\/strong><\/p>\n<pre>type userinfo struct {\n    id  uint64  `gorm:\"column:id;primary_key\"`\n    userid uint64 `gorm:\"column:user_id;type:bigint(20)\"`\n    age  uint64 `gorm:\"column:age;type:int(10)\"`\n    openid string `gorm:\"column:openid;type:varchar(250)\"`\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u4ee5\u4e0a\u5b9a\u4e49\u4e2d\uff0c\u5173\u952e\u5728\u4e8e\u6307\u5b9a\u4e24\u4e2a\u6a21\u578b\u4e4b\u95f4\u7684\u5173\u7cfb\u3002\u5728user\u7ed3\u6784\u4f53\u7684userinfo\u5b57\u6bb5\u4e2d\uff0c\u6307\u5b9a\u4e86\uff1a<\/p>\n<ul>\n<li> <strong>foreignkey:<\/strong> \u5916\u952e\u540d\u79f0\uff08\u672c\u4f8b\u4e2d\u4e3auid\uff09<\/li>\n<li> <strong>associationforeignkey:<\/strong> \u5173\u8054\u5916\u952e\u540d\u79f0\uff08\u672c\u4f8b\u4e2d\u4e3auserid\uff09<\/li>\n<\/ul>\n<p>\u8fd9\u544a\u8bc9gorm\uff0c\u5728user\u8868\u4e2d\u67e5\u8be2\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u540c\u65f6\u901a\u8fc7userid\u5173\u8054\u5230user_info\u8868\u5e76\u83b7\u53d6\u6570\u636e\u3002<\/p>\n<p><strong>\u4f7f\u7528\u65b9\u6cd5<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4e24\u79cd\u65b9\u6cd5\u4e4b\u4e00\uff0c\u901a\u8fc7user\u8868\u5173\u8054\u67e5\u8be2user_info\u8868\uff1a<\/p>\n<p><strong>\u4f7f\u7528preload<\/strong><\/p>\n<pre>var user user\norm.model(user{}).preload(\"userinfo\").find(&amp;user,1001)<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u4f7f\u7528related<\/strong><\/p>\n<pre>var u User\nvar userInfo UserInfo\norm.Find(&amp;u,1001)\norm.Model(&amp;u).Related(&amp;userInfo)<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u65b9\u6cd5\u90fd\u53ef\u4ee5\u5b9e\u73b0\u901a\u8fc7\u67e5\u8be2user\u8868\uff0c\u540c\u65f6\u83b7\u53d6\u5173\u8054\u7684user_info\u8868\u4e2d\u7684\u6570\u636e\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fGorm\u4e2d\u5982\u4f55\u5b9e\u73b0\u4e00\u5bf9\u4e00\u5173\u8054\u67e5\u8be2\uff0c\u540c\u65f6\u83b7\u53d6\u5173\u8054\u8868\u7684\u6570\u636e\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>gorm\u5982\u4f55\u5b9e\u73b0\u4e00\u5bf9\u4e00\u5bf9\u5173\u8054 \u8003\u8651\u4ee5\u4e0b\u4e24\u4e2a\u8868\uff1auser\u8868\u548cuser_info\u8868\uff0c\u4e8c\u8005\u7684\u7ed3\u6784\u5982\u4e0b\uff1a user\u8868 \u5b57\u6bb5 \u540d\u79f0 id 1001 username wang status 1 user_info\u8868 \u5b57\u6bb5 \u540d\u79f0 id 1 user_id 1001 age 40 openid xxxxx \u8981\u5b9e\u73b0\u53ea\u67e5\u8be2user\u8868\u5c31\u80fd\u540c\u65f6\u83b7\u53d6\u5173\u8054\u7684user_info\u8868\u4e2d\u7684\u6570\u636e\uff0c\u9700\u8981\u5bf9\u7ed3\u6784\u4f53\u8fdb\u884c\u5982\u4e0b\u8bbe\u7f6e\uff1a user\u7ed3\u6784\u4f53 type user struct { uid uint64 `gorm:&#8221;column:uid;primary_key&#8221;` username string `gorm:&#8221;column:username;type:varchar(50);&#8221;` status bool `gorm:&#8221;column:status;type:int(10)&#8221;` userinfo userinfo `gorm:&#8221;foreignkey:uid;associationforeignkey:userid&#8221;` } \u767b\u5f55\u540e\u590d\u5236 userinfo\u7ed3\u6784\u4f53 type userinfo struct { id uint64 `gorm:&#8221;column:id;primary_key&#8221;` userid uint64 `gorm:&#8221;column:user_id;type:bigint(20)&#8221;` age uint64 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-822","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/822","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=822"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/822\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=822"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=822"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=822"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}