{"id":207043,"date":"2025-07-08T08:22:32","date_gmt":"2025-07-08T00:22:32","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207043\/"},"modified":"2025-07-08T08:22:32","modified_gmt":"2025-07-08T00:22:32","slug":"%e5%af%b9-gorm-%e4%b8%ad%e7%9a%84-updatedat-%e5%ad%97%e6%ae%b5%e4%bd%bf%e7%94%a8-unix-%e6%97%b6%e9%97%b4%e6%88%b3","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207043\/","title":{"rendered":"\u5bf9 gorm \u4e2d\u7684 UpdatedAt \u5b57\u6bb5\u4f7f\u7528 unix \u65f6\u95f4\u6233"},"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>\u5bf9 gorm \u4e2d\u7684 UpdatedAt \u5b57\u6bb5\u4f7f\u7528 unix \u65f6\u95f4\u6233<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-19 17:24:35<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5c0f\u4f19\u4f34\u4eec\u5bf9Golang\u7f16\u7a0b\u611f\u5174\u8da3\u5417\uff1f\u662f\u5426\u6b63\u5728\u5b66\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff1f\u5982\u679c\u662f\uff0c\u90a3\u4e48\u672c\u6587\u300a\u5bf9 gorm \u4e2d\u7684 UpdatedAt \u5b57\u6bb5\u4f7f\u7528 unix \u65f6\u95f4\u6233\u300b\uff0c\u5c31\u5f88\u9002\u5408\u4f60\uff0c\u672c\u7bc7\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\u3002\u5728\u4e4b\u540e\u7684\u6587\u7ae0\u4e2d\u4e5f\u4f1a\u591a\u591a\u5206\u4eab\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6b63\u5728\u7f16\u5199\u4e00\u4e2a\u4f7f\u7528 gorm orm \u4e0e <code>sqlite3<\/code> \u6570\u636e\u5e93\u901a\u4fe1\u7684\u5e94\u7528\u7a0b\u5e8f\u3002\u73b0\u5728\u7684\u95ee\u9898\u662f\u6211\u9700\u8981\u5c06 <code>updatedat<\/code> \u5217\u8bbe\u7f6e\u4e3a unix \u65f6\u95f4\u6233\uff0c\u56e0\u4e3a\u53e6\u4e00\u4e2a\u9057\u7559\u5e94\u7528\u7a0b\u5e8f\u6b63\u5728\u4f7f\u7528\u76f8\u540c\u7684\u6570\u636e\u5e93\uff0c\u5e76\u4e14\u5b83\u4eec\u5e94\u8be5\u517c\u5bb9\u3002<\/p>\n<p>\u56e0\u6b64\uff0c\u6211\u5c1d\u8bd5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5757\u5728 <code>beforeupdate<\/code> \u6302\u94a9\u4e0a\u4f7f\u7528 unix \u65f6\u95f4\u6233\u66f4\u65b0 <code>updatedat<\/code> \u5b57\u6bb5\u3002<\/p>\n<pre>func (c *cartitems) beforeupdate() (err error) {\n    fmt.println(\"----------------------------------------\")\n    fmt.println(\"beforeupdate\")\n    fmt.println( c )\n    c.updatedat = time.now().unix()\n    fmt.println(\"----------------------------------------\")\n    return\n}<\/pre>\n<p>\u4f46\u662f\u5f53\u6211\u8fd0\u884c\u4ee3\u7801\u67e5\u8be2\u65f6\u4fdd\u6301\u4e0d\u53d8\u5e76\u4e14\u6ca1\u6709\u5c06\u65f6\u95f4\u6233\u6dfb\u52a0\u5230\u6570\u636e\u5e93\u4e2d\u3002 <\/p>\n<p>\u6208\u5c14\u59c6\u65e5\u5fd7<\/p>\n<pre>----------------------------------------\nBeforeUpdate\n&amp;{55 21 4 7 1585607114 1585607114 {0 [] [] [] {0 0 } 0 0} {0  0 0 0}}\n----------------------------------------\n\n[2020-03-31 04:30:02]  [0.46ms]  UPDATE \"cart_items\" SET \"quantity\" = 7, \"updated_at\" = '2020-03-31 04:30:02'  WHERE \"cart_items\".\"id\" = 55  \n[1 rows affected or returned ] \n[GIN] 2020\/03\/31 - 04:30:02 | 200 |   97.963597ms |       127.0.0.1 | POST     \"\/cache\/cart\/21\/item\"<\/pre>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u5982\u679c\u60a8\u5e0c\u671b\u5c06 <code>updatedat()<\/code> \u548c <code>createdat()<\/code> \u4f5c\u4e3a unix \u65f6\u95f4\u6233\uff0c\u8bf7\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u3002<\/p>\n<pre>type CartItems struct {\n    CreatedAt int64\n    UpdatedAt int64\n}\n\nfunc (m *CartItems) BeforeUpdate(scope *gorm.Scope) error {\n    scope.SetColumn(\"UpdatedAt\", time.Now().Unix())\n    return nil\n}\n\nfunc (m *CartItems) BeforeCreate(scope *gorm.Scope) error {\n    if m.UpdatedAt == 0 {\n        scope.SetColumn(\"UpdatedAt\", time.Now().Unix())\n    }\n\n    scope.SetColumn(\"CreatedAt\", time.Now().Unix())\n    return nil\n}\n<\/pre>\n<p>\u4e0d\u5e78\u7684\u662f\uff0c<code>gorm<\/code> \u6ca1\u6709\u5f88\u597d\u7684\u6587\u6863\u8bb0\u5f55\uff0c\u56e0\u6b64\u60a8\u5fc5\u987b\u9605\u8bfb\u4ee3\u7801\u624d\u80fd\u4e86\u89e3\u5176\u5de5\u4f5c\u539f\u7406\uff0c\u5373 \u884c\u8c03\u7528\u4e0a\u9762\u7684 <code>beforeupdate()<\/code> \u51fd\u6570\u3002<\/p>\n<p>\u6b63\u5982\u60a8\u5728 \u51fd\u6570\u4e2d\u770b\u5230\u7684\uff0c\u5b83\u68c0\u67e5 <code>switch<\/code> \u4e2d\u51fd\u6570\u7684\u7b7e\u540d\u6765\u51b3\u5b9a\u5982\u4f55\u8c03\u7528\u8be5\u51fd\u6570\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300a\u5bf9 gorm \u4e2d\u7684 UpdatedAt \u5b57\u6bb5\u4f7f\u7528 unix \u65f6\u95f4\u6233\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\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-207043","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207043","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=207043"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207043\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207043"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207043"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207043"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}