{"id":207763,"date":"2025-07-08T14:26:29","date_gmt":"2025-07-08T06:26:29","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207763\/"},"modified":"2025-07-08T14:26:29","modified_gmt":"2025-07-08T06:26:29","slug":"automigrate-%e4%b8%8d%e7%94%9f%e6%88%90%e8%87%aa%e5%bc%95%e7%94%a8%e5%a4%96%e9%94%ae","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207763\/","title":{"rendered":"AutoMigrate \u4e0d\u751f\u6210\u81ea\u5f15\u7528\u5916\u952e"},"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>AutoMigrate \u4e0d\u751f\u6210\u81ea\u5f15\u7528\u5916\u952e<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-25 14:24:31<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u73cd\u60dc\u65f6\u95f4\uff0c\u52e4\u594b\u5b66\u4e60\uff01\u4eca\u5929\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aAutoMigrate \u4e0d\u751f\u6210\u81ea\u5f15\u7528\u5916\u952e\u300b<\/span>\uff0c\u6b63\u6587\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\uff0c\u6216\u8005\u662f\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u6709\u7591\u95ee\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u6211\uff01\u540e\u9762\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u5185\u5bb9\u7684\uff0c\u5e0c\u671b\u90fd\u80fd\u5e2e\u5230\u6b63\u5728\u5b66\u4e60\u7684\u5927\u5bb6\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6b63\u5728\u5c1d\u8bd5\u8ba9\u81ea\u5f15\u7528\u952e\u5728\u6211\u7684\u6a21\u578b\u4e2d\u5de5\u4f5c\u3002\u7531\u4e8e\u67d0\u79cd\u539f\u56e0\uff0c\u5916\u952e\u5173\u7cfb\u6ca1\u6709\u88ab\u521b\u5efa\u3002\u6211\u5c1d\u8bd5\u4e86\u591a\u79cd\u7ed3\u6784\u6807\u8bb0\u7684\u53d8\u4f53\uff0c\u4f46\u6ca1\u6709\u6210\u529f\u3002<\/p>\n<p>\u6211\u6709\u81ea\u5df1\u7684 mixin\uff1a<\/p>\n<pre>type mixin struct {\n    id        uint      `json:\"id\"`\n    createdat time.time `json:\"created_at\"`\n    updatedat time.time `json:\"updated_at\"`\n    deleted   bool      `json:\"deleted\"`\n}\n<\/pre>\n<p>\u6b64\u6a21\u578b\u4e2d\u51fa\u73b0\u4e86\u95ee\u9898\uff0c\u5f53\u6211\u5728 postgres \u4e0a\u8fd0\u884c automigrate \u65f6\uff0c\u4e0d\u4f1a\u521b\u5efa parenttaskid \u5916\u952e\u7ea6\u675f\u3002<\/p>\n<pre>type Task struct {\n    Mixin\n    Content  string    `gorm:\"not null; default:Empty Task\" json:\"content\"`\n    Deadline time.Time `json:\"deadline,omitempty\"`\n    Priority uint      `json:\"priority,omitempty\"`\n    Complete bool      `json:\"complete,omitempty\"`\n\n    \/\/ Relations\n    BucketID *uint   `gorm:\"not null\" json:\"bucket_id\"`\n    Bucket   *Bucket `gorm:\"foreignKey: BucketID\" json:\"-\"`\n\n    ParentTaskID *uint `gorm:\"not null\" json:\"parent_task_id\"`\n    ParentTask   *Task `gorm:\"foreignKey: ParentTaskID\" json:\"-\"`\n}\n<\/pre>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u6211\u53ef\u4ee5\u786e\u8ba4\u8fd9\u4f3c\u4e4e\u662f gorm \u4e2d\u7684\u4e00\u4e2a\u9519\u8bef\u3002\u6211\u521b\u5efa\u4e86 \u548c \u6765\u91cd\u73b0\u8be5\u9519\u8bef\u3002<\/p>\n<p>\u60a8\u53ef\u4ee5\u5728 github \u4e2d\u8ba2\u9605\u66f4\u65b0\u3002<\/p>\n<p>\u672c\u7bc7\u5173\u4e8e\u300aAutoMigrate \u4e0d\u751f\u6210\u81ea\u5f15\u7528\u5916\u952e\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8eGolang\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#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-207763","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207763","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=207763"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207763\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207763"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207763"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207763"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}