{"id":7848,"date":"2024-11-14T08:23:01","date_gmt":"2024-11-14T00:23:01","guid":{"rendered":"https:\/\/fwq.ai\/blog\/7848\/"},"modified":"2024-11-14T08:23:01","modified_gmt":"2024-11-14T00:23:01","slug":"%e4%bd%bf%e7%94%a8-gorm-%e6%96%b0%e5%a2%9e%e6%95%b0%e6%8d%ae%e6%97%b6%e9%81%87%e5%88%b0-unknown-column-created_at-in-field-list-%e9%94%99%e8%af%af%ef%bc%8c%e5%a6%82%e4%bd%95","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/7848\/","title":{"rendered":"\u4f7f\u7528 Gorm \u65b0\u589e\u6570\u636e\u65f6\u9047\u5230 \u201cUnknown column &#8216;created_at&#8217; in &#8216;field list&#8217;\u201d \u9519\u8bef\uff0c\u5982\u4f55\u89e3\u51b3\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173129833232678.jpg\" class=\"aligncenter\" title=\"\u4f7f\u7528 Gorm \u65b0\u589e\u6570\u636e\u65f6\u9047\u5230 \u201cUnknown column &#8216;created_at&#8217; in &#8216;field list&#8217;\u201d \u9519\u8bef\uff0c\u5982\u4f55\u89e3\u51b3\uff1f\u63d2\u56fe\" alt=\"\u4f7f\u7528 Gorm \u65b0\u589e\u6570\u636e\u65f6\u9047\u5230 \u201cUnknown column &#8216;created_at&#8217; in &#8216;field list&#8217;\u201d \u9519\u8bef\uff0c\u5982\u4f55\u89e3\u51b3\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>gorm\u64cd\u4f5c\u6570\u636e\u5e93\u5f02\u5e38\uff1f<\/strong><\/p>\n<p><strong>\u95ee\u9898\u63cf\u8ff0\uff1a<\/strong><br \/>\u4f7f\u7528gorm\u65f6\u9047\u5230\u9519\u8bef\uff1aerror 1054 (42s22): unknown column &#8216;created_at&#8217; in &#8216;field list&#8217;\u3002\u8fd9\u4e2a\u9519\u8bef\u8868\u660e\u6846\u67b6\u4e2d\u81ea\u5e26\u7684\u5b57\u6bb5\u5bfc\u81f4\u4e86\u65b0\u589e\u5f02\u5e38\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848\uff1a<\/strong><br \/>\u6b64\u95ee\u9898\u53ef\u80fd\u662f\u56e0\u4e3a\u4e0b\u5217\u539f\u56e0\u9020\u6210\u7684\uff1a<\/p>\n<ul>\n<li>\u521b\u5efa\u8868\u65f6\u672a\u5305\u542bgorm.model\u7ed3\u6784\u4f53\u3002<\/li>\n<li>\u65b0\u589e\u7ed3\u6784\u4e2d\u5305\u542bgorm.model\uff0c\u800c\u521b\u5efa\u8868\u65f6\u672a\u5305\u542b\u3002<\/li>\n<\/ul>\n<p>\u6709\u4e24\u79cd\u89e3\u51b3\u65b9\u6848\uff1a<\/p>\n<ol>\n<li> <strong>\u79fb\u9664\u65b0\u589e\u63a5\u53e3\u7ed3\u6784\u4e2d\u7684gorm.model<\/strong><br \/>\u5982\u679c\u4f60\u5728\u65b0\u589e\u63a5\u53e3\u7684\u7ed3\u6784\u4f53\u4e2d\u4f7f\u7528\u4e86gorm.model\uff0c\u5c06\u5176\u5220\u9664\u5373\u53ef\u3002<\/li>\n<li> <strong>\u4f7f\u7528automigrate\u65b9\u6cd5\u5efa\u8868\uff0c\u5e76\u6dfb\u52a0gorm.model\u5230\u7ed3\u6784\u4f53\u4e2d<\/strong><br \/>\u5728\u521b\u5efa\u8868\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528gorm\u63d0\u4f9b\u7684automigrate\u65b9\u6cd5\uff0c\u540c\u65f6\u5c06gorm.model\u6dfb\u52a0\u5230\u7ed3\u6784\u4f53\u4e2d\u3002<\/li>\n<\/ol>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n    \"gorm.io\/gorm\"\n)\n\ntype User struct {\n    gorm.Model\n    Name string\n    Email string\n}\n\nfunc main() {\n    db, err := gorm.Open(\"mysql\", \"user:password@tcp(127.0.0.1:3306)\/database?charset=utf8mb4&amp;parseTime=True&amp;loc=Local\")\n    if err != nil {\n        panic(err)\n    }\n    \/\/ \u521b\u5efa\u8868\n    if err := db.AutoMigrate(&amp;User{}); err != nil {\n        panic(err)\n    }\n    \n    \/\/ \u67e5\u8be2\u6240\u6709\u7528\u6237\n    var users []User\n    if err := db.Find(&amp;users).Error; err != nil {\n        panic(err)\n    }\n    fmt.Println(users)\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u6709\u5173\u5d4c\u5957\u7ed3\u6784\u4f53\u7684\u66f4\u591a\u4fe1\u606f\uff0c\u8bf7\u53c2\u9605gorm\u6587\u6863\uff1ahttps:\/\/gorm.io\/zh_cn\/docs\/models.html<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u4f7f\u7528 Gorm \u65b0\u589e\u6570\u636e\u65f6\u9047\u5230 \u201cUnknown column &#8216;created_at&#8217; in &#8216;field list&#8217;\u201d \u9519\u8bef\uff0c\u5982\u4f55\u89e3\u51b3\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>gorm\u64cd\u4f5c\u6570\u636e\u5e93\u5f02\u5e38\uff1f \u95ee\u9898\u63cf\u8ff0\uff1a\u4f7f\u7528gorm\u65f6\u9047\u5230\u9519\u8bef\uff1aerror 1054 (42s22): unknown column &#8216;created_at&#8217; in &#8216;field list&#8217;\u3002\u8fd9\u4e2a\u9519\u8bef\u8868\u660e\u6846\u67b6\u4e2d\u81ea\u5e26\u7684\u5b57\u6bb5\u5bfc\u81f4\u4e86\u65b0\u589e\u5f02\u5e38\u3002 \u89e3\u51b3\u65b9\u6848\uff1a\u6b64\u95ee\u9898\u53ef\u80fd\u662f\u56e0\u4e3a\u4e0b\u5217\u539f\u56e0\u9020\u6210\u7684\uff1a \u521b\u5efa\u8868\u65f6\u672a\u5305\u542bgorm.model\u7ed3\u6784\u4f53\u3002 \u65b0\u589e\u7ed3\u6784\u4e2d\u5305\u542bgorm.model\uff0c\u800c\u521b\u5efa\u8868\u65f6\u672a\u5305\u542b\u3002 \u6709\u4e24\u79cd\u89e3\u51b3\u65b9\u6848\uff1a \u79fb\u9664\u65b0\u589e\u63a5\u53e3\u7ed3\u6784\u4e2d\u7684gorm.model\u5982\u679c\u4f60\u5728\u65b0\u589e\u63a5\u53e3\u7684\u7ed3\u6784\u4f53\u4e2d\u4f7f\u7528\u4e86gorm.model\uff0c\u5c06\u5176\u5220\u9664\u5373\u53ef\u3002 \u4f7f\u7528automigrate\u65b9\u6cd5\u5efa\u8868\uff0c\u5e76\u6dfb\u52a0gorm.model\u5230\u7ed3\u6784\u4f53\u4e2d\u5728\u521b\u5efa\u8868\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528gorm\u63d0\u4f9b\u7684automigrate\u65b9\u6cd5\uff0c\u540c\u65f6\u5c06gorm.model\u6dfb\u52a0\u5230\u7ed3\u6784\u4f53\u4e2d\u3002 \u4f8b\u5982\uff1a package main import ( &#8220;fmt&#8221; &#8220;gorm.io\/gorm&#8221; ) type User struct { gorm.Model Name string Email string } func main() { db, err := gorm.Open(&#8220;mysql&#8221;, &#8220;user:password@tcp(127.0.0.1:3306)\/database?charset=utf8mb4&amp;parseTime=True&amp;loc=Local&#8221;) if err != nil { panic(err) } \/\/ \u521b\u5efa\u8868 if err := db.AutoMigrate(&amp;User{}); [&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-7848","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7848","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=7848"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7848\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=7848"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=7848"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=7848"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}